Misc. small changes/refactoring (#712)

This commit is contained in:
Taylor Barrella 2019-07-23 18:10:22 -04:00 committed by Sharif Elgamal
parent 19fb253e9c
commit 3422d5572a
9 changed files with 24 additions and 31 deletions

View File

@ -208,12 +208,12 @@ func resolveSourceContext() error {
} }
if opts.Bucket != "" { if opts.Bucket != "" {
if !strings.Contains(opts.Bucket, "://") { if !strings.Contains(opts.Bucket, "://") {
// if no prefix use Google Cloud Storage as default for backwards compatibility
opts.SrcContext = constants.GCSBuildContextPrefix + opts.Bucket opts.SrcContext = constants.GCSBuildContextPrefix + opts.Bucket
} else { } else {
opts.SrcContext = opts.Bucket opts.SrcContext = opts.Bucket
} }
} }
// if no prefix use Google Cloud Storage as default for backwards compatibility
contextExecutor, err := buildcontext.GetBuildContext(opts.SrcContext) contextExecutor, err := buildcontext.GetBuildContext(opts.SrcContext)
if err != nil { if err != nil {
return err return err

View File

@ -43,7 +43,7 @@ type AddCommand struct {
// - If remote file has HTTP Last-Modified header, we set the mtime of the file to that timestamp // - If remote file has HTTP Last-Modified header, we set the mtime of the file to that timestamp
// - If dest doesn't end with a slash, the filepath is inferred to be <dest>/<filename> // - If dest doesn't end with a slash, the filepath is inferred to be <dest>/<filename>
// 2. If <src> is a local tar archive: // 2. If <src> is a local tar archive:
// -If <src> is a local tar archive, it is unpacked at the dest, as 'tar -x' would // - it is unpacked at the dest, as 'tar -x' would
func (a *AddCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error { func (a *AddCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error {
replacementEnvs := buildArgs.ReplacementEnvs(config.Env) replacementEnvs := buildArgs.ReplacementEnvs(config.Env)

View File

@ -54,7 +54,7 @@ func (r *ExposeCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.
} }
protocol := strings.Split(p, "/")[1] protocol := strings.Split(p, "/")[1]
if !validProtocol(protocol) { if !validProtocol(protocol) {
return fmt.Errorf("Invalid protocol: %s", protocol) return fmt.Errorf("invalid protocol: %s", protocol)
} }
logrus.Infof("Adding exposed port: %s", p) logrus.Infof("Adding exposed port: %s", p)
existingPorts[p] = struct{}{} existingPorts[p] = struct{}{}

View File

@ -54,7 +54,7 @@ func (v *VolumeCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.
if _, err := os.Stat(volume); os.IsNotExist(err) { if _, err := os.Stat(volume); os.IsNotExist(err) {
logrus.Infof("Creating directory %s", volume) logrus.Infof("Creating directory %s", volume)
if err := os.MkdirAll(volume, 0755); err != nil { if err := os.MkdirAll(volume, 0755); err != nil {
return fmt.Errorf("Could not create directory for volume %s: %s", volume, err) return fmt.Errorf("could not create directory for volume %s: %s", volume, err)
} }
} }
} }

View File

@ -111,7 +111,7 @@ func Parse(b []byte) ([]instructions.Stage, []instructions.ArgCommand, error) {
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
return stages, metaArgs, err return stages, metaArgs, nil
} }
// targetStage returns the index of the target stage kaniko is trying to build // targetStage returns the index of the target stage kaniko is trying to build

View File

@ -103,13 +103,13 @@ func (l *LayeredMap) Add(s string) error {
// Use hash function and add to layers // Use hash function and add to layers
newV, err := l.hasher(s) newV, err := l.hasher(s)
if err != nil { if err != nil {
return fmt.Errorf("Error creating hash for %s: %v", s, err) return fmt.Errorf("error creating hash for %s: %v", s, err)
} }
l.layers[len(l.layers)-1][s] = newV l.layers[len(l.layers)-1][s] = newV
return nil return nil
} }
// CheckFileChange checkes whether a given file changed // CheckFileChange checks whether a given file changed
// from the current layered map by its hashing function. // from the current layered map by its hashing function.
// Returns true if the file is changed. // Returns true if the file is changed.
func (l *LayeredMap) CheckFileChange(s string) (bool, error) { func (l *LayeredMap) CheckFileChange(s string) (bool, error) {

View File

@ -80,7 +80,7 @@ func (s *Snapshotter) TakeSnapshot(files []string) (string, error) {
// Add files to the layered map // Add files to the layered map
for _, file := range filesToAdd { for _, file := range filesToAdd {
if err := s.l.Add(file); err != nil { if err := s.l.Add(file); err != nil {
return "", fmt.Errorf("Unable to add file %s to layered map: %s", file, err) return "", fmt.Errorf("unable to add file %s to layered map: %s", file, err)
} }
} }
@ -183,13 +183,13 @@ func (s *Snapshotter) scanFullFilesystem() ([]string, []string, error) {
} }
} }
// Also add parent directories to keep the permission of them correctly. // Also add parent directories to keep their permissions correctly.
filesToAdd = filesWithParentDirs(filesToAdd) filesToAdd = filesWithParentDirs(filesToAdd)
// Add files to the layered map // Add files to the layered map
for _, file := range filesToAdd { for _, file := range filesToAdd {
if err := s.l.Add(file); err != nil { if err := s.l.Add(file); err != nil {
return nil, nil, fmt.Errorf("Unable to add file %s to layered map: %s", file, err) return nil, nil, fmt.Errorf("unable to add file %s to layered map: %s", file, err)
} }
} }

View File

@ -55,7 +55,7 @@ func ResolveEnvironmentReplacementList(values, envs []string, isFilepath bool) (
// ResolveEnvironmentReplacement resolves replacing env variables in some text from envs // ResolveEnvironmentReplacement resolves replacing env variables in some text from envs
// It takes in a string representation of the command, the value to be resolved, and a list of envs (config.Env) // It takes in a string representation of the command, the value to be resolved, and a list of envs (config.Env)
// Ex: fp = $foo/newdir, envs = [foo=/foodir], then this should return /foodir/newdir // Ex: value = $foo/newdir, envs = [foo=/foodir], then this should return /foodir/newdir
// The dockerfile/shell package handles processing env values // The dockerfile/shell package handles processing env values
// It handles escape characters and supports expansion from the config.Env array // It handles escape characters and supports expansion from the config.Env array
// Shlex handles some of the following use cases (these and more are tested in integration tests) // Shlex handles some of the following use cases (these and more are tested in integration tests)
@ -325,13 +325,12 @@ func GetUserFromUsername(userStr string, groupStr string) (string, string, error
// Lookup by username // Lookup by username
userObj, err := user.Lookup(userStr) userObj, err := user.Lookup(userStr)
if err != nil { if err != nil {
if _, ok := err.(user.UnknownUserError); ok { if _, ok := err.(user.UnknownUserError); !ok {
// Lookup by id return "", "", err
userObj, err = user.LookupId(userStr) }
if err != nil { // Lookup by id
return "", "", err userObj, err = user.LookupId(userStr)
} if err != nil {
} else {
return "", "", err return "", "", err
} }
} }
@ -341,12 +340,11 @@ func GetUserFromUsername(userStr string, groupStr string) (string, string, error
if groupStr != "" { if groupStr != "" {
group, err = user.LookupGroup(groupStr) group, err = user.LookupGroup(groupStr)
if err != nil { if err != nil {
if _, ok := err.(user.UnknownGroupError); ok { if _, ok := err.(user.UnknownGroupError); !ok {
group, err = user.LookupGroupId(groupStr) return "", "", err
if err != nil { }
return "", "", err group, err = user.LookupGroupId(groupStr)
} if err != nil {
} else {
return "", "", err return "", "", err
} }
} }

View File

@ -188,7 +188,7 @@ func extractFile(dest string, hdr *tar.Header, tr io.Reader) error {
switch hdr.Typeflag { switch hdr.Typeflag {
case tar.TypeReg: case tar.TypeReg:
logrus.Debugf("creating file %s", path) logrus.Debugf("creating file %s", path)
// It's possible a file is in the tar before it's directory. // It's possible a file is in the tar before its directory.
if _, err := os.Stat(dir); os.IsNotExist(err) { if _, err := os.Stat(dir); os.IsNotExist(err) {
logrus.Debugf("base %s for file %s does not exist. Creating.", base, path) logrus.Debugf("base %s for file %s does not exist. Creating.", base, path)
if err := os.MkdirAll(dir, 0755); err != nil { if err := os.MkdirAll(dir, 0755); err != nil {
@ -288,12 +288,7 @@ func checkWhitelistRoot(root string) bool {
if root == constants.RootDir { if root == constants.RootDir {
return false return false
} }
for _, wl := range whitelist { return CheckWhitelist(root)
if HasFilepathPrefix(root, wl.Path, wl.PrefixMatchOnly) {
return true
}
}
return false
} }
// Get whitelist from roots of mounted files // Get whitelist from roots of mounted files