forked from openshift/origin
/
tag.go
207 lines (168 loc) · 5.68 KB
/
tag.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
package cmd
import (
"fmt"
"io"
"strings"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
imageapi "github.com/openshift/origin/pkg/image/api"
"github.com/openshift/origin/pkg/image/registry/imagestreamimage"
"github.com/spf13/cobra"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
)
const (
tagLong = `Tag existing images into image streams
The tag command allows you to take an existing tag or image from an image
stream, or a Docker image pull spec, and set it as the most recent image for a
tag in 1 or more other image streams. It is similar to the 'docker tag'
command, but it operates on image streams instead.`
tagExample = ` // Tag the current image for the image stream 'openshift/ruby' and tag '2.0' into the image stream 'yourproject/ruby with tag 'tip':
$ %[1]s tag openshift/ruby:2.0 yourproject/ruby:tip
// Tag a specific image:
$ %[1]s tag openshift/ruby@sha256:6b646fa6bf5e5e4c7fa41056c27910e679c03ebe7f93e361e6515a9da7e258cc yourproject/ruby:tip
// Tag an external Docker image:
$ %[1]s tag --source=docker openshift/origin:latest yourproject/ruby:tip`
)
// NewCmdTag implements the OpenShift cli tag command.
func NewCmdTag(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
var sourceKind string
cmd := &cobra.Command{
Use: "tag [--source=SOURCETYPE] SOURCE DEST [DEST ...]",
Short: "Tag existing images into image streams",
Long: tagLong,
Example: fmt.Sprintf(tagExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
err := RunTag(f, out, cmd, args, sourceKind)
cmdutil.CheckErr(err)
},
}
cmd.Flags().StringVar(&sourceKind, "source", sourceKind, "Optional hint for the source type; valid values are 'imagestreamtag', 'istag', 'imagestreamimage', 'isimage', and 'docker'")
return cmd
}
func parseStreamName(name, defaultNamespace string) (string, string, error) {
if !strings.Contains(name, "/") {
return defaultNamespace, name, nil
}
parts := strings.Split(name, "/")
if len(parts) != 2 {
return "", "", fmt.Errorf("invalid image stream %q", name)
}
namespace := parts[0]
if len(namespace) == 0 {
return "", "", fmt.Errorf("invalid namespace %q for image stream %q", namespace, name)
}
streamName := parts[1]
if len(streamName) == 0 {
return "", "", fmt.Errorf("invalid name %q for image stream %q", streamName, name)
}
return namespace, streamName, nil
}
func determineSourceKind(f *clientcmd.Factory, input string) string {
mapper, _ := f.Object()
_, kind, err := mapper.VersionAndKindForResource(input)
if err == nil {
return kind
}
// DockerImage isn't in RESTMapper
switch strings.ToLower(input) {
case "docker", "dockerimage":
return "DockerImage"
}
return input
}
// RunTag contains all the necessary functionality for the OpenShift cli tag command.
func RunTag(f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, args []string, sourceKind string) error {
if len(args) < 2 {
return cmdutil.UsageError(cmd, "you must specify a source and at least one destination")
}
original := sourceKind
if len(sourceKind) > 0 {
sourceKind = determineSourceKind(f, sourceKind)
}
if len(sourceKind) > 0 {
validSources := util.NewStringSet("imagestreamtag", "istag", "imagestreamimage", "isimage", "docker", "dockerimage")
if !validSources.Has(strings.ToLower(sourceKind)) {
cmdutil.CheckErr(cmdutil.UsageError(cmd, "invalid source %q; valid values are %v", original, strings.Join(validSources.List(), ", ")))
}
}
namespace, err := f.DefaultNamespace()
if err != nil {
return err
}
sourceNamespace, sourceNameAndRef, err := parseStreamName(args[0], namespace)
if err != nil {
return err
}
osClient, _, err := f.Clients()
if err != nil {
return err
}
if len(sourceKind) == 0 {
if sourceName, sourceTag, ok := imageapi.SplitImageStreamTag(sourceNameAndRef); ok {
if _, err := osClient.ImageStreamTags(sourceNamespace).Get(sourceName, sourceTag); err == nil {
sourceKind = "ImageStreamTag"
}
}
}
if len(sourceKind) == 0 {
if sourceName, sourceID, err := imagestreamimage.ParseNameAndID(sourceNameAndRef); err == nil {
if _, err := osClient.ImageStreamImages(sourceNamespace).Get(sourceName, sourceID); err == nil {
sourceKind = "ImageStreamImage"
}
}
}
if len(sourceKind) == 0 {
sourceKind = "DockerImage"
}
for _, arg := range args[1:] {
destNamespace, destNameAndTag, err := parseStreamName(arg, namespace)
if err != nil {
return err
}
destName, destTag, ok := imageapi.SplitImageStreamTag(destNameAndTag)
if !ok {
return fmt.Errorf("%q must be of the form <namespace>/<stream name>:<tag>", arg)
}
isc := osClient.ImageStreams(destNamespace)
target, err := isc.Get(destName)
if err != nil {
if !kerrors.IsNotFound(err) {
return err
}
// try to create the target if it doesn't exist
target = &imageapi.ImageStream{
ObjectMeta: kapi.ObjectMeta{
Name: destName,
},
}
target, err = isc.Create(target)
if err != nil {
return nil
}
}
if target.Spec.Tags == nil {
target.Spec.Tags = make(map[string]imageapi.TagReference)
}
targetRef, ok := target.Spec.Tags[destTag]
if !ok {
targetRef = imageapi.TagReference{}
}
targetRef.From = &kapi.ObjectReference{
Kind: sourceKind,
}
switch sourceKind {
case "DockerImage":
targetRef.From.Name = args[0]
default:
targetRef.From.Namespace = sourceNamespace
targetRef.From.Name = sourceNameAndRef
}
target.Spec.Tags[destTag] = targetRef
if _, err = isc.Update(target); err != nil {
return err
}
}
return nil
}