From 0b88bb8451b0283fd5a8d032635a2c6bf6d91735 Mon Sep 17 00:00:00 2001 From: Mike Ng Date: Wed, 20 Nov 2024 22:36:26 -0500 Subject: [PATCH] feat: include the application operation field in the ManifestWork payload. Signed-off-by: Mike Ng --- controllers/application/helper.go | 8 +++++ controllers/application/helper_test.go | 41 ++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/controllers/application/helper.go b/controllers/application/helper.go index 302d237..d89f0a8 100644 --- a/controllers/application/helper.go +++ b/controllers/application/helper.go @@ -110,6 +110,7 @@ func getAppSetOwnerName(ownerRefs []metav1.OwnerReference) string { // - reset the meta // - set the namespace value // - ensures the Application Destination is set to in-cluster resource deployment +// - ensures the operation field is also set if present func prepareApplicationForWorkPayload(application *unstructured.Unstructured) unstructured.Unstructured { newApp := &unstructured.Unstructured{} newApp.SetGroupVersionKind(schema.GroupVersionKind{ @@ -120,6 +121,13 @@ func prepareApplicationForWorkPayload(application *unstructured.Unstructured) un newApp.SetNamespace(generateAppNamespace(application.GetNamespace(), application.GetAnnotations())) newApp.SetName(application.GetName()) newApp.SetFinalizers(application.GetFinalizers()) + + // set the operation field + if operation, ok := application.Object["operation"].(map[string]interface{}); ok { + newApp.Object["operation"] = operation + } + + // set the spec field if newSpec, ok := application.Object["spec"].(map[string]interface{}); ok { if destination, ok := newSpec["destination"].(map[string]interface{}); ok { // empty the name diff --git a/controllers/application/helper_test.go b/controllers/application/helper_test.go index bedc3d2..4b6dc59 100644 --- a/controllers/application/helper_test.go +++ b/controllers/application/helper_test.go @@ -311,6 +311,24 @@ func Test_prepareApplicationForWorkPayload(t *testing.T) { "server": "originalServer", }, } + app.Object["operation"] = map[string]interface{}{ + "info": []interface{}{ + map[string]interface{}{ + "name": "Reason", + "value": "ApplicationSet RollingSync triggered a sync of this Application resource.", + }, + }, + "initiatedBy": map[string]interface{}{ + "automated": true, + "username": "applicationset-controller", + }, + "retry": map[string]interface{}{}, + "sync": map[string]interface{}{ + "syncOptions": []interface{}{ + "CreateNamespace=true", + }, + }, + } type args struct { application *unstructured.Unstructured @@ -345,6 +363,24 @@ func Test_prepareApplicationForWorkPayload(t *testing.T) { "server": KubernetesInternalAPIServerAddr, }, } + expectedApp.Object["operation"] = map[string]interface{}{ + "info": []interface{}{ + map[string]interface{}{ + "name": "Reason", + "value": "ApplicationSet RollingSync triggered a sync of this Application resource.", + }, + }, + "initiatedBy": map[string]interface{}{ + "automated": true, + "username": "applicationset-controller", + }, + "retry": map[string]interface{}{}, + "sync": map[string]interface{}{ + "syncOptions": []interface{}{ + "CreateNamespace=true", + }, + }, + } return expectedApp }(), }, @@ -366,6 +402,11 @@ func Test_prepareApplicationForWorkPayload(t *testing.T) { if !reflect.DeepEqual(gotSpec, wantSpec) { t.Errorf("prepareApplicationForWorkPayload() Spec = %v, want %v", gotSpec, wantSpec) } + gotOperation, _, _ := unstructured.NestedMap(got.Object, "operation") + wantOperation, _, _ := unstructured.NestedMap(tt.want.Object, "operation") + if !reflect.DeepEqual(gotOperation, wantOperation) { + t.Errorf("prepareApplicationForWorkPayload() Operation = %v, want %v", gotOperation, wantOperation) + } if !reflect.DeepEqual(got.GetLabels(), tt.want.GetLabels()) { t.Errorf("prepareApplicationForWorkPayload() Labels = %v, want %v", got.GetLabels(), tt.want.GetLabels()) }