diff --git a/apis/placement/v1alpha1/common.go b/apis/placement/v1alpha1/common.go index 1f04a0fb0..88023cf63 100644 --- a/apis/placement/v1alpha1/common.go +++ b/apis/placement/v1alpha1/common.go @@ -35,5 +35,5 @@ const ( ApprovalTaskNameFmt = "%s-%s" // OverrideClusterNameVariable is the reserved variable in the override value that will be replaced by the actual cluster name. - OverrideClusterNameVariable = "$CLUSTER-NAME" + OverrideClusterNameVariable = "${MEMBER-CLUSTER-NAME}" ) diff --git a/apis/placement/v1alpha1/override_types.go b/apis/placement/v1alpha1/override_types.go index 38c90fd4c..5efb9a6f9 100644 --- a/apis/placement/v1alpha1/override_types.go +++ b/apis/placement/v1alpha1/override_types.go @@ -194,7 +194,7 @@ type JSONPatchOverride struct { // We have reserved a few variables in this field that will be replaced by the actual values. // Those variables all start with `$` and are case sensitive. // Here is the list of currently supported variables: - // `$CLUSTER-NAME`: this will be replaced by the actual cluster name. + // `${MEMBER-CLUSTER-NAME}`: this will be replaced by the actual cluster name. // +optional Value apiextensionsv1.JSON `json:"value,omitempty"` } diff --git a/pkg/controllers/workgenerator/override.go b/pkg/controllers/workgenerator/override.go index 30a54b1e9..d2007207a 100644 --- a/pkg/controllers/workgenerator/override.go +++ b/pkg/controllers/workgenerator/override.go @@ -198,16 +198,27 @@ func applyJSONPatchOverride(resourceContent *placementv1beta1.ResourceContent, c if len(overrides) == 0 { // do nothing return nil } + // go through the JSON patch overrides to replace the built-in variables + var processedOverrides []placementv1alpha1.JSONPatchOverride + for _, override := range overrides { + // Replace the built-in variable with the actual cluster name + processedOverride := placementv1alpha1.JSONPatchOverride{ + Operator: override.Operator, + Path: override.Path, + } + // find and replace a few special built-in variables + processedJSONStr := []byte(strings.ReplaceAll(string(override.Value.Raw), placementv1alpha1.OverrideClusterNameVariable, cluster.Name)) + processedOverride.Value.Raw = processedJSONStr + processedOverrides = append(processedOverrides, processedOverride) + } - jsonPatchBytes, err := json.Marshal(overrides) + jsonPatchBytes, err := json.Marshal(processedOverrides) if err != nil { klog.ErrorS(err, "Failed to marshal JSON Patch overrides") return err } - // Process the JSON patch to treat a few special built-in variables - processedJSONStr := strings.ReplaceAll(string(jsonPatchBytes), placementv1alpha1.OverrideClusterNameVariable, cluster.Name) - patch, err := jsonpatch.DecodePatch([]byte(processedJSONStr)) + patch, err := jsonpatch.DecodePatch([]byte(jsonPatchBytes)) if err != nil { klog.ErrorS(err, "Failed to decode the passed JSON document as an RFC 6902 patch") return err diff --git a/pkg/controllers/workgenerator/override_test.go b/pkg/controllers/workgenerator/override_test.go index 0542b22bb..7f69f064a 100644 --- a/pkg/controllers/workgenerator/override_test.go +++ b/pkg/controllers/workgenerator/override_test.go @@ -8,6 +8,7 @@ package workgenerator import ( "context" "errors" + "fmt" "testing" "github.com/google/go-cmp/cmp" @@ -2005,12 +2006,12 @@ func TestApplyOverrides_namespacedScopeResource(t *testing.T) { { Operator: placementv1alpha1.JSONPatchOverrideOpReplace, Path: "/metadata/labels/app", - Value: apiextensionsv1.JSON{Raw: []byte(`"$CLUSTER-NAME"`)}, + Value: apiextensionsv1.JSON{Raw: []byte(fmt.Sprintf(`"%s"`, placementv1alpha1.OverrideClusterNameVariable))}, }, { Operator: placementv1alpha1.JSONPatchOverrideOpAdd, Path: "/metadata/annotations", - Value: apiextensionsv1.JSON{Raw: []byte(`{"app": "$CLUSTER-NAME", "test": "nginx"}`)}, + Value: apiextensionsv1.JSON{Raw: []byte(fmt.Sprintf("{\"app\": \"%s\", \"test\": \"nginx\"}", placementv1alpha1.OverrideClusterNameVariable))}, }, }, }, @@ -2334,12 +2335,12 @@ func TestApplyJSONPatchOverride(t *testing.T) { { Operator: placementv1alpha1.JSONPatchOverrideOpReplace, Path: "/metadata/labels/app", - Value: apiextensionsv1.JSON{Raw: []byte(`"$CLUSTER-NAME"`)}, + Value: apiextensionsv1.JSON{Raw: []byte(fmt.Sprintf(`"%s"`, placementv1alpha1.OverrideClusterNameVariable))}, }, { Operator: placementv1alpha1.JSONPatchOverrideOpAdd, Path: "/metadata/annotations", - Value: apiextensionsv1.JSON{Raw: []byte(`{"app": "$CLUSTER-NAME", "test": "nginx"}`)}, + Value: apiextensionsv1.JSON{Raw: []byte(fmt.Sprintf("{\"app\": \"%s\", \"test\": \"nginx\"}", placementv1alpha1.OverrideClusterNameVariable))}, }, }, cluster: &clusterv1beta1.MemberCluster{