From 1d347815282b24ddbc80c77d5fead641aae6b08e Mon Sep 17 00:00:00 2001 From: Brian Drennan <brian.drennan@cloudzero.com> Date: Fri, 6 Sep 2024 09:46:53 -0700 Subject: [PATCH] use internal getConfig object --- pkg/cmd/config/command.go | 45 ++++++++++++++++++++----------------- pkg/diagnostic/k8s/check.go | 14 ++++++------ 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/pkg/cmd/config/command.go b/pkg/cmd/config/command.go index 36239dc..3de060c 100644 --- a/pkg/cmd/config/command.go +++ b/pkg/cmd/config/command.go @@ -9,12 +9,12 @@ import ( "github.com/pkg/errors" "github.com/urfave/cli/v2" "k8s.io/client-go/kubernetes" - "k8s.io/client-go/tools/clientcmd" "github.com/cloudzero/cloudzero-agent-validator/pkg/build" "github.com/cloudzero/cloudzero-agent-validator/pkg/config" "github.com/cloudzero/cloudzero-agent-validator/pkg/util/gh" "github.com/cloudzero/cloudzero-agent-validator/pkg/k8s" + diagnosticK8s "github.com/cloudzero/cloudzero-agent-validator/pkg/diagnostic/k8s" ) //go:embed internal/template.yml @@ -77,27 +77,32 @@ func NewCommand(ctx context.Context) *cli.Command { return nil }, }, - { - Name: "list-services", - Usage: "lists Kubernetes services in all namespaces", - Flags: []cli.Flag{ - &cli.StringFlag{Name: "kubeconfig", Usage: "absolute path to the kubeconfig file", Required: false}, - }, - Action: func(c *cli.Context) error { - kubeconfigPath := c.String("kubeconfig") - config, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath) - if err != nil { - return errors.Wrap(err, "building kubeconfig") - } + { + Name: "list-services", + Usage: "lists Kubernetes services in all namespaces", + Flags: []cli.Flag{ + &cli.StringFlag{Name: "kubeconfig", Usage: "absolute path to the kubeconfig file", Required: false}, + }, + Action: func(c *cli.Context) error { + // Create an instance of the checker struct + checker := &diagnosticK8s.Checker{} - clientset, err := kubernetes.NewForConfig(config) - if err != nil { - return errors.Wrap(err, "creating clientset") - } + // Get the Kubernetes configuration using the getConfig method + config, err := checker.GetConfig() + if err != nil { + return errors.Wrap(err, "getting Kubernetes config") + } - return k8s.ListServices(ctx, clientset) - }, - }, + // Create the Kubernetes clientset + clientset, err := kubernetes.NewForConfig(config) + if err != nil { + return errors.Wrap(err, "creating clientset") + } + + // List the services + return k8s.ListServices(ctx, clientset) + }, + }, }, } return cmd diff --git a/pkg/diagnostic/k8s/check.go b/pkg/diagnostic/k8s/check.go index ef13c41..14204ee 100644 --- a/pkg/diagnostic/k8s/check.go +++ b/pkg/diagnostic/k8s/check.go @@ -22,20 +22,20 @@ import ( const DiagnosticK8sVersion = config.DiagnosticK8sVersion -type checker struct { +type Checker struct { cfg *config.Settings logger *logrus.Entry } func NewProvider(ctx context.Context, cfg *config.Settings) diagnostic.Provider { - return &checker{ + return &Checker{ cfg: cfg, logger: logging.NewLogger(). WithContext(ctx).WithField(logging.OpField, "k8s"), } } -func (c *checker) Check(_ context.Context, client *http.Client, accessor status.Accessor) error { +func (c *Checker) Check(_ context.Context, client *http.Client, accessor status.Accessor) error { version, err := c.getK8sVersion(client) if err != nil { accessor.AddCheck( @@ -54,8 +54,8 @@ func (c *checker) Check(_ context.Context, client *http.Client, accessor status. } // getK8sVersion returns the k8s version of the cluster -func (c *checker) getK8sVersion(_ *http.Client) ([]byte, error) { - cfg, err := c.getConfig() +func (c *Checker) getK8sVersion(_ *http.Client) ([]byte, error) { + cfg, err := c.GetConfig() if err != nil { return nil, errors.Wrap(err, "read config") } @@ -76,10 +76,10 @@ func (c *checker) getK8sVersion(_ *http.Client) ([]byte, error) { return []byte(fmt.Sprintf("%s.%s", information.Major, information.Minor)), nil } -// getConfig returns a k8s config based on the environment +// GetConfig returns a k8s config based on the environment // detecting if we are on the prometheus pod or running // on a machine with a kubeconfig file -func (c *checker) getConfig() (*rest.Config, error) { +func (c *Checker) GetConfig() (*rest.Config, error) { if common.InPod() { return rest.InClusterConfig() }