Skip to content

Commit

Permalink
feedback: de-duplicate loadRESTConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
maelvls committed Aug 22, 2024
1 parent c8eebb9 commit c6d12b6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 59 deletions.
32 changes: 3 additions & 29 deletions pkg/agent/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@ import (

"github.com/cenkalti/backoff"
"github.com/hashicorp/go-multierror"
"github.com/jetstack/preflight/pkg/logs"
json "github.com/json-iterator/go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/jetstack/preflight/api"
"github.com/jetstack/preflight/pkg/client"
"github.com/jetstack/preflight/pkg/datagatherer"
"github.com/jetstack/preflight/pkg/kubeconfig"
"github.com/jetstack/preflight/pkg/logs"
"github.com/jetstack/preflight/pkg/version"
)

Expand Down Expand Up @@ -342,7 +341,7 @@ func getConfiguration() (Config, client.Client) {
logs.Log.Printf(`ignoring venafi-cloud.uploader_id. In Venafi Connection mode, this field is not needed.`)
}

cfg, err := loadRESTConfig("")
cfg, err := kubeconfig.LoadRESTConfig("")
if err != nil {
logs.Log.Fatalf("failed to load kubeconfig: %v", err)
}
Expand Down Expand Up @@ -568,28 +567,3 @@ func getInClusterNamespace() (string, error) {
}
return string(namespace), nil
}

func loadRESTConfig(path string) (*rest.Config, error) {
switch path {
// If the kubeconfig path is not provided, use the default loading rules
// so we read the regular KUBECONFIG variable or create a non-interactive
// client for agents running in cluster
case "":
loadingrules := clientcmd.NewDefaultClientConfigLoadingRules()
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
loadingrules, &clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return nil, fmt.Errorf("failed to load kubeconfig: %w", err)
}
return cfg, nil
// Otherwise use the explicitly named kubeconfig file.
default:
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: path},
&clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return nil, fmt.Errorf("failed to load kubeconfig from %s: %w", path, err)
}
return cfg, nil
}
}
35 changes: 5 additions & 30 deletions pkg/datagatherer/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"

"github.com/jetstack/preflight/pkg/kubeconfig"
)

// NewDynamicClient creates a new 'dynamic' clientset using the provided kubeconfig.
// If kubeconfigPath is not set/empty, it will attempt to load configuration using
// the default loading rules.
func NewDynamicClient(kubeconfigPath string) (dynamic.Interface, error) {
cfg, err := loadRESTConfig(kubeconfigPath)
cfg, err := kubeconfig.LoadRESTConfig(kubeconfigPath)
if err != nil {
return nil, errors.WithStack(err)
}
Expand All @@ -31,7 +31,7 @@ func NewDynamicClient(kubeconfigPath string) (dynamic.Interface, error) {
func NewDiscoveryClient(kubeconfigPath string) (discovery.DiscoveryClient, error) {
var discoveryClient *discovery.DiscoveryClient

cfg, err := loadRESTConfig(kubeconfigPath)
cfg, err := kubeconfig.LoadRESTConfig(kubeconfigPath)
if err != nil {
return discovery.DiscoveryClient{}, errors.WithStack(err)
}
Expand All @@ -49,7 +49,7 @@ func NewDiscoveryClient(kubeconfigPath string) (discovery.DiscoveryClient, error
// the default loading rules.
func NewClientSet(kubeconfigPath string) (kubernetes.Interface, error) {
var clientset *kubernetes.Clientset
cfg, err := loadRESTConfig(kubeconfigPath)
cfg, err := kubeconfig.LoadRESTConfig(kubeconfigPath)
if err != nil {
return nil, errors.WithStack(err)
}
Expand All @@ -59,28 +59,3 @@ func NewClientSet(kubeconfigPath string) (kubernetes.Interface, error) {
}
return clientset, nil
}

func loadRESTConfig(path string) (*rest.Config, error) {
switch path {
// If the kubeconfig path is not provided, use the default loading rules
// so we read the regular KUBECONFIG variable or create a non-interactive
// client for agents running in cluster
case "":
loadingrules := clientcmd.NewDefaultClientConfigLoadingRules()
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
loadingrules, &clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return nil, errors.WithStack(err)
}
return cfg, nil
// Otherwise use the explicitly named kubeconfig file.
default:
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: path},
&clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return nil, errors.WithStack(err)
}
return cfg, nil
}
}
35 changes: 35 additions & 0 deletions pkg/kubeconfig/kubeconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package kubeconfig

import (
"github.com/pkg/errors"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

// LoadRESTConfig loads the kube config from the provided path. If the path is
// empty, the kube config will be loaded from KUBECONFIG, and if KUBECONFIG
// isn't set, the in-cluster config will be used.
func LoadRESTConfig(path string) (*rest.Config, error) {
switch path {
// If the kubeconfig path is not provided, use the default loading rules
// so we read the regular KUBECONFIG variable or create a non-interactive
// client for agents running in cluster
case "":
loadingrules := clientcmd.NewDefaultClientConfigLoadingRules()
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
loadingrules, &clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return nil, errors.WithStack(err)
}
return cfg, nil
// Otherwise use the explicitly named kubeconfig file.
default:
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: path},
&clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return nil, errors.WithStack(err)
}
return cfg, nil
}
}

0 comments on commit c6d12b6

Please sign in to comment.