From 80fd27e07e5d0507c9749ba6a9aef6177c7d0fa4 Mon Sep 17 00:00:00 2001 From: Tadayoshi Sato Date: Fri, 27 May 2022 20:30:22 +0900 Subject: [PATCH] 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(-) create mode 100644 pkg/controller/integration/health_test.go diff --git a/e2e/common/traits/health_test.go b/e2e/common/traits/health_test.go index d17e5a98dc..d3679b3a90 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 e56b10add5..731e2eacb4 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 0000000000..ab7c2fc899 --- /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 0dfb695b34..91ea73f638 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 }