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: support kubeconfig directly #6

Merged
merged 1 commit into from
Mar 12, 2024
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
6 changes: 3 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func GetAllServerResources(ketalloptions *options.KetallOptions) (runtime.Object
}

start := time.Now()
response, err := fetchResourcesBulk(ketalloptions.Namespace, ketalloptions.Selector, ketalloptions.FieldSelector, ketalloptions.GenericCliFlags, grs...)
response, err := fetchResourcesBulk(ketalloptions.Namespace, ketalloptions.Selector, ketalloptions.FieldSelector, ketalloptions.Flags, grs...)
klog.V(2).Infof("Initial fetchResourcesBulk done (%s)", duration.HumanDuration(time.Since(start)))
if err == nil {
return response, nil
Expand All @@ -71,7 +71,7 @@ func getExclusions(exclusions []string, selector, fieldSelector string) []string
}

func groupResources(ketalloptions *options.KetallOptions) ([]groupResource, error) {
client, err := ketalloptions.GenericCliFlags.ToDiscoveryClient()
client, err := ketalloptions.Flags.ToDiscoveryClient()
if err != nil {
return nil, errors.Wrap(err, "discovery client")
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func fetchResourcesIncremental(ctx context.Context, ketalloptions *options.Ketal
return // context cancelled
}
defer sem.Release(1)
obj, err := fetchResourcesBulk(ketalloptions.Namespace, ketalloptions.Selector, ketalloptions.FieldSelector, ketalloptions.GenericCliFlags, gr)
obj, err := fetchResourcesBulk(ketalloptions.Namespace, ketalloptions.Selector, ketalloptions.FieldSelector, ketalloptions.Flags, gr)
if err != nil {
klog.Warningf("Cannot fetch: %v", err)
return
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
k8s.io/api v0.28.0
k8s.io/apimachinery v0.28.0
k8s.io/cli-runtime v0.28.0
k8s.io/client-go v0.28.0
k8s.io/klog/v2 v2.100.1
)

Expand Down Expand Up @@ -57,7 +58,6 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/client-go v0.28.0 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
Expand Down
23 changes: 21 additions & 2 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package options

import (
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/rest"
)

type KetallOptions struct {
GenericCliFlags *genericclioptions.ConfigFlags
UseCache bool `json:"useCache,omitempty"`
AllowIncomplete bool `json:"allowIncomplete,omitempty"`
Scope string `json:"scope,omitempty"`
Expand All @@ -32,11 +32,30 @@ type KetallOptions struct {
Namespace string `json:"namespace.omitempty"`
Exclusions []string `json:"exclusions,omitempty"` // Exclude resources by name or kind or shortname
Kind string `json:"kind,omitempty"` // Limits results on a specific kind

Flags *KetAllConfigFlags
}

// KetAllConfigFlags is a wrapper around genericclioptions.ConfigFlags
// to support kubeconfig directly without needing the kubeconfig path.
type KetAllConfigFlags struct {
*genericclioptions.ConfigFlags
KubeConfig *rest.Config `json:"kubeConfig,omitempty"`
}

func (t *KetAllConfigFlags) RawConfig() (*rest.Config, error) {
if t.KubeConfig != nil {
return t.KubeConfig, nil
}

return t.ConfigFlags.ToRESTConfig()
}

func NewDefaultCmdOptions() *KetallOptions {
return &KetallOptions{
GenericCliFlags: genericclioptions.NewConfigFlags(true),
Flags: &KetAllConfigFlags{
ConfigFlags: genericclioptions.NewConfigFlags(true),
},
}
}

Expand Down
Loading