diff --git a/pkg/config/config.go b/pkg/config/config.go index 82497a21caf..ab7bedd8b67 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -32,22 +32,20 @@ var ( ) func init() { - // Check if flag is already set so that the library may be double vendored without crashing the program - if f := flag.Lookup("kubeconfig"); f == nil { - flag.StringVar(&kubeconfig, "kubeconfig", "", - "Path to a kubeconfig. Only required if out-of-cluster.") - } + // TODO: Fix this to allow double vendoring this library but still register flags on behalf of users + flag.StringVar(&kubeconfig, "kubeconfig", "", + "Path to a kubeconfig. Only required if out-of-cluster.") - // Check if flag is already set so that the library may be double vendored without crashing the program - if f := flag.Lookup("master"); f == nil { - flag.StringVar(&masterURL, "master", "", - "The address of the Kubernetes API server. Overrides any value in kubeconfig. "+ - "Only required if out-of-cluster.") - } + flag.StringVar(&masterURL, "master", "", + "The address of the Kubernetes API server. Overrides any value in kubeconfig. "+ + "Only required if out-of-cluster.") } -// GetConfig uses the kubeconfig file at kubeconfig to create a rest.Config for talking to a Kubernetes -// apiserver. If kubeconfig is empty it will look for kubeconfig in the default locations. +// GetConfig creates a *rest.Config for talking to a Kubernetes apiserver. +// If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running +// in cluster and use the cluster provided kubeconfig. +// +// Will log.Fatal if KubernetesInformers cannot be created func GetConfig() (*rest.Config, error) { if len(kubeconfig) > 0 { return clientcmd.BuildConfigFromFlags(masterURL, kubeconfig) @@ -56,8 +54,9 @@ func GetConfig() (*rest.Config, error) { } } -// GetConfigOrDie uses the kubeconfig file at kubeconfig to create a rest.Config for talking to a Kubernetes -// apiserver. If kubeconfig is empty it will look for kubeconfig in the default locations. +// GetConfig creates a *rest.Config for talking to a Kubernetes apiserver. +// If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running +// in cluster and use the cluster provided kubeconfig. func GetConfigOrDie() *rest.Config { config, err := GetConfig() if err != nil { @@ -66,9 +65,54 @@ func GetConfigOrDie() *rest.Config { return config } -// GetKubernetesInformersOrDie uses the kubeconfig file at kubeconfig to create a informers.SharedInformerFactory -// for talking to a Kubernetes apiserver. If kubeconfig is empty it will look for kubeconfig in the -// default locations. +// GetKubernetesClientSet creates a *kubernetes.ClientSet for talking to a Kubernetes apiserver. +// If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running +// in cluster and use the cluster provided kubeconfig. +func GetKubernetesClientSet() (*kubernetes.Clientset, error) { + config, err := GetConfig() + if err != nil { + return nil, err + } + return kubernetes.NewForConfig(config) +} + +// GetKubernetesClientSetOrDie creates a *kubernetes.ClientSet for talking to a Kubernetes apiserver. +// If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running +// in cluster and use the cluster provided kubeconfig. +// +// Will log.Fatal if KubernetesInformers cannot be created +func GetKubernetesClientSetOrDie() (*kubernetes.Clientset, error) { + cs, err := GetKubernetesClientSet() + if err != nil { + log.Fatalf("%v", err) + } + return cs, nil +} + +// GetKubernetesInformers creates a informers.SharedInformerFactory for talking to a Kubernetes apiserver. +// If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running +// in cluster and use the cluster provided kubeconfig. +func GetKubernetesInformers() (informers.SharedInformerFactory, error) { + config, err := GetConfig() + if err != nil { + return nil, err + } + i, err := kubernetes.NewForConfig(config) + if err != nil { + return nil, err + } + return informers.NewSharedInformerFactory(i, time.Minute*5), nil +} + +// GetKubernetesInformers creates a informers.SharedInformerFactory for talking to a Kubernetes apiserver. +// If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running +// in cluster and use the cluster provided kubeconfig. +// +// Will log.Fatal if KubernetesInformers cannot be created func GetKubernetesInformersOrDie() informers.SharedInformerFactory { - return informers.NewSharedInformerFactory(kubernetes.NewForConfigOrDie(GetConfigOrDie()), time.Minute*5) + i, err := GetKubernetesInformers() + if err != nil { + log.Fatalf("%v", err) + } + return i } diff --git a/pkg/controller/example_controller_test.go b/pkg/controller/example_controller_test.go index 93cfb995f9c..a48eeb3c16a 100644 --- a/pkg/controller/example_controller_test.go +++ b/pkg/controller/example_controller_test.go @@ -41,7 +41,7 @@ func ExampleGenericController() { log.Fatalf("Could not set informer %v", err) } - // Step 3.1: Create a new Pod controller to reconcile Pods changes + // Step 2.1: Create a new Pod controller to reconcile Pods changes podController := &controller.GenericController{ Reconcile: func(key types.ReconcileKey) error { fmt.Printf("Reconciling Pod %v\n", key) @@ -53,7 +53,7 @@ func ExampleGenericController() { } controller.AddController(podController) - // Step 3.2: Create a new ReplicaSet controller to reconcile ReplicaSet changes + // Step 2.2: Create a new ReplicaSet controller to reconcile ReplicaSet changes rsController := &controller.GenericController{ Reconcile: func(key types.ReconcileKey) error { fmt.Printf("Reconciling ReplicaSet %v\n", key) @@ -72,6 +72,6 @@ func ExampleGenericController() { } controller.AddController(rsController) - // Step 4: RunInformersAndControllers all informers and controllers + // Step 3: RunInformersAndControllers all informers and controllers controller.RunInformersAndControllers(run.CreateRunArguments()) }