Skip to content

Commit

Permalink
Init kube client from in cluster config
Browse files Browse the repository at this point in the history
Signed-off-by: Prasad Ghangal <[email protected]>
  • Loading branch information
PrasadG193 committed Dec 19, 2024
1 parent 3048fed commit 3c052b3
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion examples/snapshot-metadata-lister/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os/signal"
"path/filepath"

"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"

Expand Down Expand Up @@ -132,7 +133,7 @@ func main() {
parseFlags()

// get the K8s config from either kubeConfig, in-cluster or default
config, err := clientcmd.BuildConfigFromFlags("", kubeConfig)
config, err := buildConfig(kubeConfig)
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading kubeconfig %s: %v\n", kubeConfig, err)
os.Exit(1)
Expand All @@ -156,3 +157,12 @@ func main() {

os.Exit(0)
}

func buildConfig(kubeconfigPath string) (*rest.Config, error) {
// If kubeconfig exists, try from kubeconfig file
if _, err := os.Stat(kubeconfigPath); err == nil {
return clientcmd.BuildConfigFromFlags("", kubeconfigPath)
}
// try in-cluster config
return rest.InClusterConfig()
}

0 comments on commit 3c052b3

Please sign in to comment.