Skip to content

Commit 7dfcee7

Browse files
Merge pull request #15541 from smarterclayton/panic_nil
Automatic merge from submit-queue Fix panic when tag is nil when creating istag Replaces #15523 which has a compilation failure
2 parents a83ece5 + a58e0e6 commit 7dfcee7

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

pkg/image/registry/imagestreamtag/rest.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ func (r *REST) Create(ctx apirequest.Context, obj runtime.Object) (runtime.Objec
148148
if exists {
149149
return nil, kapierrors.NewAlreadyExists(imageapi.Resource("imagestreamtag"), istag.Name)
150150
}
151-
target.Spec.Tags[imageTag] = *istag.Tag
151+
if istag.Tag != nil {
152+
target.Spec.Tags[imageTag] = *istag.Tag
153+
}
152154

153155
// Check the stream creation timestamp and make sure we will not
154156
// create a new image stream while deleting.

pkg/image/registry/imagestreamtag/rest_test.go

+84
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,87 @@ func TestDeleteImageStreamTag(t *testing.T) {
466466
}()
467467
}
468468
}
469+
470+
func TestCreateImageStreamTag(t *testing.T) {
471+
tests := map[string]struct {
472+
istag runtime.Object
473+
expectError bool
474+
errorTargetKind string
475+
errorTargetID string
476+
}{
477+
"valid istag": {
478+
istag: &imageapi.ImageStreamTag{
479+
ObjectMeta: metav1.ObjectMeta{
480+
Namespace: "default",
481+
Name: "test:tag",
482+
},
483+
Image: imageapi.Image{ObjectMeta: metav1.ObjectMeta{Name: "10"}, DockerImageReference: "foo/bar/baz"},
484+
Tag: &imageapi.TagReference{
485+
Name: "latest",
486+
From: &kapi.ObjectReference{Kind: "DockerImage", Name: "foo/bar/baz"},
487+
ReferencePolicy: imageapi.TagReferencePolicy{Type: imageapi.SourceTagReferencePolicy},
488+
},
489+
},
490+
},
491+
"invalid tag": {
492+
istag: &imageapi.ImageStreamTag{
493+
ObjectMeta: metav1.ObjectMeta{
494+
Namespace: "default",
495+
Name: "test:tag",
496+
},
497+
Image: imageapi.Image{ObjectMeta: metav1.ObjectMeta{Name: "10"}, DockerImageReference: "foo/bar/baz"},
498+
Tag: &imageapi.TagReference{},
499+
},
500+
expectError: true,
501+
errorTargetKind: "ImageStreamTag",
502+
errorTargetID: "test:tag",
503+
},
504+
"nil tag": {
505+
istag: &imageapi.ImageStreamTag{
506+
ObjectMeta: metav1.ObjectMeta{
507+
Namespace: "default",
508+
Name: "test:tag",
509+
},
510+
Image: imageapi.Image{ObjectMeta: metav1.ObjectMeta{Name: "10"}, DockerImageReference: "foo/bar/baz"},
511+
},
512+
},
513+
}
514+
515+
for name, tc := range tests {
516+
func() {
517+
client, server, storage := setup(t)
518+
defer server.Terminate(t)
519+
520+
client.Put(
521+
context.TODO(),
522+
etcdtest.AddPrefix("/imagestreams/default/test"),
523+
runtime.EncodeOrDie(kapi.Codecs.LegacyCodec(v1.SchemeGroupVersion),
524+
&imageapi.ImageStream{
525+
ObjectMeta: metav1.ObjectMeta{
526+
CreationTimestamp: metav1.Date(2015, 3, 24, 9, 38, 0, 0, time.UTC),
527+
Namespace: "default",
528+
Name: "test",
529+
},
530+
Spec: imageapi.ImageStreamSpec{
531+
Tags: map[string]imageapi.TagReference{},
532+
},
533+
},
534+
))
535+
536+
ctx := apirequest.WithUser(apirequest.NewDefaultContext(), &fakeUser{})
537+
_, err := storage.Create(ctx, tc.istag)
538+
gotErr := err != nil
539+
if e, a := tc.expectError, gotErr; e != a {
540+
t.Errorf("%s: Expected err=%v: got %v: %v", name, e, a, err)
541+
return
542+
}
543+
if tc.expectError {
544+
status := err.(statusError).Status()
545+
if status.Details.Kind != tc.errorTargetKind || status.Details.Name != tc.errorTargetID {
546+
t.Errorf("%s: unexpected status: %#v", name, status.Details)
547+
return
548+
}
549+
}
550+
}()
551+
}
552+
}

0 commit comments

Comments
 (0)