-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathdecode_test.go
188 lines (168 loc) · 4.86 KB
/
decode_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
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 admission
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
admissionv1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
)
var _ = Describe("Admission Webhook Decoder", func() {
var decoder Decoder
BeforeEach(func() {
By("creating a new decoder for a scheme")
decoder = NewDecoder(scheme.Scheme)
Expect(decoder).NotTo(BeNil())
})
req := Request{
AdmissionRequest: admissionv1.AdmissionRequest{
Object: runtime.RawExtension{
Raw: []byte(`{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "foo",
"namespace": "default"
},
"spec": {
"containers": [
{
"image": "bar:v2",
"name": "bar"
}
]
}
}`),
},
OldObject: runtime.RawExtension{
Raw: []byte(`{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "foo",
"namespace": "default"
},
"spec": {
"containers": [
{
"image": "bar:v1",
"name": "bar"
}
]
}
}`),
},
},
}
It("should decode a valid admission request", func() {
By("extracting the object from the request")
var actualObj corev1.Pod
Expect(decoder.Decode(req, &actualObj)).To(Succeed())
By("verifying that all data is present in the object")
Expect(actualObj).To(Equal(corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "default",
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{Image: "bar:v2", Name: "bar"},
},
},
}))
})
It("should decode a valid RawExtension object", func() {
By("decoding the RawExtension object")
var actualObj corev1.Pod
Expect(decoder.DecodeRaw(req.OldObject, &actualObj)).To(Succeed())
By("verifying that all data is present in the object")
Expect(actualObj).To(Equal(corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "default",
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{Image: "bar:v1", Name: "bar"},
},
},
}))
})
// NOTE: This will only pass if a GVK is provided. An unstructered object without a GVK may succeed
// in decoding to an alternate type.
It("should fail to decode if the object in the request doesn't match the passed-in type", func() {
By("trying to extract a pod from the quest into a node")
Expect(decoder.Decode(req, &corev1.Node{})).NotTo(Succeed())
By("trying to extract a pod in RawExtension format into a node")
Expect(decoder.DecodeRaw(req.OldObject, &corev1.Node{})).NotTo(Succeed())
})
It("should be able to decode into an unstructured object", func() {
By("decoding the request into an unstructured object")
var target unstructured.Unstructured
Expect(decoder.Decode(req, &target)).To(Succeed())
By("sanity-checking the metadata on the output object")
Expect(target.Object["metadata"]).To(Equal(map[string]interface{}{
"name": "foo",
"namespace": "default",
}))
By("decoding the RawExtension object into an unstructured object")
var target2 unstructured.Unstructured
Expect(decoder.DecodeRaw(req.Object, &target2)).To(Succeed())
By("sanity-checking the metadata on the output object")
Expect(target2.Object["metadata"]).To(Equal(map[string]interface{}{
"name": "foo",
"namespace": "default",
}))
})
req2 := Request{
AdmissionRequest: admissionv1.AdmissionRequest{
Operation: "CREATE",
Object: runtime.RawExtension{
Raw: []byte(`{
"metadata": {
"name": "foo",
"namespace": "default"
},
"spec": {
"containers": [
{
"image": "bar:v2",
"name": "bar"
}
]
}
}`),
},
OldObject: runtime.RawExtension{
Object: nil,
},
},
}
It("should decode a valid admission request without GVK", func() {
By("extracting the object from the request")
var target3 unstructured.Unstructured
Expect(decoder.DecodeRaw(req2.Object, &target3)).To(Succeed())
})
})