Reorganize kaniko code
@@ -26,7 +26,6 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/kaniko/sources"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
-
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
@@ -40,41 +39,41 @@ import (
func runKaniko(ctx context.Context, out io.Writer, artifact *latest.Artifact, cfg *latest.KanikoBuild) (string, error) {
initialTag := util.RandomID()
+ timeout, err := time.ParseDuration(cfg.Timeout)
+ if err != nil {
+ return "", errors.Wrap(err, "parsing timeout")
+ }
+
s, err := sources.Retrieve(cfg)
if err != nil {
return "", errors.Wrap(err, "retrieving build context")
}
+
context, err := s.Setup(ctx, artifact, cfg, initialTag)
if err != nil {
return "", errors.Wrap(err, "setting up build context")
}
defer s.Cleanup(ctx, cfg)
- dockerfilePath := artifact.DockerArtifact.DockerfilePath
client, err := kubernetes.GetClientset()
if err != nil {
return "", errors.Wrap(err, "")
}
- pods := client.CoreV1().Pods(cfg.Namespace)
imageDst := fmt.Sprintf("%s:%s", artifact.ImageName, initialTag)
args := []string{
- fmt.Sprintf("--dockerfile=%s", dockerfilePath),
+ fmt.Sprintf("--dockerfile=%s", artifact.DockerArtifact.DockerfilePath),
fmt.Sprintf("--context=%s", context),
fmt.Sprintf("--destination=%s", imageDst),
fmt.Sprintf("-v=%s", logrus.GetLevel().String()),
}
args = append(args, docker.GetBuildArgs(artifact.DockerArtifact)...)
+ pods := client.CoreV1().Pods(cfg.Namespace)
p, err := pods.Create(s.Pod(cfg, args))
if err != nil {
return "", errors.Wrap(err, "creating kaniko pod")
}
- if err := s.ModifyPod(p); err != nil {
- return "", errors.Wrap(err, "modifying kaniko pod")
- }
- waitForLogs := streamLogs(out, p.Name, pods)
-
defer func() {
if err := pods.Delete(p.Name, &metav1.DeleteOptions{
GracePeriodSeconds: new(int64),
@@ -83,11 +82,12 @@ func runKaniko(ctx context.Context, out io.Writer, artifact *latest.Artifact, cf
}
}()
- timeout, err := time.ParseDuration(cfg.Timeout)
- if err != nil {
- return "", errors.Wrap(err, "parsing timeout")
+ if err := s.ModifyPod(p); err != nil {
+ return "", errors.Wrap(err, "modifying kaniko pod")
}
+ waitForLogs := streamLogs(out, p.Name, pods)
+
if err := kubernetes.WaitForPodComplete(pods, p.Name, timeout); err != nil {
return "", errors.Wrap(err, "waiting for pod to complete")
}
@@ -110,13 +110,14 @@ func streamLogs(out io.Writer, name string, pods corev1.PodInterface) func() {
Follow: true,
Container: constants.DefaultKanikoContainerName,
}).Stream()
- if err == nil {
- io.Copy(out, r)
- return
- }
-
+ if err != nil {
logrus.Debugln("unable to get kaniko pod logs:", err)
time.Sleep(1 * time.Second)
+ continue
+ }
+
+ io.Copy(out, r)
+ return
}
}()
