Skip to content

Commit

Permalink
Feat: support fallback to kubeconfig namespace when env not set
Browse files Browse the repository at this point in the history
Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com>
  • Loading branch information
wonderflow committed Dec 12, 2022
1 parent e63aa44 commit 9e2bcd4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
45 changes: 37 additions & 8 deletions pkg/utils/common/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import (
"fmt"

pkgmulticluster "github.com/kubevela/pkg/multicluster"
"k8s.io/client-go/discovery"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
"k8s.io/client-go/util/flowcontrol"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
Expand All @@ -38,12 +39,13 @@ import (

// Args is args for controller-runtime client
type Args struct {
config *rest.Config
Schema *runtime.Scheme
client client.Client
dm discoverymapper.DiscoveryMapper
pd *packages.PackageDiscover
dc *discovery.DiscoveryClient
config *rest.Config
rawConfig *api.Config
Schema *runtime.Scheme
client client.Client
dm discoverymapper.DiscoveryMapper
pd *packages.PackageDiscover
dc *discovery.DiscoveryClient
}

// SetConfig insert kubeconfig into Args
Expand Down Expand Up @@ -72,6 +74,33 @@ func (a *Args) GetConfig() (*rest.Config, error) {
return a.config, nil
}

// GetRawConfig get raw kubeconfig, if not exist, will create
func (a *Args) GetRawConfig() (*api.Config, error) {
if a.rawConfig != nil {
return a.rawConfig, nil
}
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
raw, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
loadingRules, nil).RawConfig()
if err != nil {
return nil, err
}
return &raw, nil
}

// GetNamespaceFromConfig will get namespace from kube config
func (a *Args) GetNamespaceFromConfig() string {
conf, err := a.GetRawConfig()
if err != nil || conf == nil || conf.Contexts == nil {
return ""
}
ctx, ok := conf.Contexts[conf.CurrentContext]
if !ok {
return ""
}
return ctx.Namespace
}

// SetClient set custom client
func (a *Args) SetClient(c client.Client) {
a.client = c
Expand Down
7 changes: 7 additions & 0 deletions pkg/utils/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,3 +589,10 @@ func TestHTTPGetKubernetesObjects(t *testing.T) {
assert.Equal(t, "busybox", uns[1].GetName())
assert.Equal(t, "ConfigMap", uns[1].GetKind())
}

func TestGetRawConfig(t *testing.T) {
assert.NoError(t, os.Setenv("KUBECONFIG", filepath.Join("testdata", "testkube.conf")))
ag := Args{}
ns := ag.GetNamespaceFromConfig()
assert.Equal(t, "prod", ns)
}
20 changes: 20 additions & 0 deletions pkg/utils/common/testdata/testkube.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: v1
clusters:
- cluster:
server: https://127.0.0.1:6443
certificate-authority-data: YWJjMQ==
name: kubernetes
contexts:
- context:
cluster: kubernetes
user: "kubernetes-admin"
namespace: "prod"
name: kubernetes-admin
current-context: kubernetes-admin
kind: Config
preferences: {}
users:
- name: "kubernetes-admin"
user:
client-certificate-data: S1N6
client-key-data: S1N6
6 changes: 5 additions & 1 deletion references/cli/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,11 @@ func GetFlagEnvOrCurrent(cmd *cobra.Command, args common.Args) (*types.EnvMeta,
if err != nil {
// ignore this error and return a default value
// nolint:nilerr
return &types.EnvMeta{Name: "", Namespace: "default"}, nil
ns := args.GetNamespaceFromConfig()
if ns == "" {
ns = types.DefaultAppNamespace
}
return &types.EnvMeta{Name: "", Namespace: ns}, nil
}
return cur, nil
}

0 comments on commit 9e2bcd4

Please sign in to comment.