1
1
package client
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
- "github.com/robinovitch61/kl/internal/dev"
6
6
"github.com/robinovitch61/kl/internal/model"
7
+ "os"
8
+ "strings"
9
+
10
+ "github.com/robinovitch61/kl/internal/dev"
7
11
"k8s.io/client-go/kubernetes"
8
12
"k8s.io/client-go/tools/clientcmd"
9
13
"k8s.io/client-go/tools/clientcmd/api"
10
- "os"
11
- "strings"
12
14
)
13
15
14
- // getKubeConfig gets kubeconfig, accounting for multiple file paths
15
- func getKubeConfig (kubeConfigPath string ) (api.Config , * clientcmd.ClientConfigLoadingRules , error ) {
16
- loadingRules := clientcmd .NewDefaultClientConfigLoadingRules ()
17
- kubeconfigPaths := strings .Split (kubeConfigPath , string (os .PathListSeparator ))
18
- dev .Debug (fmt .Sprintf ("kubeconfig paths: %v" , kubeconfigPaths ))
19
-
20
- loadingRules .Precedence = kubeconfigPaths
21
- clientConfig := clientcmd .NewNonInteractiveDeferredLoadingClientConfig (loadingRules , nil )
22
- rawKubeConfig , err := clientConfig .RawConfig ()
16
+ func NewClient (
17
+ ctx context.Context ,
18
+ kubeConfigPath string ,
19
+ contexts []string ,
20
+ namespaces []string ,
21
+ useAllNamespaces bool ,
22
+ ) (Client , error ) {
23
+ rawKubeConfig , loadingRules , err := getKubeConfig (kubeConfigPath )
23
24
if err != nil {
24
- return api.Config {}, loadingRules , fmt .Errorf ("failed to load kubeconfig: %w" , err )
25
- }
26
- return rawKubeConfig , loadingRules , nil
27
- }
28
-
29
- func getContexts (contextString string , config api.Config ) ([]string , error ) {
30
- contextsString := strings .Trim (strings .TrimSpace (contextString ), "," )
31
- var contexts []string
32
- if len (contextsString ) > 0 {
33
- contexts = strings .Split (contextsString , "," )
34
- }
35
-
36
- if len (contexts ) == 0 && config .CurrentContext != "" {
37
- contexts = []string {config .CurrentContext }
25
+ return clientImpl {}, err
38
26
}
39
27
40
28
if len (contexts ) == 0 {
41
- return nil , fmt .Errorf ("no contexts specified and no current context found in kubeconfig" )
29
+ if rawKubeConfig .CurrentContext != "" {
30
+ dev .Debug (fmt .Sprintf ("no contexts specified, using current context %s" , rawKubeConfig .CurrentContext ))
31
+ contexts = []string {rawKubeConfig .CurrentContext }
32
+ } else {
33
+ return nil , fmt .Errorf ("no contexts specified and no current context found in kubeconfig" )
34
+ }
42
35
}
43
-
44
36
for _ , c := range contexts {
45
- if _ , exists := config .Contexts [c ]; ! exists {
37
+ if _ , exists := rawKubeConfig .Contexts [c ]; ! exists {
46
38
return nil , fmt .Errorf ("context %s not found in kubeconfig" , c )
47
39
}
48
40
}
41
+ dev .Debug (fmt .Sprintf ("using contexts %v" , contexts ))
49
42
50
- return contexts , nil
51
- }
52
-
53
- func getClustersFromContexts (contexts []string , rawKubeConfig api.Config ) []string {
54
- var clusters []string
55
- for _ , contextName := range contexts {
56
- clusterName := rawKubeConfig .Contexts [contextName ].Cluster
57
- clusters = append (clusters , clusterName )
43
+ clusters := make ([]string , len (contexts ))
44
+ for i := range contexts {
45
+ clusters [i ] = rawKubeConfig.Contexts [contexts [i ]].Cluster
58
46
}
59
- return clusters
60
- }
61
47
62
- func validateUniqueClusters (contexts []string , clusters []string , rawKubeConfig api.Config ) (map [string ]string , error ) {
63
48
clusterToContext := make (map [string ]string )
64
49
for _ , contextName := range contexts {
65
50
clusterName := rawKubeConfig .Contexts [contextName ].Cluster
@@ -68,15 +53,6 @@ func validateUniqueClusters(contexts []string, clusters []string, rawKubeConfig
68
53
}
69
54
clusterToContext [clusterName ] = contextName
70
55
}
71
- return clusterToContext , nil
72
- }
73
-
74
- func buildClusterNamespaces (allNamespaces string , useAllNamespaces bool , clusters []string , clusterToContext map [string ]string , rawKubeConfig api.Config ) []model.ClusterNamespaces {
75
- namespacesString := strings .Trim (strings .TrimSpace (allNamespaces ), "," )
76
- var namespaces []string
77
- if len (namespacesString ) > 0 {
78
- namespaces = strings .Split (namespacesString , "," )
79
- }
80
56
81
57
var allClusterNamespaces []model.ClusterNamespaces
82
58
for _ , cluster := range clusters {
@@ -96,7 +72,37 @@ func buildClusterNamespaces(allNamespaces string, useAllNamespaces bool, cluster
96
72
allClusterNamespaces = append (allClusterNamespaces , cn )
97
73
}
98
74
}
99
- return allClusterNamespaces
75
+ for _ , cn := range allClusterNamespaces {
76
+ for _ , namespace := range cn .Namespaces {
77
+ dev .Debug (fmt .Sprintf ("using cluster '%s' namespace '%s'" , cn .Cluster , namespace ))
78
+ }
79
+ }
80
+
81
+ clusterToClientSet , err := createClientSets (clusters , clusterToContext , loadingRules )
82
+ if err != nil {
83
+ return clientImpl {}, err
84
+ }
85
+
86
+ return clientImpl {
87
+ ctx : ctx ,
88
+ clusterToClientset : clusterToClientSet ,
89
+ allClusterNamespaces : allClusterNamespaces ,
90
+ }, nil
91
+ }
92
+
93
+ // getKubeConfig gets kubeconfig, accounting for multiple file paths
94
+ func getKubeConfig (kubeConfigPath string ) (api.Config , * clientcmd.ClientConfigLoadingRules , error ) {
95
+ loadingRules := clientcmd .NewDefaultClientConfigLoadingRules ()
96
+ kubeconfigPaths := strings .Split (kubeConfigPath , string (os .PathListSeparator ))
97
+ dev .Debug (fmt .Sprintf ("kubeconfig paths: %v" , kubeconfigPaths ))
98
+
99
+ loadingRules .Precedence = kubeconfigPaths
100
+ clientConfig := clientcmd .NewNonInteractiveDeferredLoadingClientConfig (loadingRules , nil )
101
+ rawKubeConfig , err := clientConfig .RawConfig ()
102
+ if err != nil {
103
+ return api.Config {}, loadingRules , fmt .Errorf ("failed to load kubeconfig: %w" , err )
104
+ }
105
+ return rawKubeConfig , loadingRules , nil
100
106
}
101
107
102
108
func createClientSets (clusters []string , clusterToContext map [string ]string , loadingRules * clientcmd.ClientConfigLoadingRules ) (map [string ]* kubernetes.Clientset , error ) {
0 commit comments