Skip to content

Commit

Permalink
fix(config): removed global config struct and do dep injection
Browse files Browse the repository at this point in the history
  • Loading branch information
bostrt committed Jun 30, 2022
1 parent 4c1eb58 commit c255f28
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 153 deletions.
43 changes: 8 additions & 35 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,21 @@ import (
"fmt"
"os"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/vmware-tanzu/sonobuoy/cmd/sonobuoy/app"
"github.com/vmware-tanzu/sonobuoy/pkg/client"
sonodynamic "github.com/vmware-tanzu/sonobuoy/pkg/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"

"github.com/openshift/provider-certification-tool/pkg"
"github.com/openshift/provider-certification-tool/pkg/assets"
"github.com/openshift/provider-certification-tool/pkg/client"
"github.com/openshift/provider-certification-tool/pkg/destroy"
"github.com/openshift/provider-certification-tool/pkg/retrieve"
"github.com/openshift/provider-certification-tool/pkg/run"
"github.com/openshift/provider-certification-tool/pkg/status"
"github.com/openshift/provider-certification-tool/pkg/version"
)

var (
config = &pkg.Config{}
)
var ()

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand All @@ -43,28 +36,8 @@ var rootCmd = &cobra.Command{
}
log.SetLevel(logrusLevel)

// Prepare kube client config
config.Kubeconfig = viper.GetString("kubeconfig")
clientConfig, err := clientcmd.BuildConfigFromFlags("", config.Kubeconfig)
if err != nil {
return err
}

config.Clientset, err = kubernetes.NewForConfig(clientConfig)
if err != nil {
return err
}

// Prepare sonobuoy client
skc, err := sonodynamic.NewAPIHelperFromRESTConfig(clientConfig)
if err != nil {
return errors.Wrap(err, "couldn't get sonobuoy api helper")
}

config.SonobuoyClient, err = client.NewSonobuoyClient(clientConfig, skc)
if err != nil {
return err
}
// Save kubeconfig
client.Kubeconfig = viper.GetString("kubeconfig")

