github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/kaniko/args.go (about)

     1  /*
     2  Copyright 2020 The Skaffold Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package kaniko
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/google/go-containerregistry/pkg/name"
    23  
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
    26  )
    27  
    28  // Args returns kaniko command arguments
    29  func Args(artifact *latest.KanikoArtifact, tag, context string) ([]string, error) {
    30  	args := []string{
    31  		"--destination", tag,
    32  		"--dockerfile", artifact.DockerfilePath,
    33  	}
    34  
    35  	if context != "" {
    36  		args = append(args, "--context", context)
    37  	}
    38  
    39  	buildArgs, err := util.MapToFlag(artifact.BuildArgs, BuildArgsFlag)
    40  	if err != nil {
    41  		return args, err
    42  	}
    43  
    44  	args = append(args, buildArgs...)
    45  
    46  	if artifact.Cache != nil {
    47  		args = append(args, CacheFlag)
    48  
    49  		if artifact.Cache.Repo != "" {
    50  			args = append(args, CacheRepoFlag, artifact.Cache.Repo)
    51  		}
    52  		if artifact.Cache.HostPath != "" {
    53  			args = append(args, CacheDirFlag, artifact.Cache.HostPath)
    54  		}
    55  		if artifact.Cache.TTL != "" {
    56  			args = append(args, CacheTTLFlag, artifact.Cache.TTL)
    57  		}
    58  		if artifact.Cache.CacheCopyLayers {
    59  			args = append(args, CacheCopyLayersFlag)
    60  		}
    61  	}
    62  
    63  	if artifact.Target != "" {
    64  		args = append(args, TargetFlag, artifact.Target)
    65  	}
    66  
    67  	if artifact.Cleanup {
    68  		args = append(args, CleanupFlag)
    69  	}
    70  
    71  	if artifact.DigestFile != "" {
    72  		args = append(args, DigestFileFlag, artifact.DigestFile)
    73  	}
    74  
    75  	if artifact.Force {
    76  		args = append(args, ForceFlag)
    77  	}
    78  
    79  	if artifact.ImageFSExtractRetry != "" {
    80  		args = append(args, ImageFSExtractRetryFlag, artifact.ImageFSExtractRetry)
    81  	}
    82  
    83  	if artifact.ImageNameWithDigestFile != "" {
    84  		args = append(args, ImageNameWithDigestFileFlag, artifact.ImageNameWithDigestFile)
    85  	}
    86  
    87  	if artifact.Insecure {
    88  		args = append(args, InsecureFlag)
    89  	}
    90  
    91  	if artifact.InsecurePull {
    92  		args = append(args, InsecurePullFlag)
    93  	}
    94  
    95  	if artifact.LogFormat != "" {
    96  		args = append(args, LogFormatFlag, artifact.LogFormat)
    97  	}
    98  
    99  	if artifact.LogTimestamp {
   100  		args = append(args, LogTimestampFlag)
   101  	}
   102  
   103  	if artifact.NoPush {
   104  		args = append(args, NoPushFlag)
   105  	}
   106  
   107  	if artifact.OCILayoutPath != "" {
   108  		args = append(args, OCILayoutFlag, artifact.OCILayoutPath)
   109  	}
   110  
   111  	if artifact.RegistryMirror != "" {
   112  		args = append(args, RegistryMirrorFlag, artifact.RegistryMirror)
   113  	}
   114  
   115  	if artifact.Reproducible {
   116  		args = append(args, ReproducibleFlag)
   117  	}
   118  
   119  	if artifact.SingleSnapshot {
   120  		args = append(args, SingleSnapshotFlag)
   121  	}
   122  
   123  	if artifact.SkipTLS {
   124  		args = append(args, SkipTLSFlag)
   125  		reg, err := artifactRegistry(tag)
   126  		if err != nil {
   127  			return nil, err
   128  		}
   129  		args = append(args, SkipTLSVerifyRegistryFlag, reg)
   130  	}
   131  
   132  	if artifact.SkipTLSVerifyPull {
   133  		args = append(args, SkipTLSVerifyPullFlag)
   134  	}
   135  
   136  	if artifact.SkipUnusedStages {
   137  		args = append(args, SkipUnusedStagesFlag)
   138  	}
   139  
   140  	if artifact.SnapshotMode != "" {
   141  		args = append(args, SnapshotModeFlag, artifact.SnapshotMode)
   142  	}
   143  
   144  	if artifact.PushRetry != "" {
   145  		args = append(args, PushRetryFlag, artifact.PushRetry)
   146  	}
   147  
   148  	if artifact.TarPath != "" {
   149  		args = append(args, TarPathFlag, artifact.TarPath)
   150  	}
   151  
   152  	if artifact.UseNewRun {
   153  		args = append(args, UseNewRunFlag)
   154  	}
   155  
   156  	if artifact.Verbosity != "" {
   157  		args = append(args, VerbosityFlag, artifact.Verbosity)
   158  	}
   159  
   160  	if artifact.WhitelistVarRun {
   161  		args = append(args, WhitelistVarRunFlag)
   162  	}
   163  
   164  	var iRegArgs []string
   165  	for _, r := range artifact.InsecureRegistry {
   166  		iRegArgs = append(iRegArgs, InsecureRegistryFlag, r)
   167  	}
   168  	args = append(args, iRegArgs...)
   169  
   170  	var sRegArgs []string
   171  	for _, r := range artifact.SkipTLSVerifyRegistry {
   172  		sRegArgs = append(sRegArgs, SkipTLSVerifyRegistryFlag, r)
   173  	}
   174  	args = append(args, sRegArgs...)
   175  
   176  	registryCertificate, err := util.MapToFlag(artifact.RegistryCertificate, RegistryCertificateFlag)
   177  	if err != nil {
   178  		return args, err
   179  	}
   180  
   181  	args = append(args, registryCertificate...)
   182  
   183  	labels, err := util.MapToFlag(artifact.Label, LabelFlag)
   184  	if err != nil {
   185  		return args, err
   186  	}
   187  
   188  	args = append(args, labels...)
   189  
   190  	return args, nil
   191  }
   192  
   193  func artifactRegistry(i string) (string, error) {
   194  	ref, err := name.ParseReference(i)
   195  	if err != nil {
   196  		return "", fmt.Errorf("unable to retrieve registry url from artifact: %w", err)
   197  	}
   198  	return ref.Context().RegistryStr(), nil
   199  }