Skip to content

Commit f68706f

Browse files
author
Phillip Wittrock
authored
Merge pull request kubernetes-sigs#20 from pwittrock/fixes
Address comments from out-of-band review
2 parents 4b02bf2 + b66daf0 commit f68706f

File tree

2 files changed

+66
-22
lines changed

2 files changed

+66
-22
lines changed

pkg/config/config.go

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,20 @@ var (
3232
)
3333

3434
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.")
4038

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.")
4742
}
4843

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
5149
func GetConfig() (*rest.Config, error) {
5250
if len(kubeconfig) > 0 {
5351
return clientcmd.BuildConfigFromFlags(masterURL, kubeconfig)
@@ -56,8 +54,9 @@ func GetConfig() (*rest.Config, error) {
5654
}
5755
}
5856

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.
6160
func GetConfigOrDie() *rest.Config {
6261
config, err := GetConfig()
6362
if err != nil {
@@ -66,9 +65,54 @@ func GetConfigOrDie() *rest.Config {
6665
return config
6766
}
6867

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
72112
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
74118
}

pkg/controller/example_controller_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func ExampleGenericController() {
4141
log.Fatalf("Could not set informer %v", err)
4242
}
4343

44-
// Step 3.1: Create a new Pod controller to reconcile Pods changes
44+
// Step 2.1: Create a new Pod controller to reconcile Pods changes
4545
podController := &controller.GenericController{
4646
Reconcile: func(key types.ReconcileKey) error {
4747
fmt.Printf("Reconciling Pod %v\n", key)
@@ -53,7 +53,7 @@ func ExampleGenericController() {
5353
}
5454
controller.AddController(podController)
5555

56-
// Step 3.2: Create a new ReplicaSet controller to reconcile ReplicaSet changes
56+
// Step 2.2: Create a new ReplicaSet controller to reconcile ReplicaSet changes
5757
rsController := &controller.GenericController{
5858
Reconcile: func(key types.ReconcileKey) error {
5959
fmt.Printf("Reconciling ReplicaSet %v\n", key)
@@ -72,6 +72,6 @@ func ExampleGenericController() {
7272
}
7373
controller.AddController(rsController)
7474

75-
// Step 4: RunInformersAndControllers all informers and controllers
75+
// Step 3: RunInformersAndControllers all informers and controllers
7676
controller.RunInformersAndControllers(run.CreateRunArguments())
7777
}

0 commit comments

Comments
 (0)