Skip to content

Commit 5340991

Browse files
committed
Replace Paths with KubeconfiFiles and Dirs
Signed-off-by: Nelo-T. Wallus <[email protected]>
1 parent 89dc416 commit 5340991

File tree

4 files changed

+67
-38
lines changed

4 files changed

+67
-38
lines changed

examples/file/main.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ import (
2929
)
3030

3131
var (
32-
fPaths = flag.String("paths", "", "Comma-separated list of file paths to process (can be files or directories), defaults to current directory")
33-
fGlobs = flag.String("globs", "", "Comma-separated list of glob patterns to match files")
34-
fContinue = flag.Bool("continue", false, "Continue processing and listing files until cancelled")
32+
fKubeconfigFiles = flag.String("kubeconfigs", "", "Comma-separated list of kubeconfig file paths to process")
33+
fKubeconfigDirs = flag.String("kubeconfig-dirs", "", "Comma-separated list of directories to search for kubeconfig files")
34+
fGlobs = flag.String("globs", "", "Comma-separated list of glob patterns to match files")
35+
fContinue = flag.Bool("continue", false, "Continue processing and listing files until cancelled")
3536
)
3637

3738
func printClusters(clusters []string) {
@@ -51,8 +52,19 @@ func main() {
5152
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
5253
defer cancel()
5354

55+
kubeconfigFiles := strings.Split(*fKubeconfigFiles, ",")
56+
if len(kubeconfigFiles) == 1 && kubeconfigFiles[0] == "" {
57+
kubeconfigFiles = []string{}
58+
}
59+
60+
kubeconfigDirs := strings.Split(*fKubeconfigDirs, ",")
61+
if len(kubeconfigDirs) == 1 && kubeconfigDirs[0] == "" {
62+
kubeconfigDirs = []string{}
63+
}
64+
5465
provider, err := file.New(file.Options{
55-
Paths: strings.Split(*fPaths, ","),
66+
KubeconfigFiles: kubeconfigFiles,
67+
KubeconfigDirs: kubeconfigDirs,
5668
KubeconfigGlobs: strings.Split(*fGlobs, ","),
5769
})
5870
if err != nil {

providers/file/clusters.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
type clusters map[string]cluster.Cluster
3131

3232
func (p *Provider) loadClusters() (clusters, error) {
33-
filepaths, err := p.collectPaths(p.opts.Paths)
33+
filepaths, err := p.collectPaths()
3434
if err != nil {
3535
return nil, err
3636
}
@@ -54,29 +54,30 @@ func (p *Provider) loadClusters() (clusters, error) {
5454
return p.fromContexts(kubeCtxs), nil
5555
}
5656

57-
func (p *Provider) collectPaths(paths []string) ([]string, error) {
58-
filepaths := make([]string, 0, len(paths))
57+
func (p *Provider) collectPaths() ([]string, error) {
58+
filepaths := []string{}
5959

60-
for _, path := range paths {
61-
if path == "" {
62-
continue
63-
}
64-
65-
stat, err := os.Stat(path)
66-
if err != nil {
60+
for _, file := range p.opts.KubeconfigFiles {
61+
if _, err := os.Stat(file); err != nil {
6762
if os.IsNotExist(err) {
68-
p.log.Info("path does not exist, skipping", "path", path)
63+
p.log.Info("kubeconfig file does not exist, skipping", "file", file)
6964
continue
7065
}
71-
return nil, fmt.Errorf("failed to stat path %q: %w", path, err)
66+
return nil, fmt.Errorf("failed to stat kubeconfig file %q: %w", file, err)
7267
}
68+
filepaths = append(filepaths, file)
69+
}
7370

74-
if !stat.IsDir() {
75-
filepaths = append(filepaths, path)
76-
continue
71+
for _, dir := range p.opts.KubeconfigDirs {
72+
if _, err := os.Stat(dir); err != nil {
73+
if os.IsNotExist(err) {
74+
p.log.Info("directory does not exist, skipping", "dir", dir)
75+
continue
76+
}
77+
return nil, fmt.Errorf("failed to stat directory %q: %w", dir, err)
7778
}
7879

79-
filepaths = append(filepaths, p.matchKubeconfigGlobs(path)...)
80+
filepaths = append(filepaths, p.matchKubeconfigGlobs(dir)...)
8081
}
8182

8283
return filepaths, nil

providers/file/provider.go

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,22 @@ type Options struct {
4646
// Default is one second.
4747
UpdateInterval time.Duration
4848

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
5965

6066
// KubeconfigGlobs are the glob patterns to match kubeconfig files
6167
// in directories.
@@ -73,16 +79,23 @@ var DefaultKubeconfigGlobs = []string{
7379
"*.kubeconfig.yml",
7480
}
7581

76-
func defaultKubeconfigPaths() []string {
82+
func (p *Provider) defaultKubeconfigPaths() ([]string, []string) {
7783
if envKubeconfig := os.Getenv("KUBECONFIG"); envKubeconfig != "" {
78-
return []string{envKubeconfig}
84+
return []string{envKubeconfig}, []string{}
7985
}
8086

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{}
8390
}
8491

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}
8699
}
87100

88101
// New returns a new Provider with the given options.
@@ -94,8 +107,8 @@ func New(opts Options) (*Provider, error) {
94107
p.opts.UpdateInterval = 1 * time.Second
95108
}
96109

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()
99112
}
100113

101114
if len(p.opts.KubeconfigGlobs) == 0 {
@@ -106,6 +119,8 @@ func New(opts Options) (*Provider, error) {
106119
p.clusters = make(map[string]cluster.Cluster)
107120
p.clusterCancel = make(map[string]func())
108121

122+
fmt.Printf("file cluster provider initialized %q %q\n", p.opts.KubeconfigFiles, p.opts.KubeconfigDirs)
123+
109124
return p, nil
110125
}
111126

providers/file/provider_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ var _ = Describe("Provider File", Ordered, func() {
5050
By("Creating a new provider", func() {
5151
var err error
5252
provider, err = New(Options{
53-
Paths: []string{discoverDir, kubeconfigPath},
53+
KubeconfigFiles: []string{kubeconfigPath},
54+
KubeconfigDirs: []string{discoverDir},
5455
})
5556
Expect(err).NotTo(HaveOccurred())
5657
})

0 commit comments

Comments
 (0)