Skip to content

Commit

Permalink
feat: add extra check on servicemesh CRD before proceeding create SMC…
Browse files Browse the repository at this point in the history
…P CR

- when we install operator we might have a race condition:
    - ossm is not ready to server
    - dsci already created smcp CR
    - since we do not know the version of smcp to use(we could assume 2.6) but still
        might have 2.7 release if v2 is maintained with more bug fix
    - ossm webhook can run into conversion problem since no version specify in smcp CR

Signed-off-by: Wen Zhou <wenzhou@redhat.com>
  • Loading branch information
zdtsw committed Nov 9, 2024
1 parent 92251e4 commit 69ab2ae
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pkg/cluster/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import (
"context"
"fmt"
"strings"
"time"

"github.com/operator-framework/api/pkg/operators/v1alpha1"
ofapiv2 "github.com/operator-framework/api/pkg/operators/v2"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -67,3 +72,31 @@ func OperatorExists(ctx context.Context, cli client.Client, operatorPrefix strin

return false, nil
}

// CustomResourceDefinitionExists checks if a CustomResourceDefinition with the given GVK exists.
func CustomResourceDefinitionExists(ctx context.Context, cli client.Client, crdGK schema.GroupKind) error {
crd := &apiextv1.CustomResourceDefinition{}
resourceInterval, resourceTimeout := 2*time.Second, 5*time.Second
name := strings.ToLower(fmt.Sprintf("%ss.%s", crdGK.Kind, crdGK.Group)) // we need plural form of the kind

err := wait.PollUntilContextTimeout(ctx, resourceInterval, resourceTimeout, false, func(ctx context.Context) (bool, error) {
err := cli.Get(ctx, client.ObjectKey{Name: name}, crd)
if err != nil {
if errors.IsNotFound(err) {
return false, nil
}
return false, err
}

for _, condition := range crd.Status.Conditions {
if condition.Type == apiextv1.Established {
if condition.Status == apiextv1.ConditionTrue {
return true, nil
}
}
}
return false, nil
})

return err
}
4 changes: 4 additions & 0 deletions pkg/feature/servicemesh/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func EnsureServiceMeshOperatorInstalled(ctx context.Context, cli client.Client,
if err := feature.EnsureOperatorIsInstalled("servicemeshoperator")(ctx, cli, f); err != nil {
return fmt.Errorf("failed to find the pre-requisite Service Mesh Operator subscription, please ensure Service Mesh Operator is installed. %w", err)
}
// Extra check SMCP CRD is installed and has become active.
if err := cluster.CustomResourceDefinitionExists(ctx, cli, gvk.ServiceMeshControlPlane.GroupKind()); err != nil {
return fmt.Errorf("failed to find the Service Mesh Control Plane CRD, please ensure Service Mesh Operator is installed. %w", err)
}

return nil
}
Expand Down

0 comments on commit 69ab2ae

Please sign in to comment.