@@ -30,7 +30,34 @@ import { Progress } from './util/progress';
30
30
import { FileContentChangeNotifier , WatchUtil } from './util/watch' ;
31
31
import { vsCommand } from './vscommand' ;
32
32
33
- const kubeConfigFolder : string = path . join ( Platform . getUserHomePath ( ) , '.kube' ) ;
33
+ /**
34
+ * Returns the list of kube config files:
35
+ * - If KUBECONFIG is not set, just ~/.kube/config
36
+ * - If KUBECONFIG is set, follows the semantics for specifying multiple config files described here:
37
+ * https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/#append-home-kube-config-to-your-kubeconfig-environment-variable
38
+ * BUT: it shows an error if multiple configs are specified, since the Kubernetes client we are using doesn't support this use case.
39
+ */
40
+ function getKubeConfigFiles ( ) : string [ ] {
41
+ if ( process . env . KUBECONFIG ) {
42
+ let configuredFiles : string [ ] = [ ] ;
43
+ if ( process . platform === 'win32' ) {
44
+ configuredFiles = process . env . KUBECONFIG . split ( ';' ) ;
45
+ } else {
46
+ configuredFiles = process . env . KUBECONFIG . split ( ':' ) ;
47
+ }
48
+ if ( configuredFiles . length > 1 ) {
49
+ void window . showWarningMessage ( 'KUBECONFIG specifies multiple files. Unfortunately, the extension doesn\'t work properly with this setup. Expect things to break.' ) ;
50
+ }
51
+ const filesThatExist : string [ ] = [ ] ;
52
+ for ( const configFile of configuredFiles ) {
53
+ if ( fs . existsSync ( configFile ) ) {
54
+ filesThatExist . push ( configFile ) ;
55
+ }
56
+ }
57
+ return filesThatExist ;
58
+ }
59
+ return [ path . join ( Platform . getUserHomePath ( ) , '.kube' , 'config' ) ] ;
60
+ }
34
61
35
62
type ExplorerItem = KubernetesObject | Helm . HelmRelease | Context | TreeItem ;
36
63
@@ -42,7 +69,7 @@ type PackageJSON = {
42
69
const CREATE_OR_SET_PROJECT_ITEM = {
43
70
label : 'Create new or set active Project' ,
44
71
command : {
45
- title : 'Create new or ser active Project' ,
72
+ title : 'Create new or set active Project' ,
46
73
command : 'openshift.project.set'
47
74
}
48
75
} ;
@@ -52,7 +79,7 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
52
79
53
80
private treeView : TreeView < ExplorerItem > ;
54
81
55
- private fsw : FileContentChangeNotifier ;
82
+ private kubeConfigWatchers : FileContentChangeNotifier [ ] ;
56
83
private kubeContext : Context ;
57
84
private kubeConfig : KubeConfigUtils ;
58
85
@@ -70,22 +97,25 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
70
97
// ignore config loading error and let odo report it on first call
71
98
}
72
99
try {
73
- this . fsw = WatchUtil . watchFileForContextChange ( kubeConfigFolder , 'config' ) ;
100
+ const kubeconfigFiles = getKubeConfigFiles ( ) ;
101
+ this . kubeConfigWatchers = kubeconfigFiles . map ( kubeconfigFile => WatchUtil . watchFileForContextChange ( path . dirname ( kubeconfigFile ) , path . basename ( kubeconfigFile ) ) ) ;
74
102
} catch ( err ) {
75
103
void window . showWarningMessage ( 'Couldn\'t install watcher for Kubernetes configuration file. OpenShift Application Explorer view won\'t be updated automatically.' ) ;
76
104
}
77
- this . fsw ?. emitter ?. on ( 'file-changed' , ( ) => {
78
- const ku2 = new KubeConfigUtils ( ) ;
79
- const newCtx = ku2 . getContextObject ( ku2 . currentContext ) ;
80
- if ( ! this . kubeContext
81
- || ( this . kubeContext . cluster !== newCtx . cluster
82
- || this . kubeContext . user !== newCtx . user
83
- || this . kubeContext . namespace !== newCtx . namespace ) ) {
84
- this . refresh ( ) ;
85
- }
86
- this . kubeContext = newCtx ;
87
- this . kubeConfig = ku2 ;
88
- } ) ;
105
+ for ( const fsw of this . kubeConfigWatchers ) {
106
+ fsw . emitter ?. on ( 'file-changed' , ( ) => {
107
+ const ku2 = new KubeConfigUtils ( ) ;
108
+ const newCtx = ku2 . getContextObject ( ku2 . currentContext ) ;
109
+ if ( Boolean ( this . kubeContext ) !== Boolean ( newCtx )
110
+ || ( this . kubeContext . cluster !== newCtx . cluster
111
+ || this . kubeContext . user !== newCtx . user
112
+ || this . kubeContext . namespace !== newCtx . namespace ) ) {
113
+ this . refresh ( ) ;
114
+ }
115
+ this . kubeContext = newCtx ;
116
+ this . kubeConfig = ku2 ;
117
+ } ) ;
118
+ }
89
119
this . treeView = window . createTreeView < ExplorerItem > ( 'openshiftProjectExplorer' , {
90
120
treeDataProvider : this ,
91
121
} ) ;
@@ -246,7 +276,9 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
246
276
}
247
277
248
278
dispose ( ) : void {
249
- this . fsw ?. watcher ?. close ( ) ;
279
+ for ( const fsw of this . kubeConfigWatchers ) {
280
+ fsw ?. watcher ?. close ( ) ;
281
+ }
250
282
this . treeView . dispose ( ) ;
251
283
}
252
284
0 commit comments