-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/entrypoint: do not interpret anything after
--
The way the `flag` package works, it can eat the flag "terminator" (aka the double dash `--`). This means that, in some very specific cases — where the first item after the `--` is also a subcommand, it would execute the entrypoint subcommand instead of the actual command. For example : ``` $ /ko-app/entrypoint -- init a b $ /ko-app/entrypoint init a b ``` This is fixed by making sure we remove anything after `--` for the subcommand processing. And then we pass the rest (after `--`) to the entrypointer to be executed. Signed-off-by: Vincent Demeester <vdemeest@redhat.com>
- Loading branch information
1 parent
9b4bba2
commit da91bf3
Showing
4 changed files
with
143 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
Copyright 2022 The Tekton Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
func extractArgs(initialArgs []string) ([]string, []string) { | ||
commandArgs := []string{} | ||
args := initialArgs | ||
if len(initialArgs) == 0 { | ||
return args, commandArgs | ||
} | ||
// Detect if `--` is present, if it is, parse only the one before. | ||
terminatorIndex := -1 | ||
for i, a := range initialArgs { | ||
if a == "--" { | ||
terminatorIndex = i | ||
break | ||
} | ||
} | ||
if terminatorIndex > 0 { | ||
commandArgs = initialArgs[terminatorIndex+1:] | ||
args = initialArgs[:terminatorIndex] | ||
} | ||
return args, commandArgs | ||
} |
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,67 @@ | ||
/* | ||
Copyright 2022 The Tekton Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestExtractArgs(t *testing.T) { | ||
for _, c := range []struct { | ||
desc string | ||
args []string | ||
expectedArgs []string | ||
expectedCommandArgs []string | ||
}{{ | ||
desc: "empty arguments", | ||
args: []string{}, | ||
expectedArgs: []string{}, | ||
expectedCommandArgs: []string{}, | ||
}, { | ||
desc: "with args", | ||
args: []string{"--foo", "bar", "a", "b", "c"}, | ||
expectedArgs: []string{"--foo", "bar", "a", "b", "c"}, | ||
expectedCommandArgs: []string{}, | ||
}, { | ||
desc: "with terminator on its own", | ||
args: []string{"--foo", "bar", "--"}, | ||
expectedArgs: []string{"--foo", "bar"}, | ||
expectedCommandArgs: []string{}, | ||
}, { | ||
desc: "with terminator", | ||
args: []string{"--foo", "bar", "--", "a", "b", "c"}, | ||
expectedArgs: []string{"--foo", "bar"}, | ||
expectedCommandArgs: []string{"a", "b", "c"}, | ||
}, { | ||
desc: "with args and terminator", | ||
args: []string{"--foo", "bar", "baz", "--", "a", "b", "c"}, | ||
expectedArgs: []string{"--foo", "bar", "baz"}, | ||
expectedCommandArgs: []string{"a", "b", "c"}, | ||
}} { | ||
t.Run(c.desc, func(t *testing.T) { | ||
args, commandArgs := extractArgs(c.args) | ||
if d := cmp.Diff(c.expectedArgs, args); d != "" { | ||
t.Errorf("args: diff(-want,+got):\n%s", d) | ||
} | ||
if d := cmp.Diff(c.expectedCommandArgs, commandArgs); d != "" { | ||
t.Errorf("commandArgs: diff(-want,+got):\n%s", d) | ||
} | ||
}) | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
examples/v1beta1/taskruns/5080-entrypoint-init-regression.yaml
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,27 @@ | ||
apiVersion: tekton.dev/v1beta1 | ||
kind: TaskRun | ||
metadata: | ||
generateName: tkn-arg-test- | ||
spec: | ||
params: | ||
- name: ARGS | ||
value: | ||
- init | ||
- foo | ||
- bar | ||
taskSpec: | ||
description: >- | ||
Test consuming args, acts as a regression test for bug 5080 | ||
params: | ||
- name: ARGS | ||
description: The terraform cli commands to tun | ||
type: array | ||
default: | ||
- "--help" | ||
steps: | ||
- name: echo-cli | ||
image: registry.access.redhat.com/ubi9/ubi-minimal:9.0.0-1580 | ||
workingDir: /tekton/home | ||
args: | ||
- "$(params.ARGS)" | ||
command: ["echo"] |