Skip to content

Commit

Permalink
feat: include the application operation field in the ManifestWork pay…
Browse files Browse the repository at this point in the history
…load.

Signed-off-by: Mike Ng <[email protected]>
  • Loading branch information
mikeshng committed Nov 21, 2024
1 parent 339170f commit 0b88bb8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions controllers/application/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
Expand Down
41 changes: 41 additions & 0 deletions controllers/application/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}(),
},
Expand All @@ -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())
}
Expand Down

0 comments on commit 0b88bb8

Please sign in to comment.