github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/cluster/cluster.go (about) 1 /* 2 Copyright 2019 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 cluster 18 19 import ( 20 "context" 21 "fmt" 22 "io" 23 24 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build" 25 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/custom" 26 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/misc" 27 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants" 28 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker" 29 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/platform" 30 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" 31 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" 32 ) 33 34 // Build builds a list of artifacts with Kaniko. 35 func (b *Builder) Build(ctx context.Context, out io.Writer, artifact *latest.Artifact) build.ArtifactBuilder { 36 builder := build.WithLogFile(b.buildArtifact, b.cfg.Muted()) 37 return builder 38 } 39 40 func (b *Builder) PreBuild(ctx context.Context, out io.Writer) error { 41 teardownPullSecret, err := b.setupPullSecret(ctx, out) 42 if err != nil { 43 return fmt.Errorf("setting up pull secret: %w", err) 44 } 45 b.teardownFunc = append(b.teardownFunc, teardownPullSecret) 46 47 if b.DockerConfig != nil { 48 teardownDockerConfigSecret, err := b.setupDockerConfigSecret(ctx, out) 49 if err != nil { 50 return fmt.Errorf("setting up docker config secret: %w", err) 51 } 52 b.teardownFunc = append(b.teardownFunc, teardownDockerConfigSecret) 53 } 54 return nil 55 } 56 57 func (b *Builder) PostBuild(_ context.Context, _ io.Writer) error { 58 for _, f := range b.teardownFunc { 59 f() 60 } 61 return nil 62 } 63 64 func (b *Builder) buildArtifact(ctx context.Context, out io.Writer, artifact *latest.Artifact, tag string, m platform.Matcher) (string, error) { 65 digest, err := b.runBuildForArtifact(ctx, out, artifact, tag, m) 66 if err != nil { 67 return "", err 68 } 69 70 return build.TagWithDigest(tag, digest), nil 71 } 72 73 func (b *Builder) Concurrency() *int { 74 return util.IntPtr(b.ClusterDetails.Concurrency) 75 } 76 77 func (b *Builder) runBuildForArtifact(ctx context.Context, out io.Writer, a *latest.Artifact, tag string, platforms platform.Matcher) (string, error) { 78 // required artifacts as build-args 79 requiredImages := docker.ResolveDependencyImages(a.Dependencies, b.artifactStore, true) 80 switch { 81 case a.KanikoArtifact != nil: 82 return b.buildWithKaniko(ctx, out, a.Workspace, a.ImageName, a.KanikoArtifact, tag, requiredImages, platforms) 83 84 case a.CustomArtifact != nil: 85 return custom.NewArtifactBuilder(nil, b.cfg, true, b.skipTests, append(b.retrieveExtraEnv(), util.EnvPtrMapToSlice(requiredImages, "=")...)).Build(ctx, out, a, tag, platforms) 86 87 default: 88 return "", fmt.Errorf("unexpected type %q for in-cluster artifact:\n%s", misc.ArtifactType(a), misc.FormatArtifact(a)) 89 } 90 } 91 92 func (b *Builder) retrieveExtraEnv() []string { 93 env := []string{ 94 fmt.Sprintf("%s=%s", constants.KubeContext, b.cfg.GetKubeContext()), 95 fmt.Sprintf("%s=%s", constants.Namespace, b.ClusterDetails.Namespace), 96 fmt.Sprintf("%s=%s", constants.PullSecretName, b.ClusterDetails.PullSecretName), 97 fmt.Sprintf("%s=%s", constants.Timeout, b.ClusterDetails.Timeout), 98 } 99 if b.ClusterDetails.DockerConfig != nil { 100 env = append(env, fmt.Sprintf("%s=%s", constants.DockerConfigSecretName, b.ClusterDetails.DockerConfig.SecretName)) 101 } 102 return env 103 }