github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/inspect/buildEnv/add_cluster.go (about)

     1  /*
     2  Copyright 2021 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 inspect
    18  
    19  import (
    20  	"context"
    21  	"io"
    22  	"reflect"
    23  
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/inspect"
    26  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    27  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
    28  )
    29  
    30  func AddClusterBuildEnv(ctx context.Context, out io.Writer, opts inspect.Options) error {
    31  	formatter := inspect.OutputFormatter(out, opts.OutFormat)
    32  	cfgs, err := inspect.GetConfigSet(ctx, config.SkaffoldOptions{
    33  		ConfigurationFile:   opts.Filename,
    34  		RepoCacheDir:        opts.RepoCacheDir,
    35  		ConfigurationFilter: opts.Modules,
    36  		SkipConfigDefaults:  true,
    37  		MakePathsAbsolute:   util.BoolPtr(false),
    38  	})
    39  	if err != nil {
    40  		formatter.WriteErr(err)
    41  		return err
    42  	}
    43  	if opts.Profile == "" {
    44  		// empty profile flag implies that the new build env needs to be added to the default pipeline.
    45  		// for these cases, don't add the new env definition to any configs imported as dependencies.
    46  		cfgs = cfgs.SelectRootConfigs()
    47  		for _, cfg := range cfgs {
    48  			if cfg.Build.Cluster != nil && !reflect.DeepEqual(cfg.Build.Cluster, &latest.ClusterDetails{}) {
    49  				formatter.WriteErr(inspect.BuildEnvAlreadyExists(inspect.BuildEnvs.Cluster, cfg.SourceFile, ""))
    50  				return err
    51  			}
    52  			cfg.Build.GoogleCloudBuild = nil
    53  			cfg.Build.LocalBuild = nil
    54  			cfg.Build.Cluster = constructClusterDefinition(cfg.Build.Cluster, opts.BuildEnvOptions)
    55  		}
    56  	} else {
    57  		for _, cfg := range cfgs {
    58  			index := -1
    59  			for i := range cfg.Profiles {
    60  				if cfg.Profiles[i].Name == opts.Profile {
    61  					index = i
    62  					break
    63  				}
    64  			}
    65  			if index < 0 {
    66  				index = len(cfg.Profiles)
    67  				cfg.Profiles = append(cfg.Profiles, latest.Profile{Name: opts.Profile})
    68  			}
    69  			if cfg.Profiles[index].Build.Cluster != nil && !reflect.DeepEqual(cfg.Profiles[index].Build.Cluster, &latest.ClusterDetails{}) {
    70  				formatter.WriteErr(inspect.BuildEnvAlreadyExists(inspect.BuildEnvs.Cluster, cfg.SourceFile, opts.Profile))
    71  				return err
    72  			}
    73  			cfg.Profiles[index].Build.GoogleCloudBuild = nil
    74  			cfg.Profiles[index].Build.LocalBuild = nil
    75  			cfg.Profiles[index].Build.Cluster = constructClusterDefinition(cfg.Build.Cluster, opts.BuildEnvOptions)
    76  
    77  			addProfileActivationStanza(cfg, opts.Profile)
    78  		}
    79  	}
    80  	return inspect.MarshalConfigSet(cfgs)
    81  }
    82  
    83  func constructClusterDefinition(existing *latest.ClusterDetails, opts inspect.BuildEnvOptions) *latest.ClusterDetails {
    84  	var b latest.ClusterDetails
    85  	if existing != nil {
    86  		b = *existing
    87  	}
    88  	if opts.PullSecretPath != "" {
    89  		b.PullSecretPath = opts.PullSecretPath
    90  	}
    91  	if opts.PullSecretName != "" {
    92  		b.PullSecretName = opts.PullSecretName
    93  	}
    94  	if opts.PullSecretMountPath != "" {
    95  		b.PullSecretMountPath = opts.PullSecretMountPath
    96  	}
    97  	if opts.Namespace != "" {
    98  		b.Namespace = opts.Namespace
    99  	}
   100  	if opts.DockerConfigPath != "" {
   101  		if b.DockerConfig == nil {
   102  			b.DockerConfig = &latest.DockerConfig{}
   103  		}
   104  		b.DockerConfig.Path = opts.DockerConfigPath
   105  	}
   106  	if opts.DockerConfigSecretName != "" {
   107  		if b.DockerConfig == nil {
   108  			b.DockerConfig = &latest.DockerConfig{}
   109  		}
   110  		b.DockerConfig.SecretName = opts.DockerConfigSecretName
   111  	}
   112  	if opts.ServiceAccount != "" {
   113  		b.ServiceAccountName = opts.ServiceAccount
   114  	}
   115  	if opts.RunAsUser >= 0 {
   116  		b.RunAsUser = &opts.RunAsUser
   117  	}
   118  	if opts.RandomPullSecret {
   119  		b.RandomPullSecret = opts.RandomPullSecret
   120  	}
   121  	if opts.RandomDockerConfigSecret {
   122  		b.RandomDockerConfigSecret = opts.RandomDockerConfigSecret
   123  	}
   124  	if opts.Concurrency >= 0 {
   125  		b.Concurrency = opts.Concurrency
   126  	}
   127  	if opts.Timeout != "" {
   128  		b.Timeout = opts.Timeout
   129  	}
   130  	return &b
   131  }