Skip to content

Commit

Permalink
Fix #815: determine correct profile at runtime when empty
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro authored and lburgazzoli committed Jul 24, 2019
1 parent d7d9508 commit d23c546
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/controller/integrationplatform/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions pkg/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 --
Expand Down Expand Up @@ -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
Expand Down
25 changes: 20 additions & 5 deletions pkg/util/knative/knative.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down

0 comments on commit d23c546

Please sign in to comment.