From efd2c38414afe286e8be33968a1bbbb47a8cc97e Mon Sep 17 00:00:00 2001 From: subhamkrai Date: Mon, 3 Jul 2023 11:06:31 +0530 Subject: [PATCH] flag: add context flag to switch between clusters we missed adding `--context` flag when migrating to golang. Adding the flag back. Signed-off-by: subhamkrai --- .github/workflows/go-test.yaml | 10 ++++++++++ cmd/commands/root.go | 9 ++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/.github/workflows/go-test.yaml b/.github/workflows/go-test.yaml index c98c574a..7817322a 100644 --- a/.github/workflows/go-test.yaml +++ b/.github/workflows/go-test.yaml @@ -47,6 +47,11 @@ jobs: set -ex kubectl rook-ceph ceph status + - name: Ceph status using context + run: | + set -ex + kubectl rook-ceph --context=$(kubectl config current-context) ceph status + - name: Mon restore env: ROOK_PLUGIN_SKIP_PROMPTS: true @@ -148,6 +153,11 @@ jobs: set -ex kubectl rook-ceph --operator-namespace test-operator -n test-cluster ceph status + - name: Ceph status using context + run: | + set -ex + kubectl rook-ceph --operator-namespace test-operator -n test-cluster --context=$(kubectl config current-context) ceph status + - name: Mon restore env: ROOK_PLUGIN_SKIP_PROMPTS: true diff --git a/cmd/commands/root.go b/cmd/commands/root.go index 7c9a7a90..b89ff161 100644 --- a/cmd/commands/root.go +++ b/cmd/commands/root.go @@ -37,6 +37,7 @@ var ( KubeConfig string OperatorNamespace string CephClusterNamespace string + KubeContext string ) // rookCmd represents the rook command @@ -67,6 +68,7 @@ func init() { RootCmd.PersistentFlags().StringVar(&KubeConfig, "kubeconfig", "", "kubernetes config path") RootCmd.PersistentFlags().StringVar(&OperatorNamespace, "operator-namespace", "", "Kubernetes namespace where rook operator is running") RootCmd.PersistentFlags().StringVarP(&CephClusterNamespace, "namespace", "n", "rook-ceph", "Kubernetes namespace where CephCluster is created") + RootCmd.PersistentFlags().StringVar(&KubeContext, "context", "", "Kubernetes context to use") } func GetClientsets(ctx context.Context) *k8sutil.Clientsets { @@ -74,10 +76,15 @@ func GetClientsets(ctx context.Context) *k8sutil.Clientsets { clientsets := &k8sutil.Clientsets{} + congfigOverride := &clientcmd.ConfigOverrides{} + if KubeContext != "" { + congfigOverride = &clientcmd.ConfigOverrides{CurrentContext: KubeContext} + } + // 1. Create Kubernetes Client kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( clientcmd.NewDefaultClientConfigLoadingRules(), - &clientcmd.ConfigOverrides{}, + congfigOverride, ) clientsets.KubeConfig, err = kubeconfig.ClientConfig()