Skip to content

Commit

Permalink
fix(ctrl): failing Deployment reported in Integration
Browse files Browse the repository at this point in the history
Closes #4974
  • Loading branch information
squakez committed Jan 2, 2024
1 parent 209231d commit 1449c19
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 9 deletions.
77 changes: 77 additions & 0 deletions e2e/commonwithcustominstall/deployment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//go:build integration
// +build integration

// To enable compilation of this file in Goland, go to "Settings -> Go -> Vendoring & Build Tags -> Custom Tags" and add "integration"

/*
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 commonwithcustominstall

import (
"testing"

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"os/exec"

. "github.com/apache/camel-k/v2/e2e/support"
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
)

func TestDeploymentFailureShouldReportIntegrationCondition(t *testing.T) {
WithNewTestNamespace(t, func(ns string) {
op := "camel-k-failing-deploy"
nsRestr := "restr"
Expect(KamelInstallWithID(op, ns, "--global", "--force").Execute()).To(Succeed())
// Create restricted namespace
ExpectExecSucceed(t,
exec.Command(
"kubectl",
"create",
"ns",
nsRestr,
),
)
ExpectExecSucceed(t,
exec.Command(
"kubectl",
"label",
"--overwrite",
"ns",
nsRestr,
"pod-security.kubernetes.io/enforce=baseline",
"pod-security.kubernetes.io/enforce-version=latest",
"pod-security.kubernetes.io/enforce=restricted",
"pod-security.kubernetes.io/warn-version=latest",
"pod-security.kubernetes.io/audit=restricted",
"pod-security.kubernetes.io/audit-version=latest",
),
)
// Create an Integration into a restricted namespace
name := RandomizedSuffixName("java-fail")
Expect(KamelRunWithID(op, ns, "files/Java.java", "--name", name, "-n", nsRestr).Execute()).To(Succeed())
// Check the error is reported into the Integration
Eventually(IntegrationPhase(nsRestr, name), TestTimeoutMedium).Should(Equal(v1.IntegrationPhaseError))
Eventually(IntegrationCondition(nsRestr, name, v1.IntegrationConditionReady)().Status).
Should(Equal(corev1.ConditionFalse))
Eventually(IntegrationCondition(nsRestr, name, v1.IntegrationConditionReady)().Message).
Should(ContainSubstring("is forbidden: violates PodSecurity"))
// Clean up
Eventually(DeleteIntegrations(nsRestr)).Should(Equal(0))
})
}
14 changes: 8 additions & 6 deletions pkg/controller/integration/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func (action *monitorAction) newController(env *trait.Environment, integration *
obj = getUpdatedController(env, &appsv1.Deployment{})
deploy, ok := obj.(*appsv1.Deployment)
if !ok {
return nil, fmt.Errorf("type assertion failed: %v", obj)
return nil, fmt.Errorf("type assertion failed, not a Deployment: %v", obj)
}
controller = &deploymentController{
obj: deploy,
Expand All @@ -275,7 +275,7 @@ func (action *monitorAction) newController(env *trait.Environment, integration *
obj = getUpdatedController(env, &servingv1.Service{})
svc, ok := obj.(*servingv1.Service)
if !ok {
return nil, fmt.Errorf("type assertion failed: %v", obj)
return nil, fmt.Errorf("type assertion failed, not a Knative Service: %v", obj)
}
controller = &knativeServiceController{
obj: svc,
Expand All @@ -285,7 +285,7 @@ func (action *monitorAction) newController(env *trait.Environment, integration *
obj = getUpdatedController(env, &batchv1.CronJob{})
cj, ok := obj.(*batchv1.CronJob)
if !ok {
return nil, fmt.Errorf("type assertion failed: %v", obj)
return nil, fmt.Errorf("type assertion failed, not a CronJob: %v", obj)
}
controller = &cronJobController{
obj: cj,
Expand Down Expand Up @@ -322,9 +322,11 @@ func (action *monitorAction) updateIntegrationPhaseAndReadyCondition(
readyPods, unreadyPods := filterPodsByReadyStatus(environment, runningPods, controller.getPodSpec())

if done, err := controller.checkReadyCondition(ctx); done || err != nil {
// There may be pods that are not ready but still probable for getting error messages.
// Ignore returned error from probing as it's expected when the ctrl obj is not ready.
_ = action.probeReadiness(ctx, environment, integration, unreadyPods, readyPods)
if len(readyPods) > 0 || len(unreadyPods) > 0 {
// There may be pods that are not ready but still probable for getting error messages.
// Ignore returned error from probing as it's expected when the ctrl obj is not ready.
_ = action.probeReadiness(ctx, environment, integration, unreadyPods, readyPods)
}
return err
}
if done := checkPodStatuses(integration, pendingPods, runningPods); done {
Expand Down
13 changes: 10 additions & 3 deletions pkg/controller/integration/monitor_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,16 @@ var _ controller = &deploymentController{}

func (c *deploymentController) checkReadyCondition(ctx context.Context) (bool, error) {
// Check the Deployment progression
if progressing := kubernetes.GetDeploymentCondition(*c.obj, appsv1.DeploymentProgressing); progressing != nil &&
progressing.Status == corev1.ConditionFalse &&
progressing.Reason == "ProgressDeadlineExceeded" {
progressing := kubernetes.GetDeploymentCondition(*c.obj, appsv1.DeploymentProgressing)
replicaFailure := kubernetes.GetDeploymentCondition(*c.obj, appsv1.DeploymentReplicaFailure)

if replicaFailure != nil && replicaFailure.Status == corev1.ConditionTrue {
c.integration.Status.Phase = v1.IntegrationPhaseError
c.integration.SetReadyConditionError(replicaFailure.Message)
return true, nil
}

if progressing != nil && progressing.Status == corev1.ConditionFalse && progressing.Reason == "ProgressDeadlineExceeded" {
c.integration.Status.Phase = v1.IntegrationPhaseError
c.integration.SetReadyConditionError(progressing.Message)
return true, nil
Expand Down

0 comments on commit 1449c19

Please sign in to comment.