-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain_test.go
89 lines (80 loc) · 1.87 KB
/
main_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
package main
import (
"context"
"encoding/json"
"fmt"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"text/template"
. "github.com/onsi/ginkgo"
admissionv1beta1 "k8s.io/api/admission/v1beta1"
)
var _ = Describe("Mutating webhook", func() {
It("TODO", func() {
By("invoking the webhook")
parsedTemplates = []*template.Template{template.Must(template.New("test").Parse(`
apiVersion: v1
kind: Pod
metadata:
name: {{.Name}}
namespace: test-pods
spec:
{{if index .Annotations "pvc.daimler.com/size" }}
volumes:
- name: build
persistentVolumeClaim:
claimName: {{.Name}}
{{ end }}
`))}
resp := getMutatingHandler().Handle(context.Background(), admission.Request{
AdmissionRequest: admissionv1beta1.AdmissionRequest{
UID: "foobar",
Object: runtime.RawExtension{
Raw: mustMarshal(json.Marshal(&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test-pods",
Labels: map[string]string{
"inject": "tproxy",
},
Annotations: map[string]string{
"pvc.daimler.com/size": "15Gi",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "test",
Image: "bazel:v1",
},
},
},
})),
},
},
})
fmt.Printf("%+v", resp.Patches)
//By("checking that the response share's the request's UID")
//Expect(resp.UID).To(Equal(machinerytypes.UID("foobar")))
})
})
func mustMarshal(bytes []byte, err error) []byte {
if err != nil {
panic(err)
}
return bytes
}
func getMutatingHandler() *mutatingHandler {
mwh := &mutatingHandler{}
decoder, err := admission.NewDecoder(runtime.NewScheme())
if err != nil {
panic(err)
}
err = mwh.InjectDecoder(decoder)
if err != nil {
panic(err)
}
return mwh
}