-
Notifications
You must be signed in to change notification settings - Fork 39.7k
/
runtimeclass.go
188 lines (161 loc) · 6.74 KB
/
runtimeclass.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
/*
Copyright 2018 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 node
import (
"fmt"
"time"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/kubernetes/pkg/kubelet/events"
runtimeclasstest "k8s.io/kubernetes/pkg/kubelet/runtimeclass/testing"
"k8s.io/kubernetes/test/e2e/framework"
imageutils "k8s.io/kubernetes/test/utils/image"
utilpointer "k8s.io/utils/pointer"
. "github.com/onsi/ginkgo"
)
const runtimeClassCRDName = "runtimeclasses.node.k8s.io"
var (
runtimeClassGVR = schema.GroupVersionResource{
Group: "node.k8s.io",
Version: "v1alpha1",
Resource: "runtimeclasses",
}
)
var _ = SIGDescribe("RuntimeClass [Feature:RuntimeClass]", func() {
f := framework.NewDefaultFramework("runtimeclass")
It("should reject a Pod requesting a non-existent RuntimeClass", func() {
rcName := f.Namespace.Name + "-nonexistent"
pod := createRuntimeClassPod(f, rcName)
expectSandboxFailureEvent(f, pod, fmt.Sprintf("\"%s\" not found", rcName))
})
It("should run a Pod requesting a RuntimeClass with an empty handler", func() {
rcName := createRuntimeClass(f, "empty-handler", "")
pod := createRuntimeClassPod(f, rcName)
expectPodSuccess(f, pod)
})
It("should reject a Pod requesting a RuntimeClass with an unconfigured handler", func() {
handler := f.Namespace.Name + "-handler"
rcName := createRuntimeClass(f, "unconfigured-handler", handler)
pod := createRuntimeClassPod(f, rcName)
expectSandboxFailureEvent(f, pod, handler)
})
It("should reject a Pod requesting a deleted RuntimeClass", func() {
rcName := createRuntimeClass(f, "delete-me", "")
By("Deleting RuntimeClass "+rcName, func() {
err := f.DynamicClient.Resource(runtimeClassGVR).Delete(rcName, nil)
framework.ExpectNoError(err, "failed to delete RuntimeClass %s", rcName)
By("Waiting for the RuntimeClass to disappear")
framework.ExpectNoError(wait.PollImmediate(framework.Poll, time.Minute, func() (bool, error) {
_, err := f.DynamicClient.Resource(runtimeClassGVR).Get(rcName, metav1.GetOptions{})
if errors.IsNotFound(err) {
return true, nil // done
}
if err != nil {
return true, err // stop wait with error
}
return false, nil
}))
})
pod := createRuntimeClassPod(f, rcName)
expectSandboxFailureEvent(f, pod, fmt.Sprintf("\"%s\" not found", rcName))
})
It("should recover when the RuntimeClass CRD is deleted [Slow]", func() {
By("Deleting the RuntimeClass CRD", func() {
crds := f.APIExtensionsClientSet.ApiextensionsV1beta1().CustomResourceDefinitions()
runtimeClassCRD, err := crds.Get(runtimeClassCRDName, metav1.GetOptions{})
framework.ExpectNoError(err, "failed to get RuntimeClass CRD %s", runtimeClassCRDName)
runtimeClassCRDUID := runtimeClassCRD.GetUID()
err = crds.Delete(runtimeClassCRDName, nil)
framework.ExpectNoError(err, "failed to delete RuntimeClass CRD %s", runtimeClassCRDName)
By("Waiting for the CRD to disappear")
framework.ExpectNoError(wait.PollImmediate(framework.Poll, time.Minute, func() (bool, error) {
crd, err := crds.Get(runtimeClassCRDName, metav1.GetOptions{})
if errors.IsNotFound(err) {
return true, nil // done
}
if err != nil {
return true, err // stop wait with error
}
// If the UID changed, that means the addon manager has already recreated it.
return crd.GetUID() != runtimeClassCRDUID, nil
}))
By("Waiting for the CRD to be recreated")
framework.ExpectNoError(wait.PollImmediate(framework.Poll, 5*time.Minute, func() (bool, error) {
crd, err := crds.Get(runtimeClassCRDName, metav1.GetOptions{})
if errors.IsNotFound(err) {
return false, nil // still not recreated
}
if err != nil {
return true, err // stop wait with error
}
if crd.GetUID() == runtimeClassCRDUID {
return true, fmt.Errorf("RuntimeClass CRD never deleted") // this shouldn't happen
}
return true, nil
}))
})
rcName := createRuntimeClass(f, "valid", "")
pod := createRuntimeClassPod(f, rcName)
expectPodSuccess(f, pod)
})
// TODO(tallclair): Test an actual configured non-default runtimeHandler.
})
// createRuntimeClass generates a RuntimeClass with the desired handler and a "namespaced" name,
// synchronously creates it with the dynamic client, and returns the resulting name.
func createRuntimeClass(f *framework.Framework, name, handler string) string {
uniqueName := fmt.Sprintf("%s-%s", f.Namespace.Name, name)
rc := runtimeclasstest.NewUnstructuredRuntimeClass(uniqueName, handler)
rc, err := f.DynamicClient.Resource(runtimeClassGVR).Create(rc, metav1.CreateOptions{})
framework.ExpectNoError(err, "failed to create RuntimeClass resource")
return rc.GetName()
}
// createRuntimeClass creates a test pod with the given runtimeClassName.
func createRuntimeClassPod(f *framework.Framework, runtimeClassName string) *v1.Pod {
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: fmt.Sprintf("test-runtimeclass-%s-", runtimeClassName),
},
Spec: v1.PodSpec{
RuntimeClassName: &runtimeClassName,
Containers: []v1.Container{{
Name: "test",
Image: imageutils.GetE2EImage(imageutils.BusyBox),
Command: []string{"true"},
}},
RestartPolicy: v1.RestartPolicyNever,
AutomountServiceAccountToken: utilpointer.BoolPtr(false),
},
}
return f.PodClient().Create(pod)
}
// expectPodSuccess waits for the given pod to terminate successfully.
func expectPodSuccess(f *framework.Framework, pod *v1.Pod) {
framework.ExpectNoError(framework.WaitForPodSuccessInNamespace(
f.ClientSet, pod.Name, f.Namespace.Name))
}
// expectSandboxFailureEvent polls for an event with reason "FailedCreatePodSandBox" containing the
// expected message string.
func expectSandboxFailureEvent(f *framework.Framework, pod *v1.Pod, msg string) {
eventSelector := fields.Set{
"involvedObject.kind": "Pod",
"involvedObject.name": pod.Name,
"involvedObject.namespace": f.Namespace.Name,
"reason": events.FailedCreatePodSandBox,
}.AsSelector().String()
framework.ExpectNoError(framework.WaitTimeoutForPodEvent(
f.ClientSet, pod.Name, f.Namespace.Name, eventSelector, msg, framework.PodEventTimeout))
}