Skip to content

Commit eb46bbb

Browse files
committed
Implement image pull according to ImatePullPolicy
fix #1 Signed-off-by: Nicolas De Loof <[email protected]>
1 parent e377d24 commit eb46bbb

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/ndeloof/kitten
33
go 1.12
44

55
require (
6+
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
67
github.com/davecgh/go-spew v1.1.1 // indirect
78
github.com/docker/distribution v2.7.1+incompatible // indirect
89
github.com/docker/docker v0.0.0-20180422163414-57142e89befe

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
2+
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw=
3+
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk=
24
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
35
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
46
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -35,6 +37,8 @@ github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
3537
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
3638
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
3739
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
40+
github.com/gotestyourself/gotestyourself v2.2.0+incompatible h1:AQwinXlbQR2HvPjQZOmDhRqsv5mZf+Jb1RnSLxcqZcI=
41+
github.com/gotestyourself/gotestyourself v2.2.0+incompatible/go.mod h1:zZKM6oeNM8k+FRljX1mnzVYeS8wiGgQyvST1/GafPbY=
3842
github.com/goware/prefixer v0.0.0-20160118172347-395022866408 h1:Y9iQJfEqnN3/Nce9cOegemcy/9Ai5k3huT6E80F3zaw=
3943
github.com/goware/prefixer v0.0.0-20160118172347-395022866408/go.mod h1:PE1ycukgRPJ7bJ9a1fdfQ9j8i/cEcRAoLZzbxYpNB/s=
4044
github.com/hashicorp/golang-lru v0.5.0 h1:CL2msUPvZTLb5O648aiLNJw3hnBxN2+1Jq8rCOH9wdo=
@@ -116,6 +120,8 @@ gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
116120
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
117121
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
118122
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
123+
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
124+
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
119125
k8s.io/api v0.0.0-20190222213804-5cb15d344471 h1:MzQGt8qWQCR+39kbYRd0uQqsvSidpYqJLFeWiJ9l4OE=
120126
k8s.io/api v0.0.0-20190222213804-5cb15d344471/go.mod h1:iuAfoD4hCxJ8Onx9kaTIt30j7jUFS00AXQi6QMi99vA=
121127
k8s.io/api v0.0.0-20190515023547-db5a9d1c40eb h1:z1fFVKHVQNtGcAPbYljoW2rZT+0ITuj99cmGH9RBrWE=

pkg/crds/testdata/pipeline.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
apiVersion: tekton.dev/v1alpha1
2+
kind: PipelineRun
3+
metadata:
4+
name: demo
5+
spec:
6+
pipelineRef:
7+
name: sample-pipeline
8+
---
9+
apiVersion: tekton.dev/v1alpha1
210
kind: Pipeline
311
metadata:
412
name: sample-pipeline
@@ -15,5 +23,5 @@ metadata:
1523
spec:
1624
steps:
1725
- name: foo
18-
image: alpine
26+
image: ubuntu
1927
command: ['echo', 'hello foo']

pkg/kitten/kitten.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import (
88
"github.com/goware/prefixer"
99
pipeline "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
1010
"github.com/tektoncd/pipeline/pkg/reconciler/v1alpha1/pipeline/dag"
11+
k8s "k8s.io/api/core/v1"
1112

13+
"encoding/json"
1214
"fmt"
1315
"github.com/docker/docker/client"
16+
message "github.com/docker/docker/pkg/jsonmessage"
1417
"github.com/ndeloof/kitten/pkg/crds"
1518
"io"
1619
"os"
@@ -91,7 +94,42 @@ func RunTask(ctx context.Context, pt pipeline.PipelineTask, task pipeline.Task,
9194
cmd = append(cmd, s.Command...)
9295
cmd = append(cmd, s.Args...)
9396

94-
cli.ImagePull(ctx, s.Image, types.ImagePullOptions{})
97+
pull := s.ImagePullPolicy == k8s.PullAlways
98+
if s.ImagePullPolicy != k8s.PullNever {
99+
_, _, err := cli.ImageInspectWithRaw(ctx, s.Image)
100+
if err != nil {
101+
if client.IsErrNotFound(err) {
102+
pull = true
103+
} else {
104+
return 0, fmt.Errorf("can't inspect image. %v", err)
105+
}
106+
}
107+
}
108+
109+
if pull {
110+
fmt.Printf("Pulling Docker image %s\n", s.Image)
111+
resp, err := cli.ImagePull(ctx, s.Image, types.ImagePullOptions{})
112+
if err != nil {
113+
return 0, fmt.Errorf("can't pull docker image. %v", err)
114+
}
115+
defer resp.Close()
116+
decoder := json.NewDecoder(resp)
117+
for {
118+
var msg message.JSONMessage
119+
err := decoder.Decode(&msg)
120+
if err == io.EOF {
121+
// Completed
122+
break
123+
}
124+
if err != nil {
125+
return 0, fmt.Errorf("failed to pull docker image. %v", err)
126+
}
127+
if msg.Error != nil {
128+
return 0, fmt.Errorf("failed to pull docker image. %v", msg.Error)
129+
}
130+
fmt.Println(msg.ProgressMessage)
131+
}
132+
}
95133

96134
c, err := cli.ContainerCreate(ctx,
97135
&container.Config{

0 commit comments

Comments
 (0)