6
6
"strings"
7
7
8
8
"k8s.io/apimachinery/pkg/util/sets"
9
+ "k8s.io/client-go/rest"
9
10
clientcmd "k8s.io/client-go/tools/clientcmd"
10
11
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
11
12
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
30
31
clustdiags .MasterNodeName ,
31
32
clustdiags .MetricsApiProxyName ,
32
33
clustdiags .NodeDefinitionsName ,
34
+ clustdiags .RouteCertificateValidationName ,
33
35
clustdiags .ServiceExternalIPsName ,
34
36
)
35
37
)
@@ -47,11 +49,14 @@ func (o DiagnosticsOptions) buildClusterDiagnostics(rawConfig *clientcmdapi.Conf
47
49
kclusterClient kclientset.Interface
48
50
)
49
51
50
- clusterClient , kclusterClient , found , serverUrl , err := o .findClusterClients (rawConfig )
52
+ config , clusterClient , kclusterClient , found , serverUrl , err := o .findClusterClients (rawConfig )
51
53
if ! found {
52
54
o .Logger .Notice ("CED1002" , "Could not configure a client with cluster-admin permissions for the current server, so cluster diagnostics will be skipped" )
53
55
return nil , true , err
54
56
}
57
+ if err != nil {
58
+ return nil , false , err
59
+ }
55
60
56
61
diagnostics := []types.Diagnostic {}
57
62
for _ , diagnosticName := range requestedDiagnostics {
@@ -75,6 +80,8 @@ func (o DiagnosticsOptions) buildClusterDiagnostics(rawConfig *clientcmdapi.Conf
75
80
d = & clustdiags.MetricsApiProxy {KubeClient : kclusterClient }
76
81
case clustdiags .ServiceExternalIPsName :
77
82
d = & clustdiags.ServiceExternalIPs {MasterConfigFile : o .MasterConfigLocation , KclusterClient : kclusterClient }
83
+ case clustdiags .RouteCertificateValidationName :
84
+ d = & clustdiags.RouteCertificateValidation {OsClient : clusterClient , RESTConfig : config }
78
85
default :
79
86
return nil , false , fmt .Errorf ("unknown diagnostic: %v" , diagnosticName )
80
87
}
@@ -84,51 +91,56 @@ func (o DiagnosticsOptions) buildClusterDiagnostics(rawConfig *clientcmdapi.Conf
84
91
}
85
92
86
93
// attempts to find which context in the config might be a cluster-admin for the server in the current context.
87
- func (o DiagnosticsOptions ) findClusterClients (rawConfig * clientcmdapi.Config ) (* client.Client , kclientset.Interface , bool , string , error ) {
94
+ func (o DiagnosticsOptions ) findClusterClients (rawConfig * clientcmdapi.Config ) (* rest. Config , * client.Client , kclientset.Interface , bool , string , error ) {
88
95
if o .ClientClusterContext != "" { // user has specified cluster context to use
89
96
if context , exists := rawConfig .Contexts [o .ClientClusterContext ]; exists {
90
97
configErr := fmt .Errorf ("Specified '%s' as cluster-admin context, but it was not found in your client configuration." , o .ClientClusterContext )
91
98
o .Logger .Error ("CED1003" , configErr .Error ())
92
- return nil , nil , false , "" , configErr
93
- } else if os , kube , found , serverUrl , err := o .makeClusterClients (rawConfig , o .ClientClusterContext , context ); found {
94
- return os , kube , true , serverUrl , err
99
+ return nil , nil , nil , false , "" , configErr
100
+ } else if config , os , kube , found , serverUrl , err := o .makeClusterClients (rawConfig , o .ClientClusterContext , context ); found {
101
+ return config , os , kube , true , serverUrl , err
95
102
} else {
96
- return nil , nil , false , "" , err
103
+ return nil , nil , nil , false , "" , err
97
104
}
98
105
}
99
106
currentContext , exists := rawConfig .Contexts [rawConfig .CurrentContext ]
100
107
if ! exists { // config specified cluster admin context that doesn't exist; complain and quit
101
108
configErr := fmt .Errorf ("Current context '%s' not found in client configuration; will not attempt cluster diagnostics." , rawConfig .CurrentContext )
102
109
o .Logger .Error ("CED1004" , configErr .Error ())
103
- return nil , nil , false , "" , configErr
110
+ return nil , nil , nil , false , "" , configErr
104
111
}
105
112
// check if current context is already cluster admin
106
- if os , kube , found , serverUrl , err := o .makeClusterClients (rawConfig , rawConfig .CurrentContext , currentContext ); found {
107
- return os , kube , true , serverUrl , err
113
+ if config , os , kube , found , serverUrl , err := o .makeClusterClients (rawConfig , rawConfig .CurrentContext , currentContext ); found {
114
+ return config , os , kube , true , serverUrl , err
108
115
}
109
116
// otherwise, for convenience, search for a context with the same server but with the system:admin user
110
117
for name , context := range rawConfig .Contexts {
111
118
if context .Cluster == currentContext .Cluster && name != rawConfig .CurrentContext && strings .HasPrefix (context .AuthInfo , "system:admin/" ) {
112
- if os , kube , found , serverUrl , err := o .makeClusterClients (rawConfig , name , context ); found {
113
- return os , kube , true , serverUrl , err
119
+ if config , os , kube , found , serverUrl , err := o .makeClusterClients (rawConfig , name , context ); found {
120
+ return config , os , kube , true , serverUrl , err
114
121
} else {
115
- return nil , nil , false , "" , err // don't try more than one such context, they'll probably fail the same
122
+ return nil , nil , nil , false , "" , err // don't try more than one such context, they'll probably fail the same
116
123
}
117
124
}
118
125
}
119
- return nil , nil , false , "" , nil
126
+ return nil , nil , nil , false , "" , nil
120
127
}
121
128
122
129
// makes the client from the specified context and determines whether it is a cluster-admin.
123
- func (o DiagnosticsOptions ) makeClusterClients (rawConfig * clientcmdapi.Config , contextName string , context * clientcmdapi.Context ) (* client.Client , kclientset.Interface , bool , string , error ) {
130
+ func (o DiagnosticsOptions ) makeClusterClients (rawConfig * clientcmdapi.Config , contextName string , context * clientcmdapi.Context ) (* rest. Config , * client.Client , kclientset.Interface , bool , string , error ) {
124
131
overrides := & clientcmd.ConfigOverrides {Context : * context }
125
132
clientConfig := clientcmd .NewDefaultClientConfig (* rawConfig , overrides )
126
133
serverUrl := rawConfig .Clusters [context .Cluster ].Server
127
134
factory := osclientcmd .NewFactory (clientConfig )
135
+ config , err := factory .ClientConfig ()
136
+ if err != nil {
137
+ o .Logger .Debug ("CED1006" , fmt .Sprintf ("Error creating client for context '%s':\n %v" , contextName , err ))
138
+ return nil , nil , nil , false , "" , nil
139
+ }
128
140
o .Logger .Debug ("CED1005" , fmt .Sprintf ("Checking if context is cluster-admin: '%s'" , contextName ))
129
141
if osClient , kubeClient , err := factory .Clients (); err != nil {
130
142
o .Logger .Debug ("CED1006" , fmt .Sprintf ("Error creating client for context '%s':\n %v" , contextName , err ))
131
- return nil , nil , false , "" , nil
143
+ return nil , nil , nil , false , "" , nil
132
144
} else {
133
145
subjectAccessReview := authorizationapi.SubjectAccessReview {Action : authorizationapi.Action {
134
146
// if you can do everything, you're the cluster admin.
@@ -139,16 +151,16 @@ func (o DiagnosticsOptions) makeClusterClients(rawConfig *clientcmdapi.Config, c
139
151
if resp , err := osClient .SubjectAccessReviews ().Create (& subjectAccessReview ); err != nil {
140
152
if regexp .MustCompile (`User "[\w:]+" cannot create \w+ at the cluster scope` ).MatchString (err .Error ()) {
141
153
o .Logger .Debug ("CED1007" , fmt .Sprintf ("Context '%s' does not have cluster-admin access:\n %v" , contextName , err ))
142
- return nil , nil , false , "" , nil
154
+ return nil , nil , nil , false , "" , nil
143
155
} else {
144
156
o .Logger .Error ("CED1008" , fmt .Sprintf ("Unknown error testing cluster-admin access for context '%s':\n %v" , contextName , err ))
145
- return nil , nil , false , "" , err
157
+ return nil , nil , nil , false , "" , err
146
158
}
147
159
} else if resp .Allowed {
148
160
o .Logger .Info ("CED1009" , fmt .Sprintf ("Using context for cluster-admin access: '%s'" , contextName ))
149
- return osClient , kubeClient , true , serverUrl , nil
161
+ return config , osClient , kubeClient , true , serverUrl , nil
150
162
}
151
163
}
152
164
o .Logger .Debug ("CED1010" , fmt .Sprintf ("Context does not have cluster-admin access: '%s'" , contextName ))
153
- return nil , nil , false , "" , nil
165
+ return nil , nil , nil , false , "" , nil
154
166
}
0 commit comments