Browse code

Read insecure annotation on an image stream when import-image is invoked.

Maciej Szulik authored on 2016/04/11 22:06:43
Showing 2 changed files
... ...
@@ -374,13 +374,15 @@ func (o *ImportImageOptions) createImageImport() (*imageapi.ImageStream, *imagea
374 374
 		},
375 375
 		Spec: imageapi.ImageStreamImportSpec{Import: true},
376 376
 	}
377
+	insecureAnnotation := stream.Annotations[imageapi.InsecureRepositoryAnnotation]
378
+	insecure := o.Insecure || insecureAnnotation == "true"
377 379
 	if o.All {
378 380
 		isi.Spec.Repository = &imageapi.RepositoryImportSpec{
379 381
 			From: kapi.ObjectReference{
380 382
 				Kind: "DockerImage",
381 383
 				Name: from,
382 384
 			},
383
-			ImportPolicy: imageapi.TagImportPolicy{Insecure: o.Insecure},
385
+			ImportPolicy: imageapi.TagImportPolicy{Insecure: insecure},
384 386
 		}
385 387
 	} else {
386 388
 		isi.Spec.Images = append(isi.Spec.Images, imageapi.ImageImportSpec{
... ...
@@ -389,7 +391,7 @@ func (o *ImportImageOptions) createImageImport() (*imageapi.ImageStream, *imagea
389 389
 				Name: from,
390 390
 			},
391 391
 			To:           &kapi.LocalObjectReference{Name: tag},
392
-			ImportPolicy: imageapi.TagImportPolicy{Insecure: o.Insecure},
392
+			ImportPolicy: imageapi.TagImportPolicy{Insecure: insecure},
393 393
 		})
394 394
 	}
395 395
 
... ...
@@ -14,11 +14,11 @@ import (
14 14
 
15 15
 func TestCreateImageImport(t *testing.T) {
16 16
 	testCases := []struct {
17
-		name   string
18
-		stream *imageapi.ImageStream
19
-		all    bool
20
-		err    string
21
-		from   []kapi.ObjectReference
17
+		name     string
18
+		stream   *imageapi.ImageStream
19
+		all      bool
20
+		err      string
21
+		expected []imageapi.ImageImportSpec
22 22
 	}{
23 23
 		{
24 24
 			// 0: checking import's from when only .spec.dockerImageRepository is set, no status
... ...
@@ -33,12 +33,12 @@ func TestCreateImageImport(t *testing.T) {
33 33
 					Tags: make(map[string]imageapi.TagReference),
34 34
 				},
35 35
 			},
36
-			from: []kapi.ObjectReference{
37
-				{
36
+			expected: []imageapi.ImageImportSpec{{
37
+				From: kapi.ObjectReference{
38 38
 					Kind: "DockerImage",
39 39
 					Name: "repo.com/somens/someimage",
40 40
 				},
41
-			},
41
+			}},
42 42
 		},
43 43
 		{
44 44
 			// 1: checking import's from when only .spec.dockerImageRepository is set, no status (with all flag set)
... ...
@@ -54,12 +54,12 @@ func TestCreateImageImport(t *testing.T) {
54 54
 				},
55 55
 			},
56 56
 			all: true,
57
-			from: []kapi.ObjectReference{
58
-				{
57
+			expected: []imageapi.ImageImportSpec{{
58
+				From: kapi.ObjectReference{
59 59
 					Kind: "DockerImage",
60 60
 					Name: "repo.com/somens/someimage",
61 61
 				},
62
-			},
62
+			}},
63 63
 		},
64 64
 		{
65 65
 			// 2: with --all flag only .spec.dockerImageRepository is handled
... ...
@@ -107,12 +107,12 @@ func TestCreateImageImport(t *testing.T) {
107 107
 					},
108 108
 				},
109 109
 			},
110
-			from: []kapi.ObjectReference{
111
-				{
110
+			expected: []imageapi.ImageImportSpec{{
111
+				From: kapi.ObjectReference{
112 112
 					Kind: "DockerImage",
113 113
 					Name: "repo.com/somens/someimage:latest",
114 114
 				},
115
-			},
115
+			}},
116 116
 		},
117 117
 		{
118 118
 			// 5: import latest from image stream which has only tags specified and no latest
... ...
@@ -132,6 +132,28 @@ func TestCreateImageImport(t *testing.T) {
132 132
 			},
133 133
 			err: "does not exist on the image stream",
134 134
 		},
135
+		{
136
+			// 6: insecure annotation should be applied to tags if exists
137
+			name: "testis",
138
+			stream: &imageapi.ImageStream{
139
+				ObjectMeta: kapi.ObjectMeta{
140
+					Name:        "testis",
141
+					Namespace:   "other",
142
+					Annotations: map[string]string{imageapi.InsecureRepositoryAnnotation: "true"},
143
+				},
144
+				Spec: imageapi.ImageStreamSpec{
145
+					DockerImageRepository: "repo.com/somens/someimage",
146
+					Tags: make(map[string]imageapi.TagReference),
147
+				},
148
+			},
149
+			expected: []imageapi.ImageImportSpec{{
150
+				From: kapi.ObjectReference{
151
+					Kind: "DockerImage",
152
+					Name: "repo.com/somens/someimage",
153
+				},
154
+				ImportPolicy: imageapi.TagImportPolicy{Insecure: true},
155
+			}},
156
+		},
135 157
 	}
136 158
 
137 159
 	for idx, test := range testCases {
... ...
@@ -164,16 +186,21 @@ func TestCreateImageImport(t *testing.T) {
164 164
 		}
165 165
 		// check values
166 166
 		if test.all {
167
-			if !kapi.Semantic.DeepEqual(isi.Spec.Repository.From, test.from[0]) {
168
-				t.Errorf("(%d) unexpected import spec, expected %#v, got %#v", idx, test.from[0], isi.Spec.Repository.From)
167
+			if !kapi.Semantic.DeepEqual(isi.Spec.Repository.From, test.expected[0].From) {
168
+				t.Errorf("(%d) unexpected import spec, expected %#v, got %#v", idx, test.expected[0].From, isi.Spec.Repository.From)
169 169
 			}
170 170
 		} else {
171
-			if len(isi.Spec.Images) != len(test.from) {
172
-				t.Errorf("(%d) unexpected number of images, expected %d, got %d", idx, len(test.from), len(isi.Spec.Images))
171
+			if len(isi.Spec.Images) != len(test.expected) {
172
+				t.Errorf("(%d) unexpected number of images, expected %d, got %d", idx, len(test.expected), len(isi.Spec.Images))
173 173
 			}
174
-			for i := 0; i < len(test.from); i++ {
175
-				if !kapi.Semantic.DeepEqual(isi.Spec.Images[i].From, test.from[i]) {
176
-					t.Errorf("(%d) unexpected import spec[%d], expected %#v, got %#v", idx, i, test.from[i], isi.Spec.Images[i].From)
174
+			for i := 0; i < len(test.expected); i++ {
175
+				actual := isi.Spec.Images[i]
176
+				expected := test.expected[i]
177
+				if !kapi.Semantic.DeepEqual(actual.ImportPolicy, expected.ImportPolicy) {
178
+					t.Errorf("(%d) unexpected import[%d] policy, expected %v, got %v", idx, i, expected.ImportPolicy, actual.ImportPolicy)
179
+				}
180
+				if !kapi.Semantic.DeepEqual(actual.From, expected.From) {
181
+					t.Errorf("(%d) unexpected import[%d] from, expected %#v, got %#v", idx, i, expected.From, actual.From)
177 182
 				}
178 183
 			}
179 184
 		}