Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(kubernetes): rmns #49

Merged
merged 2 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}