Skip to content

Commit

Permalink
webhook(hypernode): validate memberSelectorType
Browse files Browse the repository at this point in the history
Signed-off-by: xuwentao <[email protected]>
  • Loading branch information
Xu-Wentao committed Dec 18, 2024
1 parent 087d990 commit 7885fa5
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ require (
sigs.k8s.io/controller-runtime v0.13.0
sigs.k8s.io/yaml v1.4.0
stathat.com/c/consistent v1.0.0
volcano.sh/apis v1.10.0-alpha.0.0.20241016111016-bb93758bd51f
volcano.sh/apis v1.10.0-alpha.0.0.20241218081838-e5d361b6bfbe
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -510,5 +510,5 @@ sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
stathat.com/c/consistent v1.0.0 h1:ezyc51EGcRPJUxfHGSgJjWzJdj3NiMU9pNfLNGiXV0c=
stathat.com/c/consistent v1.0.0/go.mod h1:QkzMWzcbB+yQBL2AttO6sgsQS/JSTapcDISJalmCDS0=
volcano.sh/apis v1.10.0-alpha.0.0.20241016111016-bb93758bd51f h1:wqvGQgzYCPJSS07xE1LZbJ/Mxb1f/xFWThnII6BzMhg=
volcano.sh/apis v1.10.0-alpha.0.0.20241016111016-bb93758bd51f/go.mod h1:XHIjTlHDMZTLRg2Y2JAkj85iP0iiet2tv+HfPQZrsHs=
volcano.sh/apis v1.10.0-alpha.0.0.20241218081838-e5d361b6bfbe h1:iHd1Xt36a7S47IFksuF0h9W9J4LKzhBEz0C9XbkBvB8=
volcano.sh/apis v1.10.0-alpha.0.0.20241218081838-e5d361b6bfbe/go.mod h1:XHIjTlHDMZTLRg2Y2JAkj85iP0iiet2tv+HfPQZrsHs=
103 changes: 103 additions & 0 deletions pkg/webhooks/admission/hypernodes/validate/admit_hypernode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
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/topology/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 {
return util.ToAdmissionResponse(err)
}
}

return &admissionv1.AdmissionResponse{
Allowed: true,
}
}

// validateHyperNode is to validate hypernode.
func validateHyperNode(hypernode *hypernodev1alpha1.HyperNode) error {
if len(hypernode.Spec.Members) == 0 {
return nil
}

for _, member := range hypernode.Spec.Members {
if member.Selector == (hypernodev1alpha1.MemberSelector{}) {
continue
}

if member.Selector.ExactMatch == nil && member.Selector.RegexMatch == nil {
return fmt.Errorf("either exactMatch or regexMatch must be specified")
}

if member.Selector.ExactMatch != nil && member.Selector.RegexMatch != nil {
return fmt.Errorf("exactMatch and regexMatch cannot be specified together")
}
}
return nil
}
96 changes: 96 additions & 0 deletions pkg/webhooks/admission/hypernodes/validate/admit_hypernode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
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/topology/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{
ExactMatch: &hypernodev1alpha1.ExactMatch{
Name: "node-1",
},
},
},
},
},
},
ExpectErr: false,
reviewResponse: admissionv1.AdmissionResponse{},
},
{
Name: "validate invalid hypernode with empty exactMatch",
HyperNode: hypernodev1alpha1.HyperNode{
Spec: hypernodev1alpha1.HyperNodeSpec{
Members: []hypernodev1alpha1.MemberSpec{
{
Selector: hypernodev1alpha1.MemberSelector{
ExactMatch: nil,
},
},
},
},
},
ExpectErr: true,
reviewResponse: admissionv1.AdmissionResponse{},
},
{
Name: "validate invalid hypernode with empty regexMatch",
HyperNode: hypernodev1alpha1.HyperNode{
Spec: hypernodev1alpha1.HyperNodeSpec{
Members: []hypernodev1alpha1.MemberSpec{
{
Selector: hypernodev1alpha1.MemberSelector{
RegexMatch: nil,
},
},
},
},
},
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)
}
})
}

}
22 changes: 22 additions & 0 deletions pkg/webhooks/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

batchv1alpha1 "volcano.sh/apis/pkg/apis/batch/v1alpha1"
schedulingv1beta1 "volcano.sh/apis/pkg/apis/scheduling/v1beta1"
hypernodev1alpha1 "volcano.sh/apis/pkg/apis/topology/v1alpha1"
)

func init() {
Expand Down Expand Up @@ -127,3 +128,24 @@ func DecodePodGroup(object runtime.RawExtension, resource metav1.GroupVersionRes

return &podgroup, nil
}

// DecodeHyperNode decodes the hypernode using deserializer from the raw object.
func DecodeHyperNode(object runtime.RawExtension, resource metav1.GroupVersionResource) (*hypernodev1alpha1.HyperNode, error) {
hypernodeResource := metav1.GroupVersionResource{
Group: hypernodev1alpha1.SchemeGroupVersion.Group,
Version: hypernodev1alpha1.SchemeGroupVersion.Version,
Resource: "hypernodes",
}

if resource != hypernodeResource {
klog.Errorf("expect resource to be %s", hypernodeResource)
return nil, fmt.Errorf("expect resource to be %s", hypernodeResource)
}

hypernode := hypernodev1alpha1.HyperNode{}
if _, _, err := Codecs.UniversalDeserializer().Decode(object.Raw, nil, &hypernode); err != nil {
return nil, err
}

return &hypernode, nil
}

0 comments on commit 7885fa5

Please sign in to comment.