Skip to content

Commit

Permalink
feat(trait): enable health trait by default
Browse files Browse the repository at this point in the history
Closes #5024
  • Loading branch information
squakez committed Jan 24, 2024
1 parent 1db0f57 commit f89806a
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/modules/ROOT/partials/apis/camel-k-crds.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/traits/pages/health.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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**.
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/camel/v1/trait/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/trait/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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.
Expand Down
137 changes: 137 additions & 0 deletions pkg/trait/health_test.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion pkg/trait/jvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/trait/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/trait/quarkus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
2 changes: 1 addition & 1 deletion resources/traits.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f89806a

Please sign in to comment.