Skip to content

Commit

Permalink
feat(trait): enable health trait by default
Browse files Browse the repository at this point in the history
Closes #5024
  • Loading branch information
squakez committed May 3, 2024
1 parent 1897e91 commit 0a22bed
Show file tree
Hide file tree
Showing 16 changed files with 269 additions and 46 deletions.
4 changes: 2 additions & 2 deletions e2e/common/cli/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestKamelCLIGet(t *testing.T) {
kitName := IntegrationKit(t, ctx, ns, "yaml")()
kitNamespace := IntegrationKitNamespace(t, ctx, ns, "yaml")()
regex := fmt.Sprintf("^NAME\tPHASE\tKIT\n\\s*yaml\tRunning\t(%s/%s|%s)", kitNamespace, kitName, kitName)
g.Expect(GetOutputString(Kamel(t, ctx, "get", "-n", ns))).To(MatchRegexp(regex))
g.Eventually(GetOutputString(Kamel(t, ctx, "get", "-n", ns))).Should(MatchRegexp(regex))

g.Expect(Kamel(t, ctx, "delete", "--all", "-n", ns).Execute()).To(Succeed())
})
Expand All @@ -65,7 +65,7 @@ func TestKamelCLIGet(t *testing.T) {
kitNamespace2 := IntegrationKitNamespace(t, ctx, ns, "yaml")()
regex := fmt.Sprintf("^NAME\tPHASE\tKIT\n\\s*java\tRunning\t"+
"(%s/%s|%s)\n\\s*yaml\tRunning\t(%s/%s|%s)\n", kitNamespace1, kitName1, kitName1, kitNamespace2, kitName2, kitName2)
g.Expect(GetOutputString(Kamel(t, ctx, "get", "-n", ns))).To(MatchRegexp(regex))
g.Eventually(GetOutputString(Kamel(t, ctx, "get", "-n", ns))).Should(MatchRegexp(regex))

g.Expect(Kamel(t, ctx, "delete", "--all", "-n", ns).Execute()).To(Succeed())
})
Expand Down
5 changes: 5 additions & 0 deletions e2e/common/misc/files/cron-fallback.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
constant: "string!"
- setBody:
simple: "Magic${header.m}"
# Simulate a job workload
- delay:
expression:
constant: 20000
asyncDelayed: false
- to:
uri: "log:info"
parameters:
Expand Down
5 changes: 5 additions & 0 deletions e2e/common/misc/files/cron-quartz.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
constant: "string!"
- setBody:
simple: "Magic${header.m}"
# Simulate a job workload
- delay:
expression:
constant: 20000
asyncDelayed: false
- to:
uri: "log:info"
parameters:
Expand Down
5 changes: 5 additions & 0 deletions e2e/common/misc/files/cron-timer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
constant: "string!"
- setBody:
simple: "Magic${header.m}"
# Simulate a job workload
- delay:
expression:
constant: 20000
asyncDelayed: false
- to:
uri: "log:info"
parameters:
Expand Down
5 changes: 5 additions & 0 deletions e2e/common/misc/files/cron-trait-yaml.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
constant: "string!"
- setBody:
simple: "Magic${header.m}"
# Simulate a job workload
- delay:
expression:
constant: 20000
asyncDelayed: false
- to:
uri: "log:info"
parameters:
Expand Down
5 changes: 5 additions & 0 deletions e2e/common/misc/files/cron-yaml.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
constant: "string!"
- setBody:
simple: "Magic${header.m}"
# Simulate a job workload
- delay:
expression:
constant: 20000
asyncDelayed: false
- to:
uri: "log:info"
parameters:
Expand Down
15 changes: 8 additions & 7 deletions e2e/common/misc/integration_fail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestBadRouteIntegration(t *testing.T) {

t.Run("run bad java route", func(t *testing.T) {
name := RandomizedSuffixName("bad-route")
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/BadRoute.java", "--name", name).Execute()).To(Succeed())
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/BadRoute.java", "--name", name, "-t", "health.enabled=false").Execute()).To(Succeed())
g.Eventually(IntegrationPodPhase(t, ctx, ns, name), TestTimeoutLong).Should(Equal(corev1.PodRunning))
g.Eventually(IntegrationPhase(t, ctx, ns, name), TestTimeoutShort).Should(Equal(v1.IntegrationPhaseError))
g.Eventually(IntegrationConditionStatus(t, ctx, ns, name, v1.IntegrationConditionReady), TestTimeoutShort).
Expand All @@ -73,7 +73,7 @@ func TestBadRouteIntegration(t *testing.T) {
t.Run("run missing dependency java route", func(t *testing.T) {
name := RandomizedSuffixName("java-route")
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/Java.java", "--name", name,
"-d", "mvn:com.example:nonexistent:1.0").Execute()).To(Succeed())
"-d", "mvn:com.example:nonexistent:1.0", "-t", "health.enabled=false").Execute()).To(Succeed())
// Integration in error
g.Eventually(IntegrationPhase(t, ctx, ns, name), TestTimeoutLong).Should(Equal(v1.IntegrationPhaseError))
g.Eventually(IntegrationConditionStatus(t, ctx, ns, name, v1.IntegrationConditionKitAvailable), TestTimeoutShort).Should(Equal(corev1.ConditionFalse))
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestBadRouteIntegration(t *testing.T) {

t.Run("run invalid dependency java route", func(t *testing.T) {
name := RandomizedSuffixName("invalid-dependency")
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/Java.java", "--name", name, "-d", "camel:non-existent").Execute()).To(Succeed())
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/Java.java", "--name", name, "-d", "camel:non-existent", "-t", "health.enabled=false").Execute()).To(Succeed())
// Integration in error with Initialization Failed condition
g.Eventually(IntegrationPhase(t, ctx, ns, name), TestTimeoutLong).Should(Equal(v1.IntegrationPhaseError))
g.Eventually(IntegrationConditionStatus(t, ctx, ns, name, v1.IntegrationConditionReady), TestTimeoutShort).
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestBadRouteIntegration(t *testing.T) {

t.Run("run unresolvable component java route", func(t *testing.T) {
name := RandomizedSuffixName("unresolvable-route")
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/Unresolvable.java", "--name", name).Execute()).To(Succeed())
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/Unresolvable.java", "--name", name, "-t", "health.enabled=false").Execute()).To(Succeed())
// Integration in error with Initialization Failed condition
g.Eventually(IntegrationPhase(t, ctx, ns, name), TestTimeoutShort).Should(Equal(v1.IntegrationPhaseError))
g.Eventually(IntegrationConditionStatus(t, ctx, ns, name, v1.IntegrationConditionReady), TestTimeoutShort).
Expand All @@ -148,7 +148,7 @@ func TestBadRouteIntegration(t *testing.T) {
g.Consistently(IntegrationKit(t, ctx, ns, name), 10*time.Second).Should(BeEmpty())

// Fixing the route should reconcile the Integration in Initialization Failed condition to Running
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/Java.java", "--name", name).Execute()).To(Succeed())
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/Java.java", "--name", name, "-t", "health.enabled=false").Execute()).To(Succeed())
g.Eventually(IntegrationPodPhase(t, ctx, ns, name), TestTimeoutLong).Should(Equal(corev1.PodRunning))
g.Eventually(IntegrationConditionStatus(t, ctx, ns, name, v1.IntegrationConditionReady), TestTimeoutShort).
Should(Equal(corev1.ConditionTrue))
Expand All @@ -164,7 +164,8 @@ func TestBadRouteIntegration(t *testing.T) {

t.Run("run invalid java route", func(t *testing.T) {
name := RandomizedSuffixName("invalid-java-route")
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/InvalidJava.java", "--name", name).Execute()).To(Succeed())
// Skip the health check so we can quickly read from log
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/InvalidJava.java", "--name", name, "-t", "health.enabled=false").Execute()).To(Succeed())
g.Eventually(IntegrationPodPhase(t, ctx, ns, name), TestTimeoutLong).Should(Equal(corev1.PodRunning))
g.Eventually(IntegrationPhase(t, ctx, ns, name), TestTimeoutShort).Should(Equal(v1.IntegrationPhaseError))
g.Eventually(IntegrationConditionStatus(t, ctx, ns, name, v1.IntegrationConditionReady), TestTimeoutShort).
Expand All @@ -177,7 +178,7 @@ func TestBadRouteIntegration(t *testing.T) {
g.Eventually(KitPhase(t, ctx, integrationKitNamespace, kitName), TestTimeoutShort).Should(Equal(v1.IntegrationKitPhaseReady))

// Fixing the route should reconcile the Integration in Initialization Failed condition to Running
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/Java.java", "--name", name).Execute()).To(Succeed())
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/Java.java", "--name", name, "-t", "health.enabled=false").Execute()).To(Succeed())
g.Eventually(IntegrationPodPhase(t, ctx, ns, name), TestTimeoutLong).Should(Equal(corev1.PodRunning))
g.Eventually(IntegrationConditionStatus(t, ctx, ns, name, v1.IntegrationConditionReady), TestTimeoutShort).
Should(Equal(corev1.ConditionTrue))
Expand Down
32 changes: 18 additions & 14 deletions e2e/common/traits/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

. "github.com/apache/camel-k/v2/e2e/support"
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
Expand All @@ -54,9 +53,23 @@ func TestHealthTrait(t *testing.T) {

g.Eventually(SelectedPlatformPhase(t, ctx, ns, operatorID), TestTimeoutMedium).Should(Equal(v1.IntegrationPlatformPhaseReady))

t.Run("Disabled health trait", func(t *testing.T) {
name := RandomizedSuffixName("java")
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/Java.java", "-t", "health.enabled=false", "--name", name).Execute()).To(Succeed())

g.Eventually(IntegrationPodPhase(t, ctx, ns, name), TestTimeoutLong).Should(Equal(corev1.PodRunning))
g.Eventually(IntegrationPhase(t, ctx, ns, name), TestTimeoutShort).Should(Equal(v1.IntegrationPhaseRunning))
g.Eventually(IntegrationConditionStatus(t, ctx, ns, name, v1.IntegrationConditionReady), TestTimeoutShort).
Should(Equal(corev1.ConditionTrue))
g.Eventually(IntegrationLogs(t, ctx, ns, name), TestTimeoutShort).Should(ContainSubstring("Magicstring!"))

// Clean-up
g.Expect(Kamel(t, ctx, "delete", "--all", "-n", ns).Execute()).To(Succeed())
})

t.Run("Readiness condition with stopped route scaled", func(t *testing.T) {
name := RandomizedSuffixName("java")
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/Java.java", "-t", "health.enabled=true", "-t", "jolokia.enabled=true", "-t", "jolokia.use-ssl-client-authentication=false", "-t", "jolokia.protocol=http", "--name", name).Execute()).To(Succeed())
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/Java.java", "-t", "jolokia.enabled=true", "-t", "jolokia.use-ssl-client-authentication=false", "-t", "jolokia.protocol=http", "--name", name).Execute()).To(Succeed())

g.Eventually(IntegrationPodPhase(t, ctx, ns, name), TestTimeoutLong).Should(Equal(corev1.PodRunning))
g.Eventually(IntegrationPhase(t, ctx, ns, name), TestTimeoutShort).Should(Equal(v1.IntegrationPhaseRunning))
Expand All @@ -75,16 +88,7 @@ func TestHealthTrait(t *testing.T) {
// Finally check the readiness condition becomes truthy back
g.Eventually(IntegrationConditionStatus(t, ctx, ns, name, v1.IntegrationConditionReady), TestTimeoutMedium).Should(Equal(corev1.ConditionTrue))

// check integration schema does not contains unwanted default trait value.
g.Eventually(UnstructuredIntegration(t, ctx, ns, name)).ShouldNot(BeNil())
unstructuredIntegration := UnstructuredIntegration(t, ctx, ns, name)()
healthTrait, _, _ := unstructured.NestedMap(unstructuredIntegration.Object, "spec", "traits", "health")
g.Expect(healthTrait).ToNot(BeNil())
g.Expect(len(healthTrait)).To(Equal(1))
g.Expect(healthTrait["enabled"]).To(Equal(true))

pods := IntegrationPods(t, ctx, ns, name)()

for i, pod := range pods {
// Stop the Camel route
request := map[string]string{
Expand Down Expand Up @@ -155,7 +159,7 @@ func TestHealthTrait(t *testing.T) {

t.Run("Readiness condition with stopped route", func(t *testing.T) {
name := RandomizedSuffixName("java")
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/Java.java", "-t", "health.enabled=true", "-t", "jolokia.enabled=true", "-t", "jolokia.use-ssl-client-authentication=false", "-t", "jolokia.protocol=http", "--name", name).Execute()).To(Succeed())
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/Java.java", "-t", "jolokia.enabled=true", "-t", "jolokia.use-ssl-client-authentication=false", "-t", "jolokia.protocol=http", "--name", name).Execute()).To(Succeed())

g.Eventually(IntegrationPodPhase(t, ctx, ns, name), TestTimeoutLong).Should(Equal(corev1.PodRunning))
g.Eventually(IntegrationPhase(t, ctx, ns, name), TestTimeoutShort).Should(Equal(v1.IntegrationPhaseRunning))
Expand Down Expand Up @@ -410,7 +414,7 @@ func TestHealthTrait(t *testing.T) {
t.Run("Startup condition with never ready route", func(t *testing.T) {
name := RandomizedSuffixName("startup-probe-never-ready-route")

g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/NeverReady.java", "--name", name, "-t", "health.enabled=true", "-t", "health.startup-probe-enabled=true", "-t", "health.startup-timeout=60").Execute()).To(Succeed())
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/NeverReady.java", "--name", name, "-t", "health.startup-probe-enabled=true", "-t", "health.startup-timeout=60").Execute()).To(Succeed())

g.Eventually(IntegrationPodPhase(t, ctx, ns, name), TestTimeoutMedium).Should(Equal(corev1.PodRunning))
g.Eventually(IntegrationPhase(t, ctx, ns, name), TestTimeoutMedium).Should(Equal(v1.IntegrationPhaseRunning))
Expand Down Expand Up @@ -467,7 +471,7 @@ func TestHealthTrait(t *testing.T) {
t.Run("Startup condition with ready route", func(t *testing.T) {
name := RandomizedSuffixName("startup-probe-ready-route")

g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/Java.java", "--name", name, "-t", "health.enabled=true", "-t", "health.startup-probe-enabled=true", "-t", "health.startup-timeout=60").Execute()).To(Succeed())
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/Java.java", "--name", name, "-t", "health.startup-probe-enabled=true", "-t", "health.startup-timeout=60").Execute()).To(Succeed())

g.Eventually(IntegrationPodPhase(t, ctx, ns, name), TestTimeoutMedium).Should(Equal(corev1.PodRunning))
g.Eventually(IntegrationPhase(t, ctx, ns, name), TestTimeoutMedium).Should(Equal(v1.IntegrationPhaseRunning))
Expand Down
28 changes: 12 additions & 16 deletions e2e/common/traits/master_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ package traits

import (
"context"
"fmt"
"testing"
"time"

. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

. "github.com/apache/camel-k/v2/e2e/support"
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
Expand All @@ -56,24 +56,20 @@ func TestMasterTrait(t *testing.T) {

t.Run("only one integration with master runs", func(t *testing.T) {
nameFirst := RandomizedSuffixName("first")
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/Master.java", "--name", nameFirst, "--label", "leader-group=same", "-t", "master.label-key=leader-group", "-t", "master.label-value=same", "-t", "owner.target-labels=leader-group").Execute()).To(Succeed())
g.Eventually(IntegrationPodPhase(t, ctx, ns, nameFirst), TestTimeoutLong).Should(Equal(corev1.PodRunning))
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/Master.java", "--name", nameFirst,
"--label", "leader-group=same", "-t", "master.label-key=leader-group", "-t", "master.label-value=same", "-t", "owner.target-labels=leader-group",
).Execute()).To(Succeed())
g.Eventually(IntegrationConditionStatus(t, ctx, ns, nameFirst, v1.IntegrationConditionReady), TestTimeoutShort).Should(Equal(corev1.ConditionTrue))
g.Eventually(IntegrationLogs(t, ctx, ns, nameFirst), TestTimeoutShort).Should(ContainSubstring("Magicstring!"))
// Start a second integration with the same lock (it should not start the route)
// Start a second integration with the same lock (it should not start the route before 15 seconds)
nameSecond := RandomizedSuffixName("second")
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/Master.java", "--name", nameSecond, "--label", "leader-group=same", "-t", "master.label-key=leader-group", "-t", "master.label-value=same", "-t", "master.resource-name=first-lock", "-t", "owner.target-labels=leader-group").Execute()).To(Succeed())
g.Eventually(IntegrationPodPhase(t, ctx, ns, nameSecond), TestTimeoutLong).Should(Equal(corev1.PodRunning))
g.Expect(KamelRunWithID(t, ctx, operatorID, ns, "files/Master.java", "--name", nameSecond,
"--label", "leader-group=same", "-t", "master.label-key=leader-group", "-t", "master.label-value=same", "-t", "owner.target-labels=leader-group",
"-t", fmt.Sprintf("master.resource-name=%s-lock", nameFirst),
).Execute()).To(Succeed())
g.Eventually(IntegrationLogs(t, ctx, ns, nameSecond), TestTimeoutShort).Should(ContainSubstring("started in"))
g.Eventually(IntegrationLogs(t, ctx, ns, nameSecond), 30*time.Second).ShouldNot(ContainSubstring("Magicstring!"))

// check integration schema does not contains unwanted default trait value.
g.Eventually(UnstructuredIntegration(t, ctx, ns, nameFirst)).ShouldNot(BeNil())
unstructuredIntegration := UnstructuredIntegration(t, ctx, ns, nameFirst)()
builderTrait, _, _ := unstructured.NestedMap(unstructuredIntegration.Object, "spec", "traits", "addons", "master")
g.Expect(builderTrait).ToNot(BeNil())
g.Expect(len(builderTrait)).To(Equal(2))
g.Expect(builderTrait["labelKey"]).To(Equal("leader-group"))
g.Expect(builderTrait["labelValue"]).To(Equal("same"))
g.Eventually(IntegrationLogs(t, ctx, ns, nameSecond), 15*time.Second).ShouldNot(ContainSubstring("Magicstring!"))
g.Eventually(IntegrationConditionStatus(t, ctx, ns, nameSecond, v1.IntegrationConditionReady), TestTimeoutShort).Should(Equal(corev1.ConditionTrue))
})

g.Expect(Kamel(t, ctx, "delete", "--all", "-n", ns).Execute()).To(Succeed())
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/camel/v1/trait/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package trait

// The health trait is responsible for configuring the health probes on the integration container.
//
// It's disabled by default.
// NOTE: this trait is enabled by default.
//
// +camel-k:trait=health.
type HealthTrait struct {
Expand Down
7 changes: 6 additions & 1 deletion pkg/cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ func (o *debugCmdOptions) run(cmd *cobra.Command, args []string) error {
}

func (o *debugCmdOptions) toggleDebug(c camelv1.IntegrationsGetter, it *v1.Integration, active bool) (*v1.Integration, error) {
it = o.toggle(it, active)
return c.Integrations(it.Namespace).Update(o.Context, it, metav1.UpdateOptions{})
}

func (o *debugCmdOptions) toggle(it *v1.Integration, active bool) *v1.Integration {
if it.Spec.Traits.JVM == nil {
it.Spec.Traits.JVM = &traitv1.JVMTrait{}
}
Expand All @@ -147,5 +152,5 @@ func (o *debugCmdOptions) toggleDebug(c camelv1.IntegrationsGetter, it *v1.Integ
jvmTrait.DebugSuspend = nil
}

return c.Integrations(it.Namespace).Update(o.Context, it, metav1.UpdateOptions{})
return it
}
Loading

0 comments on commit 0a22bed

Please sign in to comment.