-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Remove deprecated labels from Promise (#283)
Co-authored-by: Chunyi Lyu <[email protected]>
- Loading branch information
1 parent
5cde117
commit 6c032ab
Showing
5 changed files
with
228 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-logr/logr" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const deprecatedPipelineCompletedCondition = "PipelineCompleted" | ||
|
||
func RemoveDeprecatedConditions(ctx context.Context, k8sClient client.Client, obj *unstructured.Unstructured, logger logr.Logger) (*ctrl.Result, error) { | ||
conditions, found, err := unstructured.NestedSlice(obj.Object, "status", "conditions") | ||
if err != nil { | ||
logger.Error(err, "failed to get conditions on object, skipping deprecation fix", "name", obj.GetName(), "kind", obj.GetKind()) | ||
return nil, nil | ||
} | ||
|
||
if !found { | ||
logger.Info("deprecated conditions on object not found, skipping deprecation fix", "name", obj.GetName(), "kind", obj.GetKind()) | ||
return nil, nil | ||
} | ||
|
||
newConditions := filterOutCondition(conditions, deprecatedPipelineCompletedCondition) | ||
if len(newConditions) == len(conditions) { | ||
return nil, nil | ||
} | ||
|
||
logger.Info("Removing deprecated condition", "conditionType", deprecatedPipelineCompletedCondition) | ||
if err = unstructured.SetNestedSlice(obj.Object, newConditions, "status", "conditions"); err != nil { | ||
logger.Error(err, "failed to remove deprecated conditions on object", "name", obj.GetName(), "kind", obj.GetKind()) | ||
return nil, err | ||
} | ||
|
||
if err = k8sClient.Status().Update(ctx, obj); err != nil { | ||
logger.Error(err, "failed to update resource to remove deprecated condition") | ||
return nil, err | ||
} | ||
|
||
return &ctrl.Result{Requeue: true}, nil | ||
} | ||
|
||
func filterOutCondition(conditions []interface{}, conditionType string) []interface{} { | ||
var newConditions []interface{} | ||
for _, cond := range conditions { | ||
if conditionMap, ok := cond.(map[string]interface{}); ok { | ||
if conditionMap["type"] != conditionType { | ||
newConditions = append(newConditions, cond) | ||
} | ||
} | ||
} | ||
return newConditions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package migrations_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/syntasso/kratix/api/v1alpha1" | ||
"github.com/syntasso/kratix/lib/migrations" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
var _ = Describe("Conditions", func() { | ||
When("The condition is present", func() { | ||
It("should remove deprecated condition", func() { | ||
gvk := v1alpha1.GroupVersion.Group + "/" + v1alpha1.GroupVersion.Version | ||
promise := &v1alpha1.Promise{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: gvk, | ||
Kind: "Promise", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-promise", | ||
}, | ||
Status: v1alpha1.PromiseStatus{ | ||
Conditions: []metav1.Condition{ | ||
{ | ||
Type: "PipelineCompleted", | ||
Status: metav1.ConditionTrue, | ||
}, | ||
{ | ||
Type: "ConfigureWorkflowCompleted", | ||
Status: metav1.ConditionTrue, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
Expect(fakeK8sClient.Create(ctx, promise)).To(Succeed()) | ||
promise = &v1alpha1.Promise{} | ||
Expect(fakeK8sClient.Get(ctx, client.ObjectKey{Name: "test-promise"}, promise)).To(Succeed()) | ||
|
||
usPromise, err := promise.ToUnstructured() | ||
Expect(err).ToNot(HaveOccurred()) | ||
usPromise.SetGroupVersionKind(v1alpha1.GroupVersion.WithKind("Promise")) | ||
|
||
requeue, err := migrations.RemoveDeprecatedConditions(ctx, fakeK8sClient, usPromise, logger) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(requeue).NotTo(BeNil()) | ||
Expect(requeue.Requeue).To(BeTrue()) | ||
|
||
promise = &v1alpha1.Promise{} | ||
Expect(fakeK8sClient.Get(ctx, client.ObjectKey{Name: "test-promise"}, promise)).To(Succeed()) | ||
Expect(promise.Status.Conditions).To(HaveLen(1)) | ||
Expect(promise.Status.Conditions[0].Type).To(Equal("ConfigureWorkflowCompleted")) | ||
}) | ||
}) | ||
|
||
When("The condition is not present", func() { | ||
It("should no-op", func() { | ||
|
||
gvk := v1alpha1.GroupVersion.Group + "/" + v1alpha1.GroupVersion.Version | ||
promise := &v1alpha1.Promise{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: gvk, | ||
Kind: "Promise", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-promise", | ||
}, | ||
Status: v1alpha1.PromiseStatus{ | ||
Conditions: []metav1.Condition{ | ||
{ | ||
Type: "ConfigureWorkflowCompleted", | ||
Status: metav1.ConditionTrue, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
Expect(fakeK8sClient.Create(ctx, promise)).To(Succeed()) | ||
promise = &v1alpha1.Promise{} | ||
Expect(fakeK8sClient.Get(ctx, client.ObjectKey{Name: "test-promise"}, promise)).To(Succeed()) | ||
|
||
usPromise, err := promise.ToUnstructured() | ||
Expect(err).ToNot(HaveOccurred()) | ||
usPromise.SetGroupVersionKind(v1alpha1.GroupVersion.WithKind("Promise")) | ||
|
||
requeue, err := migrations.RemoveDeprecatedConditions(ctx, fakeK8sClient, usPromise, logger) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(requeue).To(BeNil()) | ||
promise = &v1alpha1.Promise{} | ||
Expect(fakeK8sClient.Get(ctx, client.ObjectKey{Name: "test-promise"}, promise)).To(Succeed()) | ||
Expect(promise.Status.Conditions).To(HaveLen(1)) | ||
Expect(promise.Status.Conditions[0].Type).To(Equal("ConfigureWorkflowCompleted")) | ||
}) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package migrations_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/go-logr/logr" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/syntasso/kratix/api/v1alpha1" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
) | ||
|
||
var ( | ||
fakeK8sClient client.Client | ||
ctx = context.TODO() | ||
logger logr.Logger | ||
) | ||
|
||
func TestManager(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Migration Suite") | ||
} | ||
|
||
var _ = BeforeSuite(func() { | ||
err := v1alpha1.AddToScheme(scheme.Scheme) | ||
Expect(err).NotTo(HaveOccurred()) | ||
//+kubebuilder:scaffold:scheme | ||
|
||
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) | ||
|
||
fakeK8sClient = fake.NewClientBuilder().WithScheme(scheme.Scheme).WithStatusSubresource( | ||
&v1alpha1.Promise{}, | ||
).Build() | ||
}) | ||
|
||
var _ = BeforeEach(func() { | ||
fakeK8sClient = fake.NewClientBuilder().WithScheme(scheme.Scheme).WithStatusSubresource( | ||
&v1alpha1.Promise{}, | ||
).Build() | ||
logger = ctrl.Log.WithName("manager") | ||
}) |