From 2b2a6e7c04c2dd931956888be99dd02589472896 Mon Sep 17 00:00:00 2001 From: Pasquale Congiusti Date: Fri, 5 Jan 2024 13:24:57 +0100 Subject: [PATCH] fix(knative): func to check when either serving or eventing are installed Closes #4962 --- e2e/knative/knative_platform_test.go | 2 +- pkg/controller/integration/platform_setup.go | 2 +- pkg/controller/kameletbinding/integration.go | 2 +- pkg/controller/pipe/integration.go | 2 +- pkg/install/cluster.go | 2 +- pkg/trait/container.go | 2 +- pkg/trait/container_test.go | 4 +- pkg/util/bindings/knative_ref.go | 8 ++-- pkg/util/bindings/knative_ref_test.go | 4 +- pkg/util/knative/apis.go | 13 ------ pkg/util/knative/enabled.go | 43 +++++++++++--------- pkg/util/test/client.go | 17 +++++++- 12 files changed, 53 insertions(+), 48 deletions(-) diff --git a/e2e/knative/knative_platform_test.go b/e2e/knative/knative_platform_test.go index 2dc5fdedfa..f221d02f6e 100644 --- a/e2e/knative/knative_platform_test.go +++ b/e2e/knative/knative_platform_test.go @@ -41,7 +41,7 @@ import ( func TestKnativePlatformDetection(t *testing.T) { RegisterTestingT(t) - installed, err := knative.IsInstalled(TestClient()) + installed, err := knative.IsServingInstalled(TestClient()) Expect(err).NotTo(HaveOccurred()) if !installed { t.Error("Knative not installed in the cluster") diff --git a/pkg/controller/integration/platform_setup.go b/pkg/controller/integration/platform_setup.go index a272715bab..911e607b6f 100644 --- a/pkg/controller/integration/platform_setup.go +++ b/pkg/controller/integration/platform_setup.go @@ -97,7 +97,7 @@ func determineBestProfile(c client.Client, integration *v1.Integration, p *v1.In // Use platform spec profile if set return p.Spec.Profile, nil } - if ok, err := knative.IsInstalled(c); err != nil { + if ok, err := knative.IsServingInstalled(c); err != nil { return "", err } else if ok { return v1.TraitProfileKnative, nil diff --git a/pkg/controller/kameletbinding/integration.go b/pkg/controller/kameletbinding/integration.go index 04720c25f8..b2e60f599f 100644 --- a/pkg/controller/kameletbinding/integration.go +++ b/pkg/controller/kameletbinding/integration.go @@ -248,7 +248,7 @@ func determineProfile(ctx context.Context, c client.Client, binding *v1alpha1.Ka return pl.Spec.Profile, nil } } - if ok, err := knative.IsInstalled(c); err != nil { + if ok, err := knative.IsServingInstalled(c); err != nil { return "", err } else if ok { return v1.TraitProfileKnative, nil diff --git a/pkg/controller/pipe/integration.go b/pkg/controller/pipe/integration.go index cf25f0722f..7effc4869f 100644 --- a/pkg/controller/pipe/integration.go +++ b/pkg/controller/pipe/integration.go @@ -249,7 +249,7 @@ func determineProfile(ctx context.Context, c client.Client, binding *v1.Pipe) (v return pl.Spec.Profile, nil } } - if ok, err := knative.IsInstalled(c); err != nil { + if ok, err := knative.IsServingInstalled(c); err != nil { return "", err } else if ok { return v1.TraitProfileKnative, nil diff --git a/pkg/install/cluster.go b/pkg/install/cluster.go index 945c554f29..32334b8a79 100644 --- a/pkg/install/cluster.go +++ b/pkg/install/cluster.go @@ -121,7 +121,7 @@ func installClusterRoles( // === For Knative === // ClusterRole: camel-k-operator-bind-addressable-resolver - isKnative, err := knative.IsInstalled(c) + isKnative, err := knative.IsEventingInstalled(c) if err != nil { return err } diff --git a/pkg/trait/container.go b/pkg/trait/container.go index 443d977878..37429a60fb 100644 --- a/pkg/trait/container.go +++ b/pkg/trait/container.go @@ -77,7 +77,7 @@ func (t *containerTrait) Configure(e *Environment) (bool, *TraitCondition, error return false, nil, nil } - knativeInstalled, _ := knative.IsInstalled(e.Client) + knativeInstalled, _ := knative.IsEventingInstalled(e.Client) if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) && !knativeInstalled { hasKnativeEndpoint, err := containsEndpoint("knative", e, t.Client) if err != nil { diff --git a/pkg/trait/container_test.go b/pkg/trait/container_test.go index 28383b69c4..7c424eb2be 100644 --- a/pkg/trait/container_test.go +++ b/pkg/trait/container_test.go @@ -487,9 +487,9 @@ func TestRunNonKnativeEndpointWithKnativeNotInstalled(t *testing.T) { func createEnvironment() *Environment { client, _ := test.NewFakeClient() - // disable the knative service api + // disable the knative eventing api fakeClient := client.(*test.FakeClient) //nolint - fakeClient.DisableAPIGroupDiscovery("serving.knative.dev/v1") + fakeClient.DisableAPIGroupDiscovery("eventing.knative.dev/v1") replicas := int32(3) catalog, _ := camel.QuarkusCatalog() diff --git a/pkg/util/bindings/knative_ref.go b/pkg/util/bindings/knative_ref.go index 1ab488c1db..b90b3b3fe9 100644 --- a/pkg/util/bindings/knative_ref.go +++ b/pkg/util/bindings/knative_ref.go @@ -53,8 +53,8 @@ func (k KnativeRefBindingProvider) Translate(ctx BindingContext, endpointCtx End return nil, err } - if knativeInstalled, _ := knative.IsInstalled(ctx.Client); !knativeInstalled { - // works only when Knative is installed + if refInstalled, _ := knative.IsRefKindInstalled(ctx.Client, *e.Ref); !refInstalled { + // works only when Knative specific API Kind is installed return nil, fmt.Errorf("integration referencing Knative endpoint '%s' that cannot run, "+ "because Knative is not installed on the cluster", e.Ref.Name) } @@ -155,8 +155,8 @@ func (k V1alpha1KnativeRefBindingProvider) Translate(ctx V1alpha1BindingContext, return nil, err } - if knativeInstalled, _ := knative.IsInstalled(ctx.Client); !knativeInstalled { - // works only when Knative is installed + if refInstalled, _ := knative.IsRefKindInstalled(ctx.Client, *e.Ref); !refInstalled { + // works only when Knative specific API Kind is installed return nil, fmt.Errorf("integration referencing Knative endpoint '%s' that cannot run, "+ "because Knative is not installed on the cluster", e.Ref.Name) } diff --git a/pkg/util/bindings/knative_ref_test.go b/pkg/util/bindings/knative_ref_test.go index 9d06ebed3e..6cd63cbe19 100644 --- a/pkg/util/bindings/knative_ref_test.go +++ b/pkg/util/bindings/knative_ref_test.go @@ -128,9 +128,9 @@ func TestKnativeNotInstalled(t *testing.T) { client, err := test.NewFakeClient() assert.NoError(t, err) - // disable the knative service api + // disable the knative eventing api fakeClient := client.(*test.FakeClient) //nolint - fakeClient.DisableAPIGroupDiscovery("serving.knative.dev/v1") + fakeClient.DisableAPIGroupDiscovery("eventing.knative.dev/v1") bindingContext := BindingContext{ Ctx: ctx, diff --git a/pkg/util/knative/apis.go b/pkg/util/knative/apis.go index 67c0af09fc..6739be3470 100644 --- a/pkg/util/knative/apis.go +++ b/pkg/util/knative/apis.go @@ -132,19 +132,6 @@ var ( Resource: "brokers", }, } - - // RequiredKinds are Knative kinds used by Camel K for materializing integrations. - // They must be present on the cluster. - RequiredKinds = []GroupVersionKindResource{ - { - GroupVersionKind: schema.GroupVersionKind{ - Kind: "Service", - Group: "serving.knative.dev", - Version: "v1", - }, - Resource: "services", - }, - } ) // GroupVersionKindResource --. diff --git a/pkg/util/knative/enabled.go b/pkg/util/knative/enabled.go index 9a62c3f6f3..0a6b6ced2c 100644 --- a/pkg/util/knative/enabled.go +++ b/pkg/util/knative/enabled.go @@ -18,6 +18,7 @@ limitations under the License. package knative import ( + corev1 "k8s.io/api/core/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/kubernetes" @@ -25,18 +26,32 @@ import ( util "github.com/apache/camel-k/v2/pkg/util/kubernetes" ) -// IsInstalled returns true if we are connected to a cluster with Knative installed. -func IsInstalled(c kubernetes.Interface) (bool, error) { - for _, api := range getRequiredKnativeGroupVersions() { - if installed, err := isInstalled(c, api); err != nil { - return false, err - } else if installed { - return true, nil - } +// IsRefKindInstalled returns true if the cluster has the referenced Kind installed. +func IsRefKindInstalled(c kubernetes.Interface, ref corev1.ObjectReference) (bool, error) { + if installed, err := isInstalled(c, ref.GroupVersionKind().GroupVersion()); err != nil { + return false, err + } else if installed { + return true, nil } return false, nil } +// IsServingInstalled returns true if we are connected to a cluster with Knative Serving installed. +func IsServingInstalled(c kubernetes.Interface) (bool, error) { + return IsRefKindInstalled(c, corev1.ObjectReference{ + Kind: "Service", + APIVersion: "serving.knative.dev/v1", + }) +} + +// IsEventingInstalled returns true if we are connected to a cluster with Knative Eventing installed. +func IsEventingInstalled(c kubernetes.Interface) (bool, error) { + return IsRefKindInstalled(c, corev1.ObjectReference{ + Kind: "Broker", + APIVersion: "eventing.knative.dev/v1", + }) +} + func isInstalled(c kubernetes.Interface, api schema.GroupVersion) (bool, error) { _, err := c.Discovery().ServerResourcesForGroupVersion(api.String()) if err != nil && (k8serrors.IsNotFound(err) || util.IsUnknownAPIError(err)) { @@ -46,15 +61,3 @@ func isInstalled(c kubernetes.Interface, api schema.GroupVersion) (bool, error) } return true, nil } - -func getRequiredKnativeGroupVersions() []schema.GroupVersion { - apis := make(map[schema.GroupVersion]bool) - res := make([]schema.GroupVersion, 0) - for _, gvk := range RequiredKinds { - if !apis[gvk.GroupVersion()] { - apis[gvk.GroupVersion()] = true - res = append(res, gvk.GroupVersion()) - } - } - return res -} diff --git a/pkg/util/test/client.go b/pkg/util/test/client.go index 952e0da1f0..fef78d2b62 100644 --- a/pkg/util/test/client.go +++ b/pkg/util/test/client.go @@ -204,11 +204,26 @@ func (f *FakeDiscovery) ServerResourcesForGroupVersion(groupVersion string) (*me } } - // used in util/knative/enabled.go to verify if knative is installed + // used to verify if knative is installed if groupVersion == "serving.knative.dev/v1" && !util.StringSliceExists(f.disabledGroups, groupVersion) { return &metav1.APIResourceList{ GroupVersion: "serving.knative.dev/v1", }, nil } + if groupVersion == "eventing.knative.dev/v1" && !util.StringSliceExists(f.disabledGroups, groupVersion) { + return &metav1.APIResourceList{ + GroupVersion: "eventing.knative.dev/v1", + }, nil + } + if groupVersion == "messaging.knative.dev/v1" && !util.StringSliceExists(f.disabledGroups, groupVersion) { + return &metav1.APIResourceList{ + GroupVersion: "messaging.knative.dev/v1", + }, nil + } + if groupVersion == "messaging.knative.dev/v1beta1" && !util.StringSliceExists(f.disabledGroups, groupVersion) { + return &metav1.APIResourceList{ + GroupVersion: "messaging.knative.dev/v1beta1", + }, nil + } return f.DiscoveryInterface.ServerResourcesForGroupVersion(groupVersion) }