From 5b4d7f393dcb9d58433c18de827e0a9a7d343050 Mon Sep 17 00:00:00 2001 From: Philip Thompson Date: Wed, 9 Oct 2024 09:14:33 -0400 Subject: [PATCH] Safely remove injector pods that never started (#928) * Safely remove injector pods that never started * pointers can be nil, apparently * Add a unit test --- services/chaospod.go | 6 ++++++ services/chaospod_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/services/chaospod.go b/services/chaospod.go index b2d03a6cd..765a1f7f7 100644 --- a/services/chaospod.go +++ b/services/chaospod.go @@ -187,6 +187,12 @@ func (m *chaosPodService) HandleChaosPodTermination(ctx context.Context, disrupt continue } + // if the injector container definitively did not start, we can remove the finalizer + if cs.Started != nil && !(*cs.Started) { + shouldRemoveFinalizer = true + } + + // if the injector container was terminated due to a start error, it can't have injected, we can remove the finalizer if cs.State.Terminated != nil && cs.State.Terminated.Reason == "StartError" { shouldRemoveFinalizer = true } diff --git a/services/chaospod_test.go b/services/chaospod_test.go index 31cfb1372..8bce948cb 100644 --- a/services/chaospod_test.go +++ b/services/chaospod_test.go @@ -19,6 +19,7 @@ import ( "github.com/DataDog/chaos-controller/services" "github.com/DataDog/chaos-controller/targetselector" chaostypes "github.com/DataDog/chaos-controller/types" + . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "github.com/stretchr/testify/mock" @@ -243,6 +244,7 @@ var _ = Describe("Chaos Pod Service", func() { var ( isStuckOnRemoval bool + falseStarted bool cpBuilder *builderstest.ChaosPodBuilder ) @@ -250,6 +252,7 @@ var _ = Describe("Chaos Pod Service", func() { // Arrange cpBuilder = builderstest.NewPodBuilder("test-1", DefaultChaosNamespace) targetSelectorMock.EXPECT().TargetIsHealthy(mock.Anything, mock.Anything, mock.Anything).Return(nil).Maybe() + falseStarted = false }) JustBeforeEach(func() { @@ -304,6 +307,27 @@ var _ = Describe("Chaos Pod Service", func() { "test", DefaultNamespace, ).WithStatusPhase(v1.PodFailed)), + Entry( + "with a failed pod that never started", + builderstest.NewPodBuilder( + "test", + DefaultNamespace, + ).WithStatus(v1.PodStatus{ + Phase: v1.PodFailed, + Reason: "PodFailed", + ContainerStatuses: []v1.ContainerStatus{ + { + Name: "injector", + State: v1.ContainerState{ + Terminated: &v1.ContainerStateTerminated{ + Reason: "ContainerStatusUnknown", + ExitCode: 137, + }, + }, + Started: &falseStarted, + }, + }, + })), Entry( "with failed a pod exceeding its activeDeadlineSeconds", builderstest.NewPodBuilder(