forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
imagestreamtag.go
185 lines (161 loc) · 6.38 KB
/
imagestreamtag.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
package create
import (
"fmt"
"strings"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
imagev1 "github.com/openshift/api/image/v1"
imagev1client "github.com/openshift/client-go/image/clientset/versioned/typed/image/v1"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
utilenv "github.com/openshift/origin/pkg/oc/util/env"
)
const ImageStreamTagRecommendedName = "imagestreamtag"
var (
imageStreamTagLong = templates.LongDesc(`
Create a new image stream tag
Image streams tags allow you to track, tag, and import images from other registries. They also
define an access controlled destination that you can push images to. An image stream tag can
reference images from many different registries and control how those images are referenced by
pods, deployments, and builds.
If --resolve-local is passed, the image stream will be used as the source when pods reference
it by name. For example, if stream 'mysql' resolves local names, a pod that points to
'mysql:latest' will use the image the image stream points to under the "latest" tag.`)
imageStreamTagExample = templates.Examples(`
# Create a new image stream tag based on an image on a remote registry
%[1]s mysql:latest --from-image=myregistry.local/mysql/mysql:5.0
`)
)
type CreateImageStreamTagOptions struct {
CreateSubcommandOptions *CreateSubcommandOptions
Client imagev1client.ImageStreamTagsGetter
FromImage string
From string
Annotations []string
Scheduled bool
Insecure bool
Reference bool
ReferencePolicyStr string
ReferencePolicy imagev1.TagReferencePolicyType
}
// NewCmdCreateImageStreamTag is a command to create a new image stream tag.
func NewCmdCreateImageStreamTag(name, fullName string, f genericclioptions.RESTClientGetter, streams genericclioptions.IOStreams) *cobra.Command {
o := &CreateImageStreamTagOptions{
CreateSubcommandOptions: NewCreateSubcommandOptions(streams),
}
cmd := &cobra.Command{
Use: name + " NAME",
Short: "Create a new image stream tag.",
Long: imageStreamTagLong,
Example: fmt.Sprintf(imageStreamTagExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.Complete(cmd, f, args))
cmdutil.CheckErr(o.Run())
},
Aliases: []string{"istag"},
}
cmd.Flags().StringVar(&o.FromImage, "from-image", o.FromImage, "Use the provided remote image with this tag.")
cmd.Flags().StringVar(&o.From, "from", o.From, "Use the provided image stream tag or image stream image as the source: [<namespace>/]name[:<tag>|@<id>]")
cmd.Flags().StringSliceVarP(&o.Annotations, "annotation", "A", o.Annotations, "Set an annotation on this image stream tag.")
cmd.Flags().MarkShorthandDeprecated("annotation", "please use --annotation instead.")
cmd.Flags().BoolVar(&o.Scheduled, "scheduled", o.Scheduled, "If set the remote source of this image will be periodically checked for imports.")
cmd.Flags().BoolVar(&o.Insecure, "insecure", o.Insecure, "Allow importing from registries that are not fully secured by HTTPS.")
cmd.Flags().StringVar(&o.ReferencePolicyStr, "reference-policy", o.ReferencePolicyStr, "If set to 'Local', referenced images will be pulled from the integrated registry. Ignored when reference is true.")
cmd.Flags().BoolVar(&o.Reference, "reference", o.Reference, "If true, the tag value will be used whenever the image stream tag is referenced.")
o.CreateSubcommandOptions.PrintFlags.AddFlags(cmd)
cmdutil.AddDryRunFlag(cmd)
return cmd
}
func (o *CreateImageStreamTagOptions) Complete(cmd *cobra.Command, f genericclioptions.RESTClientGetter, args []string) error {
if len(o.ReferencePolicyStr) > 0 {
switch strings.ToLower(o.ReferencePolicyStr) {
case "source":
o.ReferencePolicy = imagev1.SourceTagReferencePolicy
case "local":
o.ReferencePolicy = imagev1.LocalTagReferencePolicy
default:
return fmt.Errorf("valid values for --reference-policy are: source, local")
}
}
clientConfig, err := f.ToRESTConfig()
if err != nil {
return err
}
o.Client, err = imagev1client.NewForConfig(clientConfig)
if err != nil {
return err
}
return o.CreateSubcommandOptions.Complete(f, cmd, args)
}
func (o *CreateImageStreamTagOptions) Run() error {
isTag := &imagev1.ImageStreamTag{
// this is ok because we know exactly how we want to be serialized
TypeMeta: metav1.TypeMeta{APIVersion: imagev1.SchemeGroupVersion.String(), Kind: "ImageStreamTag"},
ObjectMeta: metav1.ObjectMeta{
Name: o.CreateSubcommandOptions.Name,
},
Tag: &imagev1.TagReference{
ImportPolicy: imagev1.TagImportPolicy{
Scheduled: o.Scheduled,
Insecure: o.Insecure,
},
ReferencePolicy: imagev1.TagReferencePolicy{
Type: o.ReferencePolicy,
},
Reference: o.Reference,
},
}
annotations, remove, err := utilenv.ParseAnnotation(o.Annotations, nil)
if err != nil {
return err
}
if len(remove) > 0 {
return fmt.Errorf("annotations must be of the form name=value")
}
// to preserve backwards compatibility we are forced to set this
isTag.Annotations = annotations
isTag.Tag.Annotations = annotations
switch {
case len(o.FromImage) > 0 && len(o.From) > 0:
return fmt.Errorf("--from and --from-image may not be used together")
case len(o.FromImage) > 0:
isTag.Tag.From = &corev1.ObjectReference{
Name: o.FromImage,
Kind: "DockerImage",
}
case len(o.From) > 0:
var name string
ref, err := imageapi.ParseDockerImageReference(o.From)
if err != nil {
if !strings.HasPrefix(o.From, ":") {
return fmt.Errorf("Invalid --from, must be a valid image stream tag or image stream image: %v", err)
}
ref = imageapi.DockerImageReference{Tag: o.From[1:]}
name = o.From[1:]
} else {
name = ref.NameString()
}
if len(ref.Registry) > 0 {
return fmt.Errorf("Invalid --from, registry may not be specified")
}
kind := "ImageStreamTag"
if len(ref.ID) > 0 {
kind = "ImageStreamImage"
}
isTag.Tag.From = &corev1.ObjectReference{
Kind: kind,
Name: name,
Namespace: ref.Namespace,
}
}
if !o.CreateSubcommandOptions.DryRun {
isTag, err = o.Client.ImageStreamTags(o.CreateSubcommandOptions.Namespace).Create(isTag)
if err != nil {
return err
}
}
return o.CreateSubcommandOptions.Printer.PrintObj(isTag, o.CreateSubcommandOptions.Out)
}