diff --git a/cdq-analysis/main.go b/cdq-analysis/main.go index b80d007c2..8d86d2e1c 100644 --- a/cdq-analysis/main.go +++ b/cdq-analysis/main.go @@ -19,6 +19,7 @@ import ( "fmt" "log" "os" + "path/filepath" "strconv" "go.uber.org/zap/zapcore" @@ -28,6 +29,7 @@ import ( "github.com/redhat-appstudio/application-service/cdq-analysis/pkg" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" ) func main() { @@ -66,7 +68,18 @@ func main() { ctx = context.Background() config, err := rest.InClusterConfig() if err != nil { - fmt.Printf("Error creating InClusterConfig: %v", err) + // Couldn't find an InClusterConfig, may be running outside of Kube, so try to find a local kube config file + var kubeconfig string + if os.Getenv("KUBECONFIG") != "" { + kubeconfig = os.Getenv("KUBECONFIG") + } else { + kubeconfig = filepath.Join(os.Getenv("HOME"), ".kube", "config") + } + config, err = clientcmd.BuildConfigFromFlags("", kubeconfig) + if err != nil { + fmt.Printf("Error creating clientset with config %v: %v", config, err) + os.Exit(1) + } } clientset, err = kubernetes.NewForConfig(config) diff --git a/main.go b/main.go index 823d64860..1d473e8f6 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,7 @@ import ( "log" "net/http" "os" + "path/filepath" "strconv" spiapi "github.com/redhat-appstudio/service-provider-integration-operator/api/v1beta1" @@ -33,6 +34,7 @@ import ( "go.uber.org/zap/zapcore" _ "k8s.io/client-go/plugin/pkg/client/auth" "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" "github.com/redhat-appstudio/operator-toolkit/webhook" "k8s.io/apimachinery/pkg/runtime" @@ -192,8 +194,18 @@ func main() { } config, err := rest.InClusterConfig() if err != nil { - setupLog.Error(err, ("Error creating InClusterConfig... ")) - os.Exit(1) + // Couldn't find an InClusterConfig, may be running outside of Kube, so try to find a local kube config file + var kubeconfig string + if os.Getenv("KUBECONFIG") != "" { + kubeconfig = os.Getenv("KUBECONFIG") + } else { + kubeconfig = filepath.Join(os.Getenv("HOME"), ".kube", "config") + } + config, err = clientcmd.BuildConfigFromFlags("", kubeconfig) + if err != nil { + setupLog.Error(err, "Unable to retrieve Kubernetes InClusterConfig") + os.Exit(1) + } } if err = (&controllers.ComponentDetectionQueryReconciler{ Client: mgr.GetClient(),