You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ts...@apache.org on 2022/11/17 02:49:16 UTC

[camel-k] 03/04: chore(controller): small refactoring of health types

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 657e386106781474fdf479cd0263b9607b5158de
Author: Tadayoshi Sato <sa...@gmail.com>
AuthorDate: Wed Nov 16 18:22:09 2022 +0900

    chore(controller): small refactoring of health types
---
 pkg/controller/integration/health.go      | 17 ++++++++++-------
 pkg/controller/integration/health_test.go |  8 ++++----
 pkg/controller/integration/monitor.go     | 11 +++++------
 3 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/pkg/controller/integration/health.go b/pkg/controller/integration/health.go
index 731e2eacb..351a3c7c2 100644
--- a/pkg/controller/integration/health.go
+++ b/pkg/controller/integration/health.go
@@ -30,28 +30,31 @@ import (
 	"k8s.io/client-go/kubernetes"
 )
 
-type HealthCheckState string
+type HealthCheckStatus string
 
 const (
-	HealthCheckStateDown HealthCheckState = "DOWN"
-	HealthCheckStateUp   HealthCheckState = "UP"
+	HealthCheckStatusDown HealthCheckStatus = "DOWN"
+	HealthCheckStatusUp   HealthCheckStatus = "UP"
+
+	// The key used for propagating error details from Camel health to MicroProfile Health
+	// (See CAMEL-17138).
+	HealthCheckErrorMessage = "error.message"
 )
 
 type HealthCheck struct {
-	Status HealthCheckState      `json:"status,omitempty"`
+	Status HealthCheckStatus     `json:"status,omitempty"`
 	Checks []HealthCheckResponse `json:"checks,omitempty"`
 }
 
 type HealthCheckResponse struct {
 	Name   string                 `json:"name,omitempty"`
-	Status HealthCheckState       `json:"status,omitempty"`
+	Status HealthCheckStatus      `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 {
+	if err := json.Unmarshal(body, &health); err != nil {
 		return nil, err
 	}
 
diff --git a/pkg/controller/integration/health_test.go b/pkg/controller/integration/health_test.go
index ab7c2fc89..ef1a9c799 100644
--- a/pkg/controller/integration/health_test.go
+++ b/pkg/controller/integration/health_test.go
@@ -61,24 +61,24 @@ func TestNewHealthCheck(t *testing.T) {
 	`)
 	health, err := NewHealthCheck(body)
 	assert.NoError(t, err)
-	assert.Equal(t, HealthCheckStateDown, health.Status)
+	assert.Equal(t, HealthCheckStatusDown, 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.Equal(t, HealthCheckStatusDown, 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.Equal(t, HealthCheckStatusUp, 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.Equal(t, HealthCheckStatusDown, health.Checks[2].Status)
 	assert.True(t, reflect.DeepEqual(health.Checks[2].Data, map[string]interface{}{
 		"route.id":           "route1",
 		"route.context.name": "camel-1",
diff --git a/pkg/controller/integration/monitor.go b/pkg/controller/integration/monitor.go
index cee670a96..d913b72b6 100644
--- a/pkg/controller/integration/monitor.go
+++ b/pkg/controller/integration/monitor.go
@@ -23,6 +23,7 @@ import (
 	"fmt"
 	"reflect"
 	"strconv"
+	"strings"
 
 	appsv1 "k8s.io/api/apps/v1"
 	batchv1 "k8s.io/api/batch/v1"
@@ -41,9 +42,6 @@ import (
 	"github.com/apache/camel-k/pkg/util/kubernetes"
 )
 
-// The key used for propagating error details from Camel health to MicroProfile Health (See CAMEL-17138).
-const runtimeHealthCheckErrorMessage = "error.message"
-
 func NewMonitorAction() Action {
 	return &monitorAction{}
 }
@@ -454,10 +452,10 @@ func (action *monitorAction) probeReadiness(
 				return err
 			}
 			for _, check := range health.Checks {
-				if check.Status == HealthCheckStateUp {
+				if check.Status == HealthCheckStatusUp {
 					continue
 				}
-				if _, ok := check.Data[runtimeHealthCheckErrorMessage]; ok {
+				if _, ok := check.Data[HealthCheckErrorMessage]; ok {
 					integration.Status.Phase = v1.IntegrationPhaseError
 				}
 				runtimeNotReadyMessages = append(runtimeNotReadyMessages,
@@ -470,7 +468,8 @@ func (action *monitorAction) probeReadiness(
 		if integration.Status.Phase == v1.IntegrationPhaseError {
 			reason = v1.IntegrationConditionErrorReason
 		}
-		integration.SetReadyCondition(corev1.ConditionFalse, reason, fmt.Sprintf("%s", runtimeNotReadyMessages))
+		message := fmt.Sprintf("[%s]", strings.Join(runtimeNotReadyMessages, ", "))
+		integration.SetReadyCondition(corev1.ConditionFalse, reason, message)
 	}
 
 	return nil