Skip to content

Commit

Permalink
feat(run): SPLAT-603 check image registry is managed before running
Browse files Browse the repository at this point in the history
  • Loading branch information
bostrt committed Jun 30, 2022
1 parent 4cf09f0 commit c787b22
Showing 1 changed file with 42 additions and 12 deletions.
54 changes: 42 additions & 12 deletions pkg/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"fmt"

configv1 "github.com/openshift/api/config/v1"
operatorv1 "github.com/openshift/api/operator/v1"
coclient "github.com/openshift/client-go/config/clientset/versioned"
irclient "github.com/openshift/client-go/imageregistry/clientset/versioned"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -116,15 +118,40 @@ func (r *RunOptions) PreRunCheck(kclient kubernetes.Interface) error {
coreClient := kclient.CoreV1()
rbacClient := kclient.RbacV1()

// Get ConfigV1 client for Cluster Operators
restConfig, err := client.CreateRestConfig()
if err != nil {
return err
}
configClient, err := coclient.NewForConfig(restConfig)
if err != nil {
return err
}

// Check if Cluster Operators are stable
stable, err := checkClusterOperators()
stable, err := checkClusterOperators(configClient)
if err != nil {
return err
}
if !stable {
return errors.New("All Cluster Operators must be available, not progressing, and not degraded before certification can run")
}

// Get ConfigV1 client for Cluster Operators
irClient, err := irclient.NewForConfig(restConfig)
if err != nil {
return err
}

// Check if Registry is in managed state or exit
managed, err := checkRegistry(irClient)
if err != nil {
return err
}
if !managed {
return errors.New("OpenShift Image Registry must deployed before certification can run")
}

// Check if sonobuoy namespace already exists
p, err := coreClient.Namespaces().Get(context.TODO(), pkg.CertificationNamespace, metav1.GetOptions{})
if err != nil {
Expand Down Expand Up @@ -314,16 +341,9 @@ func (r *RunOptions) Run(kclient kubernetes.Interface, sclient sonobuoyclient.In
return err
}

func checkClusterOperators() (bool, error) {
restConfig, err := client.CreateRestConfig()
if err != nil {
return false, err
}
c, err := coclient.NewForConfig(restConfig)
if err != nil {
return false, err
}
coList, err := c.ConfigV1().ClusterOperators().List(context.TODO(), metav1.ListOptions{})
func checkClusterOperators(configClient coclient.Interface) (bool, error) {
// List all Cluster Operators
coList, err := configClient.ConfigV1().ClusterOperators().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, err
}
Expand Down Expand Up @@ -351,6 +371,16 @@ func checkClusterOperators() (bool, error) {
return true, nil
}

func checkRegistry() (bool, error) {
// Check registry is in managed state. We assume Cluster Operator is stable.
func checkRegistry(irClient irclient.Interface) (bool, error) {
irConfig, err := irClient.ImageregistryV1().Configs().Get(context.TODO(), "cluster", metav1.GetOptions{})
if err != nil {
return false, err
}

if irConfig.Spec.ManagementState != operatorv1.Managed {
return false, nil
}

return true, nil
}

0 comments on commit c787b22

Please sign in to comment.