-
Notifications
You must be signed in to change notification settings - Fork 170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ARO-1885] Implement OperatorFlagsMergeStrategy #3911
Open
hawkowl
wants to merge
2
commits into
master
Choose a base branch
from
hawkowl/ARO-1885
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+635
−367
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package admin | ||
|
||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the Apache License 2.0. | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/Azure/ARO-RP/pkg/api" | ||
"github.com/Azure/ARO-RP/pkg/operator" | ||
) | ||
|
||
const ( | ||
OperatorFlagsMergeStrategyMerge string = "merge" | ||
OperatorFlagsMergeStrategyReset string = "reset" | ||
) | ||
|
||
// When a cluster is edited via the PATCH Cluster Geneva Action (aka an Admin Update) | ||
// the flags given are treated according to the provided Update Strategy, | ||
// provided in operatorFlagsMergeStrategy | ||
|
||
// merge (default): The provided cluster flags are laid on top of the cluster’s existing flags. | ||
// reset: The provided cluster flags are laid on top of the default cluster flags, | ||
// essentially ‘resetting’ the flags if no new flags are provided. | ||
func OperatorFlagsMergeStrategy(oc *api.OpenShiftCluster, body []byte) error { | ||
payload := &OpenShiftCluster{} | ||
|
||
err := json.Unmarshal(body, &payload) | ||
if err != nil { | ||
return api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidRequestContent, "", "The request content was invalid and could not be deserialized: %q.", err) | ||
} | ||
|
||
// if it's empty, use the default of merge, which is performed by | ||
// deserialising the body JSON later | ||
if payload.OperatorFlagsMergeStrategy == "" { | ||
return nil | ||
} | ||
|
||
// return error if OperatorFlagsMergeStrategy is not merge or reset, default is merge | ||
if payload.OperatorFlagsMergeStrategy != OperatorFlagsMergeStrategyMerge && | ||
payload.OperatorFlagsMergeStrategy != OperatorFlagsMergeStrategyReset { | ||
return api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidParameter, "", "invalid operatorFlagsMergeStrategy '%s', can only be 'merge' or 'reset'", payload.OperatorFlagsMergeStrategy) | ||
} | ||
|
||
// return nil, if OperatorFlagsMergeStrategy is merge and payload has not operatorFlags | ||
// return operatorFlags of payload, if OperatorFlagsMergeStrategy is merge and payload has operatorFlags | ||
// return defaultOperatorFlags, if OperatorFlagsMergeStrategy is reset and payload has not operatorFlags | ||
// return defaultOperatorFlags + operatorFlags of payload, if OperatorFlagsMergeStrategy is reset and payload has operatorFlags | ||
if payload.OperatorFlagsMergeStrategy == OperatorFlagsMergeStrategyReset { | ||
oc.Properties.OperatorFlags = operator.DefaultOperatorFlags() | ||
} | ||
|
||
return nil | ||
} |
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,60 @@ | ||
package admin | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Azure/ARO-RP/pkg/api" | ||
utilerror "github.com/Azure/ARO-RP/test/util/error" | ||
) | ||
|
||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the Apache License 2.0. | ||
|
||
func TestOperatorFlagsMergeStrategy(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
oc *api.OpenShiftCluster | ||
wantOc *api.OpenShiftCluster | ||
body []byte | ||
wantErr string | ||
}{ | ||
{ | ||
name: "invalid_json", | ||
oc: nil, | ||
body: []byte(`{{}`), | ||
wantErr: `400: InvalidRequestContent: : The request content was invalid and could not be deserialized: "invalid character '{' looking for beginning of object key string".`, | ||
}, | ||
{ | ||
name: "OperatorFlagsMergeStrategy_is_not_merge_or_reset", | ||
oc: nil, | ||
body: []byte(`{"operatorFlagsMergeStrategy": "xyz"}`), | ||
wantErr: `400: InvalidParameter: : invalid operatorFlagsMergeStrategy 'xyz', can only be 'merge' or 'reset'`, | ||
}, | ||
{ | ||
name: "OperatorFlagsMergeStrategy_payload_is_empty", | ||
oc: &api.OpenShiftCluster{ | ||
Properties: api.OpenShiftClusterProperties{ | ||
OperatorFlags: api.OperatorFlags{"aro.feature1.enabled": "false"}, | ||
}, | ||
}, | ||
body: []byte(`{"operatorflagsmergestrategy":"merge"}`), | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "OperatorFlagsMergeStrategy_reset", | ||
oc: &api.OpenShiftCluster{ | ||
Properties: api.OpenShiftClusterProperties{ | ||
OperatorFlags: api.OperatorFlags{"aro.feature1.enabled": "false"}, | ||
}, | ||
}, | ||
body: []byte(`{"operatorflagsmergestrategy":"reset"}`), | ||
wantErr: "", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := OperatorFlagsMergeStrategy(tt.oc, tt.body) | ||
utilerror.AssertErrorMessage(t, err, tt.wantErr) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable
wantOc
is declared, but its usage is not shown anywhere.