forked from GoogleContainerTools/skaffold
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocal.go
127 lines (101 loc) · 4.18 KB
/
local.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Copyright 2019 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package local
import (
"context"
"fmt"
"io"
"strings"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/tag"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/jib"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/plugin/builders/bazel"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// Build runs a docker build on the host and tags the resulting image with
// its checksum. It streams build progress to the writer argument.
func (b *Builder) Build(ctx context.Context, out io.Writer, tags tag.ImageTags, artifacts []*latest.Artifact) ([]build.Artifact, error) {
if b.localCluster {
color.Default.Fprintf(out, "Found [%s] context, using local docker daemon.\n", b.kubeContext)
}
defer b.localDocker.Close()
// TODO(dgageot): parallel builds
return build.InSequence(ctx, out, tags, artifacts, b.buildArtifact)
}
func (b *Builder) buildArtifact(ctx context.Context, out io.Writer, artifact *latest.Artifact, tag string) (string, error) {
digestOrImageID, err := b.runBuildForArtifact(ctx, out, artifact, tag)
if err != nil {
return "", errors.Wrap(err, "build artifact")
}
if b.pushImages {
digest := digestOrImageID
return tag + "@" + digest, nil
}
// k8s doesn't recognize the imageID or any combination of the image name
// suffixed with the imageID, as a valid image name.
// So, the solution we chose is to create a tag, just for Skaffold, from
// the imageID, and use that in the manifests.
imageID := digestOrImageID
uniqueTag := artifact.ImageName + ":" + strings.TrimPrefix(imageID, "sha256:")
if err := b.localDocker.Tag(ctx, imageID, uniqueTag); err != nil {
return "", err
}
return uniqueTag, nil
}
func (b *Builder) runBuildForArtifact(ctx context.Context, out io.Writer, artifact *latest.Artifact, tag string) (string, error) {
switch {
case artifact.DockerArtifact != nil:
return b.buildDocker(ctx, out, artifact, tag)
case artifact.BazelArtifact != nil:
return b.buildBazel(ctx, out, artifact, tag)
case artifact.JibMavenArtifact != nil:
return b.buildJibMaven(ctx, out, artifact.Workspace, artifact.JibMavenArtifact, tag)
case artifact.JibGradleArtifact != nil:
return b.buildJibGradle(ctx, out, artifact.Workspace, artifact.JibGradleArtifact, tag)
default:
return "", fmt.Errorf("undefined artifact type: %+v", artifact.ArtifactType)
}
}
func (b *Builder) DependenciesForArtifact(ctx context.Context, a *latest.Artifact) ([]string, error) {
var (
paths []string
err error
)
switch {
case a.DockerArtifact != nil:
paths, err = docker.GetDependencies(ctx, a.Workspace, a.DockerArtifact)
case a.BazelArtifact != nil:
paths, err = bazel.GetDependencies(ctx, a.Workspace, a.BazelArtifact)
case a.JibMavenArtifact != nil:
paths, err = jib.GetDependenciesMaven(ctx, a.Workspace, a.JibMavenArtifact)
case a.JibGradleArtifact != nil:
paths, err = jib.GetDependenciesGradle(ctx, a.Workspace, a.JibGradleArtifact)
default:
return nil, fmt.Errorf("undefined artifact type: %+v", a.ArtifactType)
}
if err != nil {
// if the context was cancelled act as if all is well
// TODO(dgageot): this should be even higher in the call chain.
if ctx.Err() == context.Canceled {
logrus.Debugln(errors.Wrap(err, "ignore error since context is cancelled"))
return nil, nil
}
return nil, err
}
return util.AbsolutePaths(a.Workspace, paths), nil
}