@@ -16,17 +16,12 @@ public class KubernetesClientConfiguration
16
16
{
17
17
/// <summary>
18
18
/// Initializes a new instance of the <see cref="KubernetesClientConfiguration"/> class.
19
- /// Initializes a new instance of the ClientConfiguration class
20
19
/// </summary>
21
20
/// <param name="kubeconfig">kubeconfig file info</param>
22
21
/// <param name="currentContext">Context to use from kube config</param>
23
22
public KubernetesClientConfiguration ( FileInfo kubeconfig = null , string currentContext = null )
24
23
{
25
- if ( kubeconfig == null )
26
- {
27
- kubeconfig = new FileInfo ( KubeConfigDefaultLocation ) ;
28
- }
29
- var k8SConfig = this . LoadKubeConfig ( kubeconfig ) ;
24
+ var k8SConfig = this . LoadKubeConfig ( kubeconfig ?? new FileInfo ( KubeConfigDefaultLocation ) ) ;
30
25
this . Initialize ( k8SConfig , currentContext ) ;
31
26
}
32
27
@@ -108,86 +103,73 @@ public KubernetesClientConfiguration(FileInfo kubeconfig = null, string currentC
108
103
/// <param name="currentContext">Current Context</param>
109
104
private void Initialize ( K8SConfiguration k8SConfig , string currentContext = null )
110
105
{
111
- Context activeContext ;
112
-
113
106
if ( k8SConfig . Contexts == null )
114
107
{
115
108
throw new KubeConfigException ( "No contexts found in kubeconfig" ) ;
116
- }
109
+ }
117
110
118
- // set the currentCOntext to passed context if not null
119
- if ( ! string . IsNullOrWhiteSpace ( currentContext ) )
111
+ if ( k8SConfig . Clusters == null )
120
112
{
121
-
122
- activeContext = k8SConfig . Contexts . FirstOrDefault ( c => c . Name . Equals ( currentContext , StringComparison . OrdinalIgnoreCase ) ) ;
123
- if ( activeContext != null )
124
- {
125
- this . CurrentContext = activeContext . Name ;
126
- }
127
- else
128
- {
129
- throw new KubeConfigException ( $ "CurrentContext: { 0 } not found in contexts in kubeconfig") ;
130
- }
113
+ throw new KubeConfigException ( $ "No clusters found in kubeconfig") ;
131
114
}
132
- // otherwise set current context from kubeconfig
133
- else
115
+
116
+ if ( k8SConfig . Users == null )
134
117
{
135
- activeContext = k8SConfig . Contexts . FirstOrDefault ( c => c . Name . Equals ( k8SConfig . CurrentContext , StringComparison . OrdinalIgnoreCase ) ) ;
136
-
137
- if ( activeContext == null )
138
- {
139
- throw new KubeConfigException ( $ "CurrentContext: { currentContext } not found in contexts in kubeconfig") ;
140
- }
118
+ throw new KubeConfigException ( $ "No users found in kubeconfig") ;
119
+ }
141
120
142
- this . CurrentContext = activeContext . Name ;
121
+ // current context
122
+ currentContext = currentContext ?? k8SConfig . CurrentContext ;
123
+ Context activeContext = k8SConfig . Contexts . FirstOrDefault ( c => c . Name . Equals ( currentContext , StringComparison . OrdinalIgnoreCase ) ) ;
124
+ if ( activeContext == null )
125
+ {
126
+ throw new KubeConfigException ( $ "CurrentContext: { currentContext } not found in contexts in kubeconfig") ;
143
127
}
144
128
145
- if ( k8SConfig . Clusters == null )
129
+ this . CurrentContext = activeContext . Name ;
130
+
131
+ // cluster
132
+ var clusterDetails = k8SConfig . Clusters . FirstOrDefault ( c => c . Name . Equals ( activeContext . ContextDetails . Cluster , StringComparison . OrdinalIgnoreCase ) ) ;
133
+ if ( clusterDetails ? . ClusterEndpoint == null )
146
134
{
147
- throw new KubeConfigException ( $ "clusters not found for current- context : { activeContext } in kubeconfig") ;
135
+ throw new KubeConfigException ( $ "Cluster not found for context { activeContext } in kubeconfig") ;
148
136
}
149
137
150
- var clusterDetails = k8SConfig . Clusters . FirstOrDefault ( c => c . Name . Equals ( activeContext . ContextDetails . Cluster , StringComparison . OrdinalIgnoreCase ) ) ;
151
- if ( clusterDetails ? . ClusterEndpoint != null )
138
+ if ( string . IsNullOrWhiteSpace ( clusterDetails . ClusterEndpoint . Server ) )
152
139
{
153
- if ( string . IsNullOrWhiteSpace ( clusterDetails . ClusterEndpoint . Server ) )
154
- {
155
- throw new KubeConfigException ( $ "server not found for current-context :{ activeContext } in kubeconfig") ;
156
- }
140
+ throw new KubeConfigException ( $ "Server not found for current-context { activeContext } in kubeconfig") ;
141
+ }
157
142
158
- if ( ! clusterDetails . ClusterEndpoint . SkipTlsVerify &&
159
- string . IsNullOrWhiteSpace ( clusterDetails . ClusterEndpoint . CertificateAuthorityData ) &&
160
- string . IsNullOrWhiteSpace ( clusterDetails . ClusterEndpoint . CertificateAuthority ) )
161
- {
162
- throw new KubeConfigException ( $ "neither certificate-authority-data nor certificate-authority not found for current-context :{ activeContext } in kubeconfig") ;
163
- }
143
+ if ( ! clusterDetails . ClusterEndpoint . SkipTlsVerify &&
144
+ string . IsNullOrWhiteSpace ( clusterDetails . ClusterEndpoint . CertificateAuthorityData ) &&
145
+ string . IsNullOrWhiteSpace ( clusterDetails . ClusterEndpoint . CertificateAuthority ) )
146
+ {
147
+ throw new KubeConfigException ( $ "neither certificate-authority-data nor certificate-authority not found for current-context :{ activeContext } in kubeconfig") ;
148
+ }
164
149
165
- this . Host = clusterDetails . ClusterEndpoint . Server ;
166
- if ( ! string . IsNullOrEmpty ( clusterDetails . ClusterEndpoint . CertificateAuthorityData ) ) {
167
- string data = clusterDetails . ClusterEndpoint . CertificateAuthorityData ;
168
- this . SslCaCert = new X509Certificate2 ( System . Text . Encoding . UTF8 . GetBytes ( Utils . Base64Decode ( data ) ) ) ;
169
- }
170
- else if ( ! string . IsNullOrEmpty ( clusterDetails . ClusterEndpoint . CertificateAuthority ) ) {
171
- this . SslCaCert = new X509Certificate2 ( clusterDetails . ClusterEndpoint . CertificateAuthority , null ) ;
172
- }
173
- this . SkipTlsVerify = clusterDetails . ClusterEndpoint . SkipTlsVerify ;
150
+ this . Host = clusterDetails . ClusterEndpoint . Server ;
151
+ if ( ! string . IsNullOrEmpty ( clusterDetails . ClusterEndpoint . CertificateAuthorityData ) )
152
+ {
153
+ string data = clusterDetails . ClusterEndpoint . CertificateAuthorityData ;
154
+ this . SslCaCert = new X509Certificate2 ( System . Text . Encoding . UTF8 . GetBytes ( Utils . Base64Decode ( data ) ) ) ;
174
155
}
175
- else
156
+ else if ( ! string . IsNullOrEmpty ( clusterDetails . ClusterEndpoint . CertificateAuthority ) )
176
157
{
177
- throw new KubeConfigException ( $ "Cluster details not found for current-context: { activeContext } in kubeconfig" ) ;
158
+ this . SslCaCert = new X509Certificate2 ( clusterDetails . ClusterEndpoint . CertificateAuthority , null ) ;
178
159
}
160
+ this . SkipTlsVerify = clusterDetails . ClusterEndpoint . SkipTlsVerify ;
179
161
180
- // set user details from kubeconfig
181
- var userDetails = k8SConfig . Users . FirstOrDefault ( c => c . Name . Equals ( activeContext . ContextDetails . User , StringComparison . OrdinalIgnoreCase ) ) ;
182
-
183
- this . SetUserDetails ( userDetails ) ;
162
+ // user
163
+ this . SetUserDetails ( k8SConfig , activeContext ) ;
184
164
}
185
165
186
- private void SetUserDetails ( User userDetails )
166
+ private void SetUserDetails ( K8SConfiguration k8SConfig , Context activeContext )
187
167
{
168
+ var userDetails = k8SConfig . Users . FirstOrDefault ( c => c . Name . Equals ( activeContext . ContextDetails . User , StringComparison . OrdinalIgnoreCase ) ) ;
169
+
188
170
if ( userDetails == null )
189
171
{
190
- throw new KubeConfigException ( "User not found for the current context in kubeconfig" ) ;
172
+ throw new KubeConfigException ( "User not found for context {activeContext.Name} in kubeconfig" ) ;
191
173
}
192
174
193
175
if ( userDetails . UserCredentials == null )
@@ -229,7 +211,7 @@ private void SetUserDetails(User userDetails)
229
211
230
212
if ( ! userCredentialsFound )
231
213
{
232
- throw new KubeConfigException ( $ "User: { userDetails . Name } does not have appropriate auth credentials in kube config ") ;
214
+ throw new KubeConfigException ( $ "User: { userDetails . Name } does not have appropriate auth credentials in kubeconfig ") ;
233
215
}
234
216
}
235
217
0 commit comments