Skip to content

Commit

Permalink
Merge pull request #49 from chaitin/feat(kubernetes)/rmns
Browse files Browse the repository at this point in the history
feat(kubernetes): rmns
  • Loading branch information
zhoubinxuan authored Nov 16, 2022
2 parents ed771fa + 6b0feb5 commit deb9856
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 45 deletions.
8 changes: 0 additions & 8 deletions go/cmd/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ func (r kubernetesRoot) Options() plugin.ExecOption {
"--kube-config-path", r.k.ConfigPath()),
plugin.WithPrependArgs("--kube-config-bytes",
base64.StdEncoding.EncodeToString(r.k.ConfigBytes())),
plugin.WithPrependArgs(
"--namespace", r.k.CurrentNamespace()),
plugin.WithPrependArgs(
"--in-cluster", func() string {
if r.k.InCluster() {
Expand Down Expand Up @@ -67,12 +65,6 @@ func (kubernetesMode) AddFlags(fset *pflag.FlagSet) {
return nil
}, "kube-config-bytes",
`flag "--kube-config-bytes" specified kube config bytes`)
pflagext.StringVarF(fset, func(namespace string) error {
kubernetesFlags = append(kubernetesFlags,
kubernetes.WithNamespace(namespace))
return nil
}, "namespace",
`flag "--namespace" specified namespace`)
pflagext.StringVarF(fset, func(inCluster string) error {
if strings.ToLower(inCluster) == "true" {
kubernetesFlags = append(kubernetesFlags,
Expand Down
34 changes: 4 additions & 30 deletions go/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import (
)

type Kubernetes struct {
// kubernetes cluster namespace
namespace string

// kubeConfigPath path for cluster
kubeConfigPath string

Expand All @@ -42,13 +39,6 @@ type Kubernetes struct {

type NewOption func(kubernetes *Kubernetes) error

func WithNamespace(namespace string) NewOption {
return func(kubernetes *Kubernetes) error {
kubernetes.namespace = namespace
return nil
}
}

func WithKubeConfigPath(path string) NewOption {
return func(kubernetes *Kubernetes) error {
kubernetes.kubeConfigPath = path
Expand Down Expand Up @@ -85,11 +75,6 @@ func New(options ...NewOption) (*Kubernetes, error) {
err error
)

// init namespace
if k.namespace == "" {
k.namespace = "default"
}

// init rest config
if k.inCluster {
restConfig, err = rest.InClusterConfig()
Expand All @@ -98,9 +83,7 @@ func New(options ...NewOption) (*Kubernetes, error) {
}
} else {
if k.kubeConfigPath == "" {
if os.Getenv("KUBECONFIG") == "" {
return nil, errors.New("kubernetes: can't find kube config path")
} else {
if os.Getenv("KUBECONFIG") != "" {
k.kubeConfigPath = os.Getenv("KUBECONFIG")
}
}
Expand Down Expand Up @@ -147,18 +130,14 @@ func New(options ...NewOption) (*Kubernetes, error) {
}

func (k *Kubernetes) ListNamespaces() ([]string, error) {
namespaceResource, err := k.Resource(Namespaces.String())
namespaceResource, err := k.Resource("", Namespaces.String())
if err != nil {
return nil, err
}

return namespaceResource.List(context.Background())
}

func (k *Kubernetes) CurrentNamespace() string {
return k.namespace
}

func (k *Kubernetes) ConfigPath() string {
return k.kubeConfigPath
}
Expand All @@ -171,12 +150,7 @@ func (k *Kubernetes) InCluster() bool {
return k.inCluster
}

func (k *Kubernetes) Namespace(namespace string) api.Cluster {
k.namespace = namespace
return k
}

func (k *Kubernetes) Resource(kind string) (api.ClusterResource, error) {
func (k *Kubernetes) Resource(namespace string, kind string) (api.ClusterResource, error) {
gvr, err := k.restMapper.ResourceFor(schema.GroupVersionResource{Resource: kind})
if err != nil {
return nil, err
Expand All @@ -186,7 +160,7 @@ func (k *Kubernetes) Resource(kind string) (api.ClusterResource, error) {
if IsClusterKind(kind) {
return Resource{kind, k.dynamicClient.Resource(gvr)}, nil
} else if IsNamespaceKind(kind) {
return Resource{kind, k.dynamicClient.Resource(gvr).Namespace(k.namespace)}, nil
return Resource{kind, k.dynamicClient.Resource(gvr).Namespace(namespace)}, nil
} else {
return nil, errors.New("kubernetes: not support resource kind for cluster")
}
Expand Down
8 changes: 1 addition & 7 deletions go/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,10 @@ type Cluster interface {
// ListNamespaces attempt to list all namespaces in cluster
ListNamespaces() ([]string, error)

// CurrentNamespace return current namespace of cluster
CurrentNamespace() string

// InCluster return kubernetes client whether in cluster
InCluster() bool

// Namespace attempt to switch namespace
Namespace(namespace string) Cluster

// Resource attempt to open ClusterResource
// accord schema.GroupVersionResource
Resource(kind string) (ClusterResource, error)
Resource(namespace string, kind string) (ClusterResource, error)
}

0 comments on commit deb9856

Please sign in to comment.