forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
status.go
236 lines (198 loc) · 7.25 KB
/
status.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
package cmd
import (
"errors"
"fmt"
"io"
"github.com/gonum/graph/encoding/dot"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
appsclientinternal "github.com/openshift/origin/pkg/apps/generated/internalclientset"
buildclientinternal "github.com/openshift/origin/pkg/build/generated/internalclientset"
imageclientinternal "github.com/openshift/origin/pkg/image/generated/internalclientset"
loginutil "github.com/openshift/origin/pkg/oc/cli/cmd/login/util"
"github.com/openshift/origin/pkg/oc/cli/describe"
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
projectclientinternal "github.com/openshift/origin/pkg/project/generated/internalclientset"
routeclientinternal "github.com/openshift/origin/pkg/route/generated/internalclientset"
dotutil "github.com/openshift/origin/pkg/util/dot"
)
// StatusRecommendedName is the recommended command name.
const StatusRecommendedName = "status"
// ExposeRecommendedName is the recommended command name to expose app.
const ExposeRecommendedName = "expose"
var (
statusLong = templates.LongDesc(`
Show a high level overview of the current project
This command will show services, deployment configs, build configurations, and active deployments.
If you have any misconfigured components information about them will be shown. For more information
about individual items, use the describe command (e.g. %[1]s describe buildConfig,
%[1]s describe deploymentConfig, %[1]s describe service).
You can specify an output format of "-o dot" to have this command output the generated status
graph in DOT format that is suitable for use by the "dot" command.`)
statusExample = templates.Examples(`
# See an overview of the current project.
%[1]s
# Export the overview of the current project in an svg file.
%[1]s -o dot | dot -T svg -o project.svg
# See an overview of the current project including details for any identified issues.
%[1]s -v`)
)
// StatusOptions contains all the necessary options for the Openshift cli status command.
type StatusOptions struct {
namespace string
allNamespaces bool
outputFormat string
describer *describe.ProjectStatusDescriber
out io.Writer
suggest bool
logsCommandName string
securityPolicyCommandFormat string
setProbeCommandName string
patchCommandName string
}
// NewCmdStatus implements the OpenShift cli status command.
// baseCLIName is the path from root cmd to the parent of this cmd.
func NewCmdStatus(name, baseCLIName, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
opts := &StatusOptions{}
cmd := &cobra.Command{
Use: fmt.Sprintf("%s [-o dot | -s ]", StatusRecommendedName),
Short: "Show an overview of the current project",
Long: fmt.Sprintf(statusLong, baseCLIName),
Example: fmt.Sprintf(statusExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
err := opts.Complete(f, cmd, baseCLIName, args, out)
kcmdutil.CheckErr(err)
if err := opts.Validate(); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageErrorf(cmd, err.Error()))
}
err = opts.RunStatus()
kcmdutil.CheckErr(err)
},
}
cmd.Flags().StringVarP(&opts.outputFormat, "output", "o", opts.outputFormat, "Output format. One of: dot.")
cmd.Flags().BoolVarP(&opts.suggest, "verbose", "v", opts.suggest, "See details for resolving issues.")
cmd.Flags().MarkDeprecated("verbose", "Use --suggest instead.")
cmd.Flags().MarkHidden("verbose")
cmd.Flags().BoolVar(&opts.suggest, "suggest", opts.suggest, "See details for resolving issues.")
cmd.Flags().BoolVar(&opts.allNamespaces, "all-namespaces", false, "If true, display status for all namespaces (must have cluster admin)")
return cmd
}
// Complete completes the options for the Openshift cli status command.
func (o *StatusOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, baseCLIName string, args []string, out io.Writer) error {
if len(args) > 0 {
return kcmdutil.UsageErrorf(cmd, "no arguments should be provided")
}
o.logsCommandName = fmt.Sprintf("%s logs", cmd.Parent().CommandPath())
o.securityPolicyCommandFormat = "oc adm policy add-scc-to-user anyuid -n %s -z %s"
o.setProbeCommandName = fmt.Sprintf("%s set probe", cmd.Parent().CommandPath())
clientConfig, err := f.ClientConfig()
if err != nil {
return err
}
kclientset, err := f.ClientSet()
if err != nil {
return err
}
projectClient, err := projectclientinternal.NewForConfig(clientConfig)
if err != nil {
return err
}
buildClient, err := buildclientinternal.NewForConfig(clientConfig)
if err != nil {
return err
}
imageClient, err := imageclientinternal.NewForConfig(clientConfig)
if err != nil {
return err
}
appsClient, err := appsclientinternal.NewForConfig(clientConfig)
if err != nil {
return err
}
routeClient, err := routeclientinternal.NewForConfig(clientConfig)
if err != nil {
return err
}
rawConfig, err := f.RawConfig()
if err != nil {
return err
}
if o.allNamespaces {
o.namespace = metav1.NamespaceAll
} else {
namespace, _, err := f.DefaultNamespace()
if err != nil {
return err
}
o.namespace = namespace
}
if baseCLIName == "" {
baseCLIName = "oc"
}
currentNamespace := ""
if currentContext, exists := rawConfig.Contexts[rawConfig.CurrentContext]; exists {
currentNamespace = currentContext.Namespace
}
nsFlag := kcmdutil.GetFlagString(cmd, "namespace")
canRequestProjects, _ := loginutil.CanRequestProjects(clientConfig, o.namespace)
o.describer = &describe.ProjectStatusDescriber{
KubeClient: kclientset,
ProjectClient: projectClient.Project(),
BuildClient: buildClient.Build(),
ImageClient: imageClient.Image(),
AppsClient: appsClient.Apps(),
RouteClient: routeClient.Route(),
Suggest: o.suggest,
Server: clientConfig.Host,
CommandBaseName: baseCLIName,
RequestedNamespace: nsFlag,
CurrentNamespace: currentNamespace,
CanRequestProjects: canRequestProjects,
// TODO: Remove these and reference them inside the markers using constants.
LogsCommandName: o.logsCommandName,
SecurityPolicyCommandFormat: o.securityPolicyCommandFormat,
SetProbeCommandName: o.setProbeCommandName,
}
o.out = out
return nil
}
// Validate validates the options for the Openshift cli status command.
func (o StatusOptions) Validate() error {
if len(o.outputFormat) != 0 && o.outputFormat != "dot" {
return fmt.Errorf("invalid output format provided: %s", o.outputFormat)
}
if len(o.outputFormat) > 0 && o.suggest {
return errors.New("cannot provide suggestions when output format is dot")
}
return nil
}
// RunStatus contains all the necessary functionality for the OpenShift cli status command.
func (o StatusOptions) RunStatus() error {
var (
s string
err error
)
switch o.outputFormat {
case "":
s, err = o.describer.Describe(o.namespace, "")
if err != nil {
return err
}
case "dot":
g, _, err := o.describer.MakeGraph(o.namespace)
if err != nil {
return err
}
data, err := dot.Marshal(g, dotutil.Quote(o.namespace), "", " ", false)
if err != nil {
return err
}
s = string(data)
default:
return fmt.Errorf("invalid output format provided: %s", o.outputFormat)
}
fmt.Fprintf(o.out, s)
return nil
}