forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
importimage.go
138 lines (112 loc) · 3.98 KB
/
importimage.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
package cmd
import (
"errors"
"fmt"
"io"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/cmd/cli/describe"
imageapi "github.com/openshift/origin/pkg/image/api"
"github.com/spf13/cobra"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
)
const (
importImageLong = `Import tag and image information from an external Docker image repository.
Only image streams that have a value set for spec.dockerImageRepository may
have tag and image information imported.`
importImageExample = ` $ %[1]s import-image mystream`
)
// NewCmdImportImage implements the OpenShift cli import-image command.
func NewCmdImportImage(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "import-image IMAGESTREAM",
Short: "Imports images from a Docker registry",
Long: importImageLong,
Example: fmt.Sprintf(importImageExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
err := RunImportImage(f, out, cmd, args)
cmdutil.CheckErr(err)
},
}
return cmd
}
// RunImportImage contains all the necessary functionality for the OpenShift cli import-image command.
func RunImportImage(f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, args []string) error {
if len(args) == 0 || len(args[0]) == 0 {
return cmdutil.UsageError(cmd, "you must specify the name of an image stream.")
}
streamName := args[0]
namespace, err := f.DefaultNamespace()
if err != nil {
return err
}
osClient, _, err := f.Clients()
if err != nil {
return err
}
imageStreamClient := osClient.ImageStreams(namespace)
stream, err := imageStreamClient.Get(streamName)
if err != nil {
return err
}
if len(stream.Spec.DockerImageRepository) == 0 {
return errors.New("only image streams with spec.dockerImageRepository set may have images imported")
}
if stream.Annotations == nil {
stream.Annotations = make(map[string]string)
}
stream.Annotations[imageapi.DockerImageRepositoryCheckAnnotation] = ""
updatedStream, err := imageStreamClient.Update(stream)
if err != nil {
return err
}
resourceVersion := updatedStream.ResourceVersion
fmt.Fprintln(cmd.Out(), "Waiting for the import to complete, CTRL+C to stop waiting.")
updatedStream, err = waitForImport(imageStreamClient, stream.Name, resourceVersion)
if err != nil {
return fmt.Errorf("unable to determine if the import completed successfully - please run 'oc describe -n %s imagestream/%s' to see if the tags were updated as expected: %v", stream.Namespace, stream.Name, err)
}
fmt.Fprintln(cmd.Out(), "The import completed successfully.\n")
d := describe.ImageStreamDescriber{osClient}
info, err := d.Describe(updatedStream.Namespace, updatedStream.Name)
if err != nil {
return err
}
fmt.Fprintln(out, info)
return nil
}
func hasImportAnnotation(stream *imageapi.ImageStream) bool {
return stream.Annotations != nil && len(stream.Annotations[imageapi.DockerImageRepositoryCheckAnnotation]) != 0
}
func waitForImport(imageStreamClient client.ImageStreamInterface, name, resourceVersion string) (*imageapi.ImageStream, error) {
streamWatch, err := imageStreamClient.Watch(labels.Everything(), fields.SelectorFromSet(fields.Set{"name": name}), resourceVersion)
if err != nil {
return nil, err
}
defer streamWatch.Stop()
for {
select {
case event, ok := <-streamWatch.ResultChan():
if !ok {
return nil, errors.New("image stream watch ended prematurely")
}
switch event.Type {
case watch.Modified:
s, ok := event.Object.(*imageapi.ImageStream)
if !ok {
continue
}
if hasImportAnnotation(s) {
return s, nil
}
case watch.Deleted:
return nil, errors.New("the image stream was deleted")
case watch.Error:
return nil, errors.New("error watching image stream")
}
}
}
}