-
Notifications
You must be signed in to change notification settings - Fork 15
/
btpoperator_controller_deprovisioning_test.go
126 lines (107 loc) · 5.68 KB
/
btpoperator_controller_deprovisioning_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package controllers
import (
"github.com/kyma-project/btp-manager/api/v1alpha1"
"github.com/kyma-project/btp-manager/internal/conditions"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
instanceName = "my-service-instance"
bindingName = "my-service-binding"
)
var _ = Describe("BTP Operator controller - deprovisioning", func() {
var cr *v1alpha1.BtpOperator
Describe("Deprovisioning without force-delete label", func() {
BeforeEach(func() {
GinkgoWriter.Println("--- PROCESS:", GinkgoParallelProcess(), "---")
secret, err := createCorrectSecretFromYaml()
Expect(err).To(BeNil())
Expect(k8sClient.Patch(ctx, secret, client.Apply, client.ForceOwnership, client.FieldOwner(operatorName))).To(Succeed())
cr = createDefaultBtpOperator()
Expect(k8sClient.Create(ctx, cr)).To(Succeed())
Eventually(updateCh).Should(Receive(matchState(v1alpha1.StateReady)))
btpServiceOperatorDeployment := &appsv1.Deployment{}
Expect(k8sClient.Get(ctx, client.ObjectKey{Name: DeploymentName, Namespace: kymaNamespace}, btpServiceOperatorDeployment)).Should(Succeed())
})
AfterEach(func() {
Expect(k8sClient.Get(ctx, client.ObjectKey{Namespace: defaultNamespace, Name: btpOperatorName}, cr)).To(Succeed())
cr.SetLabels(map[string]string{forceDeleteLabelKey: "true"})
Expect(k8sClient.Update(ctx, cr)).To(Succeed())
Eventually(func() (bool, error) {
err := k8sClient.Get(ctx, client.ObjectKey{Namespace: defaultNamespace, Name: btpOperatorName}, cr)
return cr.Labels[forceDeleteLabelKey] == "true", err
}).WithTimeout(k8sOpsTimeout).WithPolling(k8sOpsPollingInterval).Should(BeTrue())
Eventually(updateCh).Should(Receive(matchDeleted()))
deleteSecret := &corev1.Secret{}
Expect(k8sClient.Get(ctx, client.ObjectKey{Namespace: kymaNamespace, Name: SecretName}, deleteSecret)).To(Succeed())
Expect(k8sClient.Delete(ctx, deleteSecret)).To(Succeed())
})
It("Delete should fail because of existing instances and bindings", func() {
_ = createResource(instanceGvk, kymaNamespace, instanceName)
ensureResourceExists(instanceGvk)
_ = createResource(bindingGvk, kymaNamespace, bindingName)
ensureResourceExists(bindingGvk)
Expect(k8sClient.Get(ctx, client.ObjectKey{Namespace: defaultNamespace, Name: btpOperatorName}, cr)).To(Succeed())
Expect(k8sClient.Delete(ctx, cr)).To(Succeed())
Eventually(updateCh).Should(Receive(matchReadyCondition(v1alpha1.StateWarning, metav1.ConditionFalse, conditions.ServiceInstancesAndBindingsNotCleaned)))
})
})
Describe("Deprovisioning with force-delete label", func() {
var siUnstructured, sbUnstructured *unstructured.Unstructured
BeforeEach(func() {
GinkgoWriter.Println("--- PROCESS:", GinkgoParallelProcess(), "---")
secret, err := createCorrectSecretFromYaml()
Expect(err).To(BeNil())
Expect(k8sClient.Patch(ctx, secret, client.Apply, client.ForceOwnership, client.FieldOwner(operatorName))).To(Succeed())
cr = createDefaultBtpOperator()
cr.SetLabels(map[string]string{forceDeleteLabelKey: "true"})
Expect(k8sClient.Create(ctx, cr)).To(Succeed())
Eventually(updateCh).Should(Receive(matchState(v1alpha1.StateReady)))
btpServiceOperatorDeployment := &appsv1.Deployment{}
Expect(k8sClient.Get(ctx, client.ObjectKey{Name: DeploymentName, Namespace: kymaNamespace}, btpServiceOperatorDeployment)).Should(Succeed())
siUnstructured = createResource(instanceGvk, kymaNamespace, instanceName)
ensureResourceExists(instanceGvk)
sbUnstructured = createResource(bindingGvk, kymaNamespace, bindingName)
ensureResourceExists(bindingGvk)
})
AfterEach(func() {
deleteSecret := &corev1.Secret{}
Expect(k8sClient.Get(ctx, client.ObjectKey{Namespace: kymaNamespace, Name: SecretName}, deleteSecret)).To(Succeed())
Expect(k8sClient.Delete(ctx, deleteSecret)).To(Succeed())
})
It("soft delete (after timeout) should succeed", func() {
reconciler.Client = newTimeoutK8sClient(reconciler.Client)
setFinalizers(siUnstructured)
setFinalizers(sbUnstructured)
Expect(k8sClient.Get(ctx, client.ObjectKey{Namespace: defaultNamespace, Name: btpOperatorName}, cr)).To(Succeed())
Expect(k8sClient.Delete(ctx, cr)).To(Succeed())
Eventually(updateCh).Should(Receive(matchReadyCondition(v1alpha1.StateDeleting, metav1.ConditionFalse, conditions.HardDeleting)))
Eventually(updateCh).Should(Receive(matchReadyCondition(v1alpha1.StateDeleting, metav1.ConditionFalse, conditions.SoftDeleting)))
Eventually(updateCh).Should(Receive(matchDeleted()))
doChecks()
})
It("soft delete (after hard deletion fail) should succeed", func() {
reconciler.Client = newErrorK8sClient(reconciler.Client)
setFinalizers(siUnstructured)
setFinalizers(sbUnstructured)
Expect(k8sClient.Get(ctx, client.ObjectKey{Namespace: defaultNamespace, Name: btpOperatorName}, cr)).To(Succeed())
Expect(k8sClient.Delete(ctx, cr)).To(Succeed())
Eventually(updateCh).Should(Receive(matchReadyCondition(v1alpha1.StateDeleting, metav1.ConditionFalse, conditions.SoftDeleting)))
Eventually(updateCh).Should(Receive(matchDeleted()))
doChecks()
})
It("hard delete should succeed", func() {
reconciler.Client = k8sClientFromManager
Expect(k8sClient.Get(ctx, client.ObjectKey{Namespace: defaultNamespace, Name: btpOperatorName}, cr)).To(Succeed())
Expect(k8sClient.Delete(ctx, cr)).Should(Succeed())
Eventually(updateCh).Should(Receive(matchReadyCondition(v1alpha1.StateDeleting, metav1.ConditionFalse, conditions.HardDeleting)))
Eventually(updateCh).Should(Receive(matchDeleted()))
doChecks()
})
})
})