From 0798ca84a0242d2192e4dfc1b6ed2f9cff17c028 Mon Sep 17 00:00:00 2001 From: Alex Buchanan Date: Fri, 8 Nov 2024 07:44:26 -0800 Subject: [PATCH] refactor: dedupe defaultK8s func --- internal/cmd/local/local.go | 21 --------------------- internal/cmd/local/local/cmd.go | 22 +++++++--------------- internal/cmd/local/local_credentials.go | 3 ++- internal/cmd/local/local_deployments.go | 3 ++- 4 files changed, 11 insertions(+), 38 deletions(-) diff --git a/internal/cmd/local/local.go b/internal/cmd/local/local.go index 1fbf503..597a0dc 100644 --- a/internal/cmd/local/local.go +++ b/internal/cmd/local/local.go @@ -10,8 +10,6 @@ import ( "github.com/airbytehq/abctl/internal/cmd/local/localerr" "github.com/airbytehq/abctl/internal/cmd/local/paths" "github.com/pterm/pterm" - "k8s.io/client-go/kubernetes" - "k8s.io/client-go/tools/clientcmd" ) type Cmd struct { @@ -74,22 +72,3 @@ func checkAirbyteDir() error { return nil } - -// defaultK8s returns the default k8s client for the provided kubecfg and kubectx. -func defaultK8s(kubecfg, kubectx string) (k8s.Client, error) { - k8sCfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( - &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubecfg}, - &clientcmd.ConfigOverrides{CurrentContext: kubectx}, - ) - - restCfg, err := k8sCfg.ClientConfig() - if err != nil { - return nil, fmt.Errorf("%w: could not create rest config: %w", localerr.ErrKubernetes, err) - } - k8sClient, err := kubernetes.NewForConfig(restCfg) - if err != nil { - return nil, fmt.Errorf("%w: could not create clientset: %w", localerr.ErrKubernetes, err) - } - - return &k8s.DefaultK8sClient{ClientSet: k8sClient}, nil -} diff --git a/internal/cmd/local/local/cmd.go b/internal/cmd/local/local/cmd.go index f6c650f..cc05e8f 100644 --- a/internal/cmd/local/local/cmd.go +++ b/internal/cmd/local/local/cmd.go @@ -130,7 +130,7 @@ func New(provider k8s.Provider, opts ...Option) (*Command, error) { // set k8s client, if not defined if c.k8s == nil { var err error - if c.k8s, err = defaultK8s(provider.Kubeconfig, provider.Context); err != nil { + if c.k8s, err = DefaultK8s(provider.Kubeconfig, provider.Context); err != nil { return nil, err } } @@ -173,13 +173,13 @@ func New(provider k8s.Provider, opts ...Option) (*Command, error) { return c, nil } -// defaultK8s returns the default k8s client -func defaultK8s(kubecfg, kubectx string) (k8s.Client, error) { +// DefaultK8s returns the default k8s client +func DefaultK8s(kubecfg, kubectx string) (k8s.Client, error) { rest.SetDefaultWarningHandler(k8s.Logger{}) - k8sCfg, err := k8sClientConfig(kubecfg, kubectx) - if err != nil { - return nil, fmt.Errorf("%w: %w", localerr.ErrKubernetes, err) - } + k8sCfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( + &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubecfg}, + &clientcmd.ConfigOverrides{CurrentContext: kubectx}, + ) restCfg, err := k8sCfg.ClientConfig() if err != nil { @@ -192,11 +192,3 @@ func defaultK8s(kubecfg, kubectx string) (k8s.Client, error) { return &k8s.DefaultK8sClient{ClientSet: k8sClient}, nil } - -// k8sClientConfig returns a k8s client config using the ~/.kube/config file and the k8sContext context. -func k8sClientConfig(kubecfg, kubectx string) (clientcmd.ClientConfig, error) { - return clientcmd.NewNonInteractiveDeferredLoadingClientConfig( - &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubecfg}, - &clientcmd.ConfigOverrides{CurrentContext: kubectx}, - ), nil -} diff --git a/internal/cmd/local/local_credentials.go b/internal/cmd/local/local_credentials.go index 743e71b..088ebe8 100644 --- a/internal/cmd/local/local_credentials.go +++ b/internal/cmd/local/local_credentials.go @@ -6,6 +6,7 @@ import ( "github.com/airbytehq/abctl/internal/cmd/local/airbyte" "github.com/airbytehq/abctl/internal/cmd/local/k8s" + "github.com/airbytehq/abctl/internal/cmd/local/local" "github.com/airbytehq/abctl/internal/telemetry" "github.com/pterm/pterm" "go.opencensus.io/trace" @@ -32,7 +33,7 @@ func (cc *CredentialsCmd) Run(ctx context.Context, provider k8s.Provider, telCli spinner := &pterm.DefaultSpinner return telClient.Wrap(ctx, telemetry.Credentials, func() error { - k8sClient, err := defaultK8s(provider.Kubeconfig, provider.Context) + k8sClient, err := local.DefaultK8s(provider.Kubeconfig, provider.Context) if err != nil { pterm.Error.Println("No existing cluster found") return nil diff --git a/internal/cmd/local/local_deployments.go b/internal/cmd/local/local_deployments.go index 268c35d..34a2308 100644 --- a/internal/cmd/local/local_deployments.go +++ b/internal/cmd/local/local_deployments.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/airbytehq/abctl/internal/cmd/local/k8s" + "github.com/airbytehq/abctl/internal/cmd/local/local" "github.com/airbytehq/abctl/internal/telemetry" "github.com/pterm/pterm" "go.opencensus.io/trace" @@ -18,7 +19,7 @@ func (d *DeploymentsCmd) Run(ctx context.Context, telClient telemetry.Client, pr ctx, span := trace.StartSpan(ctx, "local deployments") defer span.End() - k8sClient, err := defaultK8s(provider.Kubeconfig, provider.Context) + k8sClient, err := local.DefaultK8s(provider.Kubeconfig, provider.Context) if err != nil { return err }