Skip to content

Commit

Permalink
fix(health): support new Camel health check format
Browse files Browse the repository at this point in the history
Fix apache#2886

(cherry picked from commit apache/camel-k@80fd27e07)
  • Loading branch information
tadayosi authored and squakez committed May 31, 2022
1 parent 9ac5763 commit 9444bb9
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 9 deletions.
2 changes: 1 addition & 1 deletion e2e/common/traits/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
15 changes: 13 additions & 2 deletions pkg/controller/integration/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package integration

import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
Expand All @@ -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)
Expand Down
87 changes: 87 additions & 0 deletions pkg/controller/integration/health_test.go
Original file line number Diff line number Diff line change
@@ -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",
}))
}
7 changes: 1 addition & 6 deletions pkg/controller/integration/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package integration

import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 9444bb9

Please sign in to comment.