-
Notifications
You must be signed in to change notification settings - Fork 7.3k
feat: add support for parameters when running actions from the CLI (#28000) #28004
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
Open
olavst-spk
wants to merge
7
commits into
argoproj:master
Choose a base branch
from
olavst-spk:cli-action-parameters
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.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8fed723
feat: add support for parameters when running actions from the CLI
olavst-spk 5af81af
fix: Handle empty param key
olavst-spk 564bece
fixup! fix: Handle empty param key
olavst-spk c43a5ae
fix: handle duplicate parameter keys
olavst-spk e1b91ea
fix: remove warning for older server versions
olavst-spk 2ffec32
Revert "fix: handle duplicate parameter keys"
olavst-spk a2e9261
fix: warning instead of error when duplicate parameter keys are provided
olavst-spk 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 hidden or 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 hidden or 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 hidden or 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,45 @@ | ||
| package util | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| applicationpkg "github.com/argoproj/argo-cd/v3/pkg/apiclient/application" | ||
| ) | ||
|
|
||
| // ParseActionParameters parses a slice of "name=value" strings into ResourceActionParameters. | ||
| func ParseActionParameters(params []string) ([]*applicationpkg.ResourceActionParameters, error) { | ||
| parsedParams := make([]*applicationpkg.ResourceActionParameters, 0, len(params)) | ||
| for _, param := range params { | ||
| parts := strings.SplitN(param, "=", 2) | ||
| if len(parts) != 2 { | ||
| return nil, fmt.Errorf("invalid parameter format %q: expected name=value", param) | ||
| } | ||
| name := parts[0] | ||
| value := parts[1] | ||
|
olavst-spk marked this conversation as resolved.
|
||
| if name == "" { | ||
| return nil, fmt.Errorf("invalid parameter format %q: parameter name cannot be empty", param) | ||
| } | ||
| parsedParams = append(parsedParams, &applicationpkg.ResourceActionParameters{ | ||
| Name: &name, | ||
| Value: &value, | ||
| }) | ||
| } | ||
| return parsedParams, nil | ||
| } | ||
|
|
||
| // DuplicateActionParameterNames returns the names of any parameters specified more than once. | ||
| func DuplicateActionParameterNames(params []*applicationpkg.ResourceActionParameters) []string { | ||
| seen := make(map[string]int, len(params)) | ||
| var duplicates []string | ||
| for _, p := range params { | ||
| if p == nil || p.Name == nil { | ||
| continue | ||
| } | ||
| seen[*p.Name]++ | ||
| if seen[*p.Name] == 2 { | ||
| duplicates = append(duplicates, *p.Name) | ||
| } | ||
| } | ||
| return duplicates | ||
| } | ||
This file contains hidden or 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,161 @@ | ||
| package util | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| applicationpkg "github.com/argoproj/argo-cd/v3/pkg/apiclient/application" | ||
| ) | ||
|
|
||
| func strPtr(s string) *string { return &s } | ||
|
|
||
| func TestParseActionParameters(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| params []string | ||
| expectedParams []*applicationpkg.ResourceActionParameters | ||
| expectError bool | ||
| }{ | ||
| { | ||
| name: "empty", | ||
| params: []string{}, | ||
| expectedParams: []*applicationpkg.ResourceActionParameters{}, | ||
| }, | ||
| { | ||
| name: "single parameter", | ||
| params: []string{"replicas=2"}, | ||
| expectedParams: []*applicationpkg.ResourceActionParameters{ | ||
| {Name: strPtr("replicas"), Value: strPtr("2")}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "multiple parameters", | ||
| params: []string{"replicas=2", "image=foo"}, | ||
| expectedParams: []*applicationpkg.ResourceActionParameters{ | ||
| {Name: strPtr("replicas"), Value: strPtr("2")}, | ||
| {Name: strPtr("image"), Value: strPtr("foo")}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "value containing equals sign", | ||
| params: []string{"url=http://example.com?foo=bar"}, | ||
| expectedParams: []*applicationpkg.ResourceActionParameters{ | ||
| {Name: strPtr("url"), Value: strPtr("http://example.com?foo=bar")}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "empty key", | ||
| params: []string{"=value"}, | ||
| expectError: true, | ||
| }, | ||
| { | ||
| name: "empty value", | ||
| params: []string{"key="}, | ||
| expectedParams: []*applicationpkg.ResourceActionParameters{ | ||
| {Name: strPtr("key"), Value: strPtr("")}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "missing equals sign", | ||
| params: []string{"replicas"}, | ||
| expectError: true, | ||
| }, | ||
| { | ||
| name: "duplicate keys", | ||
| params: []string{"replicas=2", "replicas=3"}, | ||
| expectedParams: []*applicationpkg.ResourceActionParameters{ | ||
| {Name: strPtr("replicas"), Value: strPtr("2")}, | ||
| {Name: strPtr("replicas"), Value: strPtr("3")}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| result, err := ParseActionParameters(tc.params) | ||
| if tc.expectError { | ||
| assert.Error(t, err) | ||
| return | ||
| } | ||
| require.NoError(t, err) | ||
| assert.Len(t, result, len(tc.expectedParams)) | ||
| for i, p := range result { | ||
| assert.Equal(t, *tc.expectedParams[i].Name, *p.Name) | ||
| assert.Equal(t, *tc.expectedParams[i].Value, *p.Value) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestDuplicateActionParameterNames(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| params []*applicationpkg.ResourceActionParameters | ||
| expectedDupes []string | ||
| }{ | ||
| { | ||
| name: "empty", | ||
| params: []*applicationpkg.ResourceActionParameters{}, | ||
| expectedDupes: nil, | ||
| }, | ||
| { | ||
| name: "no duplicates", | ||
| params: []*applicationpkg.ResourceActionParameters{ | ||
| {Name: strPtr("replicas"), Value: strPtr("2")}, | ||
| }, | ||
| expectedDupes: nil, | ||
| }, | ||
| { | ||
| name: "single duplicate", | ||
| params: []*applicationpkg.ResourceActionParameters{ | ||
| {Name: strPtr("replicas"), Value: strPtr("2")}, | ||
| {Name: strPtr("replicas"), Value: strPtr("3")}, | ||
| }, | ||
| expectedDupes: []string{"replicas"}, | ||
| }, | ||
| { | ||
| name: "multiple duplicates", | ||
| params: []*applicationpkg.ResourceActionParameters{ | ||
| {Name: strPtr("replicas"), Value: strPtr("2")}, | ||
| {Name: strPtr("replicas"), Value: strPtr("3")}, | ||
| {Name: strPtr("replicas"), Value: strPtr("4")}, | ||
| }, | ||
| expectedDupes: []string{"replicas"}, | ||
| }, | ||
| { | ||
| name: "multiple distinct duplicates", | ||
| params: []*applicationpkg.ResourceActionParameters{ | ||
| {Name: strPtr("replicas"), Value: strPtr("2")}, | ||
| {Name: strPtr("replicas"), Value: strPtr("3")}, | ||
| {Name: strPtr("replicas"), Value: strPtr("4")}, | ||
| {Name: strPtr("image"), Value: strPtr("foo")}, | ||
| {Name: strPtr("image"), Value: strPtr("bar")}, | ||
| {Name: strPtr("image"), Value: strPtr("baz")}, | ||
| }, | ||
| expectedDupes: []string{"replicas", "image"}, | ||
| }, | ||
| { | ||
| name: "nil element in slice", | ||
| params: []*applicationpkg.ResourceActionParameters{ | ||
| nil, | ||
| }, | ||
| expectedDupes: nil, | ||
| }, | ||
| { | ||
| name: "nil name field", | ||
| params: []*applicationpkg.ResourceActionParameters{ | ||
| {Name: nil, Value: nil}, | ||
| }, | ||
| expectedDupes: nil, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| result := DuplicateActionParameterNames(tc.params) | ||
| assert.ElementsMatch(t, tc.expectedDupes, result) | ||
| }) | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
11 changes: 8 additions & 3 deletions
11
resource_customizations/apps/Deployment/actions/scale/action.lua
This file contains hidden or 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 |
|---|---|---|
| @@ -1,9 +1,14 @@ | ||
| local os = require("os") | ||
|
|
||
| local replicas = tonumber(actionParams["replicas"]) | ||
| local replicas = actionParams["replicas"] | ||
| if not replicas then | ||
| error("invalid number: " .. actionParams["replicas"], 0) | ||
| error("replicas parameter is required", 0) | ||
| end | ||
|
|
||
| obj.spec.replicas = replicas | ||
| local replicasNum = tonumber(replicas) | ||
| if not replicasNum or replicasNum < 0 then | ||
| error("invalid number: " .. replicas .. " (must be >= 0)", 0) | ||
| end | ||
|
|
||
| obj.spec.replicas = replicasNum | ||
| return obj |
This file contains hidden or 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
11 changes: 8 additions & 3 deletions
11
resource_customizations/apps/StatefulSet/actions/scale/action.lua
This file contains hidden or 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 |
|---|---|---|
| @@ -1,9 +1,14 @@ | ||
| local os = require("os") | ||
|
|
||
| local replicas = tonumber(actionParams["replicas"]) | ||
| local replicas = actionParams["replicas"] | ||
| if not replicas then | ||
| error("invalid number: " .. actionParams["replicas"], 0) | ||
| error("replicas parameter is required", 0) | ||
| end | ||
|
|
||
| obj.spec.replicas = replicas | ||
| local replicasNum = tonumber(replicas) | ||
| if not replicasNum or replicasNum < 0 then | ||
| error("invalid number: " .. replicas .. " (must be >= 0)", 0) | ||
| end | ||
|
|
||
| obj.spec.replicas = replicasNum | ||
| return obj |
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.
Uh oh!
There was an error while loading. Please reload this page.