@@ -5,8 +5,7 @@ import * as k8s from '@kubernetes/client-node';
5
5
import { SoloError } from '../../errors.js' ;
6
6
import { type ConfigManager } from '../../config_manager.js' ;
7
7
import { type SoloLogger } from '../../logging.js' ;
8
- import { inject , injectable } from 'tsyringe-neo' ;
9
- import { patchInject } from '../../dependency_injection/container_helper.js' ;
8
+ import { container } from 'tsyringe-neo' ;
10
9
import { type K8 } from '../k8.js' ;
11
10
import { type Namespaces } from '../resources/namespace/namespaces.js' ;
12
11
import { K8ClientClusters } from '../k8_client/resources/cluster/k8_client_clusters.js' ;
@@ -16,7 +15,6 @@ import {K8ClientConfigMaps} from '../k8_client/resources/config_map/k8_client_co
16
15
import { K8ClientContainers } from '../k8_client/resources/container/k8_client_containers.js' ;
17
16
import { type Containers } from '../resources/container/containers.js' ;
18
17
import { type Contexts } from '../resources/context/contexts.js' ;
19
- import { K8ClientContexts } from '../k8_client/resources/context/k8_client_contexts.js' ;
20
18
import { K8ClientPods } from '../k8_client/resources/pod/k8_client_pods.js' ;
21
19
import { type Pods } from '../resources/pod/pods.js' ;
22
20
import { type Services } from '../resources/service/services.js' ;
@@ -33,14 +31,14 @@ import {K8ClientSecrets} from '../k8_client/resources/secret/k8_client_secrets.j
33
31
import { type Ingresses } from '../resources/ingress/ingresses.js' ;
34
32
import { K8ClientIngresses } from './resources/ingress/k8_client_ingresses.js' ;
35
33
import { InjectTokens } from '../../dependency_injection/inject_tokens.js' ;
34
+ import * as constants from '../../constants.js' ;
36
35
37
36
/**
38
37
* A kubernetes API wrapper class providing custom functionalities required by solo
39
38
*
40
39
* Note: Take care if the same instance is used for parallel execution, as the behaviour may be unpredictable.
41
40
* For parallel execution, create separate instances by invoking clone()
42
41
*/
43
- @injectable ( )
44
42
export class K8Client implements K8 {
45
43
private kubeConfig ! : k8s . KubeConfig ;
46
44
private kubeClient ! : k8s . CoreV1Api ;
@@ -60,20 +58,19 @@ export class K8Client implements K8 {
60
58
private k8Secrets : Secrets ;
61
59
private k8Ingresses : Ingresses ;
62
60
63
- constructor (
64
- @inject ( InjectTokens . ConfigManager ) private readonly configManager ?: ConfigManager ,
65
- @inject ( InjectTokens . SoloLogger ) private readonly logger ?: SoloLogger ,
66
- ) {
67
- this . configManager = patchInject ( configManager , InjectTokens . ConfigManager , this . constructor . name ) ;
68
- this . logger = patchInject ( logger , InjectTokens . SoloLogger , this . constructor . name ) ;
61
+ private readonly configManager : ConfigManager ;
62
+ private readonly logger : SoloLogger ;
69
63
70
- this . init ( ) ;
64
+ constructor ( private readonly context : string ) {
65
+ this . configManager = container . resolve ( InjectTokens . ConfigManager ) ;
66
+ this . logger = container . resolve ( InjectTokens . SoloLogger ) ;
67
+
68
+ this . init ( context ) ;
71
69
}
72
70
73
71
// TODO make private, but first we need to require a cluster to be set and address the test cases using this
74
- init ( ) : K8 {
75
- this . kubeConfig = new k8s . KubeConfig ( ) ;
76
- this . kubeConfig . loadFromDefault ( ) ;
72
+ init ( context : string = undefined ) : K8 {
73
+ this . kubeConfig = this . getKubeConfig ( context ) ;
77
74
78
75
if ( ! this . kubeConfig . getCurrentContext ( ) ) {
79
76
throw new SoloError ( 'No active kubernetes context found. ' + 'Please set current kubernetes context.' ) ;
@@ -89,7 +86,6 @@ export class K8Client implements K8 {
89
86
90
87
this . k8Clusters = new K8ClientClusters ( this . kubeConfig ) ;
91
88
this . k8ConfigMaps = new K8ClientConfigMaps ( this . kubeClient ) ;
92
- this . k8Contexts = new K8ClientContexts ( this . kubeConfig ) ;
93
89
this . k8Services = new K8ClientServices ( this . kubeClient ) ;
94
90
this . k8Pods = new K8ClientPods ( this . kubeClient , this . kubeConfig ) ;
95
91
this . k8Containers = new K8ClientContainers ( this . kubeConfig , this . k8Pods ) ;
@@ -100,7 +96,34 @@ export class K8Client implements K8 {
100
96
this . k8Secrets = new K8ClientSecrets ( this . kubeClient ) ;
101
97
this . k8Ingresses = new K8ClientIngresses ( this . networkingApi ) ;
102
98
103
- return this ; // to enable chaining
99
+ // TODO this will run in the background, change this to use an await somehow, possibly after init is made private
100
+ this . contexts ( )
101
+ . testContextConnection ( this . kubeConfig . getCurrentContext ( ) )
102
+ . then ( ( ) => {
103
+ this . logger . info ( `successfully established connection to context: ${ this . kubeConfig . getCurrentContext ( ) } ` ) ;
104
+ } )
105
+ . catch ( e => {
106
+ const message = `Failed to create a connect to the K8 context '${ this . kubeConfig . getCurrentContext ( ) } '` ;
107
+ this . logger . error ( message , e ) ;
108
+ throw new SoloError ( message , e ) ;
109
+ } ) ;
110
+
111
+ return this ;
112
+ }
113
+
114
+ private getKubeConfig ( context : string ) : k8s . KubeConfig {
115
+ const kubeConfig = new k8s . KubeConfig ( ) ;
116
+ kubeConfig . loadFromDefault ( ) ;
117
+
118
+ if ( context !== constants . K8_DEFAULT_INSTANCE ) {
119
+ const kubeConfigContext : k8s . Context = kubeConfig . getContextObject ( context ) ;
120
+ if ( ! kubeConfigContext ) {
121
+ throw new SoloError ( `No kube config context found with name ${ context } ` ) ;
122
+ }
123
+ kubeConfig . setCurrentContext ( context ) ;
124
+ }
125
+
126
+ return kubeConfig ;
104
127
}
105
128
106
129
public namespaces ( ) : Namespaces {
0 commit comments