Skip to content

Commit

Permalink
fix: create k8s client once
Browse files Browse the repository at this point in the history
  • Loading branch information
bostrt committed Jun 30, 2022
1 parent f086b9a commit 4ceb01b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 44 deletions.
12 changes: 9 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"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"
Expand Down Expand Up @@ -44,18 +45,23 @@ var rootCmd = &cobra.Command{

// Prepare kube client config
config.Kubeconfig = viper.GetString("kubeconfig")
config.ClientConfig, err = clientcmd.BuildConfigFromFlags("", config.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(config.ClientConfig)
skc, err := sonodynamic.NewAPIHelperFromRESTConfig(clientConfig)
if err != nil {
return errors.Wrap(err, "couldn't get sonobuoy api helper")
}

config.SonobuoyClient, err = client.NewSonobuoyClient(config.ClientConfig, skc)
config.SonobuoyClient, err = client.NewSonobuoyClient(clientConfig, skc)

return nil
},
Expand Down
21 changes: 6 additions & 15 deletions pkg/destroy/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"github.com/spf13/cobra"
"github.com/vmware-tanzu/sonobuoy/pkg/client"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
nsv1 "k8s.io/client-go/kubernetes/typed/core/v1"
rbacv1client "k8s.io/client-go/kubernetes/typed/rbac/v1"

"github.com/openshift/provider-certification-tool/pkg"
)
Expand All @@ -32,7 +30,6 @@ func NewDestroyOptions(config *pkg.Config) *DestroyOptions {

func NewCmdDestroy(config *pkg.Config) *cobra.Command {
o := NewDestroyOptions(config)

cmd := &cobra.Command{
Use: "destroy",
Aliases: []string{"delete"},
Expand Down Expand Up @@ -91,13 +88,10 @@ func (d *DestroyOptions) DeleteStateFile() error {

// DeleteTestNamespaces deletes any non-openshift namespace.
func (d *DestroyOptions) DeleteTestNamespaces() error {
nsClient, err := nsv1.NewForConfig(d.config.ClientConfig)
if err != nil {
return err
}
client := d.config.Clientset.CoreV1()

// Get list of all namespaces (TODO is there way to filter these server-side?)
nsList, err := nsClient.Namespaces().List(context.TODO(), metav1.ListOptions{})
nsList, err := client.Namespaces().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
Expand All @@ -114,7 +108,7 @@ func (d *DestroyOptions) DeleteTestNamespaces() error {

// Delete filtered namespaces
for _, ns := range nonOpenShiftNamespaces {
err := nsClient.Namespaces().Delete(context.TODO(), ns, metav1.DeleteOptions{})
err := client.Namespaces().Delete(context.TODO(), ns, metav1.DeleteOptions{})
if err != nil {
log.WithError(err).Warnf("error deleting namespace %s", ns)
}
Expand All @@ -124,18 +118,15 @@ func (d *DestroyOptions) DeleteTestNamespaces() error {
}

func (d *DestroyOptions) RestoreSCC() error {
rbacClient, err := rbacv1client.NewForConfig(d.config.ClientConfig)
if err != nil {
return err
}
client := d.config.Clientset.RbacV1()

err = rbacClient.ClusterRoleBindings().Delete(context.TODO(), pkg.AnyUIDClusterRoleBinding, metav1.DeleteOptions{})
err := client.ClusterRoleBindings().Delete(context.TODO(), pkg.AnyUIDClusterRoleBinding, metav1.DeleteOptions{})
if err != nil {
return err
}
log.Infof("Deleted %s ClusterRoleBinding", pkg.AnyUIDClusterRoleBinding)

err = rbacClient.ClusterRoleBindings().Delete(context.TODO(), pkg.PrivilegedClusterRoleBinding, metav1.DeleteOptions{})
err = client.ClusterRoleBindings().Delete(context.TODO(), pkg.PrivilegedClusterRoleBinding, metav1.DeleteOptions{})
if err != nil {
return err
}
Expand Down
14 changes: 3 additions & 11 deletions pkg/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
nsv1 "k8s.io/client-go/kubernetes/typed/core/v1"
rbacv1client "k8s.io/client-go/kubernetes/typed/rbac/v1"

"github.com/openshift/provider-certification-tool/pkg"
"github.com/openshift/provider-certification-tool/pkg/assets"
Expand Down Expand Up @@ -103,13 +101,10 @@ func NewCmdRun(config *pkg.Config) *cobra.Command {

// PreRunCheck performs some checks before kicking off Sonobuoy
func (r *RunOptions) PreRunCheck() error {
nsClient, err := nsv1.NewForConfig(r.config.ClientConfig)
if err != nil {
return err
}
client := r.config.Clientset.CoreV1()

// Check if sonobuoy namespace already exists
p, err := nsClient.Namespaces().Get(context.TODO(), pkg.CertificationNamespace, metav1.GetOptions{})
p, err := client.Namespaces().Get(context.TODO(), pkg.CertificationNamespace, metav1.GetOptions{})
if err != nil {
// If error is due to namespace not being found, we continue.
if !kerrors.IsNotFound(err) {
Expand All @@ -124,10 +119,7 @@ func (r *RunOptions) PreRunCheck() error {

log.Info("Ensuring the tool will run in the privileged environment...")
// Configure SCC
rbacClient, err := rbacv1client.NewForConfig(r.config.ClientConfig)
if err != nil {
return err
}
rbacClient := r.config.Clientset.RbacV1()

anyuid := &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Expand Down
8 changes: 2 additions & 6 deletions pkg/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
wait2 "k8s.io/apimachinery/pkg/util/wait"
nsv1 "k8s.io/client-go/kubernetes/typed/core/v1"

"github.com/openshift/provider-certification-tool/pkg"
"github.com/openshift/provider-certification-tool/pkg/wait"
Expand Down Expand Up @@ -79,13 +78,10 @@ func NewCmdStatus(config *pkg.Config) *cobra.Command {
}

func (s *StatusOptions) PreRunCheck() error {
nsClient, err := nsv1.NewForConfig(s.config.ClientConfig)
if err != nil {
return err
}
client := s.config.Clientset.CoreV1()

// Check if sonobuoy namespac already exists
_, err = nsClient.Namespaces().Get(context.TODO(), pkg.CertificationNamespace, metav1.GetOptions{})
_, err := client.Namespaces().Get(context.TODO(), pkg.CertificationNamespace, metav1.GetOptions{})
if err != nil {
// If error is due to namespace not being found, return guidance.
if kerrors.IsNotFound(err) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/adrg/xdg"
log "github.com/sirupsen/logrus"
"github.com/vmware-tanzu/sonobuoy/pkg/client"
"k8s.io/client-go/rest"
"k8s.io/client-go/kubernetes"
)

const (
Expand All @@ -31,7 +31,7 @@ func init() {

type Config struct {
Kubeconfig string
ClientConfig *rest.Config
Clientset *kubernetes.Clientset
SonobuoyClient *client.SonobuoyClient
SonobuoyImage string
Timeout int
Expand Down
10 changes: 3 additions & 7 deletions pkg/wait/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
watchtools "k8s.io/client-go/tools/watch"

Expand All @@ -22,19 +21,16 @@ import (
func WaitForRequiredResources(config *pkg.Config) error {
var obj kruntime.Object

client, err := kubernetes.NewForConfig(config.ClientConfig)
if err != nil {
return err
}
restClient := config.Clientset.CoreV1().RESTClient()

lw := cache.NewFilteredListWatchFromClient(client.CoreV1().RESTClient(), "pods", pkg.CertificationNamespace, func(options *metav1.ListOptions) {
lw := cache.NewFilteredListWatchFromClient(restClient, "pods", pkg.CertificationNamespace, func(options *metav1.ListOptions) {
options.LabelSelector = "component=sonobuoy,sonobuoy-component=aggregator"
})

// Wait for Sonobuoy Pods to become Ready
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute*10)
defer cancel()
_, err = watchtools.UntilWithSync(ctx, lw, obj, nil, func(event watch.Event) (bool, error) {
_, err := watchtools.UntilWithSync(ctx, lw, obj, nil, func(event watch.Event) (bool, error) {
switch event.Type {
case watch.Error:
return false, fmt.Errorf("error waiting for sonobuoy to start: %w", event.Object.(error))
Expand Down

0 comments on commit 4ceb01b

Please sign in to comment.