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

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

commit de09f4c0452b13550300aafed211f482db34b108
Author: Tadayoshi Sato <sato.tadayo...@gmail.com>
AuthorDate: Fri May 27 20:30:22 2022 +0900

    fix(health): support new Camel health check format
    
    Fix #2886
---
 e2e/common/traits/health_test.go          |  2 +-
 pkg/controller/integration/health.go      | 15 +++++-
 pkg/controller/integration/health_test.go | 87 +++++++++++++++++++++++++++++++
 pkg/controller/integration/monitor.go     |  7 +--
 4 files changed, 102 insertions(+), 9 deletions(-)

diff --git a/e2e/common/traits/health_test.go b/e2e/common/traits/health_test.go
index d17e5a98d..d3679b3a9 100644
--- a/e2e/common/traits/health_test.go
+++ b/e2e/common/traits/health_test.go
@@ -99,7 +99,7 @@ func TestHealthTrait(t *testing.T) {
                        //
                        Eventually(IntegrationCondition(ns, "java", 
v1.IntegrationConditionReady), TestTimeoutMedium).Should(And(
                                WithTransform(IntegrationConditionReason, 
Equal(v1.IntegrationConditionRuntimeNotReadyReason)),
-                               WithTransform(IntegrationConditionMessage, 
HavePrefix(fmt.Sprintf("[Pod %s runtime is not ready: map[consumer:route1:DOWN 
context:UP", pod.Name))),
+                               WithTransform(IntegrationConditionMessage, 
HavePrefix(fmt.Sprintf("[Pod %s runtime is not ready: 
map[route.context.name:camel-1 route.id:route1 route.status:Stopped]", 
pod.Name))),
                        ))
                        // Check the Integration is still in running phase
                        Eventually(IntegrationPhase(ns, "java"), 
TestTimeoutShort).Should(Equal(v1.IntegrationPhaseRunning))
diff --git a/pkg/controller/integration/health.go 
b/pkg/controller/integration/health.go
index e56b10add..731e2eacb 100644
--- a/pkg/controller/integration/health.go
+++ b/pkg/controller/integration/health.go
@@ -19,6 +19,7 @@ package integration
 
 import (
        "context"
+       "encoding/json"
        "fmt"
        "strconv"
        "strings"
@@ -37,16 +38,26 @@ const (
 )
 
 type HealthCheck struct {
-       Status HealthCheckState      `json:"state,omitempty"`
+       Status HealthCheckState      `json:"status,omitempty"`
        Checks []HealthCheckResponse `json:"checks,omitempty"`
 }
 
 type HealthCheckResponse struct {
        Name   string                 `json:"name,omitempty"`
-       Status HealthCheckState       `json:"state,omitempty"`
+       Status HealthCheckState       `json:"status,omitempty"`
        Data   map[string]interface{} `json:"data,omitempty"`
 }
 
+func NewHealthCheck(body []byte) (*HealthCheck, error) {
+       health := HealthCheck{}
+       err := json.Unmarshal(body, &health)
+       if err != nil {
+               return nil, err
+       }
+
+       return &health, nil
+}
+
 func proxyGetHTTPProbe(ctx context.Context, c kubernetes.Interface, p 
*corev1.Probe, pod *corev1.Pod, container *corev1.Container) ([]byte, error) {
        if p.HTTPGet == nil {
                return nil, fmt.Errorf("missing probe handler for %s/%s", 
pod.Namespace, pod.Name)
diff --git a/pkg/controller/integration/health_test.go 
b/pkg/controller/integration/health_test.go
new file mode 100644
index 000000000..ab7c2fc89
--- /dev/null
+++ b/pkg/controller/integration/health_test.go
@@ -0,0 +1,87 @@
+/*
+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 integration
+
+import (
+       "reflect"
+       "testing"
+
+       "github.com/stretchr/testify/assert"
+)
+
+func TestNewHealthCheck(t *testing.T) {
+       body := []byte(`
+               {
+                       "status": "DOWN",
+                       "checks": [
+                       {
+                               "name": "camel-routes",
+                               "status": "DOWN",
+                               "data": {
+                                       "route.id": "route1",
+                                       "route.context.name": "camel-1",
+                                       "route.status": "Stopped"
+                               }
+                       },
+                       {
+                               "name": "context",
+                               "status": "UP",
+                               "data": {
+                                       "context.name": "camel-1",
+                                       "context.version": "3.16.0",
+                                       "context.status": "Started"
+                               }
+                       },
+                       {
+                               "name": "camel-consumers",
+                               "status": "DOWN",
+                               "data": {
+                                       "route.id": "route1",
+                                       "route.context.name": "camel-1",
+                                       "route.status": "Stopped"
+                               }
+                       }
+                       ]
+               }
+       `)
+       health, err := NewHealthCheck(body)
+       assert.NoError(t, err)
+       assert.Equal(t, HealthCheckStateDown, health.Status)
+       assert.Len(t, health.Checks, 3)
+       assert.Equal(t, "camel-routes", health.Checks[0].Name)
+       assert.Equal(t, HealthCheckStateDown, health.Checks[0].Status)
+       assert.True(t, reflect.DeepEqual(health.Checks[0].Data, 
map[string]interface{}{
+               "route.id":           "route1",
+               "route.context.name": "camel-1",
+               "route.status":       "Stopped",
+       }))
+       assert.Equal(t, "context", health.Checks[1].Name)
+       assert.Equal(t, HealthCheckStateUp, health.Checks[1].Status)
+       assert.True(t, reflect.DeepEqual(health.Checks[1].Data, 
map[string]interface{}{
+               "context.name":    "camel-1",
+               "context.version": "3.16.0",
+               "context.status":  "Started",
+       }))
+       assert.Equal(t, "camel-consumers", health.Checks[2].Name)
+       assert.Equal(t, HealthCheckStateDown, health.Checks[2].Status)
+       assert.True(t, reflect.DeepEqual(health.Checks[2].Data, 
map[string]interface{}{
+               "route.id":           "route1",
+               "route.context.name": "camel-1",
+               "route.status":       "Stopped",
+       }))
+}
diff --git a/pkg/controller/integration/monitor.go 
b/pkg/controller/integration/monitor.go
index 0dfb695b3..91ea73f63 100644
--- a/pkg/controller/integration/monitor.go
+++ b/pkg/controller/integration/monitor.go
@@ -19,7 +19,6 @@ package integration
 
 import (
        "context"
-       "encoding/json"
        "errors"
        "fmt"
        "reflect"
@@ -339,15 +338,11 @@ func (action *monitorAction) probeReadiness(ctx 
context.Context, environment *tr
                                runtimeNotReadyMessages = 
append(runtimeNotReadyMessages, fmt.Sprintf("readiness probe failed for Pod 
%s/%s: %s", pod.Namespace, pod.Name, err.Error()))
                                continue
                        }
-                       health := HealthCheck{}
-                       err = json.Unmarshal(body, &health)
+                       health, err := NewHealthCheck(body)
                        if err != nil {
                                return err
                        }
                        for _, check := range health.Checks {
-                               if check.Name != "camel-readiness-checks" {
-                                       continue
-                               }
                                if check.Status == HealthCheckStateUp {
                                        continue
                                }

Reply via email to