Skip to content

Commit

Permalink
Feat: support fallback to kubeconfig namespace when env not set (#5182)
Browse files Browse the repository at this point in the history
Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com>

Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com>
  • Loading branch information
wonderflow committed Dec 13, 2022
1 parent b6f4328 commit ac9cf58
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 11 deletions.
3 changes: 2 additions & 1 deletion pkg/oam/util/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"github.com/oam-dev/kubevela/apis/core.oam.dev/condition"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
types2 "github.com/oam-dev/kubevela/apis/types"
"github.com/oam-dev/kubevela/pkg/oam"
"github.com/oam-dev/kubevela/pkg/oam/discoverymapper"
)
Expand Down Expand Up @@ -297,7 +298,7 @@ func GetDefinitionNamespaceWithCtx(ctx context.Context) string {
func SetNamespaceInCtx(ctx context.Context, namespace string) context.Context {
if namespace == "" {
// compatible with some webhook handlers that maybe receive empty string as app namespace which means `default` namespace
namespace = "default"
namespace = types2.DefaultAppNamespace
}
ctx = context.WithValue(ctx, AppDefinitionNamespace, namespace)
return ctx
Expand Down
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
2 changes: 1 addition & 1 deletion references/cli/dryrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ You can also specify a remote url for app:
}

// Set the namespace to default to match behavior of `GetFlagNamespaceOrEnv`
namespace = "default"
namespace = types.DefaultAppNamespace
}

buff, err := DryRunApplication(o, c, namespace)
Expand Down
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 ac9cf58

Please sign in to comment.