Merge pull request #526 from phantooom/rui.zou/svc-webhook

webhook: when clusterIP=None do not remove clusterIP
This commit is contained in:
karmada-bot 2021-07-15 14:26:35 +08:00 committed by GitHub
commit 595c361d54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 1 deletions

View File

@ -5,12 +5,14 @@ import (
"encoding/json"
"net/http"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
"github.com/karmada-io/karmada/pkg/util"
)
// MutatingAdmission mutates API request if necessary.
@ -97,5 +99,11 @@ func removeIrrelevantField(workload *unstructured.Unstructured) {
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")
}
}
}