Skip to content

Commit bcbf425

Browse files
zclyneRainbowMango
authored andcommitted
added validation check for the components section of resourcebinding
Signed-off-by: Yifan Zhang <[email protected]>
1 parent caa18b1 commit bcbf425

File tree

7 files changed

+178
-75
lines changed

7 files changed

+178
-75
lines changed

api/openapi-spec/swagger.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21216,12 +21216,14 @@
2121621216
"description": "Component represents the requirements for a specific component.",
2121721217
"type": "object",
2121821218
"required": [
21219+
"name",
2121921220
"replicas"
2122021221
],
2122121222
"properties": {
2122221223
"name": {
2122321224
"description": "Name of this component. It is required when the resource contains multiple components to ensure proper identification, and must also be unique within the same resource.",
21224-
"type": "string"
21225+
"type": "string",
21226+
"default": ""
2122521227
},
2122621228
"replicaRequirements": {
2122721229
"description": "ReplicaRequirements represents the requirements required by each replica for this component.",

charts/karmada/_crds/bases/work/work.karmada.io_clusterresourcebindings.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ spec:
450450
format: int32
451451
type: integer
452452
required:
453+
- name
453454
- replicas
454455
type: object
455456
type: array

charts/karmada/_crds/bases/work/work.karmada.io_resourcebindings.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ spec:
450450
format: int32
451451
type: integer
452452
required:
453+
- name
453454
- replicas
454455
type: object
455456
type: array

pkg/apis/work/v1alpha2/binding_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ type Component struct {
229229
// It is required when the resource contains multiple components to ensure proper identification,
230230
// and must also be unique within the same resource.
231231
// +kubebuilder:validation:MaxLength=32
232-
// +optional
233-
Name string `json:"name,omitempty"`
232+
// +required
233+
Name string `json:"name"`
234234

235235
// Replicas represents the replica number of the resource's component.
236236
// +required

pkg/generated/openapi/zz_generated.openapi.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/webhook/resourcebinding/validating.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
admissionv1 "k8s.io/api/admission/v1"
2727
corev1 "k8s.io/api/core/v1"
2828
apierrors "k8s.io/apimachinery/pkg/api/errors"
29+
"k8s.io/apimachinery/pkg/util/validation/field"
2930
"k8s.io/client-go/util/retry"
3031
"k8s.io/klog/v2"
3132
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -72,7 +73,12 @@ func (v *ValidatingAdmission) Handle(ctx context.Context, req admission.Request)
7273
return admission.Denied(err.Error())
7374
}
7475

75-
// further validation can be added here if needed
76+
if features.FeatureGate.Enabled(features.MultiplePodTemplatesScheduling) {
77+
if err := v.validateComponents(rb.Spec.Components, field.NewPath("spec").Child("components")); err != nil {
78+
klog.Errorf("Admission denied for ResourceBinding %s/%s: %v", rb.Namespace, rb.Name, err)
79+
return admission.Denied(err.Error())
80+
}
81+
}
7682

7783
return admission.Allowed("")
7884
}
@@ -324,6 +330,22 @@ func (v *ValidatingAdmission) processSingleFRQ(frqItem *policyv1alpha1.Federated
324330
return potentialNewOverallUsedForThisFRQ, msg, nil
325331
}
326332

333+
// validateComponents checks the validity of the Components field in the ResourceBinding.
334+
func (v *ValidatingAdmission) validateComponents(components []workv1alpha2.Component, fldPath *field.Path) error {
335+
var allErrs field.ErrorList
336+
componentNames := make(map[string]struct{})
337+
for index, component := range components {
338+
if len(component.Name) == 0 {
339+
allErrs = append(allErrs, field.Invalid(fldPath.Index(index).Child("name"), component.Name, "component names must be non-empty"))
340+
} else if _, exists := componentNames[component.Name]; exists {
341+
allErrs = append(allErrs, field.Invalid(fldPath.Index(index).Child("name"), component.Name, "component names must be unique"))
342+
} else {
343+
componentNames[component.Name] = struct{}{}
344+
}
345+
}
346+
return allErrs.ToAggregate()
347+
}
348+
327349
func isQuotaRelevantFieldChanged(oldRB, newRB *workv1alpha2.ResourceBinding) bool {
328350
if oldRB == nil || newRB == nil {
329351
return true

0 commit comments

Comments
 (0)