diff --git a/pkg/server.go b/pkg/server.go index 323c83656..4af77a24c 100644 --- a/pkg/server.go +++ b/pkg/server.go @@ -19,6 +19,7 @@ import ( "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/record" "github.com/openshift/monitoring-plugin/internal/managementrouter" @@ -127,18 +128,11 @@ func createHTTPServer(ctx context.Context, cfg *Config) (*http.Server, error) { var k8sconfig *rest.Config var err error - // Uncomment the following line for local development: - // k8sconfig, err = clientcmd.BuildConfigFromFlags("", os.Getenv("KUBECONFIG")) - // if err != nil { - // return nil, fmt.Errorf("cannot get kubeconfig from file: %w", err) - // } - - // Comment the following line for local development: var k8sclient *dynamic.DynamicClient if acmMode || alertManagementAPIMode { - k8sconfig, err = rest.InClusterConfig() + k8sconfig, err = loadKubeConfig() if err != nil { - return nil, fmt.Errorf("cannot get in cluster config: %w", err) + return nil, fmt.Errorf("cannot load kubernetes config: %w", err) } k8sclient, err = dynamic.NewForConfig(k8sconfig) @@ -402,6 +396,23 @@ func configHandler(cfg *Config) (http.HandlerFunc, *PluginConfig) { }), &pluginConfig } +// loadKubeConfig returns a *rest.Config by preferring KUBECONFIG (useful for +// local development and CI) and falling back to in-cluster service-account config. +func loadKubeConfig() (*rest.Config, error) { + if kubeconfig := os.Getenv("KUBECONFIG"); kubeconfig != "" { + cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig) + if err != nil { + return nil, fmt.Errorf("cannot build config from KUBECONFIG: %w", err) + } + return cfg, nil + } + cfg, err := rest.InClusterConfig() + if err != nil { + return nil, fmt.Errorf("cannot get in-cluster config: %w", err) + } + return cfg, nil +} + func startProxy(cfg *Config, k8sclient *dynamic.DynamicClient, tlsConfig *tls.Config, timeout time.Duration, kind proxy.KindType, port proxy.ProxyPort) { proxyRouter := setupProxyRoutes(cfg, k8sclient, kind) proxyRouter.Use(corsHeaderMiddleware())