From 3d4eccf3a7129b2db488402106ac7488f9997b3f Mon Sep 17 00:00:00 2001 From: Satish Matti Date: Thu, 15 Jun 2023 16:53:41 -0700 Subject: [PATCH] Use KUBECONFIG env for kuberenetes client if specified Change-Id: I0ce80ee726d5e5864752a81a6bad946b89d5a6ec --- node/pkg/cni/token_watch.go | 2 +- node/pkg/lifecycle/shutdown/shutdown.go | 4 ++-- node/pkg/lifecycle/startup/startup.go | 5 +++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/node/pkg/cni/token_watch.go b/node/pkg/cni/token_watch.go index 91915780b64..ad60157480c 100644 --- a/node/pkg/cni/token_watch.go +++ b/node/pkg/cni/token_watch.go @@ -222,7 +222,7 @@ func Run() { for tu := range tokenChan { logrus.Info("Update of CNI kubeconfig triggered based on elapsed time.") - cfg, err := rest.InClusterConfig() + cfg, err := clientcmd.BuildConfigFromFlags("", os.Getenv("KUBECONFIG")) if err != nil { logrus.WithError(err).Error("Error generating kube config.") continue diff --git a/node/pkg/lifecycle/shutdown/shutdown.go b/node/pkg/lifecycle/shutdown/shutdown.go index 9b80525a70e..21a3fc06cd7 100644 --- a/node/pkg/lifecycle/shutdown/shutdown.go +++ b/node/pkg/lifecycle/shutdown/shutdown.go @@ -19,7 +19,7 @@ import ( log "github.com/sirupsen/logrus" "k8s.io/client-go/kubernetes" - "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" "github.com/projectcalico/calico/node/pkg/lifecycle/utils" ) @@ -43,7 +43,7 @@ func Run() { var clientset *kubernetes.Clientset // If running under kubernetes with secrets to call k8s API - if config, err := rest.InClusterConfig(); err == nil { + if config, err := clientcmd.BuildConfigFromFlags("", os.Getenv("KUBECONFIG")); err == nil { // default timeout is 30 seconds, which isn't appropriate for this kind of // shutdown action because network services, like kube-proxy might not be // running and we don't want to block the full 30 seconds if they are just diff --git a/node/pkg/lifecycle/startup/startup.go b/node/pkg/lifecycle/startup/startup.go index 7d7e1c91784..45b4cb79acb 100644 --- a/node/pkg/lifecycle/startup/startup.go +++ b/node/pkg/lifecycle/startup/startup.go @@ -31,6 +31,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" api "github.com/projectcalico/api/pkg/apis/projectcalico/v3" "github.com/projectcalico/api/pkg/lib/numorstring" @@ -129,7 +130,7 @@ func Run() { } // If running under kubernetes with secrets to call k8s API - if config, err := rest.InClusterConfig(); err == nil { + if config, err := clientcmd.BuildConfigFromFlags("", os.Getenv("KUBECONFIG")); err == nil { // default timeout is 30 seconds, which isn't appropriate for this kind of // startup action because network services, like kube-proxy might not be // running and we don't want to block the full 30 seconds if they are just @@ -329,7 +330,7 @@ func MonitorIPAddressSubnets() { if nodeRef := os.Getenv("CALICO_K8S_NODE_REF"); nodeRef != "" { k8sNodeName = nodeRef } - if config, err = rest.InClusterConfig(); err == nil { + if config, err = clientcmd.BuildConfigFromFlags("", os.Getenv("KUBECONFIG")); err == nil { // Create the k8s clientset. clientset, err = kubernetes.NewForConfig(config) if err != nil {