Skip to content

Commit

Permalink
refactor: dedupe defaultK8s func
Browse files Browse the repository at this point in the history
  • Loading branch information
abuchanan-airbyte committed Nov 8, 2024
1 parent 985bb74 commit 0798ca8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 38 deletions.
21 changes: 0 additions & 21 deletions internal/cmd/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
22 changes: 7 additions & 15 deletions internal/cmd/local/local/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
3 changes: 2 additions & 1 deletion internal/cmd/local/local_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/local/local_deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down

0 comments on commit 0798ca8

Please sign in to comment.