forked from k3s-io/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespace.go
236 lines (206 loc) · 8.15 KB
/
namespace.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
/*
Copyright 2016 The Kubernetes 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 e2e_federation
import (
"fmt"
"strings"
"time"
"k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
clientset "k8s.io/kubernetes/federation/client/clientset_generated/federation_clientset/typed/core/v1"
k8s_api_v1 "k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/test/e2e/framework"
fedframework "k8s.io/kubernetes/test/e2e_federation/framework"
. "github.com/onsi/ginkgo"
)
const (
eventNamePrefix = "e2e-namespace-test-event-"
namespacePrefix = "e2e-namespace-test-"
replicaSetNamePrefix = "e2e-namespace-test-rs-"
)
// Create/delete ingress api objects
var _ = framework.KubeDescribe("Federation namespace [Feature:Federation]", func() {
f := fedframework.NewDefaultFederatedFramework("federation-namespace")
Describe("Namespace objects", func() {
var clusters fedframework.ClusterSlice
var nsName string
BeforeEach(func() {
fedframework.SkipUnlessFederated(f.ClientSet)
clusters = f.GetRegisteredClusters()
})
AfterEach(func() {
fedframework.SkipUnlessFederated(f.ClientSet)
deleteNamespace(nil, nsName,
f.FederationClientset.Core().Namespaces().Get,
f.FederationClientset.Core().Namespaces().Delete)
for _, cluster := range clusters {
deleteNamespace(nil, nsName,
cluster.CoreV1().Namespaces().Get,
cluster.CoreV1().Namespaces().Delete)
}
})
// See https://github.com/kubernetes/kubernetes/issues/38225
It("deletes replicasets in the namespace when the namespace is deleted", func() {
fedframework.SkipUnlessFederated(f.ClientSet)
nsName = createNamespace(f.FederationClientset.Core().Namespaces())
rsName := k8s_api_v1.SimpleNameGenerator.GenerateName(replicaSetNamePrefix)
replicaCount := int32(2)
rs := &v1beta1.ReplicaSet{
ObjectMeta: metav1.ObjectMeta{
Name: rsName,
Namespace: nsName,
},
Spec: v1beta1.ReplicaSetSpec{
Replicas: &replicaCount,
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"name": "myrs"},
},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"name": "myrs"},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "nginx",
Image: "nginx",
},
},
},
},
},
}
By(fmt.Sprintf("Creating replicaset %s in namespace %s", rsName, nsName))
_, err := f.FederationClientset.Extensions().ReplicaSets(nsName).Create(rs)
if err != nil {
framework.Failf("Failed to create replicaset %v in namespace %s, err: %s", rs, nsName, err)
}
By(fmt.Sprintf("Deleting namespace %s", nsName))
deleteNamespace(nil, nsName,
f.FederationClientset.Core().Namespaces().Get,
f.FederationClientset.Core().Namespaces().Delete)
By(fmt.Sprintf("Verify that replicaset %s was deleted as well", rsName))
waitForReplicaSetToBeDeletedOrFail(f.FederationClientset, nsName, rsName)
})
It("all resources in the namespace should be deleted when namespace is deleted", func() {
fedframework.SkipUnlessFederated(f.ClientSet)
nsName = createNamespace(f.FederationClientset.Core().Namespaces())
// Create resources in the namespace.
event := v1.Event{
ObjectMeta: metav1.ObjectMeta{
Name: k8s_api_v1.SimpleNameGenerator.GenerateName(eventNamePrefix),
Namespace: nsName,
},
InvolvedObject: v1.ObjectReference{
Kind: "Pod",
Namespace: nsName,
Name: "sample-pod",
},
}
By(fmt.Sprintf("Creating event %s in namespace %s", event.Name, nsName))
_, err := f.FederationClientset.Core().Events(nsName).Create(&event)
if err != nil {
framework.Failf("Failed to create event %v in namespace %s, err: %s", event, nsName, err)
}
By(fmt.Sprintf("Deleting namespace %s", nsName))
deleteNamespace(nil, nsName,
f.FederationClientset.Core().Namespaces().Get,
f.FederationClientset.Core().Namespaces().Delete)
By(fmt.Sprintf("Verify that event %s was deleted as well", event.Name))
latestEvent, err := f.FederationClientset.Core().Events(nsName).Get(event.Name, metav1.GetOptions{})
if !errors.IsNotFound(err) {
framework.Failf("Event %s should have been deleted. Found: %v", event.Name, latestEvent)
}
By(fmt.Sprintf("Verified that deletion succeeded"))
})
})
})
// verifyNsCascadingDeletion verifies that namespaces are deleted from
// underlying clusters when orphan dependents is false and they are not
// deleted when orphan dependents is true.
func verifyNsCascadingDeletion(nsClient clientset.NamespaceInterface, clusters fedframework.ClusterSlice, orphanDependents *bool) string {
nsName := createNamespace(nsClient)
// Check subclusters if the namespace was created there.
By(fmt.Sprintf("Waiting for namespace %s to be created in all underlying clusters", nsName))
err := wait.Poll(5*time.Second, 2*time.Minute, func() (bool, error) {
for _, cluster := range clusters {
_, err := cluster.CoreV1().Namespaces().Get(nsName, metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
return false, err
}
if err != nil {
return false, nil
}
}
return true, nil
})
framework.ExpectNoError(err, "Not all namespaces created")
By(fmt.Sprintf("Deleting namespace %s", nsName))
deleteNamespace(orphanDependents, nsName, nsClient.Get, nsClient.Delete)
By(fmt.Sprintf("Verifying namespaces %s in underlying clusters", nsName))
errMessages := []string{}
// namespace should be present in underlying clusters unless orphanDependents is false.
shouldExist := orphanDependents == nil || *orphanDependents == true
for _, cluster := range clusters {
clusterName := cluster.Name
_, err := cluster.CoreV1().Namespaces().Get(nsName, metav1.GetOptions{})
if shouldExist && errors.IsNotFound(err) {
errMessages = append(errMessages, fmt.Sprintf("unexpected NotFound error for namespace %s in cluster %s, expected namespace to exist", nsName, clusterName))
} else if !shouldExist && !errors.IsNotFound(err) {
errMessages = append(errMessages, fmt.Sprintf("expected NotFound error for namespace %s in cluster %s, got error: %v", nsName, clusterName, err))
}
}
if len(errMessages) != 0 {
framework.Failf("%s", strings.Join(errMessages, "; "))
}
return nsName
}
func createNamespace(nsClient clientset.NamespaceInterface) string {
ns := v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: k8s_api_v1.SimpleNameGenerator.GenerateName(namespacePrefix),
},
}
By(fmt.Sprintf("Creating namespace %s", ns.Name))
_, err := nsClient.Create(&ns)
framework.ExpectNoError(err, "Failed to create namespace %s", ns.Name)
By(fmt.Sprintf("Created namespace %s", ns.Name))
return ns.Name
}
func deleteNamespace(orphanDependents *bool, namespace string, getter func(name string, options metav1.GetOptions) (*v1.Namespace, error), deleter func(string, *metav1.DeleteOptions) error) {
By(fmt.Sprintf("Deleting namespace: %s", namespace))
err := deleter(namespace, &metav1.DeleteOptions{OrphanDependents: orphanDependents})
if errors.IsNotFound(err) {
return
} else if err != nil {
framework.Failf("Failed to set %s for deletion: %v", namespace, err)
}
waitForNamespaceDeletion(namespace, getter)
}
func waitForNamespaceDeletion(namespace string, getter func(name string, options metav1.GetOptions) (*v1.Namespace, error)) {
err := wait.Poll(5*time.Second, 2*time.Minute, func() (bool, error) {
_, err := getter(namespace, metav1.GetOptions{})
if errors.IsNotFound(err) {
return true, nil
} else if err != nil {
return false, err
}
return false, nil
})
if err != nil {
framework.Failf("Namespaces not deleted: %v", err)
}
}