zhangguanzhang's Blog

编写一个动态准入控制

字数统计: 454阅读时长: 2 min
2020/04/03
1
2
3
4
5
6
7
8
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- name: test
image: nginx:alpine
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
{
"kind":"Pod",
"apiVersion":"v1",
"metadata":{
"name":"test",
"namespace":"default",
"creationTimestamp":null,
"annotations":{
"kubectl.kubernetes.io/last-applied-configuration":"{\"apiVersion\":\"v1\",\"kind\":\"Pod\",\"metadata\":{\"annotations\":{},\"name\":\"test\",\"namespace\":\"default\"},\"spec\":{\"containers\":[{\"image\":\"nginx:alpine\",\"name\":\"test\"}]}}\n"
}
},
"spec":{
"volumes":[
{
"name":"default-token-lsh6v",
"secret":{
"secretName":"default-token-lsh6v"
}
}
],
"containers":[
{
"name":"test",
"image":"nginx:alpine",
"resources":{

},
"volumeMounts":[
{
"name":"default-token-lsh6v",
"readOnly":true,
"mountPath":"/var/run/secrets/kubernetes.io/serviceaccount"
}
],
"terminationMessagePath":"/dev/termination-log",
"terminationMessagePolicy":"File",
"imagePullPolicy":"IfNotPresent"
}
],
"restartPolicy":"Always",
"terminationGracePeriodSeconds":30,
"dnsPolicy":"ClusterFirst",
"serviceAccountName":"default",
"serviceAccount":"default",
"securityContext":{

},
"schedulerName":"default-scheduler",
"enableServiceLinks":true
},
"status":{

}
}
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

var (
TerminationGracePeriodSeconds int64 = 30
EnableServiceLinks = true
)

var pod = corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "default",
CreationTimestamp: metav1.Time{},
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
{
Name: "default-token-lsh6v",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: "default-token-lsh6v",
},
},
},
},
Containers: []corev1.Container{
{
Name: "test",
Image: "nginx:alpine",
Resources: corev1.ResourceRequirements{},
VolumeMounts: []corev1.VolumeMount{
{
Name: "default-token-lsh6v",
ReadOnly: true,
MountPath: "/var/run/secrets/kubernetes.io/serviceaccount",
},
},
},
},
RestartPolicy: corev1.RestartPolicyAlways,
TerminationGracePeriodSeconds: &TerminationGracePeriodSeconds,
DNSPolicy: corev1.DNSClusterFirst,
ServiceAccountName: "default",
SecurityContext: &corev1.PodSecurityContext{},
SchedulerName: "default-scheduler",
EnableServiceLinks: &EnableServiceLinks,
},
Status: corev1.PodStatus{},
}

https://github.com/redhat-cop/podpreset-webhook/blob/master/pkg/handler/handler.go#L107
https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/webhook/admission#PatchResponseFromRaw

CATALOG