Skip to content

Commit

Permalink
Fix restconfig retrieval - don't (#397)
Browse files Browse the repository at this point in the history
Signed-off-by: John Collier <[email protected]>
  • Loading branch information
johnmcollier authored Oct 5, 2023
1 parent 99b9e45 commit 7441eb6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
15 changes: 14 additions & 1 deletion cdq-analysis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"strconv"

"go.uber.org/zap/zapcore"
Expand All @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 14 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"strconv"

spiapi "github.com/redhat-appstudio/service-provider-integration-operator/api/v1beta1"
Expand All @@ -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"
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 7441eb6

Please sign in to comment.