Skip to content

Commit

Permalink
use internal getConfig object
Browse files Browse the repository at this point in the history
  • Loading branch information
bdrennz committed Sep 6, 2024
1 parent 02a5d76 commit 1d34781
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
45 changes: 25 additions & 20 deletions pkg/cmd/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions pkg/diagnostic/k8s/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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")
}
Expand All @@ -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()
}
Expand Down

0 comments on commit 1d34781

Please sign in to comment.