forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
203 lines (186 loc) · 5.33 KB
/
controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package controller
import (
"fmt"
"time"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/dockerregistry"
"github.com/openshift/origin/pkg/image/api"
)
const dockerImageRepositoryCheckAnnotation = "openshift.io/image.dockerRepositoryCheck"
type ImportController struct {
repositories client.ImageStreamsNamespacer
mappings client.ImageStreamMappingsNamespacer
client dockerregistry.Client
}
// needsImport returns true if the provided repository should have its tags imported.
func needsImport(repo *api.ImageStream) bool {
if len(repo.Spec.DockerImageRepository) == 0 {
return false
}
if repo.Annotations != nil && len(repo.Annotations[dockerImageRepositoryCheckAnnotation]) != 0 {
return false
}
return true
/*
if len(repo.Spec.Tags) == 0 {
return true
}
emptyTags := 0
for _, v := range repo.Spec.Tags {
if len(v.DockerImageReference) == 0 {
emptyTags++
}
}
return emptyTags > 0
*/
}
// Next processes the given image repository, looking for repos that have DockerImageRepository
// set but have not yet been marked as "ready". If transient errors occur, err is returned but
// the image repository is not modified (so it will be tried again later). If a permanent
// failure occurs the image is marked with an annotation. The tags of the original spec image
// are left as is (those are updated through status).
func (c *ImportController) Next(repo *api.ImageStream) error {
if !needsImport(repo) {
return nil
}
name := repo.Spec.DockerImageRepository
ref, err := api.ParseDockerImageReference(name)
if err != nil {
err = fmt.Errorf("invalid docker image repository, cannot import data: %v", err)
util.HandleError(err)
return c.done(repo, err.Error())
}
conn, err := c.client.Connect(ref.Registry)
if err != nil {
return err
}
tags, err := conn.ImageTags(ref.Namespace, ref.Name)
switch {
case dockerregistry.IsRepositoryNotFound(err), dockerregistry.IsRegistryNotFound(err):
return c.done(repo, err.Error())
case err != nil:
return err
}
newTags := make(map[string]string) //, len(repo.Spec.Tags))
imageToTag := make(map[string][]string)
//switch {
//case len(repo.Tags) == 0:
// copy all tags
for tag := range tags {
// TODO: switch to image when pull by ID is automatic
newTags[tag] = tag
}
for tag, image := range tags {
imageToTag[image] = append(imageToTag[image], tag)
}
/*
default:
for tag, v := range repo.Tags {
if len(v) != 0 {
newTags[tag] = v
continue
}
image, ok := tags[tag]
if !ok {
// tag not found, set empty
continue
}
imageToTag[image] = append(imageToTag[image], tag)
// TODO: switch to image when pull by ID is automatic
newTags[tag] = tag
}
}
*/
// nothing to tag - no images in the upstream repo, or we're in sync
if len(imageToTag) == 0 {
return c.done(repo, "")
}
for id, tags := range imageToTag {
dockerImage, err := conn.ImageByID(ref.Namespace, ref.Name, id)
switch {
case dockerregistry.IsRepositoryNotFound(err), dockerregistry.IsRegistryNotFound(err):
return c.done(repo, err.Error())
case dockerregistry.IsImageNotFound(err):
for _, tag := range tags {
delete(newTags, tag)
}
continue
case err != nil:
return err
}
var image api.DockerImage
if err := kapi.Scheme.Convert(dockerImage, &image); err != nil {
err = fmt.Errorf("could not convert image: %#v", err)
util.HandleError(err)
return c.done(repo, err.Error())
}
idTagPresent := false
if len(tags) > 1 && hasTag(tags, id) {
// only set to true if we have at least 1 tag that isn't the image id
idTagPresent = true
}
for _, tag := range tags {
if idTagPresent && id == tag {
continue
}
pullRefTag := tag
if idTagPresent {
// if there is a tag for the image by its id (tag=tag), we can pull by id
pullRefTag = id
}
pullRef := api.DockerImageReference{
Registry: ref.Registry,
Namespace: ref.Namespace,
Name: ref.Name,
Tag: pullRefTag,
}
mapping := &api.ImageStreamMapping{
ObjectMeta: kapi.ObjectMeta{
Name: repo.Name,
Namespace: repo.Namespace,
},
Tag: tag,
Image: api.Image{
ObjectMeta: kapi.ObjectMeta{
Name: id,
},
DockerImageReference: pullRef.String(),
DockerImageMetadata: image,
},
}
if err := c.mappings.ImageStreamMappings(repo.Namespace).Create(mapping); err != nil {
if errors.IsNotFound(err) {
return c.done(repo, err.Error())
}
return err
}
}
}
// we've completed our updates
return c.done(repo, "")
}
// done marks the repository as being processed due to an error or failure condition
func (c *ImportController) done(repo *api.ImageStream, reason string) error {
if len(reason) == 0 {
reason = util.Now().UTC().Format(time.RFC3339)
}
if repo.Annotations == nil {
repo.Annotations = make(map[string]string)
}
repo.Annotations[dockerImageRepositoryCheckAnnotation] = reason
if _, err := c.repositories.ImageStreams(repo.Namespace).Update(repo); err != nil && !errors.IsNotFound(err) {
return err
}
return nil
}
func hasTag(tags []string, tag string) bool {
for _, s := range tags {
if s == tag {
return true
}
}
return false
}