Validate topology
This commit is contained in:
parent
a7ca6fa728
commit
d419bdfcd9
@ -6,6 +6,7 @@ go_library(
|
||||
importpath = "k8s.io/kubernetes/pkg/apis/node/validation",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//pkg/apis/core/validation:go_default_library",
|
||||
"//pkg/apis/node:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/validation:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/validation/field:go_default_library",
|
||||
@ -17,6 +18,7 @@ go_test(
|
||||
srcs = ["validation_test.go"],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//pkg/apis/core:go_default_library",
|
||||
"//pkg/apis/node:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//vendor/github.com/stretchr/testify/assert:go_default_library",
|
||||
|
@ -19,6 +19,7 @@ package validation
|
||||
import (
|
||||
apivalidation "k8s.io/apimachinery/pkg/api/validation"
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
corevalidation "k8s.io/kubernetes/pkg/apis/core/validation"
|
||||
"k8s.io/kubernetes/pkg/apis/node"
|
||||
)
|
||||
|
||||
@ -30,6 +31,10 @@ func ValidateRuntimeClass(rc *node.RuntimeClass) field.ErrorList {
|
||||
allErrs = append(allErrs, field.Invalid(field.NewPath("handler"), rc.Handler, msg))
|
||||
}
|
||||
|
||||
if rc.Topology != nil {
|
||||
allErrs = append(allErrs, validateTopology(rc.Topology, field.NewPath("topology"))...)
|
||||
}
|
||||
|
||||
return allErrs
|
||||
}
|
||||
|
||||
@ -41,3 +46,12 @@ func ValidateRuntimeClassUpdate(new, old *node.RuntimeClass) field.ErrorList {
|
||||
|
||||
return allErrs
|
||||
}
|
||||
|
||||
func validateTopology(t *node.Topology, fldPath *field.Path) field.ErrorList {
|
||||
var allErrs field.ErrorList
|
||||
if t.NodeSelector != nil {
|
||||
allErrs = append(allErrs, corevalidation.ValidateNodeSelector(t.NodeSelector, fldPath.Child("nodeSelector"))...)
|
||||
}
|
||||
allErrs = append(allErrs, corevalidation.ValidateTolerations(t.Tolerations, fldPath.Child("tolerations"))...)
|
||||
return allErrs
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ import (
|
||||
"testing"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/kubernetes/pkg/apis/core"
|
||||
"k8s.io/kubernetes/pkg/apis/node"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@ -126,3 +127,91 @@ func TestValidateRuntimeUpdate(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateTopology(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
topology *node.Topology
|
||||
expectErrs int
|
||||
}{{
|
||||
name: "valid topology",
|
||||
topology: &node.Topology{
|
||||
NodeSelector: &core.NodeSelector{
|
||||
NodeSelectorTerms: []core.NodeSelectorTerm{{
|
||||
MatchExpressions: []core.NodeSelectorRequirement{{
|
||||
Key: "valid",
|
||||
Operator: core.NodeSelectorOpExists,
|
||||
}},
|
||||
}},
|
||||
},
|
||||
Tolerations: []core.Toleration{{
|
||||
Key: "valid",
|
||||
Operator: core.TolerationOpExists,
|
||||
Effect: core.TaintEffectNoSchedule,
|
||||
}},
|
||||
},
|
||||
}, {
|
||||
name: "empty topology",
|
||||
topology: &node.Topology{},
|
||||
}, {
|
||||
name: "invalid nodeSelector",
|
||||
topology: &node.Topology{
|
||||
NodeSelector: &core.NodeSelector{
|
||||
NodeSelectorTerms: []core.NodeSelectorTerm{{
|
||||
MatchExpressions: []core.NodeSelectorRequirement{{
|
||||
Key: "not a valid key!!!",
|
||||
Operator: core.NodeSelectorOpExists,
|
||||
}},
|
||||
}},
|
||||
},
|
||||
},
|
||||
expectErrs: 1,
|
||||
}, {
|
||||
name: "invalid toleration",
|
||||
topology: &node.Topology{
|
||||
Tolerations: []core.Toleration{{
|
||||
Key: "valid",
|
||||
Operator: core.TolerationOpExists,
|
||||
Effect: core.TaintEffectNoSchedule,
|
||||
}, {
|
||||
Key: "not a valid key!!!",
|
||||
Operator: core.TolerationOpExists,
|
||||
Effect: core.TaintEffectNoSchedule,
|
||||
}},
|
||||
},
|
||||
expectErrs: 1,
|
||||
}, {
|
||||
name: "invalid topology",
|
||||
topology: &node.Topology{
|
||||
NodeSelector: &core.NodeSelector{
|
||||
NodeSelectorTerms: []core.NodeSelectorTerm{{
|
||||
MatchExpressions: []core.NodeSelectorRequirement{{
|
||||
Key: "not a valid label key!!!",
|
||||
Operator: core.NodeSelectorOpExists,
|
||||
}},
|
||||
}},
|
||||
},
|
||||
Tolerations: []core.Toleration{{
|
||||
Key: "valid",
|
||||
Operator: core.TolerationOpExists,
|
||||
Effect: core.TaintEffectNoSchedule,
|
||||
}, {
|
||||
Key: "not a valid toleration key!!!",
|
||||
Operator: core.TolerationOpExists,
|
||||
Effect: core.TaintEffectNoSchedule,
|
||||
}},
|
||||
},
|
||||
expectErrs: 2,
|
||||
}}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
rc := &node.RuntimeClass{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
|
||||
Handler: "bar",
|
||||
Topology: test.topology,
|
||||
}
|
||||
assert.Len(t, ValidateRuntimeClass(rc), test.expectErrs)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user