Feat: support fallback to kubeconfig namespace when env not set (#5182)

Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com>

Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com>
This commit is contained in:
Jianbo Sun
2022-12-13 14:53:54 +08:00
committed by GitHub
parent b6f4328167
commit ac9cf58afa
6 changed files with 72 additions and 11 deletions
+2 -1
View File
@@ -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"
)
@@ -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
+37 -8
View File
@@ -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"
@@ -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
@@ -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
+7
View File
@@ -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
View File
@@ -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
+1 -1
View File
@@ -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)
+5 -1
View File
@@ -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
}