-
Notifications
You must be signed in to change notification settings - Fork 993
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
webhook(hypernode): validate memberSelectorType
- Loading branch information
Showing
3 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
pkg/webhooks/admission/hypernodes/validate/admit_hypernode.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
Copyright 2024 The Volcano Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package validate | ||
|
||
import ( | ||
"fmt" | ||
|
||
admissionv1 "k8s.io/api/admission/v1" | ||
whv1 "k8s.io/api/admissionregistration/v1" | ||
"k8s.io/klog/v2" | ||
|
||
hypernodev1alpha1 "volcano.sh/apis/pkg/apis/hypernode/v1alpha1" | ||
"volcano.sh/volcano/pkg/webhooks/router" | ||
"volcano.sh/volcano/pkg/webhooks/schema" | ||
"volcano.sh/volcano/pkg/webhooks/util" | ||
) | ||
|
||
func init() { | ||
router.RegisterAdmission(service) | ||
} | ||
|
||
var config = &router.AdmissionServiceConfig{} | ||
|
||
var service = &router.AdmissionService{ | ||
Path: "/hypernodes/validate", | ||
Func: AdmitHypernode, | ||
|
||
Config: config, | ||
|
||
ValidatingConfig: &whv1.ValidatingWebhookConfiguration{ | ||
Webhooks: []whv1.ValidatingWebhook{{ | ||
Name: "validatehypernode.volcano.sh", | ||
Rules: []whv1.RuleWithOperations{ | ||
{ | ||
Operations: []whv1.OperationType{whv1.Create, whv1.Update}, | ||
Rule: whv1.Rule{ | ||
APIGroups: []string{hypernodev1alpha1.SchemeGroupVersion.Group}, | ||
APIVersions: []string{hypernodev1alpha1.SchemeGroupVersion.Version}, | ||
Resources: []string{"hypernodes"}, | ||
}, | ||
}, | ||
}, | ||
}}, | ||
}, | ||
} | ||
|
||
// AdmitHypernode is to admit hypernode and return response. | ||
func AdmitHypernode(ar admissionv1.AdmissionReview) *admissionv1.AdmissionResponse { | ||
klog.V(3).Infof("admitting hypernode -- %s", ar.Request.Operation) | ||
|
||
hypernode, err := schema.DecodeHypernode(ar.Request.Object, ar.Request.Resource) | ||
if err != nil { | ||
return util.ToAdmissionResponse(err) | ||
} | ||
|
||
switch ar.Request.Operation { | ||
case admissionv1.Create, admissionv1.Update: | ||
err = validateHypernode(hypernode) | ||
if err != nil { | ||
break | ||
} | ||
} | ||
|
||
return &admissionv1.AdmissionResponse{ | ||
Allowed: true, | ||
} | ||
} | ||
|
||
// validateHypernode is to validate hypernode. | ||
func validateHypernode(hypernode *hypernodev1alpha1.Hypernode) error { | ||
if hypernode.Spec.Members == nil { | ||
return nil | ||
} | ||
|
||
for _, member := range hypernode.Spec.Members { | ||
if member.Selector == nil { | ||
continue | ||
} | ||
|
||
if member.Selector.Type == hypernodev1alpha1.ExactMatchMemberSelectorType && member.Selector.ExactMatch == "" { | ||
return fmt.Errorf("exactMatch is required when type is Exact") | ||
} | ||
if member.Selector.Type == hypernodev1alpha1.RegexMatchMemberSelectorType && member.Selector.RegexMatch == "" { | ||
return fmt.Errorf("regexMatch is required when type is Regex") | ||
} | ||
} | ||
return nil | ||
} |
80 changes: 80 additions & 0 deletions
80
pkg/webhooks/admission/hypernodes/validate/admit_hypernode_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
Copyright 2024 The Volcano Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package validate | ||
|
||
import ( | ||
"testing" | ||
|
||
admissionv1 "k8s.io/api/admission/v1" | ||
|
||
hypernodev1alpha1 "volcano.sh/apis/pkg/apis/hypernode/v1alpha1" | ||
) | ||
|
||
func TestValidateHypernode(t *testing.T) { | ||
testCases := []struct { | ||
Name string | ||
Hypernode hypernodev1alpha1.Hypernode | ||
ExpectErr bool | ||
reviewResponse admissionv1.AdmissionResponse | ||
ret string | ||
}{ | ||
{ | ||
Name: "validate valid hypernode", | ||
Hypernode: hypernodev1alpha1.Hypernode{ | ||
Spec: hypernodev1alpha1.HyperNodeSpec{ | ||
Members: []hypernodev1alpha1.MemberSpec{ | ||
{ | ||
Selector: hypernodev1alpha1.MemberSelector{ | ||
Type: hypernodev1alpha1.ExactMatchMemberSelectorType, | ||
ExactMatch: "node-1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
ExpectErr: false, | ||
reviewResponse: admissionv1.AdmissionResponse{}, | ||
}, | ||
{ | ||
Name: "validate invalid hypernode", | ||
Hypernode: hypernodev1alpha1.Hypernode{ | ||
Spec: hypernodev1alpha1.HyperNodeSpec{ | ||
Members: []hypernodev1alpha1.MemberSpec{ | ||
{ | ||
Selector: hypernodev1alpha1.MemberSelector{ | ||
Type: hypernodev1alpha1.ExactMatchMemberSelectorType, | ||
ExactMatch: "", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
ExpectErr: true, | ||
reviewResponse: admissionv1.AdmissionResponse{}, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.Name, func(t *testing.T) { | ||
err := validateHypernode(&testCase.Hypernode) | ||
if err != nil { | ||
t.Errorf("validateHypernode failed: %v", err) | ||
} | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters