Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: prevent updates to mcs.types or when multiple types are involved. #4454

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pkg/webhook/multiclusterservice/validating.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package multiclusterservice
import (
"context"
"net/http"
"reflect"
"strings"

admissionv1 "k8s.io/api/admission/v1"
Expand Down Expand Up @@ -73,11 +74,21 @@ func (v *ValidatingAdmission) Handle(ctx context.Context, req admission.Request)

func (v *ValidatingAdmission) validateMCSUpdate(oldMcs, newMcs *networkingv1alpha1.MultiClusterService) field.ErrorList {
allErrs := apimachineryvalidation.ValidateObjectMetaUpdate(&newMcs.ObjectMeta, &oldMcs.ObjectMeta, field.NewPath("metadata"))
allErrs = append(allErrs, v.validateMCSTypesUpdate(oldMcs, newMcs)...)
allErrs = append(allErrs, v.validateMCS(newMcs)...)
allErrs = append(allErrs, lifted.ValidateLoadBalancerStatus(&newMcs.Status.LoadBalancer, field.NewPath("status", "loadBalancer"))...)
return allErrs
}

func (v *ValidatingAdmission) validateMCSTypesUpdate(oldMcs, newMcs *networkingv1alpha1.MultiClusterService) field.ErrorList {
allErrs := field.ErrorList{}
if !reflect.DeepEqual(oldMcs.Spec.Types, newMcs.Spec.Types) {
typePath := field.NewPath("spec").Child("types")
allErrs = append(allErrs, field.Invalid(typePath, oldMcs.Spec.Types, "MultiClusterService types are immutable"))
}
return allErrs
}

func (v *ValidatingAdmission) validateMCS(mcs *networkingv1alpha1.MultiClusterService) field.ErrorList {
allErrs := apimachineryvalidation.ValidateObjectMeta(&mcs.ObjectMeta, true,
apimachineryvalidation.NameIsDNS1035Label, field.NewPath("metadata"))
Expand All @@ -97,12 +108,19 @@ func (v *ValidatingAdmission) validateMultiClusterServiceSpec(mcs *networkingv1a
port := mcs.Spec.Ports[i]
allErrs = append(allErrs, v.validateExposurePort(&port, allPortNames, portPath)...)
}

typesSet := sets.Set[string]{}
typesPath := specPath.Child("types")
for i := range mcs.Spec.Types {
typePath := typesPath.Index(i)
exposureType := mcs.Spec.Types[i]
typesSet.Insert(string(exposureType))
allErrs = append(allErrs, v.validateExposureType(&exposureType, typePath)...)
}
if len(typesSet) > 1 {
allErrs = append(allErrs, field.Invalid(typesPath, mcs.Spec.Types, "MultiClusterService types should not contain more than one type"))
}
Comment on lines +120 to +122
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we don't need to introduce another set, how about this:

Suggested change
if len(typesSet) > 1 {
allErrs = append(allErrs, field.Invalid(typesPath, mcs.Spec.Types, "MultiClusterService types should not contain more than one type"))
}
// temporarily disable two expose types
if len(mcs.Spec.Types) > 1 {
allErrs = append(allErrs, field.Invalid(typesPath, mcs.Spec.Types, "MultiClusterService types should not contain more than one type"))
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mcs.Spec.Types can be configured as {LoadBalancer, LoadBalancer}. In this situation, it's the same type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, we should deduplicate, but in another PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to consider whether to prohibit users from filling in the same type of element. My idea is that it should be prohibited.


clusterNamesPath := specPath.Child("range").Child("providerClusters")
for i := range mcs.Spec.ProviderClusters {
clusterNamePath := clusterNamesPath.Index(i)
Expand Down
34 changes: 33 additions & 1 deletion pkg/webhook/multiclusterservice/validating_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func TestValidateMultiClusterServiceSpec(t *testing.T) {
},
Types: []networkingv1alpha1.ExposureType{
networkingv1alpha1.ExposureTypeLoadBalancer,
networkingv1alpha1.ExposureTypeCrossCluster,
},
ProviderClusters: []networkingv1alpha1.ClusterSelector{
{Name: "member1"},
Expand All @@ -65,6 +64,39 @@ func TestValidateMultiClusterServiceSpec(t *testing.T) {
},
expectedErr: field.ErrorList{},
},
{
name: "multiple exposure type mcs",
mcs: &networkingv1alpha1.MultiClusterService{
Spec: networkingv1alpha1.MultiClusterServiceSpec{
Ports: []networkingv1alpha1.ExposurePort{
{
Name: "foo",
Port: 16312,
},
{
Name: "bar",
Port: 16313,
},
},
Types: []networkingv1alpha1.ExposureType{
networkingv1alpha1.ExposureTypeLoadBalancer,
networkingv1alpha1.ExposureTypeCrossCluster,
},
ProviderClusters: []networkingv1alpha1.ClusterSelector{
{Name: "member1"},
{Name: "member2"},
},
ConsumerClusters: []networkingv1alpha1.ClusterSelector{
{Name: "member1"},
{Name: "member2"},
},
},
},
expectedErr: field.ErrorList{field.Invalid(specFld.Child("types"), []networkingv1alpha1.ExposureType{
networkingv1alpha1.ExposureTypeLoadBalancer,
networkingv1alpha1.ExposureTypeCrossCluster,
}, "MultiClusterService types should not contain more than one type")},
},
{
name: "duplicated svc name",
mcs: &networkingv1alpha1.MultiClusterService{
Expand Down
Loading