Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-v2.1] Fix finalizer removal on interrupt #72

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/terraformer/terraformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,12 @@ func (t *Terraformer) execute(command Command) (rErr error) {

// after a successful execution of destroy command, remove the finalizers from the resources
if command == Destroy {
if err := t.removeFinalizer(ctx); err != nil {
// root context might have been cancelled during terraform execution, but execution was still successful.
// use a new background context here, otherwise the finalizers can't be removed
finalizerCtx, finalizerCancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer finalizerCancel()

if err := t.removeFinalizer(finalizerCtx); err != nil {
return fmt.Errorf("error removing finalizers: %w", err)
}
}
Expand Down
67 changes: 67 additions & 0 deletions test/e2e/binary/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import (
testutils "github.com/gardener/terraformer/test/utils"
)

const (
expectedFinalizer = "gardener.cloud/terraformer"
)

var _ = Describe("terraformer", func() {
var (
writer io.Writer
Expand Down Expand Up @@ -314,6 +318,69 @@ var _ = Describe("terraformer", func() {
})
})

Describe("finalizer handling", func() {
BeforeEach(func() {
overwriteSleepDuration = testutils.OverwriteSleepDuration("200ms")

args = append(args,
"--namespace="+testObjs.Namespace,
"--configuration-configmap-name="+testObjs.ConfigurationConfigMap.Name,
"--state-configmap-name="+testObjs.StateConfigMap.Name,
"--variables-secret-name="+testObjs.VariablesSecret.Name,
"--kubeconfig="+kubeconfigFile,
)
})

It("should add finalizer on apply", func() {
cmd := exec.Command(pathToTerraformer, append(args, "apply")...)
cmd.Env = fakeTerraform.TerraformerEnv()

session, err := gexec.Start(cmd, writer, writer)
Expect(err).NotTo(HaveOccurred())

Eventually(session.Err).Should(Say("successfully updated finalizers"))

Eventually(session.Err).Should(Say("terraform process finished successfully"))
Eventually(session).Should(gexec.Exit(0))

testObjs.Refresh()
Expect(testObjs.ConfigurationConfigMap.Finalizers).To(ContainElement(expectedFinalizer))
Expect(testObjs.StateConfigMap.Finalizers).To(ContainElement(expectedFinalizer))
Expect(testObjs.VariablesSecret.Finalizers).To(ContainElement(expectedFinalizer))
})

It("should remove finalizer on destroy even when interrupted", func() {
cmd := exec.Command(pathToTerraformer, append(args, "destroy")...)
cmd.Env = fakeTerraform.TerraformerEnv()

session, err := gexec.Start(cmd, writer, writer)
Expect(err).NotTo(HaveOccurred())

// wait for finalizers to be added
Eventually(session.Err).Should(Say("successfully updated finalizers"))
testObjs.Refresh()
Expect(testObjs.ConfigurationConfigMap.Finalizers).To(ContainElement(expectedFinalizer))
Expect(testObjs.StateConfigMap.Finalizers).To(ContainElement(expectedFinalizer))
Expect(testObjs.VariablesSecret.Finalizers).To(ContainElement(expectedFinalizer))

// interrupt during running terraform op
Eventually(session.Err).Should(Say("doing some long running IaaS ops"))
session.Interrupt()
Eventually(session.Err).Should(Say("interrupt received"))

// wait for terraformer to exit
Eventually(session.Err).Should(Say("terraform process finished successfully"))
Eventually(session.Err).Should(Say("successfully updated finalizers"))
Eventually(session).Should(gexec.Exit(0))

// expect that finalizers are removed after successful terraform execution
testObjs.Refresh()
Expect(testObjs.ConfigurationConfigMap.Finalizers).NotTo(ContainElement(expectedFinalizer))
Expect(testObjs.StateConfigMap.Finalizers).NotTo(ContainElement(expectedFinalizer))
Expect(testObjs.VariablesSecret.Finalizers).NotTo(ContainElement(expectedFinalizer))
})
})

Describe("exit code handling", func() {
runExitCodeTest := func(command string, expectedExitCode int) {
args = append(args,
Expand Down
11 changes: 11 additions & 0 deletions test/utils/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ func (o *TestObjects) CleanupTestObjects(ctx context.Context) {

// Refresh retrieves a fresh copy of the objects from the API server, so that tests can make assertions on them.
func (o *TestObjects) Refresh() {
// reset all objects, json encoder doesn't reset removed fields on in-memory objects (e.g. finalizers)
o.ConfigurationConfigMap = &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: o.ConfigurationConfigMap.Name, Namespace: o.Namespace},
}
o.StateConfigMap = &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: o.StateConfigMap.Name, Namespace: o.Namespace},
}
o.VariablesSecret = &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: o.VariablesSecret.Name, Namespace: o.Namespace},
}

Expect(o.client.Get(o.ctx, ObjectKeyFromObject(o.ConfigurationConfigMap), o.ConfigurationConfigMap)).To(Succeed())
Expect(o.client.Get(o.ctx, ObjectKeyFromObject(o.StateConfigMap), o.StateConfigMap)).To(Succeed())
Expect(o.client.Get(o.ctx, ObjectKeyFromObject(o.VariablesSecret), o.VariablesSecret)).To(Succeed())
Expand Down