Skip to content

Commit

Permalink
fix the format
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Zhang committed Dec 19, 2024
1 parent 4c3b40c commit 4333b5b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion apis/placement/v1alpha1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
)
2 changes: 1 addition & 1 deletion apis/placement/v1alpha1/override_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
19 changes: 15 additions & 4 deletions pkg/controllers/workgenerator/override.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 202 in pkg/controllers/workgenerator/override.go

View workflow job for this annotation

GitHub Actions / Lint

Consider pre-allocating `processedOverrides` (prealloc)
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))

Check failure on line 221 in pkg/controllers/workgenerator/override.go

View workflow job for this annotation

GitHub Actions / Lint

unnecessary conversion (unconvert)
if err != nil {
klog.ErrorS(err, "Failed to decode the passed JSON document as an RFC 6902 patch")
return err
Expand Down
9 changes: 5 additions & 4 deletions pkg/controllers/workgenerator/override_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package workgenerator
import (
"context"
"errors"
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -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))},
},
},
},
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 4333b5b

Please sign in to comment.