Skip to content

Commit 2dd97c2

Browse files
authored
FailurePolicy of PodMutatingWebhook turn to Fail (openkruise#129)
Signed-off-by: ChrisLiu <[email protected]>
1 parent 9c203d0 commit 2dd97c2

File tree

6 files changed

+20
-32
lines changed

6 files changed

+20
-32
lines changed

cloudprovider/alibabacloud/slb.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,7 @@ func initLbCache(svcList []corev1.Service, minPort, maxPort int32) (map[string]p
120120
}
121121

122122
func (s *SlbPlugin) OnPodAdded(c client.Client, pod *corev1.Pod, ctx context.Context) (*corev1.Pod, cperrors.PluginError) {
123-
networkManager := utils.NewNetworkManager(pod, c)
124-
networkConfig := networkManager.GetNetworkConfig()
125-
sc := parseLbConfig(networkConfig)
126-
err := c.Create(ctx, s.consSvc(sc, pod, c, ctx))
127-
return pod, cperrors.ToPluginError(err, cperrors.ApiCallError)
123+
return pod, nil
128124
}
129125

130126
func (s *SlbPlugin) OnPodUpdated(c client.Client, pod *corev1.Pod, ctx context.Context) (*corev1.Pod, cperrors.PluginError) {

cloudprovider/kubernetes/hostPort.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,15 @@ func (hpp *HostPortPlugin) Alias() string {
7070
}
7171

7272
func (hpp *HostPortPlugin) OnPodAdded(c client.Client, pod *corev1.Pod, ctx context.Context) (*corev1.Pod, errors.PluginError) {
73+
log.Infof("Receiving pod %s/%s ADD Operation", pod.GetNamespace(), pod.GetName())
7374
podNow := &corev1.Pod{}
7475
err := c.Get(ctx, types.NamespacedName{
7576
Namespace: pod.GetNamespace(),
7677
Name: pod.GetName(),
7778
}, podNow)
78-
// There is a pod with same ns/name exists in cluster, do not allocate
7979
if err == nil {
80-
return pod, nil
80+
log.Infof("There is a pod with same ns/name(%s/%s) exists in cluster, do not allocate", pod.GetNamespace(), pod.GetName())
81+
return pod, errors.NewPluginError(errors.InternalError, "There is a pod with same ns/name exists in cluster")
8182
}
8283
if !k8serrors.IsNotFound(err) {
8384
return pod, errors.NewPluginError(errors.ApiCallError, err.Error())
@@ -118,6 +119,7 @@ func (hpp *HostPortPlugin) OnPodAdded(c client.Client, pod *corev1.Pod, ctx cont
118119
}
119120

120121
func (hpp *HostPortPlugin) OnPodUpdated(c client.Client, pod *corev1.Pod, ctx context.Context) (*corev1.Pod, errors.PluginError) {
122+
log.Infof("Receiving pod %s/%s UPDATE Operation", pod.GetNamespace(), pod.GetName())
121123
node := &corev1.Node{}
122124
err := c.Get(ctx, types.NamespacedName{
123125
Name: pod.Spec.NodeName,
@@ -183,6 +185,7 @@ func (hpp *HostPortPlugin) OnPodUpdated(c client.Client, pod *corev1.Pod, ctx co
183185
}
184186

185187
func (hpp *HostPortPlugin) OnPodDeleted(c client.Client, pod *corev1.Pod, ctx context.Context) errors.PluginError {
188+
log.Infof("Receiving pod %s/%s DELETE Operation", pod.GetNamespace(), pod.GetName())
186189
if _, ok := hpp.podAllocated[pod.GetNamespace()+"/"+pod.GetName()]; !ok {
187190
return nil
188191
}

cloudprovider/kubernetes/ingress.go

-17
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,6 @@ func (i IngressPlugin) Init(client client.Client, options cloudprovider.CloudPro
7878
}
7979

8080
func (i IngressPlugin) OnPodAdded(c client.Client, pod *corev1.Pod, ctx context.Context) (*corev1.Pod, cperrors.PluginError) {
81-
networkManager := utils.NewNetworkManager(pod, c)
82-
conf := networkManager.GetNetworkConfig()
83-
ic, err := parseIngConfig(conf, pod)
84-
if err != nil {
85-
return pod, cperrors.NewPluginError(cperrors.ParameterError, err.Error())
86-
}
87-
88-
err = c.Create(ctx, consSvc(ic, pod, c, ctx))
89-
if err != nil {
90-
return pod, cperrors.NewPluginError(cperrors.ApiCallError, err.Error())
91-
}
92-
93-
err = c.Create(ctx, consIngress(ic, pod, c, ctx))
94-
if err != nil {
95-
return pod, cperrors.NewPluginError(cperrors.ApiCallError, err.Error())
96-
}
97-
9881
return pod, nil
9982
}
10083

cloudprovider/volcengine/clb.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,7 @@ func initLbCache(svcList []corev1.Service, minPort, maxPort int32) (map[string]p
128128
}
129129

130130
func (c *ClbPlugin) OnPodAdded(client client.Client, pod *corev1.Pod, ctx context.Context) (*corev1.Pod, cperrors.PluginError) {
131-
networkManager := utils.NewNetworkManager(pod, client)
132-
networkConfig := networkManager.GetNetworkConfig()
133-
sc := parseLbConfig(networkConfig)
134-
err := client.Create(ctx, c.consSvc(sc, pod, client, ctx))
135-
return pod, cperrors.ToPluginError(err, cperrors.ApiCallError)
131+
return pod, nil
136132
}
137133

138134
func (c *ClbPlugin) OnPodUpdated(client client.Client, pod *corev1.Pod, ctx context.Context) (*corev1.Pod, cperrors.PluginError) {

pkg/webhook/mutating_pod.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func getPodFromRequest(req admission.Request, decoder *admission.Decoder) (*core
129129

130130
func getAdmissionResponse(req admission.Request, result patchResult) admission.Response {
131131
if result.err != nil {
132-
return admission.Allowed(result.err.Error())
132+
return admission.Denied(result.err.Error())
133133
}
134134
if req.Operation == admissionv1.Delete {
135135
return admission.Allowed("delete successfully")

pkg/webhook/webhook.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"flag"
2222
"fmt"
23+
gamekruiseiov1alpha1 "github.com/openkruise/kruise-game/apis/v1alpha1"
2324
manager2 "github.com/openkruise/kruise-game/cloudprovider/manager"
2425
"github.com/openkruise/kruise-game/pkg/webhook/util/generator"
2526
"github.com/openkruise/kruise-game/pkg/webhook/util/writer"
@@ -247,12 +248,12 @@ func getValidatingWebhookConf(dnsName string, caBundle []byte) []admissionregist
247248

248249
func getMutatingWebhookConf(dnsName string, caBundle []byte) []admissionregistrationv1.MutatingWebhook {
249250
sideEffectClassNone := admissionregistrationv1.SideEffectClassNone
250-
ignore := admissionregistrationv1.Ignore
251+
fail := admissionregistrationv1.Fail
251252
return []admissionregistrationv1.MutatingWebhook{
252253
{
253254
Name: dnsName,
254255
SideEffects: &sideEffectClassNone,
255-
FailurePolicy: &ignore,
256+
FailurePolicy: &fail,
256257
AdmissionReviewVersions: []string{"v1", "v1beta1"},
257258
ClientConfig: admissionregistrationv1.WebhookClientConfig{
258259
Service: &admissionregistrationv1.ServiceReference{
@@ -272,6 +273,15 @@ func getMutatingWebhookConf(dnsName string, caBundle []byte) []admissionregistra
272273
},
273274
},
274275
},
276+
ObjectSelector: &metav1.LabelSelector{
277+
MatchExpressions: []metav1.LabelSelectorRequirement{
278+
{
279+
Key: gamekruiseiov1alpha1.GameServerOwnerGssKey,
280+
Operator: metav1.LabelSelectorOpExists,
281+
Values: []string{},
282+
},
283+
},
284+
},
275285
},
276286
}
277287
}

0 commit comments

Comments
 (0)