This is an automated email from the ASF dual-hosted git repository.

pcongiusti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/main by this push:
     new b2d3eb810 feat(traits): health refactoring
b2d3eb810 is described below

commit b2d3eb8104a3fd4c319553b176a7a39621584a9f
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Fri Mar 22 12:59:24 2024 +0100

    feat(traits): health refactoring
---
 pkg/trait/health.go      |  25 +++++++++-
 pkg/trait/health_test.go | 120 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 143 insertions(+), 2 deletions(-)

diff --git a/pkg/trait/health.go b/pkg/trait/health.go
index cb357a39e..c01c5cb31 100644
--- a/pkg/trait/health.go
+++ b/pkg/trait/health.go
@@ -96,6 +96,29 @@ func (t *healthTrait) Apply(e *Environment) error {
                port = &p
        }
 
+       if e.CamelCatalog.Runtime.Capabilities["health"].Metadata != nil {
+               t.setCatalogConfiguration(container, port, 
e.CamelCatalog.Runtime.Capabilities["health"].Metadata)
+       } else {
+               t.setDefaultConfiguration(container, port)
+       }
+
+       return nil
+}
+
+func (t *healthTrait) setCatalogConfiguration(container *corev1.Container, 
port *intstr.IntOrString, metadata map[string]string) {
+       if pointer.BoolDeref(t.LivenessProbeEnabled, false) {
+               container.LivenessProbe = t.newLivenessProbe(port, 
metadata["defaultLivenessProbePath"])
+       }
+       if pointer.BoolDeref(t.ReadinessProbeEnabled, true) {
+               container.ReadinessProbe = t.newReadinessProbe(port, 
metadata["defaultReadinessProbePath"])
+       }
+       if pointer.BoolDeref(t.StartupProbeEnabled, false) {
+               container.StartupProbe = t.newStartupProbe(port, 
metadata["defaultStartupProbePath"])
+       }
+}
+
+// Deprecated: to be removed in future release in favor of func 
setCatalogConfiguration().
+func (t *healthTrait) setDefaultConfiguration(container *corev1.Container, 
port *intstr.IntOrString) {
        if pointer.BoolDeref(t.LivenessProbeEnabled, false) {
                container.LivenessProbe = t.newLivenessProbe(port, 
defaultLivenessProbePath)
        }
@@ -105,8 +128,6 @@ func (t *healthTrait) Apply(e *Environment) error {
        if pointer.BoolDeref(t.StartupProbeEnabled, false) {
                container.StartupProbe = t.newStartupProbe(port, 
defaultStartupProbePath)
        }
-
-       return nil
 }
 
 func (t *healthTrait) newLivenessProbe(port *intstr.IntOrString, path string) 
*corev1.Probe {
diff --git a/pkg/trait/health_test.go b/pkg/trait/health_test.go
new file mode 100644
index 000000000..1de406671
--- /dev/null
+++ b/pkg/trait/health_test.go
@@ -0,0 +1,120 @@
+/*
+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"
+
+       "github.com/stretchr/testify/assert"
+       "github.com/stretchr/testify/require"
+
+       corev1 "k8s.io/api/core/v1"
+       metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+       "k8s.io/utils/pointer"
+
+       v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
+       "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
+       "github.com/apache/camel-k/v2/pkg/util/camel"
+       "github.com/apache/camel-k/v2/pkg/util/kubernetes"
+       "github.com/apache/camel-k/v2/pkg/util/test"
+)
+
+func TestHealthTrait(t *testing.T) {
+       catalog, err := camel.DefaultCatalog()
+       require.NoError(t, err)
+
+       client, _ := test.NewFakeClient()
+       traitCatalog := NewCatalog(nil)
+
+       environment := Environment{
+               CamelCatalog: catalog,
+               Catalog:      traitCatalog,
+               Client:       client,
+               Integration: &v1.Integration{
+                       ObjectMeta: metav1.ObjectMeta{
+                               Name:      ServiceTestName,
+                               Namespace: "ns",
+                       },
+                       Status: v1.IntegrationStatus{
+                               Phase: v1.IntegrationPhaseDeploying,
+                       },
+                       Spec: v1.IntegrationSpec{
+                               Profile: v1.TraitProfileKubernetes,
+                               Traits: v1.Traits{
+                                       Health: &trait.HealthTrait{
+                                               Trait: trait.Trait{
+                                                       Enabled: 
pointer.Bool(true),
+                                               },
+                                               LivenessProbeEnabled:  
pointer.Bool(true),
+                                               ReadinessProbeEnabled: 
pointer.Bool(true),
+                                       },
+                               },
+                       },
+               },
+               IntegrationKit: &v1.IntegrationKit{
+                       Status: v1.IntegrationKitStatus{
+                               Phase: v1.IntegrationKitPhaseReady,
+                       },
+               },
+               Platform: &v1.IntegrationPlatform{
+                       Spec: v1.IntegrationPlatformSpec{
+                               Cluster: v1.IntegrationPlatformClusterOpenShift,
+                               Build: v1.IntegrationPlatformBuildSpec{
+                                       PublishStrategy: 
v1.IntegrationPlatformBuildPublishStrategyS2I,
+                                       Registry:        
v1.RegistrySpec{Address: "registry"},
+                                       RuntimeVersion:  
catalog.Runtime.Version,
+                               },
+                       },
+                       Status: v1.IntegrationPlatformStatus{
+                               Phase: v1.IntegrationPlatformPhaseReady,
+                       },
+               },
+               EnvVars:        make([]corev1.EnvVar, 0),
+               ExecutedTraits: make([]Trait, 0),
+               Resources:      kubernetes.NewCollection(),
+       }
+       environment.Platform.ResyncStatusFullConfig()
+       _, err = traitCatalog.apply(&environment)
+       require.NoError(t, err)
+
+       d := 
environment.Resources.GetDeploymentForIntegration(environment.Integration)
+       assert.NotNil(t, d)
+       assert.Len(t, d.Spec.Template.Spec.Containers, 1)
+       assert.NotNil(t, d.Spec.Template.Spec.Containers[0].LivenessProbe)
+       assert.Equal(t, "/q/health/live", 
d.Spec.Template.Spec.Containers[0].LivenessProbe.HTTPGet.Path)
+       assert.NotNil(t, d.Spec.Template.Spec.Containers[0].ReadinessProbe)
+       assert.Equal(t, "/q/health/ready", 
d.Spec.Template.Spec.Containers[0].ReadinessProbe.HTTPGet.Path)
+       assert.Nil(t, d.Spec.Template.Spec.Containers[0].StartupProbe)
+
+       // Change traits configuration
+       environment.Integration.Spec.Traits.Health.LivenessProbeEnabled = 
pointer.Bool(false)
+       environment.Integration.Spec.Traits.Health.ReadinessProbeEnabled = 
pointer.Bool(false)
+       environment.Integration.Spec.Traits.Health.StartupProbeEnabled = 
pointer.Bool(true)
+
+       environment.Platform.ResyncStatusFullConfig()
+       _, err = traitCatalog.apply(&environment)
+       require.NoError(t, err)
+       d = 
environment.Resources.GetDeploymentForIntegration(environment.Integration)
+       assert.NotNil(t, d)
+       assert.Len(t, d.Spec.Template.Spec.Containers, 1)
+       assert.Nil(t, d.Spec.Template.Spec.Containers[0].LivenessProbe)
+       assert.Nil(t, d.Spec.Template.Spec.Containers[0].ReadinessProbe)
+       assert.NotNil(t, d.Spec.Template.Spec.Containers[0].StartupProbe)
+       assert.Equal(t, "/q/health/started", 
d.Spec.Template.Spec.Containers[0].StartupProbe.HTTPGet.Path)
+
+}

Reply via email to