From 35ef59870fd847a5e1bbb34a8b578761447773a4 Mon Sep 17 00:00:00 2001 From: Christoph Deppisch Date: Wed, 24 Apr 2024 19:34:11 +0200 Subject: [PATCH] fix(#5402): Make sure to enable Knative profile - Use Knative profile when Serving or Eventing is installed on cluster - Make sure to enable Knative trait when Serving or Eventing is installed - Enable knative-service trait only when Knative Serving is installed --- pkg/controller/integration/platform_setup.go | 2 +- pkg/controller/kameletbinding/integration.go | 2 +- pkg/controller/pipe/integration.go | 2 +- pkg/trait/knative_service.go | 6 ++++++ pkg/util/knative/enabled.go | 17 +++++++++++++++-- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/pkg/controller/integration/platform_setup.go b/pkg/controller/integration/platform_setup.go index 2c578276de..e1851f7696 100644 --- a/pkg/controller/integration/platform_setup.go +++ b/pkg/controller/integration/platform_setup.go @@ -97,7 +97,7 @@ func determineBestTraitProfile(c client.Client, integration *v1.Integration, p * // Use platform spec profile if set return p.Spec.Profile, nil } - if ok, err := knative.IsServingInstalled(c); err != nil { + if ok, err := knative.IsInstalled(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 1f46302777..b28780de88 100644 --- a/pkg/controller/kameletbinding/integration.go +++ b/pkg/controller/kameletbinding/integration.go @@ -248,7 +248,7 @@ func determineTraitProfile(ctx context.Context, c client.Client, binding *v1alph return pl.Spec.Profile, nil } } - if ok, err := knative.IsServingInstalled(c); err != nil { + if ok, err := knative.IsInstalled(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 80b06d662a..aa2a9c4be4 100644 --- a/pkg/controller/pipe/integration.go +++ b/pkg/controller/pipe/integration.go @@ -248,7 +248,7 @@ func determineTraitProfile(ctx context.Context, c client.Client, binding *v1.Pip return pl.Spec.Profile, nil } } - if ok, err := knative.IsServingInstalled(c); err != nil { + if ok, err := knative.IsInstalled(c); err != nil { return "", err } else if ok { return v1.TraitProfileKnative, nil diff --git a/pkg/trait/knative_service.go b/pkg/trait/knative_service.go index a8e53a779e..0fee5f9da7 100644 --- a/pkg/trait/knative_service.go +++ b/pkg/trait/knative_service.go @@ -30,6 +30,7 @@ import ( v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1" traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait" "github.com/apache/camel-k/v2/pkg/metadata" + "github.com/apache/camel-k/v2/pkg/util/knative" "github.com/apache/camel-k/v2/pkg/util/kubernetes" ) @@ -149,6 +150,11 @@ func (t *knativeServiceTrait) SelectControllerStrategy(e *Environment) (*Control return nil, nil } + // Knative serving is required + if ok, _ := knative.IsServingInstalled(e.Client); !ok { + return nil, nil + } + var sources []v1.SourceSpec var err error if sources, err = kubernetes.ResolveIntegrationSources(e.Ctx, t.Client, e.Integration, e.Resources); err != nil { diff --git a/pkg/util/knative/enabled.go b/pkg/util/knative/enabled.go index 0a6b6ced2c..3cb503d5f0 100644 --- a/pkg/util/knative/enabled.go +++ b/pkg/util/knative/enabled.go @@ -28,7 +28,7 @@ import ( // 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 { + if installed, err := isServerResourceAvailable(c, ref.GroupVersionKind().GroupVersion()); err != nil { return false, err } else if installed { return true, nil @@ -36,6 +36,19 @@ func IsRefKindInstalled(c kubernetes.Interface, ref corev1.ObjectReference) (boo return false, nil } +// IsInstalled returns true if we are connected to a cluster with either Knative Serving or Eventing installed. +func IsInstalled(c kubernetes.Interface) (bool, error) { + if ok, err := IsServingInstalled(c); ok { + return ok, err + } else if ok, err = IsEventingInstalled(c); ok { + return ok, err + } else if err != nil { + return false, err + } + + 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{ @@ -52,7 +65,7 @@ func IsEventingInstalled(c kubernetes.Interface) (bool, error) { }) } -func isInstalled(c kubernetes.Interface, api schema.GroupVersion) (bool, error) { +func isServerResourceAvailable(c kubernetes.Interface, api schema.GroupVersion) (bool, error) { _, err := c.Discovery().ServerResourcesForGroupVersion(api.String()) if err != nil && (k8serrors.IsNotFound(err) || util.IsUnknownAPIError(err)) { return false, nil