-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
run.go
101 lines (83 loc) · 2.9 KB
/
run.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
/*
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 kaniko
import (
"context"
"fmt"
"io"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/kaniko/sources"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func (b *Builder) run(ctx context.Context, out io.Writer, artifact *latest.Artifact, tag string) (string, error) {
if artifact.DockerArtifact == nil {
return "", errors.New("kaniko builder supports only Docker artifacts")
}
// Prepare context
s := sources.Retrieve(b.KanikoBuild)
dependencies, err := b.DependenciesForArtifact(ctx, artifact)
if err != nil {
return "", errors.Wrapf(err, "getting dependencies for %s", artifact.ImageName)
}
context, err := s.Setup(ctx, out, artifact, util.RandomID(), dependencies)
if err != nil {
return "", errors.Wrap(err, "setting up build context")
}
defer s.Cleanup(ctx)
// Create pod spec
args := []string{
"--dockerfile", artifact.DockerArtifact.DockerfilePath,
"--context", context,
"--destination", tag,
"-v", logLevel().String()}
args = append(args, b.AdditionalFlags...)
args = append(args, docker.GetBuildArgs(artifact.DockerArtifact)...)
if b.Cache != nil {
args = append(args, "--cache=true")
if b.Cache.Repo != "" {
args = append(args, fmt.Sprintf("--cache-repo=%s", b.Cache.Repo))
}
}
podSpec := s.Pod(args)
// Create pod
client, err := kubernetes.GetClientset()
if err != nil {
return "", errors.Wrap(err, "")
}
pods := client.CoreV1().Pods(b.Namespace)
pod, err := pods.Create(podSpec)
if err != nil {
return "", errors.Wrap(err, "creating kaniko pod")
}
defer func() {
if err := pods.Delete(pod.Name, &metav1.DeleteOptions{
GracePeriodSeconds: new(int64),
}); err != nil {
logrus.Fatalf("deleting pod: %s", err)
}
}()
if err := s.ModifyPod(ctx, pod); err != nil {
return "", errors.Wrap(err, "modifying kaniko pod")
}
waitForLogs := streamLogs(out, pod.Name, pods)
if err := kubernetes.WaitForPodComplete(ctx, pods, pod.Name, b.timeout); err != nil {
return "", errors.Wrap(err, "waiting for pod to complete")
}
waitForLogs()
return docker.RemoteDigest(tag)
}