-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Review config for cmd/entrypoint after building a stage #348
Changes from 3 commits
5d2d282
d923d5e
da6f099
cd1b957
1e1c982
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FROM gcr.io/distroless/base@sha256:628939ac8bf3f49571d05c6c76b8688cb4a851af6c7088e599388259875bde20 AS first | ||
CMD ["mycmd"] | ||
|
||
FROM first | ||
ENTRYPOINT ["myentrypoint"] # This should clear out CMD in the config metadata |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,6 +145,9 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) { | |
return nil, err | ||
} | ||
} | ||
if err := reviewConfig(stage, &imageConfig.Config); err != nil { | ||
return nil, err | ||
} | ||
sourceImage, err = mutate.Config(sourceImage, imageConfig.Config) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -228,3 +231,24 @@ func resolveOnBuild(stage *config.KanikoStage, config *v1.Config) error { | |
config.OnBuild = nil | ||
return nil | ||
} | ||
|
||
// reviewConfig makes sure the value of CMD is correct after building the stage | ||
// If ENTRYPOINT was set in this stage but CMD wasn't, then CMD should be cleared out | ||
// See Issue #346 for more info | ||
func reviewConfig(stage config.KanikoStage, config *v1.Config) error { | ||
entrypoint := false | ||
cmd := false | ||
|
||
for _, c := range stage.Commands { | ||
if c.Name() == "cmd" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use constants. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
cmd = true | ||
} | ||
if c.Name() == "entrypoint" { | ||
entrypoint = true | ||
} | ||
} | ||
if entrypoint && !cmd { | ||
config.Cmd = nil | ||
} | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
Copyright 2018 Google LLC | ||
|
||
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 executor | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/GoogleContainerTools/kaniko/pkg/config" | ||
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" | ||
"github.com/GoogleContainerTools/kaniko/testutil" | ||
"github.com/google/go-containerregistry/pkg/v1" | ||
) | ||
|
||
func Test_reviewConfig(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
dockerfile string | ||
originalCmd []string | ||
originalEntrypoint []string | ||
expectedCmd []string | ||
}{ | ||
{ | ||
name: "entrypoint and cmd declared", | ||
dockerfile: ` | ||
FROM scratch | ||
CMD ["mycmd"] | ||
ENTRYPOINT ["myentrypoint"]`, | ||
originalEntrypoint: []string{"myentrypoint"}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. by originalCmd, do you mean command from previous stage? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use different values for dockerfile.CMD and dockerfile.ENTRYPOINT. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. by originalCmd, I mean the original value of Cmd in the config file (it should match the value specified in the Dockerfile) The function reviews the Dockerfile and then clears the Cmd field if it isn't declared in the Dockerfile. |
||
originalCmd: []string{"mycmd"}, | ||
expectedCmd: []string{"mycmd"}, | ||
}, | ||
{ | ||
name: "only entrypoint declared", | ||
dockerfile: ` | ||
FROM scratch | ||
ENTRYPOINT ["myentrypoint"]`, | ||
originalEntrypoint: []string{"myentrypoint"}, | ||
originalCmd: []string{"mycmd"}, | ||
expectedCmd: nil, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
config := &v1.Config{ | ||
Cmd: test.originalCmd, | ||
Entrypoint: test.originalEntrypoint, | ||
} | ||
err := reviewConfig(stage(t, test.dockerfile), config) | ||
testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedCmd, config.Cmd) | ||
}) | ||
} | ||
} | ||
|
||
func stage(t *testing.T, d string) config.KanikoStage { | ||
stages, err := dockerfile.Parse([]byte(d)) | ||
if err != nil { | ||
t.Fatalf("error parsing dockerfile: %v", err) | ||
} | ||
return config.KanikoStage{ | ||
Stage: stages[0], | ||
} | ||
} |
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.
This function does not encounter an error flow and always returns nil.
There is no need to return an error.
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.
done