github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/inspect/buildEnv/modify_gcb.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  
    23  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/inspect"
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
    26  )
    27  
    28  func ModifyGcbBuildEnv(ctx context.Context, out io.Writer, opts inspect.Options) error {
    29  	formatter := inspect.OutputFormatter(out, opts.OutFormat)
    30  	cfgs, err := inspect.GetConfigSet(ctx, config.SkaffoldOptions{ConfigurationFile: opts.Filename, ConfigurationFilter: opts.Modules, SkipConfigDefaults: true, MakePathsAbsolute: util.BoolPtr(false)})
    31  	if err != nil {
    32  		formatter.WriteErr(err)
    33  		return err
    34  	}
    35  	if opts.Profile == "" {
    36  		// empty profile flag implies that only modify the default pipelines in the target `skaffold.yaml`
    37  		cfgs = cfgs.SelectRootConfigs()
    38  		for _, cfg := range cfgs {
    39  			if cfg.Build.GoogleCloudBuild == nil {
    40  				if opts.Strict {
    41  					formatter.WriteErr(inspect.BuildEnvNotFound(inspect.BuildEnvs.GoogleCloudBuild, cfg.SourceFile, ""))
    42  					return err
    43  				}
    44  				continue
    45  			}
    46  			cfg.Build.GoogleCloudBuild = constructGcbDefinition(cfg.Build.GoogleCloudBuild, opts.BuildEnvOptions)
    47  			cfg.Build.LocalBuild = nil
    48  			cfg.Build.Cluster = nil
    49  		}
    50  	} else {
    51  		profileFound := false
    52  		for _, cfg := range cfgs {
    53  			index := -1
    54  			for i := range cfg.Profiles {
    55  				if cfg.Profiles[i].Name == opts.Profile {
    56  					index = i
    57  					break
    58  				}
    59  			}
    60  			if index < 0 {
    61  				continue
    62  			}
    63  			profileFound = true
    64  			if cfg.Profiles[index].Build.GoogleCloudBuild == nil {
    65  				if opts.Strict {
    66  					formatter.WriteErr(inspect.BuildEnvNotFound(inspect.BuildEnvs.GoogleCloudBuild, cfg.SourceFile, opts.Profile))
    67  					return err
    68  				}
    69  				continue
    70  			}
    71  			cfg.Profiles[index].Build.GoogleCloudBuild = constructGcbDefinition(cfg.Profiles[index].Build.GoogleCloudBuild, opts.BuildEnvOptions)
    72  			cfg.Profiles[index].Build.LocalBuild = nil
    73  			cfg.Profiles[index].Build.Cluster = nil
    74  		}
    75  		if !profileFound {
    76  			formatter.WriteErr(inspect.ProfileNotFound(opts.Profile))
    77  			return err
    78  		}
    79  	}
    80  	return inspect.MarshalConfigSet(cfgs)
    81  }