@@ -43,52 +43,50 @@ func availableClusterDiagnostics() types.DiagnosticList {
43
43
}
44
44
45
45
// buildClusterDiagnostics builds cluster Diagnostic objects if a cluster-admin client can be extracted from the rawConfig passed in.
46
- // Returns the Diagnostics built, "ok" bool for whether to proceed or abort, and an error if any was encountered during the building of diagnostics.) {
47
- func (o DiagnosticsOptions ) buildClusterDiagnostics (rawConfig * clientcmdapi.Config ) ([]types.Diagnostic , bool , error ) {
46
+ // Returns the Diagnostics built and any fatal error encountered during the building of diagnostics.
47
+ func (o DiagnosticsOptions ) buildClusterDiagnostics (rawConfig * clientcmdapi.Config ) ([]types.Diagnostic , error ) {
48
48
requestedDiagnostics := availableClusterDiagnostics ().Names ().Intersection (sets .NewString (o .RequestedDiagnostics .List ()... )).List ()
49
49
if len (requestedDiagnostics ) == 0 { // no diagnostics to run here
50
- return nil , true , nil // don't waste time on discovery
50
+ return nil , nil // don't waste time on discovery
51
51
}
52
52
53
- var (
54
- kclusterClient kclientset.Interface
55
- )
53
+ var kclusterClient kclientset.Interface
56
54
57
- config , kclusterClient , found , serverUrl , err := o .findClusterClients (rawConfig )
58
- if ! found {
59
- o .Logger .Notice ("CED1002" , "Could not configure a client with cluster-admin permissions for the current server, so cluster diagnostics will be skipped" )
60
- return nil , true , err
55
+ config , kclusterClient , serverUrl , err := o .findClusterClients (rawConfig )
56
+ if config == nil {
57
+ o .Logger () .Notice ("CED1002" , "Could not configure a client with cluster-admin permissions for the current server, so cluster diagnostics will be skipped" )
58
+ return nil , nil
61
59
}
62
60
if err != nil {
63
- return nil , false , err
61
+ return nil , err
64
62
}
65
63
imageClient , err := imageclient .NewForConfig (config )
66
64
if err != nil {
67
- return nil , false , err
65
+ return nil , err
68
66
}
69
67
projectClient , err := projectclient .NewForConfig (config )
70
68
if err != nil {
71
- return nil , false , err
69
+ return nil , err
72
70
}
73
71
routeClient , err := routeclient .NewForConfig (config )
74
72
if err != nil {
75
- return nil , false , err
73
+ return nil , err
76
74
}
77
75
appsClient , err := appsclient .NewForConfig (config )
78
76
if err != nil {
79
- return nil , false , err
77
+ return nil , err
80
78
}
81
79
oauthClient , err := oauthclient .NewForConfig (config )
82
80
if err != nil {
83
- return nil , false , err
81
+ return nil , err
84
82
}
85
83
oauthorizationClient , err := oauthorizationclient .NewForConfig (config )
86
84
if err != nil {
87
- return nil , false , err
85
+ return nil , err
88
86
}
89
87
securityClient , err := securityclient .NewForConfig (config )
90
88
if err != nil {
91
- return nil , false , err
89
+ return nil , err
92
90
}
93
91
94
92
diagnostics := []types.Diagnostic {}
@@ -116,64 +114,68 @@ func (o DiagnosticsOptions) buildClusterDiagnostics(rawConfig *clientcmdapi.Conf
116
114
case clustdiags .RouteCertificateValidationName :
117
115
d = & clustdiags.RouteCertificateValidation {SARClient : kclusterClient .Authorization (), RESTConfig : config }
118
116
default :
119
- return nil , false , fmt .Errorf ("unknown diagnostic: %v" , diagnosticName )
117
+ return nil , fmt .Errorf ("unknown diagnostic: %v" , diagnosticName )
120
118
}
121
119
diagnostics = append (diagnostics , d )
122
120
}
123
- return diagnostics , true , nil
121
+ return diagnostics , nil
124
122
}
125
123
126
124
// attempts to find which context in the config might be a cluster-admin for the server in the current context.
127
- func (o DiagnosticsOptions ) findClusterClients (rawConfig * clientcmdapi.Config ) (* rest.Config , kclientset.Interface , bool , string , error ) {
125
+ // returns config for the context chosen, kclusterClient for same, serverUrl of same, and any fatal error
126
+ func (o DiagnosticsOptions ) findClusterClients (rawConfig * clientcmdapi.Config ) (* rest.Config , kclientset.Interface , string , error ) {
128
127
if o .ClientClusterContext != "" { // user has specified cluster context to use
129
- if context , exists := rawConfig .Contexts [o .ClientClusterContext ]; ! exists {
128
+ context , exists := rawConfig .Contexts [o .ClientClusterContext ]
129
+ if ! exists {
130
130
configErr := fmt .Errorf ("Specified '%s' as cluster-admin context, but it was not found in your client configuration." , o .ClientClusterContext )
131
- o .Logger .Error ("CED1003" , configErr .Error ())
132
- return nil , nil , false , "" , configErr
133
- } else if config , kube , found , serverUrl , err := o .makeClusterClients (rawConfig , o .ClientClusterContext , context ); found {
134
- return config , kube , true , serverUrl , err
135
- } else {
136
- return nil , nil , false , "" , err
131
+ o .Logger ().Error ("CED1003" , configErr .Error ())
132
+ return nil , nil , "" , configErr
137
133
}
134
+ config , kube , serverUrl , err := o .makeClusterClients (rawConfig , o .ClientClusterContext , context )
135
+ if err != nil || config == nil {
136
+ return nil , nil , "" , err
137
+ }
138
+ return config , kube , serverUrl , nil
138
139
}
139
140
currentContext , exists := rawConfig .Contexts [rawConfig .CurrentContext ]
140
141
if ! exists { // config specified cluster admin context that doesn't exist; complain and quit
141
142
configErr := fmt .Errorf ("Current context '%s' not found in client configuration; will not attempt cluster diagnostics." , rawConfig .CurrentContext )
142
- o .Logger .Error ("CED1004" , configErr .Error ())
143
- return nil , nil , false , "" , configErr
143
+ o .Logger () .Error ("CED1004" , configErr .Error ())
144
+ return nil , nil , "" , configErr
144
145
}
145
146
// check if current context is already cluster admin
146
- if config , kube , found , serverUrl , err := o .makeClusterClients (rawConfig , rawConfig .CurrentContext , currentContext ); found {
147
- return config , kube , true , serverUrl , err
147
+ config , kube , serverUrl , err := o .makeClusterClients (rawConfig , rawConfig .CurrentContext , currentContext )
148
+ if err == nil && config != nil {
149
+ return config , kube , serverUrl , nil
148
150
}
149
151
// otherwise, for convenience, search for a context with the same server but with the system:admin user
150
152
for name , context := range rawConfig .Contexts {
151
153
if context .Cluster == currentContext .Cluster && name != rawConfig .CurrentContext && strings .HasPrefix (context .AuthInfo , "system:admin/" ) {
152
- if config , kube , found , serverUrl , err := o .makeClusterClients (rawConfig , name , context ); found {
153
- return config , kube , true , serverUrl , err
154
- } else {
155
- return nil , nil , false , "" , err // don't try more than one such context, they'll probably fail the same
154
+ config , kube , serverUrl , err := o .makeClusterClients (rawConfig , name , context )
155
+ if err != nil || config == nil {
156
+ break // don't try more than one such context, they'll probably fail the same
156
157
}
158
+ return config , kube , serverUrl , nil
157
159
}
158
160
}
159
- return nil , nil , false , "" , nil
161
+ return nil , nil , "" , nil
160
162
}
161
163
162
164
// makes the client from the specified context and determines whether it is a cluster-admin.
163
- func (o DiagnosticsOptions ) makeClusterClients (rawConfig * clientcmdapi.Config , contextName string , context * clientcmdapi.Context ) (* rest.Config , kclientset.Interface , bool , string , error ) {
165
+ func (o DiagnosticsOptions ) makeClusterClients (rawConfig * clientcmdapi.Config , contextName string , context * clientcmdapi.Context ) (* rest.Config , kclientset.Interface , string , error ) {
164
166
overrides := & clientcmd.ConfigOverrides {Context : * context }
165
167
clientConfig := clientcmd .NewDefaultClientConfig (* rawConfig , overrides )
166
168
serverUrl := rawConfig .Clusters [context .Cluster ].Server
167
169
factory := osclientcmd .NewFactory (clientConfig )
168
170
config , err := factory .ClientConfig ()
169
171
if err != nil {
170
- o .Logger .Debug ("CED1006" , fmt .Sprintf ("Error creating client for context '%s':\n %v" , contextName , err ))
171
- return nil , nil , false , "" , nil
172
+ o .Logger () .Debug ("CED1006" , fmt .Sprintf ("Error creating client for context '%s':\n %v" , contextName , err ))
173
+ return nil , nil , "" , nil
172
174
}
173
- o .Logger .Debug ("CED1005" , fmt .Sprintf ("Checking if context is cluster-admin: '%s'" , contextName ))
175
+ o .Logger () .Debug ("CED1005" , fmt .Sprintf ("Checking if context is cluster-admin: '%s'" , contextName ))
174
176
if kubeClient , err := factory .ClientSet (); err != nil {
175
- o .Logger .Debug ("CED1006" , fmt .Sprintf ("Error creating client for context '%s':\n %v" , contextName , err ))
176
- return nil , nil , false , "" , nil
177
+ o .Logger () .Debug ("CED1006" , fmt .Sprintf ("Error creating client for context '%s':\n %v" , contextName , err ))
178
+ return nil , nil , "" , nil
177
179
} else {
178
180
subjectAccessReview := & authorization.SelfSubjectAccessReview {
179
181
Spec : authorization.SelfSubjectAccessReviewSpec {
@@ -187,17 +189,17 @@ func (o DiagnosticsOptions) makeClusterClients(rawConfig *clientcmdapi.Config, c
187
189
}
188
190
if resp , err := kubeClient .Authorization ().SelfSubjectAccessReviews ().Create (subjectAccessReview ); err != nil {
189
191
if regexp .MustCompile (`User "[\w:]+" cannot create \w+ at the cluster scope` ).MatchString (err .Error ()) {
190
- o .Logger .Debug ("CED1007" , fmt .Sprintf ("Context '%s' does not have cluster-admin access:\n %v" , contextName , err ))
191
- return nil , nil , false , "" , nil
192
+ o .Logger () .Debug ("CED1007" , fmt .Sprintf ("Context '%s' does not have cluster-admin access:\n %v" , contextName , err ))
193
+ return nil , nil , "" , nil
192
194
} else {
193
- o .Logger .Error ("CED1008" , fmt .Sprintf ("Unknown error testing cluster-admin access for context '%s':\n %v" , contextName , err ))
194
- return nil , nil , false , "" , err
195
+ o .Logger () .Error ("CED1008" , fmt .Sprintf ("Unknown error testing cluster-admin access for context '%s':\n %v" , contextName , err ))
196
+ return nil , nil , "" , err
195
197
}
196
198
} else if resp .Status .Allowed {
197
- o .Logger .Info ("CED1009" , fmt .Sprintf ("Using context for cluster-admin access: '%s'" , contextName ))
198
- return config , kubeClient , true , serverUrl , nil
199
+ o .Logger () .Info ("CED1009" , fmt .Sprintf ("Using context for cluster-admin access: '%s'" , contextName ))
200
+ return config , kubeClient , serverUrl , nil
199
201
}
200
202
}
201
- o .Logger .Debug ("CED1010" , fmt .Sprintf ("Context does not have cluster-admin access: '%s'" , contextName ))
202
- return nil , nil , false , "" , nil
203
+ o .Logger () .Debug ("CED1010" , fmt .Sprintf ("Context does not have cluster-admin access: '%s'" , contextName ))
204
+ return nil , nil , "" , nil
203
205
}
0 commit comments