@@ -32,22 +32,20 @@ var (
32
32
)
33
33
34
34
func init () {
35
- // Check if flag is already set so that the library may be double vendored without crashing the program
36
- if f := flag .Lookup ("kubeconfig" ); f == nil {
37
- flag .StringVar (& kubeconfig , "kubeconfig" , "" ,
38
- "Path to a kubeconfig. Only required if out-of-cluster." )
39
- }
35
+ // TODO: Fix this to allow double vendoring this library but still register flags on behalf of users
36
+ flag .StringVar (& kubeconfig , "kubeconfig" , "" ,
37
+ "Path to a kubeconfig. Only required if out-of-cluster." )
40
38
41
- // Check if flag is already set so that the library may be double vendored without crashing the program
42
- if f := flag .Lookup ("master" ); f == nil {
43
- flag .StringVar (& masterURL , "master" , "" ,
44
- "The address of the Kubernetes API server. Overrides any value in kubeconfig. " +
45
- "Only required if out-of-cluster." )
46
- }
39
+ flag .StringVar (& masterURL , "master" , "" ,
40
+ "The address of the Kubernetes API server. Overrides any value in kubeconfig. " +
41
+ "Only required if out-of-cluster." )
47
42
}
48
43
49
- // GetConfig uses the kubeconfig file at kubeconfig to create a rest.Config for talking to a Kubernetes
50
- // apiserver. If kubeconfig is empty it will look for kubeconfig in the default locations.
44
+ // GetConfig creates a *rest.Config for talking to a Kubernetes apiserver.
45
+ // If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running
46
+ // in cluster and use the cluster provided kubeconfig.
47
+ //
48
+ // Will log.Fatal if KubernetesInformers cannot be created
51
49
func GetConfig () (* rest.Config , error ) {
52
50
if len (kubeconfig ) > 0 {
53
51
return clientcmd .BuildConfigFromFlags (masterURL , kubeconfig )
@@ -56,8 +54,9 @@ func GetConfig() (*rest.Config, error) {
56
54
}
57
55
}
58
56
59
- // GetConfigOrDie uses the kubeconfig file at kubeconfig to create a rest.Config for talking to a Kubernetes
60
- // apiserver. If kubeconfig is empty it will look for kubeconfig in the default locations.
57
+ // GetConfig creates a *rest.Config for talking to a Kubernetes apiserver.
58
+ // If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running
59
+ // in cluster and use the cluster provided kubeconfig.
61
60
func GetConfigOrDie () * rest.Config {
62
61
config , err := GetConfig ()
63
62
if err != nil {
@@ -66,9 +65,54 @@ func GetConfigOrDie() *rest.Config {
66
65
return config
67
66
}
68
67
69
- // GetKubernetesInformersOrDie uses the kubeconfig file at kubeconfig to create a informers.SharedInformerFactory
70
- // for talking to a Kubernetes apiserver. If kubeconfig is empty it will look for kubeconfig in the
71
- // default locations.
68
+ // GetKubernetesClientSet creates a *kubernetes.ClientSet for talking to a Kubernetes apiserver.
69
+ // If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running
70
+ // in cluster and use the cluster provided kubeconfig.
71
+ func GetKubernetesClientSet () (* kubernetes.Clientset , error ) {
72
+ config , err := GetConfig ()
73
+ if err != nil {
74
+ return nil , err
75
+ }
76
+ return kubernetes .NewForConfig (config )
77
+ }
78
+
79
+ // GetKubernetesClientSetOrDie creates a *kubernetes.ClientSet for talking to a Kubernetes apiserver.
80
+ // If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running
81
+ // in cluster and use the cluster provided kubeconfig.
82
+ //
83
+ // Will log.Fatal if KubernetesInformers cannot be created
84
+ func GetKubernetesClientSetOrDie () (* kubernetes.Clientset , error ) {
85
+ cs , err := GetKubernetesClientSet ()
86
+ if err != nil {
87
+ log .Fatalf ("%v" , err )
88
+ }
89
+ return cs , nil
90
+ }
91
+
92
+ // GetKubernetesInformers creates a informers.SharedInformerFactory for talking to a Kubernetes apiserver.
93
+ // If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running
94
+ // in cluster and use the cluster provided kubeconfig.
95
+ func GetKubernetesInformers () (informers.SharedInformerFactory , error ) {
96
+ config , err := GetConfig ()
97
+ if err != nil {
98
+ return nil , err
99
+ }
100
+ i , err := kubernetes .NewForConfig (config )
101
+ if err != nil {
102
+ return nil , err
103
+ }
104
+ return informers .NewSharedInformerFactory (i , time .Minute * 5 ), nil
105
+ }
106
+
107
+ // GetKubernetesInformers creates a informers.SharedInformerFactory for talking to a Kubernetes apiserver.
108
+ // If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running
109
+ // in cluster and use the cluster provided kubeconfig.
110
+ //
111
+ // Will log.Fatal if KubernetesInformers cannot be created
72
112
func GetKubernetesInformersOrDie () informers.SharedInformerFactory {
73
- return informers .NewSharedInformerFactory (kubernetes .NewForConfigOrDie (GetConfigOrDie ()), time .Minute * 5 )
113
+ i , err := GetKubernetesInformers ()
114
+ if err != nil {
115
+ log .Fatalf ("%v" , err )
116
+ }
117
+ return i
74
118
}
0 commit comments