Skip to content

Commit

Permalink
FailurePolicy of PodMutatingWebhook turn to Fail (#129)
Browse files Browse the repository at this point in the history
Signed-off-by: ChrisLiu <[email protected]>
  • Loading branch information
chrisliu1995 authored Feb 22, 2024
1 parent 9c203d0 commit 2dd97c2
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 32 deletions.
6 changes: 1 addition & 5 deletions cloudprovider/alibabacloud/slb.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,7 @@ func initLbCache(svcList []corev1.Service, minPort, maxPort int32) (map[string]p
}

func (s *SlbPlugin) OnPodAdded(c client.Client, pod *corev1.Pod, ctx context.Context) (*corev1.Pod, cperrors.PluginError) {
networkManager := utils.NewNetworkManager(pod, c)
networkConfig := networkManager.GetNetworkConfig()
sc := parseLbConfig(networkConfig)
err := c.Create(ctx, s.consSvc(sc, pod, c, ctx))
return pod, cperrors.ToPluginError(err, cperrors.ApiCallError)
return pod, nil
}

func (s *SlbPlugin) OnPodUpdated(c client.Client, pod *corev1.Pod, ctx context.Context) (*corev1.Pod, cperrors.PluginError) {
Expand Down
7 changes: 5 additions & 2 deletions cloudprovider/kubernetes/hostPort.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ func (hpp *HostPortPlugin) Alias() string {
}

func (hpp *HostPortPlugin) OnPodAdded(c client.Client, pod *corev1.Pod, ctx context.Context) (*corev1.Pod, errors.PluginError) {
log.Infof("Receiving pod %s/%s ADD Operation", pod.GetNamespace(), pod.GetName())
podNow := &corev1.Pod{}
err := c.Get(ctx, types.NamespacedName{
Namespace: pod.GetNamespace(),
Name: pod.GetName(),
}, podNow)
// There is a pod with same ns/name exists in cluster, do not allocate
if err == nil {
return pod, nil
log.Infof("There is a pod with same ns/name(%s/%s) exists in cluster, do not allocate", pod.GetNamespace(), pod.GetName())
return pod, errors.NewPluginError(errors.InternalError, "There is a pod with same ns/name exists in cluster")
}
if !k8serrors.IsNotFound(err) {
return pod, errors.NewPluginError(errors.ApiCallError, err.Error())
Expand Down Expand Up @@ -118,6 +119,7 @@ func (hpp *HostPortPlugin) OnPodAdded(c client.Client, pod *corev1.Pod, ctx cont
}

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

func (hpp *HostPortPlugin) OnPodDeleted(c client.Client, pod *corev1.Pod, ctx context.Context) errors.PluginError {
log.Infof("Receiving pod %s/%s DELETE Operation", pod.GetNamespace(), pod.GetName())
if _, ok := hpp.podAllocated[pod.GetNamespace()+"/"+pod.GetName()]; !ok {
return nil
}
Expand Down
17 changes: 0 additions & 17 deletions cloudprovider/kubernetes/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,6 @@ func (i IngressPlugin) Init(client client.Client, options cloudprovider.CloudPro
}

func (i IngressPlugin) OnPodAdded(c client.Client, pod *corev1.Pod, ctx context.Context) (*corev1.Pod, cperrors.PluginError) {
networkManager := utils.NewNetworkManager(pod, c)
conf := networkManager.GetNetworkConfig()
ic, err := parseIngConfig(conf, pod)
if err != nil {
return pod, cperrors.NewPluginError(cperrors.ParameterError, err.Error())
}

err = c.Create(ctx, consSvc(ic, pod, c, ctx))
if err != nil {
return pod, cperrors.NewPluginError(cperrors.ApiCallError, err.Error())
}

err = c.Create(ctx, consIngress(ic, pod, c, ctx))
if err != nil {
return pod, cperrors.NewPluginError(cperrors.ApiCallError, err.Error())
}

return pod, nil
}

Expand Down
6 changes: 1 addition & 5 deletions cloudprovider/volcengine/clb.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,7 @@ func initLbCache(svcList []corev1.Service, minPort, maxPort int32) (map[string]p
}

func (c *ClbPlugin) OnPodAdded(client client.Client, pod *corev1.Pod, ctx context.Context) (*corev1.Pod, cperrors.PluginError) {
networkManager := utils.NewNetworkManager(pod, client)
networkConfig := networkManager.GetNetworkConfig()
sc := parseLbConfig(networkConfig)
err := client.Create(ctx, c.consSvc(sc, pod, client, ctx))
return pod, cperrors.ToPluginError(err, cperrors.ApiCallError)
return pod, nil
}

func (c *ClbPlugin) OnPodUpdated(client client.Client, pod *corev1.Pod, ctx context.Context) (*corev1.Pod, cperrors.PluginError) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/webhook/mutating_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func getPodFromRequest(req admission.Request, decoder *admission.Decoder) (*core

func getAdmissionResponse(req admission.Request, result patchResult) admission.Response {
if result.err != nil {
return admission.Allowed(result.err.Error())
return admission.Denied(result.err.Error())
}
if req.Operation == admissionv1.Delete {
return admission.Allowed("delete successfully")
Expand Down
14 changes: 12 additions & 2 deletions pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"flag"
"fmt"
gamekruiseiov1alpha1 "github.com/openkruise/kruise-game/apis/v1alpha1"
manager2 "github.com/openkruise/kruise-game/cloudprovider/manager"
"github.com/openkruise/kruise-game/pkg/webhook/util/generator"
"github.com/openkruise/kruise-game/pkg/webhook/util/writer"
Expand Down Expand Up @@ -247,12 +248,12 @@ func getValidatingWebhookConf(dnsName string, caBundle []byte) []admissionregist

func getMutatingWebhookConf(dnsName string, caBundle []byte) []admissionregistrationv1.MutatingWebhook {
sideEffectClassNone := admissionregistrationv1.SideEffectClassNone
ignore := admissionregistrationv1.Ignore
fail := admissionregistrationv1.Fail
return []admissionregistrationv1.MutatingWebhook{
{
Name: dnsName,
SideEffects: &sideEffectClassNone,
FailurePolicy: &ignore,
FailurePolicy: &fail,
AdmissionReviewVersions: []string{"v1", "v1beta1"},
ClientConfig: admissionregistrationv1.WebhookClientConfig{
Service: &admissionregistrationv1.ServiceReference{
Expand All @@ -272,6 +273,15 @@ func getMutatingWebhookConf(dnsName string, caBundle []byte) []admissionregistra
},
},
},
ObjectSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: gamekruiseiov1alpha1.GameServerOwnerGssKey,
Operator: metav1.LabelSelectorOpExists,
Values: []string{},
},
},
},
},
}
}

0 comments on commit 2dd97c2

Please sign in to comment.