@@ -57,14 +57,14 @@ func (o DiagnosticsOptions) buildClusterDiagnostics(rawConfig *clientcmdapi.Conf
57
57
58
58
var kclusterClient kclientset.Interface
59
59
60
- config , kclusterClient , serverUrl , err := o .findClusterClients (rawConfig )
60
+ config , kclusterClient , rawAdminConfig , err := o .findClusterClients (rawConfig )
61
+ if err != nil {
62
+ return nil , err
63
+ }
61
64
if config == nil {
62
65
o .Logger ().Notice ("CED1002" , "Could not configure a client with cluster-admin permissions for the current server, so cluster diagnostics will be skipped" )
63
66
return nil , nil
64
67
}
65
- if err != nil {
66
- return nil , err
67
- }
68
68
imageClient , err := imageclient .NewForConfig (config )
69
69
if err != nil {
70
70
return nil , err
@@ -118,6 +118,7 @@ func (o DiagnosticsOptions) buildClusterDiagnostics(rawConfig *clientcmdapi.Conf
118
118
case clustdiags .NodeDefinitionsName :
119
119
d = & clustdiags.NodeDefinitions {KubeClient : kclusterClient }
120
120
case clustdiags .MasterNodeName :
121
+ serverUrl := rawAdminConfig .Clusters [rawAdminConfig .Contexts [rawAdminConfig .CurrentContext ].Cluster ].Server
121
122
d = & clustdiags.MasterNode {KubeClient : kclusterClient , ServerUrl : serverUrl , MasterConfigFile : o .MasterConfigLocation }
122
123
case clustdiags .ClusterRegistryName :
123
124
d = & clustdiags.ClusterRegistry {KubeClient : kclusterClient , ImageStreamClient : imageClient .Image (), PreventModification : o .PreventModification }
@@ -141,8 +142,9 @@ func (o DiagnosticsOptions) buildClusterDiagnostics(rawConfig *clientcmdapi.Conf
141
142
nd .ClientFlags = o .ClientFlags
142
143
nd .Level = o .LogOptions .Level
143
144
nd .Factory = o .Factory
145
+ nd .RawConfig = rawAdminConfig
144
146
nd .PreventModification = o .PreventModification
145
- diagnostics = append ( diagnostics , nd )
147
+ d = nd
146
148
default :
147
149
return nil , fmt .Errorf ("unknown diagnostic: %v" , diagnosticName )
148
150
}
@@ -152,84 +154,96 @@ func (o DiagnosticsOptions) buildClusterDiagnostics(rawConfig *clientcmdapi.Conf
152
154
}
153
155
154
156
// attempts to find which context in the config might be a cluster-admin for the server in the current context.
155
- // returns config for the context chosen, kclusterClient for same, serverUrl of same, and any fatal error
156
- func (o DiagnosticsOptions ) findClusterClients (rawConfig * clientcmdapi.Config ) (* rest.Config , kclientset.Interface , string , error ) {
157
+ // returns openshift client config for the context chosen, kclusterClient and raw config of same, and any fatal error
158
+ func (o DiagnosticsOptions ) findClusterClients (rawConfig * clientcmdapi.Config ) (* rest.Config , kclientset.Interface , * clientcmdapi. Config , error ) {
157
159
if o .ClientClusterContext != "" { // user has specified cluster context to use
158
160
context , exists := rawConfig .Contexts [o .ClientClusterContext ]
159
161
if ! exists {
160
162
configErr := fmt .Errorf ("Specified '%s' as cluster-admin context, but it was not found in your client configuration." , o .ClientClusterContext )
161
163
o .Logger ().Error ("CED1003" , configErr .Error ())
162
- return nil , nil , "" , configErr
164
+ return nil , nil , nil , configErr
163
165
}
164
- config , kube , serverUrl , err := o .makeClusterClients (rawConfig , o .ClientClusterContext , context )
165
- if err != nil || config == nil {
166
- return nil , nil , "" , err
167
- }
168
- return config , kube , serverUrl , nil
166
+ return o .makeClusterClients (rawConfig , o .ClientClusterContext , context )
169
167
}
170
168
currentContext , exists := rawConfig .Contexts [rawConfig .CurrentContext ]
171
169
if ! exists { // config specified cluster admin context that doesn't exist; complain and quit
172
170
configErr := fmt .Errorf ("Current context '%s' not found in client configuration; will not attempt cluster diagnostics." , rawConfig .CurrentContext )
173
171
o .Logger ().Error ("CED1004" , configErr .Error ())
174
- return nil , nil , "" , configErr
172
+ return nil , nil , nil , configErr
175
173
}
174
+
176
175
// check if current context is already cluster admin
177
- config , kube , serverUrl , err := o .makeClusterClients (rawConfig , rawConfig .CurrentContext , currentContext )
176
+ config , kube , rawAdminConfig , err := o .makeClusterClients (rawConfig , rawConfig .CurrentContext , currentContext )
178
177
if err == nil && config != nil {
179
- return config , kube , serverUrl , nil
178
+ return config , kube , rawAdminConfig , nil
180
179
}
180
+
181
181
// otherwise, for convenience, search for a context with the same server but with the system:admin user
182
182
for name , context := range rawConfig .Contexts {
183
183
if context .Cluster == currentContext .Cluster && name != rawConfig .CurrentContext && strings .HasPrefix (context .AuthInfo , "system:admin/" ) {
184
- config , kube , serverUrl , err := o .makeClusterClients (rawConfig , name , context )
184
+ config , kube , rawAdminConfig , err := o .makeClusterClients (rawConfig , name , context )
185
185
if err != nil || config == nil {
186
186
break // don't try more than one such context, they'll probably fail the same
187
187
}
188
- return config , kube , serverUrl , nil
188
+ return config , kube , rawAdminConfig , nil
189
189
}
190
190
}
191
- return nil , nil , "" , nil
191
+ return nil , nil , nil , nil
192
192
}
193
193
194
194
// makes the client from the specified context and determines whether it is a cluster-admin.
195
- func (o DiagnosticsOptions ) makeClusterClients (rawConfig * clientcmdapi.Config , contextName string , context * clientcmdapi.Context ) (* rest.Config , kclientset.Interface , string , error ) {
195
+ func (o DiagnosticsOptions ) makeClusterClients (rawConfig * clientcmdapi.Config , contextName string , context * clientcmdapi.Context ) (* rest.Config , kclientset.Interface , * clientcmdapi. Config , error ) {
196
196
overrides := & clientcmd.ConfigOverrides {Context : * context }
197
197
clientConfig := clientcmd .NewDefaultClientConfig (* rawConfig , overrides )
198
- serverUrl := rawConfig .Clusters [context .Cluster ].Server
199
198
factory := osclientcmd .NewFactory (clientConfig )
199
+
200
+ // create a config for making openshift clients
200
201
config , err := factory .ClientConfig ()
201
202
if err != nil {
202
- o .Logger ().Debug ("CED1006" , fmt .Sprintf ("Error creating client for context '%s':\n %v" , contextName , err ))
203
- return nil , nil , "" , nil
203
+ o .Logger ().Debug ("CED1006" , fmt .Sprintf ("Error creating client config for context '%s':\n %v" , contextName , err ))
204
+ return nil , nil , nil , nil
205
+ }
206
+
207
+ // create a kube client
208
+ kubeClient , err := factory .ClientSet ()
209
+ if err != nil {
210
+ o .Logger ().Debug ("CED1006" , fmt .Sprintf ("Error creating kube client for context '%s':\n %v" , contextName , err ))
211
+ return nil , nil , nil , nil
204
212
}
213
+
205
214
o .Logger ().Debug ("CED1005" , fmt .Sprintf ("Checking if context is cluster-admin: '%s'" , contextName ))
206
- if kubeClient , err := factory .ClientSet (); err != nil {
207
- o .Logger ().Debug ("CED1006" , fmt .Sprintf ("Error creating client for context '%s':\n %v" , contextName , err ))
208
- return nil , nil , "" , nil
209
- } else {
210
- subjectAccessReview := & authorization.SelfSubjectAccessReview {
211
- Spec : authorization.SelfSubjectAccessReviewSpec {
212
- ResourceAttributes : & authorization.ResourceAttributes {
213
- // if you can do everything, you're the cluster admin.
214
- Verb : "*" ,
215
- Group : "*" ,
216
- Resource : "*" ,
217
- },
215
+ subjectAccessReview := & authorization.SelfSubjectAccessReview {
216
+ Spec : authorization.SelfSubjectAccessReviewSpec {
217
+ ResourceAttributes : & authorization.ResourceAttributes {
218
+ // if you can do everything, you're the cluster admin.
219
+ Verb : "*" ,
220
+ Group : "*" ,
221
+ Resource : "*" ,
218
222
},
219
- }
220
- if resp , err := kubeClient .Authorization ().SelfSubjectAccessReviews ().Create (subjectAccessReview ); err != nil {
221
- if regexp .MustCompile (`User "[\w:]+" cannot create \w+ at the cluster scope` ).MatchString (err .Error ()) {
222
- o .Logger ().Debug ("CED1007" , fmt .Sprintf ("Context '%s' does not have cluster-admin access:\n %v" , contextName , err ))
223
- return nil , nil , "" , nil
224
- } else {
225
- o .Logger ().Error ("CED1008" , fmt .Sprintf ("Unknown error testing cluster-admin access for context '%s':\n %v" , contextName , err ))
226
- return nil , nil , "" , err
227
- }
228
- } else if resp .Status .Allowed {
229
- o .Logger ().Info ("CED1009" , fmt .Sprintf ("Using context for cluster-admin access: '%s'" , contextName ))
230
- return config , kubeClient , serverUrl , nil
231
- }
223
+ },
224
+ }
225
+ resp , err := kubeClient .Authorization ().SelfSubjectAccessReviews ().Create (subjectAccessReview )
226
+ if err != nil && regexp .MustCompile (`User "[\w:]+" cannot create \w+ at the cluster scope` ).MatchString (err .Error ()) {
227
+ o .Logger ().Debug ("CED1007" , fmt .Sprintf ("Context '%s' does not have cluster-admin access:\n %v" , contextName , err ))
228
+ return nil , nil , nil , nil
229
+ }
230
+ if err != nil {
231
+ o .Logger ().Error ("CED1008" , fmt .Sprintf ("Unknown error testing cluster-admin access for context '%s':\n %v" , contextName , err ))
232
+ return nil , nil , nil , err
233
+ }
234
+ if ! resp .Status .Allowed {
235
+ o .Logger ().Debug ("CED1010" , fmt .Sprintf ("Context does not have cluster-admin access: '%s'" , contextName ))
236
+ return nil , nil , nil , nil
237
+ }
238
+
239
+ o .Logger ().Info ("CED1009" , fmt .Sprintf ("Using context for cluster-admin access: '%s'" , contextName ))
240
+ adminConfig := rawConfig .DeepCopy ()
241
+ adminConfig .CurrentContext = contextName
242
+ if err := clientcmdapi .MinifyConfig (adminConfig ); err != nil {
243
+ return nil , nil , nil , err
244
+ }
245
+ if err := clientcmdapi .FlattenConfig (adminConfig ); err != nil {
246
+ return nil , nil , nil , err
232
247
}
233
- o .Logger ().Debug ("CED1010" , fmt .Sprintf ("Context does not have cluster-admin access: '%s'" , contextName ))
234
- return nil , nil , "" , nil
248
+ return config , kubeClient , adminConfig , nil
235
249
}
0 commit comments