Skip to content

Commit 22b3508

Browse files
Merge pull request #19452 from smarterclayton/create_conflict
Creating an image stream tag is too likely to conflict
2 parents daaafeb + 8ca11cc commit 22b3508

File tree

2 files changed

+42
-37
lines changed

2 files changed

+42
-37
lines changed

pkg/image/registry/imagestreamtag/rest.go

+42-34
Original file line numberDiff line numberDiff line change
@@ -139,46 +139,54 @@ func (r *REST) Create(ctx apirequest.Context, obj runtime.Object, createValidati
139139
return nil, fmt.Errorf("%q must be of the form <stream_name>:<tag>", istag.Name)
140140
}
141141

142-
target, err := r.imageStreamRegistry.GetImageStream(ctx, imageStreamName, &metav1.GetOptions{})
143-
if err != nil {
144-
if !kapierrors.IsNotFound(err) {
145-
return nil, err
146-
}
142+
for i := 10; i > 0; i-- {
143+
target, err := r.imageStreamRegistry.GetImageStream(ctx, imageStreamName, &metav1.GetOptions{})
144+
if err != nil {
145+
if !kapierrors.IsNotFound(err) {
146+
return nil, err
147+
}
147148

148-
// try to create the target if it doesn't exist
149-
target = &imageapi.ImageStream{
150-
ObjectMeta: metav1.ObjectMeta{
151-
Name: imageStreamName,
152-
Namespace: namespace,
153-
},
149+
// try to create the target if it doesn't exist
150+
target = &imageapi.ImageStream{
151+
ObjectMeta: metav1.ObjectMeta{
152+
Name: imageStreamName,
153+
Namespace: namespace,
154+
},
155+
}
154156
}
155-
}
156157

157-
if target.Spec.Tags == nil {
158-
target.Spec.Tags = make(map[string]imageapi.TagReference)
159-
}
158+
if target.Spec.Tags == nil {
159+
target.Spec.Tags = make(map[string]imageapi.TagReference)
160+
}
160161

161-
// The user wants to symlink a tag.
162-
_, exists := target.Spec.Tags[imageTag]
163-
if exists {
164-
return nil, kapierrors.NewAlreadyExists(imageapi.Resource("imagestreamtag"), istag.Name)
165-
}
166-
if istag.Tag != nil {
167-
target.Spec.Tags[imageTag] = *istag.Tag
168-
}
162+
// The user wants to symlink a tag.
163+
_, exists := target.Spec.Tags[imageTag]
164+
if exists {
165+
return nil, kapierrors.NewAlreadyExists(imageapi.Resource("imagestreamtag"), istag.Name)
166+
}
167+
if istag.Tag != nil {
168+
target.Spec.Tags[imageTag] = *istag.Tag
169+
}
169170

170-
// Check the stream creation timestamp and make sure we will not
171-
// create a new image stream while deleting.
172-
if target.CreationTimestamp.IsZero() {
173-
_, err = r.imageStreamRegistry.CreateImageStream(ctx, target)
174-
} else {
175-
_, err = r.imageStreamRegistry.UpdateImageStream(ctx, target)
176-
}
177-
if err != nil {
178-
return nil, err
171+
// Check the stream creation timestamp and make sure we will not
172+
// create a new image stream while deleting.
173+
if target.CreationTimestamp.IsZero() {
174+
target, err = r.imageStreamRegistry.CreateImageStream(ctx, target)
175+
} else {
176+
target, err = r.imageStreamRegistry.UpdateImageStream(ctx, target)
177+
}
178+
if kapierrors.IsAlreadyExists(err) || kapierrors.IsConflict(err) {
179+
continue
180+
}
181+
if err != nil {
182+
return nil, err
183+
}
184+
image, _ := r.imageFor(ctx, imageTag, target)
185+
return newISTag(imageTag, target, image, true)
179186
}
180-
181-
return istag, nil
187+
// We tried to update resource, but we kept conflicting. Inform the client that we couldn't complete
188+
// the operation but that they may try again.
189+
return nil, kapierrors.NewServerTimeout(imageapi.Resource("imagestreamtags"), "create", 2)
182190
}
183191

184192
func (r *REST) Update(ctx apirequest.Context, tagName string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc) (runtime.Object, bool, error) {

test/integration/imagestream_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -626,9 +626,6 @@ func TestRegistryWhitelistingValidation(t *testing.T) {
626626
if err != nil {
627627
t.Fatalf("unexpected error: %v", err)
628628
}
629-
if istag.Annotations[imageapi.InsecureRepositoryAnnotation] != "true" {
630-
t.Fatalf("missing %q annotation to image stream tag", imageapi.InsecureRepositoryAnnotation)
631-
}
632629

633630
istag.Tag.From = &kapi.ObjectReference{
634631
Kind: "DockerImage",

0 commit comments

Comments
 (0)