This is an automated email from the ASF dual-hosted git repository. astefanutti pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-k.git
The following commit(s) were added to refs/heads/main by this push: new 3652ee748 chore: Use global client discovery API to check Knative install 3652ee748 is described below commit 3652ee74813567f255e7f353a5045301e3d08ed4 Author: Antonin Stefanutti <anto...@stefanutti.fr> AuthorDate: Thu Sep 29 14:32:52 2022 +0200 chore: Use global client discovery API to check Knative install --- e2e/global/knative/knative_platform_test.go | 4 ++- pkg/controller/integration/platform_setup.go | 24 ++++++++++------ pkg/controller/kameletbinding/integration.go | 4 ++- pkg/install/cluster.go | 2 +- pkg/install/knative.go | 2 +- pkg/install/operator.go | 2 +- pkg/util/knative/enabled.go | 43 ++++------------------------ 7 files changed, 29 insertions(+), 52 deletions(-) diff --git a/e2e/global/knative/knative_platform_test.go b/e2e/global/knative/knative_platform_test.go index d35c6867f..5b4bdb88d 100644 --- a/e2e/global/knative/knative_platform_test.go +++ b/e2e/global/knative/knative_platform_test.go @@ -40,7 +40,9 @@ import ( func TestKnativePlatform(t *testing.T) { WithNewTestNamespace(t, func(ns string) { - if !knative.IsEnabledInNamespace(TestContext, TestClient(), ns) { + installed, err := knative.IsInstalled(TestClient()) + Expect(err).NotTo(HaveOccurred()) + if !installed { t.Error("Knative not installed in the cluster") t.FailNow() } diff --git a/pkg/controller/integration/platform_setup.go b/pkg/controller/integration/platform_setup.go index 6c1b85bb1..92a90ce80 100644 --- a/pkg/controller/integration/platform_setup.go +++ b/pkg/controller/integration/platform_setup.go @@ -59,7 +59,11 @@ func (action *platformSetupAction) Handle(ctx context.Context, integration *v1.I if err != nil && !k8serrors.IsNotFound(err) { return nil, err } else if pl != nil { - integration.Status.Profile = determineBestProfile(ctx, action.client, integration, pl) + profile, err := determineBestProfile(action.client, integration, pl) + if err != nil { + return nil, err + } + integration.Status.Profile = profile } // Change the integration phase to Initialization after traits have been applied @@ -73,24 +77,26 @@ func (action *platformSetupAction) Handle(ctx context.Context, integration *v1.I } // DetermineBestProfile tries to detect the best trait profile for the integration. -func determineBestProfile(ctx context.Context, c client.Client, integration *v1.Integration, p *v1.IntegrationPlatform) v1.TraitProfile { +func determineBestProfile(c client.Client, integration *v1.Integration, p *v1.IntegrationPlatform) (v1.TraitProfile, error) { if integration.Spec.Profile != "" { - return integration.Spec.Profile + return integration.Spec.Profile, nil } if integration.Status.Profile != "" { // Integration already has a profile - return integration.Status.Profile + return integration.Status.Profile, nil } if p.Status.Profile != "" { // Use platform profile if set - return p.Status.Profile + return p.Status.Profile, nil } if p.Spec.Profile != "" { // Use platform spec profile if set - return p.Spec.Profile + return p.Spec.Profile, nil } - if knative.IsEnabledInNamespace(ctx, c, integration.Namespace) { - return v1.TraitProfileKnative + if ok, err := knative.IsInstalled(c); err != nil { + return "", err + } else if ok { + return v1.TraitProfileKnative, nil } - return platform.GetProfile(p) + return platform.GetProfile(p), nil } diff --git a/pkg/controller/kameletbinding/integration.go b/pkg/controller/kameletbinding/integration.go index 3f988d595..e9e1add70 100644 --- a/pkg/controller/kameletbinding/integration.go +++ b/pkg/controller/kameletbinding/integration.go @@ -249,7 +249,9 @@ func determineProfile(ctx context.Context, c client.Client, binding *v1alpha1.Ka return pl.Spec.Profile, nil } } - if knative.IsEnabledInNamespace(ctx, c, binding.Namespace) { + if ok, err := knative.IsInstalled(c); err != nil { + return "", err + } else if ok { return v1.TraitProfileKnative, nil } if pl != nil { diff --git a/pkg/install/cluster.go b/pkg/install/cluster.go index b135b2d64..6e18919a8 100644 --- a/pkg/install/cluster.go +++ b/pkg/install/cluster.go @@ -201,7 +201,7 @@ func SetupClusterWideResourcesOrCollect(ctx context.Context, clientProvider clie } } - isKnative, err := knative.IsInstalled(ctx, c) + isKnative, err := knative.IsInstalled(c) if err != nil { return err } diff --git a/pkg/install/knative.go b/pkg/install/knative.go index 23d779c60..cf98ef5e8 100644 --- a/pkg/install/knative.go +++ b/pkg/install/knative.go @@ -35,7 +35,7 @@ const knativeAddressableResolverClusterRoleName = "addressable-resolver" // BindKnativeAddressableResolverClusterRole binds the Knative addressable resolver aggregated ClusterRole // to the operator ServiceAccount. func BindKnativeAddressableResolverClusterRole(ctx context.Context, c kubernetes.Interface, namespace string, operatorNamespace string) error { - if isKnative, err := knative.IsInstalled(ctx, c); err != nil { + if isKnative, err := knative.IsInstalled(c); err != nil { return err } else if !isKnative { return nil diff --git a/pkg/install/operator.go b/pkg/install/operator.go index ce33bbea9..00d01f1d0 100644 --- a/pkg/install/operator.go +++ b/pkg/install/operator.go @@ -242,7 +242,7 @@ func OperatorOrCollect(ctx context.Context, cmd *cobra.Command, c client.Client, } // Additionally, install Knative resources (roles and bindings) - isKnative, err := knative.IsInstalled(ctx, c) + isKnative, err := knative.IsInstalled(c) if err != nil { return err } diff --git a/pkg/util/knative/enabled.go b/pkg/util/knative/enabled.go index 00e6adc65..80e94b9f8 100644 --- a/pkg/util/knative/enabled.go +++ b/pkg/util/knative/enabled.go @@ -18,48 +18,15 @@ limitations under the License. package knative import ( - "context" - - "github.com/apache/camel-k/pkg/client" - kubernetesutils "github.com/apache/camel-k/pkg/util/kubernetes" - "github.com/apache/camel-k/pkg/util/log" k8serrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" - "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" -) -// IsEnabledInNamespace returns true if we can list some basic knative objects in the given namespace. -// -// This method can be used at operator level to check if knative resources can be accessed. -func IsEnabledInNamespace(ctx context.Context, c client.Client, namespace string) bool { - dyn, err := dynamic.NewForConfig(c.GetConfig()) - if err != nil { - log.Infof("could not create dynamic client to check knative installation in namespace %s, got error: %v", namespace, err) - return false - } - for _, kgv := range RequiredKinds { - _, err = dyn.Resource(schema.GroupVersionResource{ - Group: kgv.Group, - Version: kgv.Version, - Resource: kgv.Resource, - }).Namespace(namespace).List(ctx, metav1.ListOptions{}) - - if err == nil { - return true - } - } - - log.Infof("could not find any knative type in namespace %s, last error was: %v", namespace, err) - return false -} + util "github.com/apache/camel-k/pkg/util/kubernetes" +) -// IsInstalled returns true if we are connected to a cluster with Knative installed -// -// This method should not be called from the operator, as it might require permissions that are not available. -func IsInstalled(ctx context.Context, c kubernetes.Interface) (bool, error) { - // check some Knative APIs +// 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 @@ -72,7 +39,7 @@ func IsInstalled(ctx context.Context, c kubernetes.Interface) (bool, error) { func isInstalled(c kubernetes.Interface, api schema.GroupVersion) (bool, error) { _, err := c.Discovery().ServerResourcesForGroupVersion(api.String()) - if err != nil && (k8serrors.IsNotFound(err) || kubernetesutils.IsUnknownAPIError(err)) { + if err != nil && (k8serrors.IsNotFound(err) || util.IsUnknownAPIError(err)) { return false, nil } else if err != nil { return false, err