Skip to content

Commit

Permalink
controlruntime.GetConfig instead of just inClusterConfig to also cove…
Browse files Browse the repository at this point in the history
…r unit tests correctly
  • Loading branch information
shalberd committed Aug 2, 2024
1 parent 9339d68 commit bc06562
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions components/odh-notebook-controller/controllers/notebook_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"k8s.io/client-go/rest"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlruntime "sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

Expand Down Expand Up @@ -456,19 +457,23 @@ func InjectCertConfig(notebook *nbv1.Notebook, configMapName string) error {
}

func getControllerNamespace() (string, error) {
// Check if running inside a Kubernetes cluster
if _, err := rest.InClusterConfig(); err != nil {
return "", fmt.Errorf("not running inside a Kubernetes cluster")
}

// Try to get the namespace from the service account secret
if data, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); err == nil {
if ns := string(data); len(ns) > 0 {
return ns, nil
}
}

return "", fmt.Errorf("unable to determine the namespace")
// Check if running inside a Kubernetes cluster
// GetConfig(): If KUBECONFIG env variable is set, it is used to create
// the client, else the inClusterConfig() is used.
// Lastly if none of them are set, it uses $HOME/.kube/config to create the client.
config, err := ctrlruntime.GetConfig()

Check failure on line 464 in components/odh-notebook-controller/controllers/notebook_webhook.go

View workflow job for this annotation

GitHub Actions / govulncheck (components/odh-notebook-controller)

config declared and not used
if err != nil {
return nil, fmt.Errorf("error creating the config object, not running inside a Kubernetes/Openshift Cluster %v", err)

Check failure on line 466 in components/odh-notebook-controller/controllers/notebook_webhook.go

View workflow job for this annotation

GitHub Actions / govulncheck (components/odh-notebook-controller)

cannot use nil as string value in return statement
}

// Try to get the namespace from the service account secret
if data, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); err == nil {
if ns := string(data); len(ns) > 0 {
return ns, nil
}
}

return "", fmt.Errorf("unable to determine the namespace")
}

// SetContainerImageFromRegistry checks if there is an internal registry and takes the corresponding actions to set the container.image value.
Expand Down

0 comments on commit bc06562

Please sign in to comment.