Browse code

Preserve tag when v1 pull by id is possible

Also process all tags for an image id.

Andy Goldstein authored on 2015/03/18 01:18:18
Showing 1 changed files
... ...
@@ -125,37 +125,47 @@ func (c *ImportController) Next(repo *api.ImageRepository) error {
125 125
 			return c.done(repo, err.Error())
126 126
 		}
127 127
 
128
-		// if there is a tag for the image by its id (tag=tag), we can pull by id
129
-		tag := tags[0]
130
-		if hasTag(tags, id) {
131
-			tag = id
132
-		}
133
-		pullRef := api.DockerImageReference{
134
-			Registry:  ref.Registry,
135
-			Namespace: ref.Namespace,
136
-			Name:      ref.Name,
137
-			Tag:       tag,
128
+		idTagPresent := false
129
+		if len(tags) > 1 && hasTag(tags, id) {
130
+			// only set to true if we have at least 1 tag that isn't the image id
131
+			idTagPresent = true
138 132
 		}
133
+		for _, tag := range tags {
134
+			if idTagPresent && id == tag {
135
+				continue
136
+			}
137
+			pullRefTag := tag
138
+			if idTagPresent {
139
+				// if there is a tag for the image by its id (tag=tag), we can pull by id
140
+				pullRefTag = id
141
+			}
142
+			pullRef := api.DockerImageReference{
143
+				Registry:  ref.Registry,
144
+				Namespace: ref.Namespace,
145
+				Name:      ref.Name,
146
+				Tag:       pullRefTag,
147
+			}
139 148
 
140
-		mapping := &api.ImageRepositoryMapping{
141
-			ObjectMeta: kapi.ObjectMeta{
142
-				Name:      repo.Name,
143
-				Namespace: repo.Namespace,
144
-			},
145
-			Tag: tag,
146
-			Image: api.Image{
149
+			mapping := &api.ImageRepositoryMapping{
147 150
 				ObjectMeta: kapi.ObjectMeta{
148
-					Name: id,
151
+					Name:      repo.Name,
152
+					Namespace: repo.Namespace,
153
+				},
154
+				Tag: tag,
155
+				Image: api.Image{
156
+					ObjectMeta: kapi.ObjectMeta{
157
+						Name: id,
158
+					},
159
+					DockerImageReference: pullRef.String(),
160
+					DockerImageMetadata:  image,
149 161
 				},
150
-				DockerImageReference: pullRef.String(),
151
-				DockerImageMetadata:  image,
152
-			},
153
-		}
154
-		if err := c.mappings.ImageRepositoryMappings(repo.Namespace).Create(mapping); err != nil {
155
-			if errors.IsNotFound(err) {
156
-				return c.done(repo, err.Error())
157 162
 			}
158
-			return err
163
+			if err := c.mappings.ImageRepositoryMappings(repo.Namespace).Create(mapping); err != nil {
164
+				if errors.IsNotFound(err) {
165
+					return c.done(repo, err.Error())
166
+				}
167
+				return err
168
+			}
159 169
 		}
160 170
 	}
161 171