@@ -46,16 +46,22 @@ type Options struct {
46
46
// Default is one second.
47
47
UpdateInterval time.Duration
48
48
49
- // Paths can be either paths to kubeconfig files or directories.
50
- // If a directory is specified, the provider will look for files
51
- // matching the KubeconfigGlobs.
52
- // The provider will collect all kubeconfig files from all paths.
53
- // The default is:
54
- // - KUBECONFIG if it is set
55
- // - ~/.kube/config if it exists
56
- // - the current directory otherwise.
57
- // Default is the current directory.
58
- Paths []string
49
+ // KubeconfigFiles are paths to kubeconfig files.
50
+ // The default depends on the KubeconfigDirs variable.
51
+ KubeconfigFiles []string
52
+
53
+ // KubeconfigDirs are directories to search for kubeconfig files matching the
54
+ // globs specified in KubeconfigGlobs.
55
+ //
56
+ // If either one or both of KubeconfigFiles or KubeconfigDirs are
57
+ // set both are used as input for the provider.
58
+ // If both are empty defaults are in this order:
59
+ // 1. If the KUBECONFIG environment variable is set it is set in
60
+ // KubeconfigFiles.
61
+ // 2. If ~/.kube/config exists it is set in KubeconfigFiles.
62
+ // 3. The working directory of the provider process is set in
63
+ // KubeconfigDirs.
64
+ KubeconfigDirs []string
59
65
60
66
// KubeconfigGlobs are the glob patterns to match kubeconfig files
61
67
// in directories.
@@ -73,16 +79,23 @@ var DefaultKubeconfigGlobs = []string{
73
79
"*.kubeconfig.yml" ,
74
80
}
75
81
76
- func defaultKubeconfigPaths () []string {
82
+ func ( p * Provider ) defaultKubeconfigPaths () ( []string , [] string ) {
77
83
if envKubeconfig := os .Getenv ("KUBECONFIG" ); envKubeconfig != "" {
78
- return []string {envKubeconfig }
84
+ return []string {envKubeconfig }, [] string {}
79
85
}
80
86
81
- if fstat , err := os .Stat (os .ExpandEnv ("$HOME/.kube/config" )); err == nil && ! fstat .IsDir () {
82
- return []string {os .ExpandEnv ("$HOME/.kube/config" )}
87
+ defaultKubeconfig := os .ExpandEnv ("$HOME/.kube/config" )
88
+ if _ , err := os .Stat (defaultKubeconfig ); err == nil {
89
+ return []string {defaultKubeconfig }, []string {}
83
90
}
84
91
85
- return []string {"." }
92
+ pwd , err := os .Getwd ()
93
+ if err != nil {
94
+ p .log .Error (err , "error getting working directory, defaulting to '.'" )
95
+ pwd = "."
96
+ }
97
+
98
+ return []string {}, []string {pwd }
86
99
}
87
100
88
101
// New returns a new Provider with the given options.
@@ -94,8 +107,8 @@ func New(opts Options) (*Provider, error) {
94
107
p .opts .UpdateInterval = 1 * time .Second
95
108
}
96
109
97
- if len (p .opts .Paths ) == 0 {
98
- p .opts .Paths = defaultKubeconfigPaths ()
110
+ if len (p .opts .KubeconfigFiles ) == 0 && len ( p . opts . KubeconfigDirs ) == 0 {
111
+ p .opts .KubeconfigFiles , p . opts . KubeconfigDirs = p . defaultKubeconfigPaths ()
99
112
}
100
113
101
114
if len (p .opts .KubeconfigGlobs ) == 0 {
@@ -106,6 +119,8 @@ func New(opts Options) (*Provider, error) {
106
119
p .clusters = make (map [string ]cluster.Cluster )
107
120
p .clusterCancel = make (map [string ]func ())
108
121
122
+ fmt .Printf ("file cluster provider initialized %q %q\n " , p .opts .KubeconfigFiles , p .opts .KubeconfigDirs )
123
+
109
124
return p , nil
110
125
}
111
126
0 commit comments