Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix restconfig retrieval - don't get inclusterconfig when local #397

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading