webhook: when clusterIP=None do not remove clusterIP

Signed-off-by: phantooom <xiaorui.zou@gmail.com>
This commit is contained in:
phantooom 2021-07-14 18:25:00 +08:00
parent 9495cf398f
commit 1a242b9671
1 changed files with 9 additions and 1 deletions

View File

@ -5,12 +5,14 @@ import (
"encoding/json" "encoding/json"
"net/http" "net/http"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2" "k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1" workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
"github.com/karmada-io/karmada/pkg/util"
) )
// MutatingAdmission mutates API request if necessary. // MutatingAdmission mutates API request if necessary.
@ -97,5 +99,11 @@ func removeIrrelevantField(workload *unstructured.Unstructured) {
unstructured.RemoveNestedField(workload.Object, "status") unstructured.RemoveNestedField(workload.Object, "status")
unstructured.RemoveNestedField(workload.Object, "spec", "clusterIP") if workload.GetKind() == util.ServiceKind {
// In the case spec.clusterIP is set to `None`, means user want a headless service, then it shouldn't be removed.
clusterIP, exist, _ := unstructured.NestedString(workload.Object, "spec", "clusterIP")
if exist && clusterIP != corev1.ClusterIPNone {
unstructured.RemoveNestedField(workload.Object, "spec", "clusterIP")
}
}
} }