Skip to content

Commit

Permalink
Merge pull request #20 from pwittrock/fixes
Browse files Browse the repository at this point in the history
Address comments from out-of-band review
  • Loading branch information
Phillip Wittrock authored Mar 21, 2018
2 parents 4b02bf2 + b66daf0 commit f68706f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 22 deletions.
82 changes: 63 additions & 19 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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
}
6 changes: 3 additions & 3 deletions pkg/controller/example_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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())
}

0 comments on commit f68706f

Please sign in to comment.