From 5eea60dca8943d9c7fc33ed534267fe1406f1966 Mon Sep 17 00:00:00 2001 From: Alexander Greene Date: Tue, 6 Jul 2021 12:37:25 -0700 Subject: [PATCH] Update condition lib to use v2 (#61) The v2 OperatorCondition CRD was introduced to the operator-framework/api repository. This change updates the condition library to consume the new changes. --- conditions/conditions.go | 14 +++++++------- conditions/conditions_test.go | 24 ++++++++++++------------ go.sum | 1 + 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/conditions/conditions.go b/conditions/conditions.go index 0e658cc..50bab5f 100644 --- a/conditions/conditions.go +++ b/conditions/conditions.go @@ -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" @@ -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 } @@ -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 @@ -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) @@ -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 @@ -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 diff --git a/conditions/conditions_test.go b/conditions/conditions_test.go index 5cc3baf..2f9dc80 100644 --- a/conditions/conditions_test.go +++ b/conditions/conditions_test.go @@ -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" @@ -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() { @@ -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() }) @@ -75,7 +75,7 @@ var _ = Describe("Condition", func() { }) Describe("Get/Set", func() { - var operatorCond *apiv1.OperatorCondition + var operatorCond *apiv2.OperatorCondition objKey := types.NamespacedName{ Name: "operator-condition-test", @@ -83,9 +83,9 @@ var _ = Describe("Condition", func() { } 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), @@ -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() @@ -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")) @@ -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()) }) diff --git a/go.sum b/go.sum index f97a510..2d195b1 100644 --- a/go.sum +++ b/go.sum @@ -36,6 +36,7 @@ github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb0 github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= +github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=