This is an automated email from the ASF dual-hosted git repository. lburgazzoli pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k.git
The following commit(s) were added to refs/heads/master by this push: new d23c546 Fix #815: determine correct profile at runtime when empty d23c546 is described below commit d23c546720b263d3ccdaa2d1afdf0c079a3df912 Author: nferraro <ni.ferr...@gmail.com> AuthorDate: Wed Jul 24 12:09:44 2019 +0200 Fix #815: determine correct profile at runtime when empty --- pkg/controller/integrationplatform/initialize.go | 2 +- pkg/platform/platform.go | 17 +++++++++++++--- pkg/util/knative/knative.go | 25 +++++++++++++++++++----- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/pkg/controller/integrationplatform/initialize.go b/pkg/controller/integrationplatform/initialize.go index fc2ac59..3e2d404 100644 --- a/pkg/controller/integrationplatform/initialize.go +++ b/pkg/controller/integrationplatform/initialize.go @@ -176,7 +176,7 @@ func (action *initializeAction) isDuplicate(ctx context.Context, thisPlatform *v func (action *initializeAction) setDefaults(ctx context.Context, platform *v1alpha1.IntegrationPlatform) error { if platform.Spec.Profile == "" { - platform.Spec.Profile = platformutil.GetProfile(platform) + platform.Spec.Profile = platformutil.DetermineBestProfile(ctx, action.client, platform) } if platform.Spec.Build.CamelVersion == "" { platform.Spec.Build.CamelVersion = defaults.CamelVersionConstraint diff --git a/pkg/platform/platform.go b/pkg/platform/platform.go index 92682c2..3584835 100644 --- a/pkg/platform/platform.go +++ b/pkg/platform/platform.go @@ -21,10 +21,10 @@ import ( "context" "errors" - k8sclient "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" + "github.com/apache/camel-k/pkg/util/knative" "github.com/apache/camel-k/pkg/util/kubernetes" + k8sclient "sigs.k8s.io/controller-runtime/pkg/client" ) // GetOrLookup -- @@ -71,7 +71,18 @@ func IsActive(p *v1alpha1.IntegrationPlatform) bool { return p.Status.Phase != "" && p.Status.Phase != v1alpha1.IntegrationPlatformPhaseDuplicate } -// GetProfile returns the current profile of the platform (if present) or computes it +// DetermineBestProfile tries to detect the best trait profile for the platform +func DetermineBestProfile(ctx context.Context, c k8sclient.Reader, p *v1alpha1.IntegrationPlatform) v1alpha1.TraitProfile { + if p.Spec.Profile != "" { + return p.Spec.Profile + } + if knative.IsEnabledInNamespace(ctx, c, p.Namespace) { + return v1alpha1.TraitProfileKnative + } + return GetProfile(p) +} + +// GetProfile returns the current profile of the platform (if present) or returns the default one for the cluster func GetProfile(p *v1alpha1.IntegrationPlatform) v1alpha1.TraitProfile { if p.Spec.Profile != "" { return p.Spec.Profile diff --git a/pkg/util/knative/knative.go b/pkg/util/knative/knative.go index a7290b7..54bfc6f 100644 --- a/pkg/util/knative/knative.go +++ b/pkg/util/knative/knative.go @@ -20,13 +20,12 @@ package knative import ( "context" - "github.com/pkg/errors" - "github.com/apache/camel-k/pkg/client" - - "k8s.io/client-go/kubernetes" + "github.com/apache/camel-k/pkg/util/log" + "github.com/pkg/errors" k8serrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/client-go/kubernetes" k8sclient "sigs.k8s.io/controller-runtime/pkg/client" eventing "github.com/knative/eventing/pkg/apis/eventing/v1alpha1" @@ -35,9 +34,25 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) +// IsEnabledInNamespace returns true if we can list some basic knative objects in the given namespace +func IsEnabledInNamespace(ctx context.Context, c k8sclient.Reader, namespace string) bool { + channels := eventing.ChannelList{ + TypeMeta: metav1.TypeMeta{ + Kind: "Channel", + APIVersion: eventing.SchemeGroupVersion.String(), + }, + } + if err := c.List(ctx, &k8sclient.ListOptions{Namespace: namespace}, &channels); err != nil { + log.Infof("could not find knative in namespace %s, got error: %v", namespace, err) + return false + } + return true +} + // IsInstalled returns true if we are connected to a cluster with Knative installed func IsInstalled(ctx context.Context, c kubernetes.Interface) (bool, error) { - _, err := c.Discovery().ServerResourcesForGroupVersion("serving.knative.dev/v1alpha1") + // check knative eventing, since serving may be on v1beta1 in some clusters + _, err := c.Discovery().ServerResourcesForGroupVersion("eventing.knative.dev/v1alpha1") if err != nil && k8serrors.IsNotFound(err) { return false, nil } else if err != nil {