Skip to content

Commit

Permalink
fix(#5402): Make sure to enable Knative profile
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
christophd committed Apr 24, 2024
1 parent e9568a6 commit 35ef598
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/controller/integration/platform_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/kameletbinding/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/pipe/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pkg/trait/knative_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 15 additions & 2 deletions pkg/util/knative/enabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,27 @@ 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
}
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{
Expand All @@ -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
Expand Down

0 comments on commit 35ef598

Please sign in to comment.