Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add extra check on servicemesh CRD and svc before proceeding create SMCP CR #1359

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
"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 @@

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

Check warning on line 88 in pkg/cluster/operator.go

View check run for this annotation

Codecov / codecov/patch

pkg/cluster/operator.go#L77-L88

Added lines #L77 - L88 were not covered by tests
}

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

Check warning on line 95 in pkg/cluster/operator.go

View check run for this annotation

Codecov / codecov/patch

pkg/cluster/operator.go#L91-L95

Added lines #L91 - L95 were not covered by tests
}
}
return false, nil

Check warning on line 98 in pkg/cluster/operator.go

View check run for this annotation

Codecov / codecov/patch

pkg/cluster/operator.go#L98

Added line #L98 was not covered by tests
})

return err

Check warning on line 101 in pkg/cluster/operator.go

View check run for this annotation

Codecov / codecov/patch

pkg/cluster/operator.go#L101

Added line #L101 was not covered by tests
}
17 changes: 17 additions & 0 deletions pkg/feature/servicemesh/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
k8serr "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -37,6 +39,21 @@ 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 is 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)
}
// Extra check smcp validation service is running.
validationService := &corev1.Service{}
if err := cli.Get(ctx, client.ObjectKey{
Name: "istio-operator-service",
Namespace: "openshift-operators",
}, validationService); err != nil {
if k8serr.IsNotFound(err) {
return fmt.Errorf("failed to find the Service Mesh VWC service, please ensure Service Mesh Operator is running. %w", err)
}
return fmt.Errorf("failed to find the Service Mesh VWC service. %w", err)
}

return nil
}
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/features/fixtures/cluster_test_fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,28 @@ func GetService(ctx context.Context, client client.Client, namespace, name strin
return svc, err
}

func CreateService(ctx context.Context, client client.Client, namespace, svcName string) (*corev1.Service, error) {
if err := client.Create(ctx, &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: svcName,
Namespace: namespace,
},
Spec: corev1.ServiceSpec{
Selector: map[string]string{
"name": "istio-operator",
},
Ports: []corev1.ServicePort{
{
Port: 443,
},
},
},
}); err != nil {
return nil, err
}
return GetService(ctx, client, namespace, svcName)
}

func CreateSecret(name, namespace string) feature.Action {
return func(ctx context.Context, cli client.Client, f *feature.Feature) error {
secret := &corev1.Secret{
Expand Down
62 changes: 59 additions & 3 deletions tests/integration/features/servicemesh_feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"path"

corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
k8serr "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -49,9 +50,11 @@ var _ = Describe("Service Mesh setup", func() {

Context("operator setup", func() {

When("operator is not installed", func() {
Context("operator is not installed", Ordered, func() {

It("should fail using precondition check", func(ctx context.Context) {
var smcpCrdObj *apiextensionsv1.CustomResourceDefinition

It("should fail using precondition subscription check", func(ctx context.Context) {
// given
featuresHandler := feature.ClusterFeaturesHandler(dsci, func(registry feature.FeaturesRegistry) error {
errFeatureAdd := registry.Add(feature.Define("no-service-mesh-operator-check").
Expand All @@ -69,19 +72,72 @@ var _ = Describe("Service Mesh setup", func() {
// then
Expect(applyErr).To(MatchError(ContainSubstring("failed to find the pre-requisite operator subscription \"servicemeshoperator\"")))
})

It("should fail using precondition CRD check", func(ctx context.Context) {
// given
err := fixtures.CreateSubscription(ctx, envTestClient, "openshift-operators", fixtures.OssmSubscription)
Expect(err).ToNot(HaveOccurred())

featuresHandler := feature.ClusterFeaturesHandler(dsci, func(registry feature.FeaturesRegistry) error {
errFeatureAdd := registry.Add(feature.Define("no-service-mesh-crd-check").
PreConditions(servicemesh.EnsureServiceMeshOperatorInstalled),
)

Expect(errFeatureAdd).ToNot(HaveOccurred())

return nil
})

// when
applyErr := featuresHandler.Apply(ctx, envTestClient)

// then
Expect(applyErr).To(MatchError(ContainSubstring("failed to find the Service Mesh Control Plane CRD")))
})

It("should fail using precondition service check", func(ctx context.Context) {
// given
smcpCrdObj = installServiceMeshCRD(ctx)

featuresHandler := feature.ClusterFeaturesHandler(dsci, func(registry feature.FeaturesRegistry) error {
errFeatureAdd := registry.Add(feature.Define("no-service-mesh-service-check").
PreConditions(servicemesh.EnsureServiceMeshOperatorInstalled),
)

Expect(errFeatureAdd).ToNot(HaveOccurred())

return nil
})

// when
applyErr := featuresHandler.Apply(ctx, envTestClient)

// then
Expect(applyErr).To(MatchError(ContainSubstring("failed to find the Service Mesh VWC service")))
})

AfterAll(func(ctx context.Context) {
objectCleaner.DeleteAll(ctx, smcpCrdObj)
})
})

When("operator is installed", func() {
var smcpCrdObj *apiextensionsv1.CustomResourceDefinition
var svc *corev1.Service

BeforeEach(func(ctx context.Context) {
err := fixtures.CreateSubscription(ctx, envTestClient, "openshift-operators", fixtures.OssmSubscription)
Expect(err).ToNot(HaveOccurred())

smcpCrdObj = installServiceMeshCRD(ctx)

svc, err = fixtures.CreateService(ctx, envTestClient, "openshift-operators", "istio-operator-service")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably add a test for the case where the operator is installed but not the service ?
we may also need a test for the missing CRD ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:D you are right
the test first failed because i did not create service, so i need to put a negative test here

Expect(err).ToNot(HaveOccurred())

})

AfterEach(func(ctx context.Context) {
objectCleaner.DeleteAll(ctx, smcpCrdObj)
objectCleaner.DeleteAll(ctx, smcpCrdObj, svc)
})

It("should succeed using precondition check", func(ctx context.Context) {
Expand Down