Skip to content

Commit

Permalink
Update condition lib to use v2
Browse files Browse the repository at this point in the history
The v2 OperatorCondition CRD was introduced to the operator-framework/api
repository. This change updates the condition library to consume the new
changes.
  • Loading branch information
awgreene committed May 27, 2021
1 parent 064486c commit 3ae2713
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 53 deletions.
14 changes: 7 additions & 7 deletions conditions/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"fmt"
"os"

apiv1 "github.com/operator-framework/api/pkg/operators/v1"
apiv2 "github.com/operator-framework/api/pkg/operators/v2"
"github.com/operator-framework/operator-lib/internal/utils"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -46,7 +46,7 @@ const (
// conditionType in the OperatorCondition CR.
type condition struct {
namespacedName types.NamespacedName
condType apiv1.ConditionType
condType apiv2.ConditionType
client client.Client
}

Expand All @@ -55,7 +55,7 @@ var _ Condition = &condition{}
// NewCondition returns a new Condition interface using the provided client
// for the specified conditionType. The condition will internally fetch the namespacedName
// of the operatorConditionCRD.
func NewCondition(cl client.Client, condType apiv1.ConditionType) (Condition, error) {
func NewCondition(cl client.Client, condType apiv2.ConditionType) (Condition, error) {
objKey, err := GetNamespacedName()
if err != nil {
return nil, err
Expand All @@ -69,12 +69,12 @@ func NewCondition(cl client.Client, condType apiv1.ConditionType) (Condition, er

// Get implements conditions.Get
func (c *condition) Get(ctx context.Context) (*metav1.Condition, error) {
operatorCond := &apiv1.OperatorCondition{}
operatorCond := &apiv2.OperatorCondition{}
err := c.client.Get(ctx, c.namespacedName, operatorCond)
if err != nil {
return nil, ErrNoOperatorCondition
}
con := meta.FindStatusCondition(operatorCond.Status.Conditions, string(c.condType))
con := meta.FindStatusCondition(operatorCond.Spec.Conditions, string(c.condType))

if con == nil {
return nil, fmt.Errorf("conditionType %v not found", c.condType)
Expand All @@ -84,7 +84,7 @@ func (c *condition) Get(ctx context.Context) (*metav1.Condition, error) {

// Set implements conditions.Set
func (c *condition) Set(ctx context.Context, status metav1.ConditionStatus, option ...Option) error {
operatorCond := &apiv1.OperatorCondition{}
operatorCond := &apiv2.OperatorCondition{}
err := c.client.Get(ctx, c.namespacedName, operatorCond)
if err != nil {
return ErrNoOperatorCondition
Expand All @@ -100,7 +100,7 @@ func (c *condition) Set(ctx context.Context, status metav1.ConditionStatus, opti
opt(newCond)
}
}
meta.SetStatusCondition(&operatorCond.Status.Conditions, *newCond)
meta.SetStatusCondition(&operatorCond.Spec.Conditions, *newCond)
err = c.client.Status().Update(ctx, operatorCond)
if err != nil {
return err
Expand Down
24 changes: 12 additions & 12 deletions conditions/conditions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
apiv1 "github.com/operator-framework/api/pkg/operators/v1"
apiv2 "github.com/operator-framework/api/pkg/operators/v2"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -32,8 +32,8 @@ import (
)

const (
conditionFoo apiv1.ConditionType = "conditionFoo"
conditionBar apiv1.ConditionType = "conditionBar"
conditionFoo apiv2.ConditionType = "conditionFoo"
conditionBar apiv2.ConditionType = "conditionBar"
)

var _ = Describe("Condition", func() {
Expand All @@ -46,7 +46,7 @@ var _ = Describe("Condition", func() {

BeforeEach(func() {
sch := runtime.NewScheme()
err = apiv1.AddToScheme(sch)
err = apiv2.AddToScheme(sch)
Expect(err).NotTo(HaveOccurred())
cl = fake.NewClientBuilder().WithScheme(sch).Build()
})
Expand Down Expand Up @@ -75,17 +75,17 @@ var _ = Describe("Condition", func() {
})

Describe("Get/Set", func() {
var operatorCond *apiv1.OperatorCondition
var operatorCond *apiv2.OperatorCondition

objKey := types.NamespacedName{
Name: "operator-condition-test",
Namespace: ns,
}

BeforeEach(func() {
operatorCond = &apiv1.OperatorCondition{
operatorCond = &apiv2.OperatorCondition{
ObjectMeta: metav1.ObjectMeta{Name: "operator-condition-test", Namespace: ns},
Status: apiv1.OperatorConditionStatus{
Spec: apiv2.OperatorConditionSpec{
Conditions: []metav1.Condition{
{
Type: string(conditionFoo),
Expand All @@ -107,7 +107,7 @@ var _ = Describe("Condition", func() {

// create a new client
sch := runtime.NewScheme()
err = apiv1.AddToScheme(sch)
err = apiv2.AddToScheme(sch)
Expect(err).NotTo(HaveOccurred())
cl = fake.NewClientBuilder().WithScheme(sch).Build()

Expand Down Expand Up @@ -171,12 +171,12 @@ var _ = Describe("Condition", func() {
Expect(err).NotTo(HaveOccurred())

By("fetching the condition from cluster")
op := &apiv1.OperatorCondition{}
op := &apiv2.OperatorCondition{}
err = cl.Get(ctx, objKey, op)
Expect(err).NotTo(HaveOccurred())

By("checking if the condition has been updated")
res := op.Status.Conditions[0]
res := op.Spec.Conditions[0]
Expect(res.Message).To(BeEquivalentTo("test"))
Expect(res.Status).To(BeEquivalentTo(metav1.ConditionFalse))
Expect(res.Reason).To(BeEquivalentTo("not_in_foo_state"))
Expand All @@ -190,12 +190,12 @@ var _ = Describe("Condition", func() {
Expect(err).NotTo(HaveOccurred())

By("fetching the condition from cluster")
op := &apiv1.OperatorCondition{}
op := &apiv2.OperatorCondition{}
err = cl.Get(ctx, objKey, op)
Expect(err).NotTo(HaveOccurred())

By("checking if the condition has been updated")
res := op.Status.Conditions
res := op.Spec.Conditions
Expect(len(res)).To(BeEquivalentTo(2))
Expect(meta.IsStatusConditionTrue(res, string(conditionBar))).To(BeTrue())
})
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.15
require (
github.com/onsi/ginkgo v1.14.1
github.com/onsi/gomega v1.10.2
github.com/operator-framework/api v0.5.2
github.com/operator-framework/api v0.9.1
github.com/prometheus/client_golang v1.7.1
github.com/prometheus/client_model v0.2.0
k8s.io/api v0.20.2
Expand Down
Loading

0 comments on commit 3ae2713

Please sign in to comment.