github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/inspect/buildEnv/add_local.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/schema/latest" 26 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" 27 ) 28 29 func AddLocalBuildEnv(ctx context.Context, out io.Writer, opts inspect.Options) error { 30 formatter := inspect.OutputFormatter(out, opts.OutFormat) 31 cfgs, err := inspect.GetConfigSet(ctx, config.SkaffoldOptions{ConfigurationFile: opts.Filename, ConfigurationFilter: opts.Modules, SkipConfigDefaults: true, MakePathsAbsolute: util.BoolPtr(false)}) 32 if err != nil { 33 formatter.WriteErr(err) 34 return err 35 } 36 if opts.Profile == "" { 37 // empty profile flag implies that the new build env needs to be added to the default pipeline. 38 // for these cases, don't add the new env definition to any configs imported as dependencies. 39 cfgs = cfgs.SelectRootConfigs() 40 for _, cfg := range cfgs { 41 if cfg.Build.LocalBuild != nil && (*cfg.Build.LocalBuild != latest.LocalBuild{}) { 42 formatter.WriteErr(inspect.BuildEnvAlreadyExists(inspect.BuildEnvs.Local, cfg.SourceFile, "")) 43 return err 44 } 45 cfg.Build.LocalBuild = constructLocalDefinition(cfg.Build.LocalBuild, opts.BuildEnvOptions) 46 cfg.Build.GoogleCloudBuild = nil 47 cfg.Build.Cluster = nil 48 } 49 } else { 50 for _, cfg := range cfgs { 51 index := -1 52 for i := range cfg.Profiles { 53 if cfg.Profiles[i].Name == opts.Profile { 54 index = i 55 break 56 } 57 } 58 if index < 0 { 59 index = len(cfg.Profiles) 60 cfg.Profiles = append(cfg.Profiles, latest.Profile{Name: opts.Profile}) 61 } 62 if cfg.Profiles[index].Build.LocalBuild != nil && (*cfg.Profiles[index].Build.LocalBuild != latest.LocalBuild{}) { 63 formatter.WriteErr(inspect.BuildEnvAlreadyExists(inspect.BuildEnvs.Local, cfg.SourceFile, opts.Profile)) 64 return err 65 } 66 cfg.Profiles[index].Build.LocalBuild = constructLocalDefinition(cfg.Profiles[index].Build.LocalBuild, opts.BuildEnvOptions) 67 cfg.Profiles[index].Build.GoogleCloudBuild = nil 68 cfg.Profiles[index].Build.Cluster = nil 69 } 70 } 71 return inspect.MarshalConfigSet(cfgs) 72 } 73 74 func constructLocalDefinition(existing *latest.LocalBuild, opts inspect.BuildEnvOptions) *latest.LocalBuild { 75 var b latest.LocalBuild 76 if existing != nil { 77 b = *existing 78 } 79 if opts.Concurrency >= 0 { 80 b.Concurrency = util.IntPtr(opts.Concurrency) 81 } 82 if opts.Push != nil { 83 b.Push = opts.Push 84 } 85 if opts.TryImportMissing != nil { 86 b.TryImportMissing = *opts.TryImportMissing 87 } 88 if opts.UseDockerCLI != nil { 89 b.UseDockerCLI = *opts.UseDockerCLI 90 } 91 b.UseBuildkit = opts.UseBuildkit 92 return &b 93 }