/
deletionprotection.go
301 lines (266 loc) · 13.1 KB
/
deletionprotection.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
Copyright 2021 The Kruise Authors.
Licensed 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 policy
import (
"context"
"fmt"
"time"
"github.com/onsi/ginkgo"
"github.com/onsi/gomega"
appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
policyv1alpha1 "github.com/openkruise/kruise/apis/policy/v1alpha1"
kruiseclientset "github.com/openkruise/kruise/pkg/client/clientset/versioned"
"github.com/openkruise/kruise/test/e2e/framework"
apps "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/dynamic"
clientset "k8s.io/client-go/kubernetes"
utilpointer "k8s.io/utils/pointer"
)
const (
deleteForbiddenMessage = "forbidden by ResourcesProtectionDeletion"
)
var _ = SIGDescribe("DeletionProtection", func() {
f := framework.NewDefaultFramework("deletionprotection")
var ns string
var c clientset.Interface
var kc kruiseclientset.Interface
var ec apiextensionsclientset.Interface
var dc dynamic.Interface
var randStr string
ginkgo.BeforeEach(func() {
c = f.ClientSet
kc = f.KruiseClientSet
ec = f.ApiExtensionsClientSet
dc = f.DynamicClient
ns = f.Namespace.Name
randStr = rand.String(10)
_ = ns
})
framework.KruiseDescribe("namespace deletion", func() {
ginkgo.It("should be protected", func() {
ns := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{
Name: "kruise-e2e-deletion-protection-" + randStr,
Labels: map[string]string{policyv1alpha1.DeletionProtectionKey: policyv1alpha1.DeletionProtectionTypeAlways},
}}
ginkgo.By("Create test namespace " + ns.Name + " with Always")
_, err := c.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
ginkgo.By("Delete the namespace should be rejected")
err = c.CoreV1().Namespaces().Delete(context.TODO(), ns.Name, metav1.DeleteOptions{})
gomega.Expect(err).To(gomega.HaveOccurred())
gomega.Expect(err.Error()).Should(gomega.ContainSubstring(deleteForbiddenMessage))
ginkgo.By("Create a CloneSet in this namespace and wait for pod created")
tester := framework.NewCloneSetTester(c, kc, ns.Name)
cs := tester.NewCloneSet("clone-"+randStr, 1, appsv1alpha1.CloneSetUpdateStrategy{})
cs, err = tester.CreateCloneSet(cs)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Eventually(func() int32 {
cs, err = tester.GetCloneSet(cs.Name)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
return cs.Status.Replicas
}, 5*time.Second, time.Second).Should(gomega.Equal(int32(1)))
ginkgo.By("Patch the namespace deletion to Cascading")
_, err = c.CoreV1().Namespaces().Patch(context.TODO(), ns.Name, types.StrategicMergePatchType,
[]byte(fmt.Sprintf(`{"metadata":{"labels":{"%s":"%s"}}}`, policyv1alpha1.DeletionProtectionKey, policyv1alpha1.DeletionProtectionTypeCascading)), metav1.PatchOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
ginkgo.By("Delete the namespace should be rejected")
err = c.CoreV1().Namespaces().Delete(context.TODO(), ns.Name, metav1.DeleteOptions{})
gomega.Expect(err).To(gomega.HaveOccurred())
gomega.Expect(err.Error()).Should(gomega.ContainSubstring(deleteForbiddenMessage))
ginkgo.By("Scale CloneSet replicas to 0")
err = tester.UpdateCloneSet(cs.Name, func(cs *appsv1alpha1.CloneSet) {
cs.Spec.Replicas = utilpointer.Int32Ptr(0)
})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Eventually(func() int32 {
cs, err = tester.GetCloneSet(cs.Name)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
return cs.Status.Replicas
}, 5*time.Second, time.Second).Should(gomega.Equal(int32(0)))
time.Sleep(time.Second)
ginkgo.By("Delete the namespace successfully")
err = c.CoreV1().Namespaces().Delete(context.TODO(), ns.Name, metav1.DeleteOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
})
})
framework.KruiseDescribe("CRD deletion", func() {
ginkgo.It("should be protected", func() {
crd := &apiextensionsv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: "testdeletioncases.e2e.kruise.io",
Labels: map[string]string{policyv1alpha1.DeletionProtectionKey: policyv1alpha1.DeletionProtectionTypeAlways},
},
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
Group: "e2e.kruise.io",
Scope: apiextensionsv1.NamespaceScoped,
Names: apiextensionsv1.CustomResourceDefinitionNames{Plural: "testdeletioncases", Singular: "testdeletioncase", Kind: "TestDeletionCase"},
Versions: []apiextensionsv1.CustomResourceDefinitionVersion{{
Name: "v1beta1",
Schema: &apiextensionsv1.CustomResourceValidation{OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{
Type: "object",
Properties: apiextensionsv1.JSONSchemaDefinitions{
"apiVersion": apiextensionsv1.JSONSchemaProps{Type: "string"},
"kind": apiextensionsv1.JSONSchemaProps{Type: "string"},
"metadata": apiextensionsv1.JSONSchemaProps{Type: "object"},
},
}},
Storage: true,
Served: true,
}},
},
}
ginkgo.By("Create test CRD " + crd.Name + " with Always")
_, err := ec.ApiextensionsV1().CustomResourceDefinitions().Create(context.TODO(), crd, metav1.CreateOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
ginkgo.By("Delete the CRD should be rejected")
err = ec.ApiextensionsV1().CustomResourceDefinitions().Delete(context.TODO(), crd.Name, metav1.DeleteOptions{})
gomega.Expect(err).To(gomega.HaveOccurred())
gomega.Expect(err.Error()).Should(gomega.ContainSubstring(deleteForbiddenMessage))
ginkgo.By("Create a test CR")
time.Sleep(time.Second)
obj := &unstructured.Unstructured{}
obj.SetAPIVersion("e2e.kruise.io/v1beta1")
obj.SetKind("TestDeletionCase")
obj.SetNamespace(ns)
obj.SetName("test-cr-" + randStr)
obj, err = dc.Resource(schema.GroupVersionResource{Group: "e2e.kruise.io", Version: "v1beta1", Resource: "testdeletioncases"}).Namespace(ns).Create(context.TODO(), obj, metav1.CreateOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
ginkgo.By("Patch the CRD deletion to Cascading")
_, err = ec.ApiextensionsV1().CustomResourceDefinitions().Patch(context.TODO(), crd.Name, types.StrategicMergePatchType,
[]byte(fmt.Sprintf(`{"metadata":{"labels":{"%s":"%s"}}}`, policyv1alpha1.DeletionProtectionKey, policyv1alpha1.DeletionProtectionTypeCascading)), metav1.PatchOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
ginkgo.By("Delete the CRD should be rejected")
err = ec.ApiextensionsV1().CustomResourceDefinitions().Delete(context.TODO(), crd.Name, metav1.DeleteOptions{})
gomega.Expect(err).To(gomega.HaveOccurred())
gomega.Expect(err.Error()).Should(gomega.ContainSubstring(deleteForbiddenMessage))
ginkgo.By("Delete the test CR")
err = dc.Resource(schema.GroupVersionResource{Group: "e2e.kruise.io", Version: "v1beta1", Resource: "testdeletioncases"}).Namespace(ns).Delete(context.TODO(), obj.GetName(), metav1.DeleteOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
ginkgo.By("Delete the CRD should be successful")
err = ec.ApiextensionsV1().CustomResourceDefinitions().Delete(context.TODO(), crd.Name, metav1.DeleteOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
})
})
framework.KruiseDescribe("Workload deletion", func() {
var err error
ginkgo.It("CloneSet should be protected", func() {
ginkgo.By("Create a CloneSet with Always")
tester := framework.NewCloneSetTester(c, kc, ns)
cs := tester.NewCloneSet("clone-"+randStr, 0, appsv1alpha1.CloneSetUpdateStrategy{})
cs.Labels = map[string]string{policyv1alpha1.DeletionProtectionKey: policyv1alpha1.DeletionProtectionTypeAlways}
cs, err = tester.CreateCloneSet(cs)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
ginkgo.By("Delete the CloneSet should be rejected")
err = tester.DeleteCloneSet(cs.Name)
gomega.Expect(err).To(gomega.HaveOccurred())
gomega.Expect(err.Error()).Should(gomega.ContainSubstring(deleteForbiddenMessage))
ginkgo.By("Scale CloneSet replicas to 2 and protection to Cascading")
err = tester.UpdateCloneSet(cs.Name, func(cs *appsv1alpha1.CloneSet) {
cs.Spec.Replicas = utilpointer.Int32Ptr(2)
cs.Labels[policyv1alpha1.DeletionProtectionKey] = policyv1alpha1.DeletionProtectionTypeCascading
})
gomega.Eventually(func() int32 {
cs, err = tester.GetCloneSet(cs.Name)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
return cs.Status.Replicas
}, 5*time.Second, time.Second).Should(gomega.Equal(int32(2)))
ginkgo.By("Delete the CloneSet should be rejected")
err = tester.DeleteCloneSet(cs.Name)
gomega.Expect(err).To(gomega.HaveOccurred())
gomega.Expect(err.Error()).Should(gomega.ContainSubstring(deleteForbiddenMessage))
ginkgo.By("Scale CloneSet replicas to 0")
err = tester.UpdateCloneSet(cs.Name, func(cs *appsv1alpha1.CloneSet) {
cs.Spec.Replicas = utilpointer.Int32Ptr(0)
})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Eventually(func() int32 {
cs, err = tester.GetCloneSet(cs.Name)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
return cs.Status.Replicas
}, 5*time.Second, time.Second).Should(gomega.Equal(int32(0)))
ginkgo.By("Delete the CloneSet should be successful")
err = tester.DeleteCloneSet(cs.Name)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
})
ginkgo.It("Deployment should be protected", func() {
ginkgo.By("Create a Deployment with Always")
name := "deploy-" + randStr
deploy := &apps.Deployment{
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: name,
Labels: map[string]string{policyv1alpha1.DeletionProtectionKey: policyv1alpha1.DeletionProtectionTypeAlways},
},
Spec: apps.DeploymentSpec{
Replicas: utilpointer.Int32Ptr(0),
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"owner": name}},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"owner": name},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "nginx",
Image: "nginx:1.9.1",
Env: []v1.EnvVar{
{Name: "test", Value: "foo"},
},
},
},
},
},
},
}
deploy, err = c.AppsV1().Deployments(ns).Create(context.TODO(), deploy, metav1.CreateOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
ginkgo.By("Delete the Deployment should be rejected")
err = c.AppsV1().Deployments(ns).Delete(context.TODO(), deploy.Name, metav1.DeleteOptions{})
gomega.Expect(err).To(gomega.HaveOccurred())
gomega.Expect(err.Error()).Should(gomega.ContainSubstring(deleteForbiddenMessage))
ginkgo.By("Scale Deployment replicas to 2 and protection to Cascading")
deploy, err = c.AppsV1().Deployments(ns).Patch(context.TODO(), deploy.Name, types.StrategicMergePatchType,
[]byte(fmt.Sprintf(`{"metadata":{"labels":{"%s":"%s"}},"spec":{"replicas":%d}}`, policyv1alpha1.DeletionProtectionKey, policyv1alpha1.DeletionProtectionTypeCascading, 2)), metav1.PatchOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Eventually(func() int32 {
deploy, err = c.AppsV1().Deployments(ns).Get(context.TODO(), deploy.Name, metav1.GetOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
return deploy.Status.Replicas
}, 5*time.Second, time.Second).Should(gomega.Equal(int32(2)))
ginkgo.By("Delete the Deployment should be rejected")
err = c.AppsV1().Deployments(ns).Delete(context.TODO(), deploy.Name, metav1.DeleteOptions{})
gomega.Expect(err).To(gomega.HaveOccurred())
gomega.Expect(err.Error()).Should(gomega.ContainSubstring(deleteForbiddenMessage))
ginkgo.By("Scale Deployment replicas to 0")
deploy, err = c.AppsV1().Deployments(ns).Patch(context.TODO(), deploy.Name, types.StrategicMergePatchType,
[]byte(fmt.Sprintf(`{"spec":{"replicas":%d}}`, 0)), metav1.PatchOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Eventually(func() int32 {
deploy, err = c.AppsV1().Deployments(ns).Get(context.TODO(), deploy.Name, metav1.GetOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
return deploy.Status.Replicas
}, 5*time.Second, time.Second).Should(gomega.Equal(int32(0)))
ginkgo.By("Delete the Deployment should successful")
err = c.AppsV1().Deployments(ns).Delete(context.TODO(), deploy.Name, metav1.DeleteOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
})
})
})