Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic when tag is nil when creating istag #15541

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/image/registry/imagestreamtag/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ func (r *REST) Create(ctx apirequest.Context, obj runtime.Object) (runtime.Objec
if exists {
return nil, kapierrors.NewAlreadyExists(imageapi.Resource("imagestreamtag"), istag.Name)
}
target.Spec.Tags[imageTag] = *istag.Tag
if istag.Tag != nil {
target.Spec.Tags[imageTag] = *istag.Tag
}

// Check the stream creation timestamp and make sure we will not
// create a new image stream while deleting.
Expand Down
84 changes: 84 additions & 0 deletions pkg/image/registry/imagestreamtag/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,87 @@ func TestDeleteImageStreamTag(t *testing.T) {
}()
}
}

func TestCreateImageStreamTag(t *testing.T) {
tests := map[string]struct {
istag runtime.Object
expectError bool
errorTargetKind string
errorTargetID string
}{
"valid istag": {
istag: &imageapi.ImageStreamTag{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "test:tag",
},
Image: imageapi.Image{ObjectMeta: metav1.ObjectMeta{Name: "10"}, DockerImageReference: "foo/bar/baz"},
Tag: &imageapi.TagReference{
Name: "latest",
From: &kapi.ObjectReference{Kind: "DockerImage", Name: "foo/bar/baz"},
ReferencePolicy: imageapi.TagReferencePolicy{Type: imageapi.SourceTagReferencePolicy},
},
},
},
"invalid tag": {
istag: &imageapi.ImageStreamTag{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "test:tag",
},
Image: imageapi.Image{ObjectMeta: metav1.ObjectMeta{Name: "10"}, DockerImageReference: "foo/bar/baz"},
Tag: &imageapi.TagReference{},
},
expectError: true,
errorTargetKind: "ImageStreamTag",
errorTargetID: "test:tag",
},
"nil tag": {
istag: &imageapi.ImageStreamTag{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "test:tag",
},
Image: imageapi.Image{ObjectMeta: metav1.ObjectMeta{Name: "10"}, DockerImageReference: "foo/bar/baz"},
},
},
}

for name, tc := range tests {
func() {
client, server, storage := setup(t)
defer server.Terminate(t)

client.Put(
context.TODO(),
etcdtest.AddPrefix("/imagestreams/default/test"),
runtime.EncodeOrDie(kapi.Codecs.LegacyCodec(v1.SchemeGroupVersion),
&imageapi.ImageStream{
ObjectMeta: metav1.ObjectMeta{
CreationTimestamp: metav1.Date(2015, 3, 24, 9, 38, 0, 0, time.UTC),
Namespace: "default",
Name: "test",
},
Spec: imageapi.ImageStreamSpec{
Tags: map[string]imageapi.TagReference{},
},
},
))

ctx := apirequest.WithUser(apirequest.NewDefaultContext(), &fakeUser{})
_, err := storage.Create(ctx, tc.istag)
gotErr := err != nil
if e, a := tc.expectError, gotErr; e != a {
t.Errorf("%s: Expected err=%v: got %v: %v", name, e, a, err)
return
}
if tc.expectError {
status := err.(statusError).Status()
if status.Details.Kind != tc.errorTargetKind || status.Details.Name != tc.errorTargetID {
t.Errorf("%s: unexpected status: %#v", name, status.Details)
return
}
}
}()
}
}