forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
info.go
197 lines (168 loc) · 5.22 KB
/
info.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
package info
import (
"fmt"
"net/http"
"net/url"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
imageclient "github.com/openshift/client-go/image/clientset/versioned"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
"github.com/openshift/origin/pkg/image/registryclient"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
)
var (
desc = templates.LongDesc(`
Display information about the integrated registry
This command exposes information about the integrated registry, if configured.
Use --check to verify your local client can access the registry. If the adminstrator
has not configured a public hostname for the registry then this command may fail when
run outside of the server.
Experimental: This command is under active development and may change without notice.`)
example = templates.Examples(`
# Display information about the integrated registry
%[1]s
`)
)
type Options struct {
Check bool
Quiet bool
ShowInternal bool
ShowPublic bool
Namespaces []string
Client imageclient.Interface
genericclioptions.IOStreams
}
func NewRegistryInfoOptions(streams genericclioptions.IOStreams) *Options {
return &Options{
IOStreams: streams,
}
}
// New creates a command that displays info about the registry.
func NewRegistryInfoCmd(name string, f kcmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
o := NewRegistryInfoOptions(streams)
cmd := &cobra.Command{
Use: "info ",
Short: "Print info about the integrated registry",
Long: desc,
Example: fmt.Sprintf(example, name+" login"),
Run: func(c *cobra.Command, args []string) {
kcmdutil.CheckErr(o.Complete(f, args))
kcmdutil.CheckErr(o.Validate())
kcmdutil.CheckErr(o.Run())
},
}
flag := cmd.Flags()
flag.BoolVar(&o.Check, "check", o.Check, "Attempt to contact the integrated registry.")
flag.BoolVarP(&o.Check, "quiet", "q", o.Quiet, "Suppress normal output and only print status.")
flag.BoolVar(&o.ShowInternal, "internal", o.ShowInternal, "Only check the internal registry hostname.")
flag.BoolVar(&o.ShowPublic, "public", o.ShowPublic, "Only check the public registry hostname.")
return cmd
}
func (o *Options) Complete(f kcmdutil.Factory, args []string) error {
cfg, err := f.ToRESTConfig()
if err != nil {
return err
}
client, err := imageclient.NewForConfig(cfg)
if err != nil {
return err
}
o.Client = client
ns, _, err := f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}
o.Namespaces = []string{ns, "openshift"}
return nil
}
type RegistryInfo struct {
Public string
Internal string
}
func (i *RegistryInfo) Installed() bool {
return len(i.Public) > 0 || len(i.Internal) > 0
}
func (i *RegistryInfo) HostPort() (string, bool) {
if len(i.Public) > 0 {
return i.Public, true
}
return i.Internal, false
}
func findRegistryInfo(client imageclient.Interface, namespaces ...string) (*RegistryInfo, error) {
for _, ns := range namespaces {
imageStreams, err := client.Image().ImageStreams(ns).List(metav1.ListOptions{})
if err != nil || len(imageStreams.Items) == 0 {
continue
}
is := imageStreams.Items[0]
info := &RegistryInfo{}
if value := is.Status.PublicDockerImageRepository; len(value) > 0 {
ref, err := imageapi.ParseDockerImageReference(value)
if err != nil {
return nil, fmt.Errorf("unable to parse public registry info from the server")
}
info.Public = ref.Registry
}
if value := is.Status.DockerImageRepository; len(value) > 0 {
ref, err := imageapi.ParseDockerImageReference(value)
if err != nil {
return nil, fmt.Errorf("unable to parse internal registry info from the server")
}
info.Internal = ref.Registry
}
if !info.Installed() {
return nil, fmt.Errorf("the integrated registry has not been configured")
}
return info, nil
}
return nil, fmt.Errorf("no image streams could be located to retrieve registry info, please specify a namespace with image streams")
}
func (o *Options) Validate() error {
if o.ShowInternal && o.ShowPublic {
return fmt.Errorf("only one of --internal or --public may be specified at a time")
}
return nil
}
func (o *Options) Run() error {
info, err := findRegistryInfo(o.Client, o.Namespaces...)
if err != nil {
return err
}
var host string
var public bool
switch {
case o.ShowPublic:
host = info.Public
if len(host) == 0 {
return fmt.Errorf("registry does not have public hostname")
}
case o.ShowInternal:
host = info.Internal
public = false
if len(host) == 0 {
return fmt.Errorf("registry does not have an internal hostname")
}
default:
host, public = info.HostPort()
}
if o.Check {
ctx := apirequest.NewContext()
if !public && !o.ShowInternal {
fmt.Fprintf(o.ErrOut, "info: Registry does not have a public hostname\n")
}
url := &url.URL{Host: host}
c := registryclient.NewContext(http.DefaultTransport, http.DefaultTransport)
_, src, err := c.Ping(ctx, url, false)
if err != nil {
return fmt.Errorf("registry could not be contacted at %s: %v", url.Host, err)
}
host = src.Host
}
if !o.Quiet {
fmt.Fprintf(o.Out, "%s\n", host)
}
return nil
}