@@ -7,21 +7,37 @@ import (
7
7
"net/http"
8
8
"strings"
9
9
10
+ admission_v1 "k8s.io/api/admission/v1"
10
11
v1 "k8s.io/api/admissionregistration/v1"
11
12
k8s_error "k8s.io/apimachinery/pkg/api/errors"
12
13
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13
14
"k8s.io/client-go/kubernetes"
14
15
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
15
16
)
16
17
17
- func ValidatingPod () http.Handler {
18
+ func ValidatingPod (k8sClient * kubernetes. Clientset ) http.Handler {
18
19
return & admission.Webhook {
19
20
Handler : admission .HandlerFunc (
20
21
func (ctx context.Context , req admission.Request ) admission.Response {
21
- if req .Namespace == "default" && req .Operation == "delete" {
22
+ podCanNotBeDeleted := false
23
+
24
+ podName := req .Name
25
+ podNamespace := req .Namespace
26
+
27
+ pod , err := k8sClient .CoreV1 ().Pods (podNamespace ).Get (ctx , podName , metav1.GetOptions {})
28
+ if err != nil {
29
+ return admission .ValidationResponse (false , "get pod error" )
30
+ }
31
+
32
+ if v , ok := pod .Labels ["allow-delete" ]; ok && v == "false" {
33
+ podCanNotBeDeleted = true
34
+ }
35
+
36
+ if req .Operation == admission_v1 .Delete && podCanNotBeDeleted {
37
+ slog .Info ("pod can not be deleted labels allow-delete=false" , "name" , req .Name , "namespace" , req .Namespace )
22
38
return admission .ValidationResponse (false , "not allow by webhook" )
23
39
}
24
- return admission .ValidationResponse (true , "ok, you can do it " )
40
+ return admission .ValidationResponse (true , "ok" )
25
41
},
26
42
),
27
43
}
@@ -42,10 +58,10 @@ func CreateValidatingWebhook(k8sClient *kubernetes.Clientset) error {
42
58
},
43
59
Webhooks : []v1.ValidatingWebhook {
44
60
{
45
- Name : webhookServiceName ,
61
+ Name : "pod-webhook.some.cn" ,
46
62
NamespaceSelector : & metav1.LabelSelector {
47
63
MatchLabels : map [string ]string {
48
- "kubernetes.io/metadata" : "default" ,
64
+ "kubernetes.io/metadata.name " : "default" ,
49
65
},
50
66
},
51
67
Rules : []v1.RuleWithOperations {
@@ -54,7 +70,7 @@ func CreateValidatingWebhook(k8sClient *kubernetes.Clientset) error {
54
70
v1 .Delete ,
55
71
},
56
72
Rule : v1.Rule {
57
- APIGroups : []string {"core " },
73
+ APIGroups : []string {"" },
58
74
APIVersions : []string {"v1" },
59
75
Resources : []string {"pods" },
60
76
},
@@ -82,12 +98,13 @@ func CreateValidatingWebhook(k8sClient *kubernetes.Clientset) error {
82
98
Create (context .Background (), & valid_webhook , metav1.CreateOptions {})
83
99
if err != nil {
84
100
if k8s_error .IsAlreadyExists (err ) {
85
- slog .Info ("ValidatingWebhookConfiguration already exists, %s.%s" , valid_webhook .Name , valid_webhook .Namespace )
101
+ slog .Info ("validatingWebhookConfiguration already exists" , "name" , valid_webhook .Name , "namespace" , valid_webhook .Namespace )
86
102
return nil
87
103
} else {
88
104
return err
89
105
}
90
106
}
107
+ slog .Info ("validatingWebhookConfiguration create success" , "name" , valid_webhook .Name , "namespace" , valid_webhook .Namespace )
91
108
92
109
return nil
93
110
}
0 commit comments