Skip to content

Commit

Permalink
Merge pull request #6 from flanksource/feat/support-kubeconfig
Browse files Browse the repository at this point in the history
feat: support kubeconfig directly
  • Loading branch information
moshloop authored Mar 12, 2024
2 parents 579235c + 72eae5a commit 2a69009
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
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

0 comments on commit 2a69009

Please sign in to comment.