diff --git a/docs/modules/ROOT/partials/apis/camel-k-crds.adoc b/docs/modules/ROOT/partials/apis/camel-k-crds.adoc index 0a65eef48b..ba68494f78 100644 --- a/docs/modules/ROOT/partials/apis/camel-k-crds.adoc +++ b/docs/modules/ROOT/partials/apis/camel-k-crds.adoc @@ -6512,7 +6512,7 @@ Deprecated: to be removed from trait configuration. The health trait is responsible for configuring the health probes on the integration container. -It's disabled by default. +It's enabled by default. [cols="2,2a",options="header"] diff --git a/docs/modules/traits/pages/health.adoc b/docs/modules/traits/pages/health.adoc index 1f44025088..faf6433dec 100755 --- a/docs/modules/traits/pages/health.adoc +++ b/docs/modules/traits/pages/health.adoc @@ -3,7 +3,7 @@ // Start of autogenerated code - DO NOT EDIT! (description) The health trait is responsible for configuring the health probes on the integration container. -It's disabled by default. +It's enabled by default. This trait is available in the following profiles: **Kubernetes, Knative, OpenShift**. diff --git a/pkg/apis/camel/v1/trait/health.go b/pkg/apis/camel/v1/trait/health.go index 587249afe2..5b4e26a69a 100644 --- a/pkg/apis/camel/v1/trait/health.go +++ b/pkg/apis/camel/v1/trait/health.go @@ -19,7 +19,7 @@ package trait // The health trait is responsible for configuring the health probes on the integration container. // -// It's disabled by default. +// It's enabled by default. // // +camel-k:trait=health. type HealthTrait struct { diff --git a/pkg/trait/health.go b/pkg/trait/health.go index cb357a39ec..d400763fed 100644 --- a/pkg/trait/health.go +++ b/pkg/trait/health.go @@ -57,7 +57,7 @@ func (t *healthTrait) Configure(e *Environment) (bool, *TraitCondition, error) { !e.IntegrationInPhase(v1.IntegrationPhaseInitialization) && !e.IntegrationInRunningPhases() { return false, nil, nil } - if !pointer.BoolDeref(t.Enabled, false) { + if !pointer.BoolDeref(t.Enabled, true) { return false, nil, nil } @@ -82,7 +82,7 @@ func (t *healthTrait) Apply(e *Environment) error { container := e.GetIntegrationContainer() if container == nil { - return fmt.Errorf("unable to find integration container: %s", e.Integration.Name) + return fmt.Errorf("unable to find integration container for %s", e.Integration.Name) } var port *intstr.IntOrString // Use the default named HTTP container port if it exists. diff --git a/pkg/trait/health_test.go b/pkg/trait/health_test.go new file mode 100644 index 0000000000..fbcf8f1b15 --- /dev/null +++ b/pkg/trait/health_test.go @@ -0,0 +1,137 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package trait + +import ( + "testing" + + v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1" + "github.com/apache/camel-k/v2/pkg/util/camel" + "github.com/apache/camel-k/v2/pkg/util/kubernetes" + "github.com/stretchr/testify/assert" + + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestConfigureHealthTraitDoesSucceed(t *testing.T) { + ht, environment := createNominalHealthTrait(t) + configured, condition, err := ht.Configure(environment) + + assert.True(t, configured) + assert.Nil(t, err) + assert.Nil(t, condition) +} + +func TestConfigureHealthTraitDisabled(t *testing.T) { + enabled := false + ht, environment := createNominalHealthTrait(t) + ht.Enabled = &enabled + configured, condition, err := ht.Configure(environment) + + assert.False(t, configured) + assert.Nil(t, err) + assert.Nil(t, condition) +} + +func TestApplyHealthTraitDefault(t *testing.T) { + ht, environment := createNominalHealthTrait(t) + configured, condition, err := ht.Configure(environment) + assert.True(t, configured) + assert.Nil(t, err) + assert.Nil(t, condition) + + err = ht.Apply(environment) + assert.Nil(t, err) + assert.Equal(t, "/q/health/ready", environment.GetIntegrationContainer().ReadinessProbe.HTTPGet.Path) + assert.Equal(t, corev1.URISchemeHTTP, environment.GetIntegrationContainer().ReadinessProbe.HTTPGet.Scheme) + assert.Equal(t, "8080", environment.GetIntegrationContainer().ReadinessProbe.HTTPGet.Port.String()) +} +func TestApplyHealthTraitLivenessDefault(t *testing.T) { + enabled := true + ht, environment := createNominalHealthTrait(t) + ht.LivenessProbeEnabled = &enabled + configured, condition, err := ht.Configure(environment) + assert.True(t, configured) + assert.Nil(t, err) + assert.Nil(t, condition) + + err = ht.Apply(environment) + assert.Nil(t, err) + assert.Equal(t, "/q/health/live", environment.GetIntegrationContainer().LivenessProbe.HTTPGet.Path) + assert.Equal(t, corev1.URISchemeHTTP, environment.GetIntegrationContainer().LivenessProbe.HTTPGet.Scheme) + assert.Equal(t, "8080", environment.GetIntegrationContainer().LivenessProbe.HTTPGet.Port.String()) +} + +func TestApplyHealthTraitStartupDefault(t *testing.T) { + enabled := true + ht, environment := createNominalHealthTrait(t) + ht.StartupProbeEnabled = &enabled + configured, condition, err := ht.Configure(environment) + assert.True(t, configured) + assert.Nil(t, err) + assert.Nil(t, condition) + + err = ht.Apply(environment) + assert.Nil(t, err) + assert.Equal(t, "/q/health/started", environment.GetIntegrationContainer().StartupProbe.HTTPGet.Path) + assert.Equal(t, corev1.URISchemeHTTP, environment.GetIntegrationContainer().StartupProbe.HTTPGet.Scheme) + assert.Equal(t, "8080", environment.GetIntegrationContainer().StartupProbe.HTTPGet.Port.String()) +} + +func createNominalHealthTrait(t *testing.T) (*healthTrait, *Environment) { + t.Helper() + catalog, err := camel.DefaultCatalog() + assert.Nil(t, err) + trait, _ := newHealthTrait().(*healthTrait) + + environment := &Environment{ + CamelCatalog: catalog, + Catalog: NewCatalog(nil), + Integration: &v1.Integration{ + ObjectMeta: metav1.ObjectMeta{ + Name: "integration-name", + Generation: 1, + }, + Status: v1.IntegrationStatus{ + Phase: v1.IntegrationPhaseRunning, + }, + }, + Resources: kubernetes.NewCollection(), + } + + deployment := appsv1.Deployment{ + TypeMeta: metav1.TypeMeta{ + Kind: "Deployment", + APIVersion: appsv1.SchemeGroupVersion.String(), + }, + Spec: appsv1.DeploymentSpec{ + Template: corev1.PodTemplateSpec{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + {Name: "integration"}, + }, + }, + }, + }, + } + environment.Resources.Add(&deployment) + + return trait, environment +} diff --git a/pkg/trait/jvm.go b/pkg/trait/jvm.go index ffcf2ccd98..96734335ef 100644 --- a/pkg/trait/jvm.go +++ b/pkg/trait/jvm.go @@ -129,7 +129,7 @@ func (t *jvmTrait) Apply(e *Environment) error { container := e.GetIntegrationContainer() if container == nil { - return fmt.Errorf("unable to find integration container: %s", e.Integration.Name) + return fmt.Errorf("unable to find integration container for %s", e.Integration.Name) } // Build the container command diff --git a/pkg/trait/mount.go b/pkg/trait/mount.go index 75aa96faaa..17e299a114 100644 --- a/pkg/trait/mount.go +++ b/pkg/trait/mount.go @@ -72,7 +72,7 @@ func (t *mountTrait) Configure(e *Environment) (bool, *TraitCondition, error) { func (t *mountTrait) Apply(e *Environment) error { container := e.GetIntegrationContainer() if container == nil { - return fmt.Errorf("unable to find integration container: %s", e.Integration.Name) + return fmt.Errorf("unable to find integration container for %s", e.Integration.Name) } var volumes *[]corev1.Volume diff --git a/pkg/trait/quarkus.go b/pkg/trait/quarkus.go index cb8b216f0b..de84986316 100644 --- a/pkg/trait/quarkus.go +++ b/pkg/trait/quarkus.go @@ -410,7 +410,7 @@ func (t *quarkusTrait) applyWhenKitReady(e *Environment) error { if e.IntegrationInRunningPhases() && t.isNativeIntegration(e) { container := e.GetIntegrationContainer() if container == nil { - return fmt.Errorf("unable to find integration container: %s", e.Integration.Name) + return fmt.Errorf("unable to find integration container for %s", e.Integration.Name) } container.Command = []string{"./camel-k-integration-" + defaults.Version + "-runner"} diff --git a/resources/traits.yaml b/resources/traits.yaml index 5fd331dd26..d0036e4a5a 100755 --- a/resources/traits.yaml +++ b/resources/traits.yaml @@ -675,7 +675,7 @@ traits: - Knative - OpenShift description: The health trait is responsible for configuring the health probes on - the integration container. It's disabled by default. + the integration container. It's enabled by default. properties: - name: enabled type: bool