forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
imagelookup.go
335 lines (283 loc) · 10.5 KB
/
imagelookup.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package set
import (
"fmt"
"io"
"text/tabwriter"
"github.com/golang/glog"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
ometa "github.com/openshift/origin/pkg/api/meta"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
)
var (
imageLookupLong = templates.LongDesc(`
Use an image stream from pods and other objects
Image streams make it easy to tag images, track changes from other registries, and centralize
access control to images. Local name lookup allows an image stream to be the source of
images for pods, deployments, replica sets, and other resources that reference images, without
having to provide the full registry URL. If local name lookup is enabled for an image stream
named 'mysql', a pod or other resource that references 'mysql:latest' (or any other tag) will
pull from the location specified by the image stream tag, not from an upstream registry.
Once lookup is enabled, simply reference the image stream tag in the image field of the object.
For example:
$ %[2]s import-image mysql:latest --confirm
$ %[1]s image-lookup mysql
$ %[2]s run mysql --image=mysql
will import the latest MySQL image from the DockerHub, set that image stream to handle the
"mysql" name within the project, and then launch a deployment that points to the image we
imported.
You may also force image lookup for all of the images on a resource with this command. An
annotation is placed on the object which forces an image stream tag lookup in the current
namespace for any image that matches, regardless of whether the image stream has lookup
enabled.
$ %[2]s run mysql --image=myregistry:5000/test/mysql:v1
$ %[2]s tag --source=docker myregistry:5000/test/mysql:v1 mysql:v1
$ %[1]s image-lookup deploy/mysql
Which should trigger a deployment pointing to the imported mysql:v1 tag.
Experimental: This feature is under active development and may change without notice.`)
imageLookupExample = templates.Examples(`
# Print all of the image streams and whether they resolve local names
%[1]s image-lookup
# Use local name lookup on image stream mysql
%[1]s image-lookup mysql
# Force a deployment to use local name lookup
%[1]s image-lookup deploy/mysql
# Show the current status of the deployment lookup
%[1]s image-lookup deploy/mysql --list
# Disable local name lookup on image stream mysql
%[1]s image-lookup mysql --enabled=false
# Set local name lookup on all image streams
%[1]s image-lookup --all`)
)
const alphaResolveNamesAnnotation = "alpha.image.policy.openshift.io/resolve-names"
type ImageLookupOptions struct {
Out io.Writer
Err io.Writer
Filenames []string
Selector string
All bool
Builder *resource.Builder
Infos []*resource.Info
Encoder runtime.Encoder
ShortOutput bool
Mapper meta.RESTMapper
OutputVersion schema.GroupVersion
PrintTable bool
PrintObject func(runtime.Object) error
List bool
Local bool
Enabled bool
}
// NewCmdImageLookup implements the set image-lookup command
func NewCmdImageLookup(fullName, parentName string, f *clientcmd.Factory, out, errOut io.Writer) *cobra.Command {
options := &ImageLookupOptions{
Out: out,
Err: errOut,
Enabled: true,
}
cmd := &cobra.Command{
Use: "image-lookup STREAMNAME [...]",
Short: "Change how images are resolved when deploying applications",
Long: fmt.Sprintf(imageLookupLong, fullName, parentName),
Example: fmt.Sprintf(imageLookupExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
kcmdutil.CheckErr(options.Complete(f, cmd, args))
kcmdutil.CheckErr(options.Validate())
kcmdutil.CheckErr(options.Run())
},
}
kcmdutil.AddPrinterFlags(cmd)
cmd.Flags().StringVarP(&options.Selector, "selector", "l", options.Selector, "Selector (label query) to filter on.")
cmd.Flags().BoolVar(&options.All, "all", options.All, "If true, select all resources in the namespace of the specified resource types.")
cmd.Flags().StringSliceVarP(&options.Filenames, "filename", "f", options.Filenames, "Filename, directory, or URL to file to use to edit the resource.")
cmd.Flags().BoolVar(&options.List, "list", false, "Display the current states of the requested resources.")
cmd.Flags().BoolVar(&options.Enabled, "enabled", options.Enabled, "Mark the image stream as resolving tagged images in this namespace.")
cmd.Flags().BoolVar(&options.Local, "local", false, "If true, operations will be performed locally.")
kcmdutil.AddDryRunFlag(cmd)
cmd.MarkFlagFilename("filename", "yaml", "yml", "json")
return cmd
}
// Complete takes command line information to fill out ImageLookupOptions or returns an error.
func (o *ImageLookupOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, args []string) error {
cmdNamespace, explicit, err := f.DefaultNamespace()
if err != nil {
return err
}
clientConfig, err := f.ClientConfig()
if err != nil {
return err
}
outputVersionString := kcmdutil.GetFlagString(cmd, "output-version")
if len(outputVersionString) == 0 {
o.OutputVersion = *clientConfig.GroupVersion
} else {
o.OutputVersion, err = schema.ParseGroupVersion(outputVersionString)
if err != nil {
return err
}
}
o.PrintTable = (len(args) == 0 && !o.All) || o.List
mapper, _ := f.Object()
o.Builder = f.NewBuilder(!o.Local).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(explicit, &resource.FilenameOptions{Recursive: false, Filenames: o.Filenames}).
Flatten()
switch {
case o.Local && len(args) > 0:
return kcmdutil.UsageError(cmd, "Pass files with -f when using --local")
case o.Local:
// perform no lookups on the server
// TODO: discovery still requires a running server, doesn't fall back correctly
case len(args) == 0 && len(o.Filenames) == 0:
o.Builder = o.Builder.
SelectorParam(o.Selector).
SelectAllParam(true).
ResourceTypes("imagestreams")
case o.List:
o.Builder = o.Builder.
SelectorParam(o.Selector).
SelectAllParam(o.All).
ResourceTypeOrNameArgs(true, args...)
default:
o.Builder = o.Builder.
SelectorParam(o.Selector).
SelectAllParam(o.All).
ResourceNames("imagestreams", args...)
}
output := kcmdutil.GetFlagString(cmd, "output")
if len(output) != 0 || o.Local || kcmdutil.GetDryRunFlag(cmd) {
o.PrintObject = func(obj runtime.Object) error { return f.PrintObject(cmd, o.Local, mapper, obj, o.Out) }
}
o.Encoder = f.JSONEncoder()
o.ShortOutput = kcmdutil.GetFlagString(cmd, "output") == "name"
o.Mapper = mapper
return nil
}
// Validate verifies the provided options are valid or returns an error.
func (o *ImageLookupOptions) Validate() error {
return nil
}
// Run executes the ImageLookupOptions or returns an error.
func (o *ImageLookupOptions) Run() error {
infos := o.Infos
singleItemImplied := len(o.Infos) <= 1
if o.Builder != nil {
loaded, err := o.Builder.Do().IntoSingleItemImplied(&singleItemImplied).Infos()
if err != nil {
return err
}
infos = loaded
}
if o.PrintTable && o.PrintObject == nil {
return o.printImageLookup(infos)
}
patches := CalculatePatches(infos, o.Encoder, func(info *resource.Info) (bool, error) {
switch t := info.Object.(type) {
case *imageapi.ImageStream:
t.Spec.LookupPolicy.Local = o.Enabled
return true, nil
default:
accessor, ok := ometa.GetAnnotationAccessor(info.Object)
if !ok {
return true, fmt.Errorf("the resource %s/%s does not support altering image lookup", info.Mapping.Resource, info.Name)
}
templateAnnotations, ok := accessor.TemplateAnnotations()
if ok {
if o.Enabled {
if templateAnnotations == nil {
templateAnnotations = make(map[string]string)
}
templateAnnotations[alphaResolveNamesAnnotation] = "*"
} else {
delete(templateAnnotations, alphaResolveNamesAnnotation)
}
accessor.SetTemplateAnnotations(templateAnnotations)
return true, nil
}
annotations := accessor.Annotations()
if o.Enabled {
if annotations == nil {
annotations = make(map[string]string)
}
annotations[alphaResolveNamesAnnotation] = "*"
} else {
delete(annotations, alphaResolveNamesAnnotation)
}
accessor.SetAnnotations(annotations)
return true, nil
}
})
if singleItemImplied && len(patches) == 0 {
return fmt.Errorf("%s/%s no changes", infos[0].Mapping.Resource, infos[0].Name)
}
if o.PrintObject != nil {
object, err := resource.AsVersionedObject(infos, !singleItemImplied, o.OutputVersion, kapi.Codecs.LegacyCodec(o.OutputVersion))
if err != nil {
return err
}
return o.PrintObject(object)
}
failed := false
for _, patch := range patches {
info := patch.Info
if patch.Err != nil {
failed = true
fmt.Fprintf(o.Err, "error: %s/%s %v\n", info.Mapping.Resource, info.Name, patch.Err)
continue
}
if string(patch.Patch) == "{}" || len(patch.Patch) == 0 {
fmt.Fprintf(o.Err, "info: %s %q was not changed\n", info.Mapping.Resource, info.Name)
continue
}
glog.V(4).Infof("Calculated patch %s", patch.Patch)
obj, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, types.StrategicMergePatchType, patch.Patch)
if err != nil {
handlePodUpdateError(o.Err, err, "altered")
failed = true
continue
}
info.Refresh(obj, true)
kcmdutil.PrintSuccess(o.Mapper, o.ShortOutput, o.Out, info.Mapping.Resource, info.Name, false, "updated")
}
if failed {
return kcmdutil.ErrExit
}
return nil
}
// printImageLookup displays a tabular output of the imageLookup for each object.
func (o *ImageLookupOptions) printImageLookup(infos []*resource.Info) error {
w := tabwriter.NewWriter(o.Out, 0, 2, 2, ' ', 0)
defer w.Flush()
fmt.Fprintf(w, "NAME\tLOCAL\n")
for _, info := range infos {
switch t := info.Object.(type) {
case *imageapi.ImageStream:
fmt.Fprintf(w, "%s\t%t\n", info.Name, t.Spec.LookupPolicy.Local)
default:
accessor, ok := ometa.GetAnnotationAccessor(info.Object)
if !ok {
// has no annotations
fmt.Fprintf(w, "%s/%s\tUNKNOWN\n", info.Mapping.Resource, info.Name)
break
}
var enabled bool
if a, ok := accessor.TemplateAnnotations(); ok {
enabled = a[alphaResolveNamesAnnotation] == "*"
}
if !enabled {
enabled = accessor.Annotations()[alphaResolveNamesAnnotation] == "*"
}
fmt.Fprintf(w, "%s/%s\t%t\n", info.Mapping.Resource, info.Name, enabled)
}
}
return nil
}