return nil
},
Expand All @@ -89,10 +62,10 @@ func init() {

// Link in child commands
rootCmd.AddCommand(assets.NewCmdAssets())
rootCmd.AddCommand(destroy.NewCmdDestroy(config))
rootCmd.AddCommand(retrieve.NewCmdRetrieve(config))
rootCmd.AddCommand(run.NewCmdRun(config))
rootCmd.AddCommand(status.NewCmdStatus(config))
rootCmd.AddCommand(destroy.NewCmdDestroy())
rootCmd.AddCommand(retrieve.NewCmdRetrieve())
rootCmd.AddCommand(run.NewCmdRun())
rootCmd.AddCommand(status.NewCmdStatus())
rootCmd.AddCommand(version.NewCmdVersion())

// Link in child commands direct from Sonobuoy
Expand Down
35 changes: 35 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package client

import (
"github.com/vmware-tanzu/sonobuoy/pkg/client"
sonodynamic "github.com/vmware-tanzu/sonobuoy/pkg/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)

var Kubeconfig string

// CreateClients creates kubernetes and sonobuoy client instances
func CreateClients() (kubernetes.Interface, client.Interface, error) {
clientConfig, err := clientcmd.BuildConfigFromFlags("", Kubeconfig)
if err != nil {
return nil, nil, err
}

clientset, err := kubernetes.NewForConfig(clientConfig)
if err != nil {
return nil, nil, err
}

skc, err := sonodynamic.NewAPIHelperFromRESTConfig(clientConfig)
if err != nil {
return nil, nil, err
}

sonobuoyClient, err := client.NewSonobuoyClient(clientConfig, skc)
if err != nil {
return nil, nil, err
}

return clientset, sonobuoyClient, nil
}
42 changes: 23 additions & 19 deletions pkg/destroy/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/vmware-tanzu/sonobuoy/pkg/client"
sonobuoyclient "github.com/vmware-tanzu/sonobuoy/pkg/client"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

"github.com/openshift/provider-certification-tool/pkg"
"github.com/openshift/provider-certification-tool/pkg/client"
)

const (
Expand All @@ -19,17 +21,14 @@ const (
)

type DestroyOptions struct {
config *pkg.Config
}

func NewDestroyOptions(config *pkg.Config) *DestroyOptions {
return &DestroyOptions{
config: config,
}
func NewDestroyOptions() *DestroyOptions {
return &DestroyOptions{}
}

func NewCmdDestroy(config *pkg.Config) *cobra.Command {
o := NewDestroyOptions(config)
func NewCmdDestroy() *cobra.Command {
o := NewDestroyOptions()
cmd := &cobra.Command{
Use: "destroy",
Aliases: []string{"delete"},
Expand All @@ -38,21 +37,26 @@ func NewCmdDestroy(config *pkg.Config) *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
log.Info("Starting the destroy flow...")

// TODO Should we exit on error anywhere below?
// Client setup
kclient, sclient, err := client.CreateClients()
if err != nil {
log.Error(err)
return
}

err := o.DeleteSonobuoyEnv()
err = o.DeleteSonobuoyEnv(sclient)
if err != nil {
log.Warn(err)
}

log.Info("removing non-openshift NS...")
err = o.DeleteTestNamespaces()
err = o.DeleteTestNamespaces(kclient)
if err != nil {
log.Warn(err)
}

log.Info("restoring privileged environment...")
err = o.RestoreSCC()
err = o.RestoreSCC(kclient)
if err != nil {
log.Warn(err)
}
Expand All @@ -65,18 +69,18 @@ func NewCmdDestroy(config *pkg.Config) *cobra.Command {
}

// DeleteSonobuoyEnv initiates deletion of Sonobuoy environment and waits until completion.
func (d *DestroyOptions) DeleteSonobuoyEnv() error {
deleteConfig := &client.DeleteConfig{
func (d *DestroyOptions) DeleteSonobuoyEnv(sclient sonobuoyclient.Interface) error {
deleteConfig := &sonobuoyclient.DeleteConfig{
Namespace: pkg.CertificationNamespace,
Wait: DeleteSonobuoyEnvWaitTime,
}

return d.config.SonobuoyClient.Delete(deleteConfig)
return sclient.Delete(deleteConfig)
}

// DeleteTestNamespaces deletes any non-openshift namespace.
func (d *DestroyOptions) DeleteTestNamespaces() error {
client := d.config.Clientset.CoreV1()
func (d *DestroyOptions) DeleteTestNamespaces(kclient kubernetes.Interface) error {
client := kclient.CoreV1()

// Get list of all namespaces (TODO is there way to filter these server-side?)
nsList, err := client.Namespaces().List(context.TODO(), metav1.ListOptions{})
Expand Down Expand Up @@ -105,8 +109,8 @@ func (d *DestroyOptions) DeleteTestNamespaces() error {
return nil
}

func (d *DestroyOptions) RestoreSCC() error {
client := d.config.Clientset.RbacV1()
func (d *DestroyOptions) RestoreSCC(kclient kubernetes.Interface) error {
client := kclient.RbacV1()

err := client.ClusterRoleBindings().Delete(context.TODO(), pkg.AnyUIDClusterRoleBinding, metav1.DeleteOptions{})
if err != nil {
Expand Down
10 changes: 2 additions & 8 deletions pkg/destroy/destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake"

"github.com/openshift/provider-certification-tool/pkg"
)

func Test_DeleteTestNamespaces(t *testing.T) {
Expand Down Expand Up @@ -41,14 +39,10 @@ func Test_DeleteTestNamespaces(t *testing.T) {

// Get Clientset and initialize with namespaces
clientset := fake.NewSimpleClientset(namespaces...)
d := DestroyOptions{
config: &pkg.Config{
Clientset: clientset,
},
}
d := DestroyOptions{}

// Delete namespaces
err := d.DeleteTestNamespaces()
err := d.DeleteTestNamespaces(clientset)
assert.Nil(t, err)

// Get actual list of namespaces after deletion
Expand Down
27 changes: 17 additions & 10 deletions pkg/retrieve/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import (
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/vmware-tanzu/sonobuoy/pkg/client"
sonobuoyclient "github.com/vmware-tanzu/sonobuoy/pkg/client"
config2 "github.com/vmware-tanzu/sonobuoy/pkg/config"
"golang.org/x/sync/errgroup"

"github.com/openshift/provider-certification-tool/pkg"
"github.com/openshift/provider-certification-tool/pkg/client"
"github.com/openshift/provider-certification-tool/pkg/status"
)

func NewCmdRetrieve(config *pkg.Config) *cobra.Command {
func NewCmdRetrieve() *cobra.Command {
return &cobra.Command{
Use: "retrieve",
Args: cobra.MaximumNArgs(1),
Expand All @@ -41,16 +42,22 @@ func NewCmdRetrieve(config *pkg.Config) *cobra.Command {
}
}

s := status.NewStatusOptions(config)
err = s.PreRunCheck()
kclient, sclient, err := client.CreateClients()
if err != nil {
log.Error(err)
return
}

s := status.NewStatusOptions(false)
err = s.PreRunCheck(kclient)
if err != nil {
log.Error(err)
return
}

log.Info("Collecting results...")

if err := retrieveResultsRetry(config, destinationDirectory); err != nil {
if err := retrieveResultsRetry(sclient, destinationDirectory); err != nil {
log.Error(err)
return
}
Expand All @@ -60,13 +67,13 @@ func NewCmdRetrieve(config *pkg.Config) *cobra.Command {
}
}

func retrieveResultsRetry(config *pkg.Config, destinationDirectory string) error {
func retrieveResultsRetry(sclient sonobuoyclient.Interface, destinationDirectory string) error {
var err error
limit := 10 // Retry retrieve 10 times
pause := time.Second * 2
retries := 1
for retries <= limit {
err = retrieveResults(config, destinationDirectory)
err = retrieveResults(sclient, destinationDirectory)
if err != nil {
log.Warn(err)
if retries+1 < limit {
Expand All @@ -82,9 +89,9 @@ func retrieveResultsRetry(config *pkg.Config, destinationDirectory string) error
return errors.New("Retrieval retry limit reached")
}

func retrieveResults(config *pkg.Config, destinationDirectory string) error {
func retrieveResults(sclient sonobuoyclient.Interface, destinationDirectory string) error {
// Get a reader that contains the tar output of the results directory.
reader, ec, err := config.SonobuoyClient.RetrieveResults(&client.RetrieveConfig{
reader, ec, err := sclient.RetrieveResults(&sonobuoyclient.RetrieveConfig{
Namespace: pkg.CertificationNamespace,
Path: config2.AggregatorResultsPath,
})
Expand Down Expand Up @@ -112,7 +119,7 @@ func writeResultsToDirectory(outputDir string, r io.Reader, ec <-chan error) ([]
eg.Go(func() error { return <-ec })
eg.Go(func() error {
// This untars the request itself, which is tar'd as just part of the API request, not the sonobuoy logic.
filesCreated, err := client.UntarAll(r, outputDir, "")
filesCreated, err := sonobuoyclient.UntarAll(r, outputDir, "")
if err != nil {
return err
}
Expand Down

0 comments on commit c255f28

Please sign in to comment.