Add validation for node creation.
This commit is contained in:
@@ -909,6 +909,28 @@ func ValidateReadOnlyPersistentDisks(volumes []api.Volume) errs.ValidationErrorL
|
||||
func ValidateMinion(node *api.Node) errs.ValidationErrorList {
|
||||
allErrs := errs.ValidationErrorList{}
|
||||
allErrs = append(allErrs, ValidateObjectMeta(&node.ObjectMeta, false, ValidateNodeName).Prefix("metadata")...)
|
||||
// Capacity is required. Within capacity, memory and cpu resources are required.
|
||||
if len(node.Spec.Capacity) == 0 {
|
||||
allErrs = append(allErrs, errs.NewFieldRequired("spec.Capacity"))
|
||||
} else {
|
||||
if val, ok := node.Spec.Capacity[api.ResourceMemory]; !ok {
|
||||
allErrs = append(allErrs, errs.NewFieldRequired("spec.Capacity[memory]"))
|
||||
} else if val.Value() < 0 {
|
||||
allErrs = append(allErrs, errs.NewFieldInvalid("spec.Capacity[memory]", val, "memory capacity cannot be negative"))
|
||||
}
|
||||
if val, ok := node.Spec.Capacity[api.ResourceCPU]; !ok {
|
||||
allErrs = append(allErrs, errs.NewFieldRequired("spec.Capacity[cpu]"))
|
||||
} else if val.Value() < 0 {
|
||||
allErrs = append(allErrs, errs.NewFieldInvalid("spec.Capacity[cpu]", val, "cpu capacity cannot be negative"))
|
||||
}
|
||||
}
|
||||
|
||||
// external ID is required.
|
||||
if len(node.Spec.ExternalID) == 0 {
|
||||
allErrs = append(allErrs, errs.NewFieldRequired("spec.ExternalID"))
|
||||
}
|
||||
|
||||
// TODO(rjnagal): Ignore PodCIDR till its completely implemented.
|
||||
return allErrs
|
||||
}
|
||||
|
||||
|
@@ -1823,6 +1823,14 @@ func TestValidateMinion(t *testing.T) {
|
||||
{Type: api.NodeLegacyHostIP, Address: "something"},
|
||||
},
|
||||
},
|
||||
Spec: api.NodeSpec{
|
||||
ExternalID: "external",
|
||||
Capacity: api.ResourceList{
|
||||
api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
|
||||
api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
|
||||
api.ResourceName("my.org/gpu"): resource.MustParse("10"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
@@ -1833,6 +1841,13 @@ func TestValidateMinion(t *testing.T) {
|
||||
{Type: api.NodeLegacyHostIP, Address: "something"},
|
||||
},
|
||||
},
|
||||
Spec: api.NodeSpec{
|
||||
ExternalID: "external",
|
||||
Capacity: api.ResourceList{
|
||||
api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
|
||||
api.ResourceName(api.ResourceMemory): resource.MustParse("0"),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, successCase := range successCases {
|
||||
@@ -1850,12 +1865,97 @@ func TestValidateMinion(t *testing.T) {
|
||||
Status: api.NodeStatus{
|
||||
Addresses: []api.NodeAddress{},
|
||||
},
|
||||
Spec: api.NodeSpec{
|
||||
ExternalID: "external",
|
||||
Capacity: api.ResourceList{
|
||||
api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
|
||||
api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
|
||||
},
|
||||
},
|
||||
},
|
||||
"invalid-labels": {
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "abc-123",
|
||||
Labels: invalidSelector,
|
||||
},
|
||||
Spec: api.NodeSpec{
|
||||
ExternalID: "external",
|
||||
Capacity: api.ResourceList{
|
||||
api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
|
||||
api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
|
||||
},
|
||||
},
|
||||
},
|
||||
"missing-external-id": {
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "abc-123",
|
||||
Labels: validSelector,
|
||||
},
|
||||
Spec: api.NodeSpec{
|
||||
Capacity: api.ResourceList{
|
||||
api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
|
||||
api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
|
||||
},
|
||||
},
|
||||
},
|
||||
"missing-capacity": {
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "abc-123",
|
||||
Labels: validSelector,
|
||||
},
|
||||
Spec: api.NodeSpec{
|
||||
ExternalID: "external",
|
||||
},
|
||||
},
|
||||
"missing-memory": {
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "abc-123",
|
||||
Labels: validSelector,
|
||||
},
|
||||
Spec: api.NodeSpec{
|
||||
ExternalID: "external",
|
||||
Capacity: api.ResourceList{
|
||||
api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
|
||||
},
|
||||
},
|
||||
},
|
||||
"missing-cpu": {
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "abc-123",
|
||||
Labels: validSelector,
|
||||
},
|
||||
Spec: api.NodeSpec{
|
||||
ExternalID: "external",
|
||||
Capacity: api.ResourceList{
|
||||
api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
|
||||
},
|
||||
},
|
||||
},
|
||||
"invalid-memory": {
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "abc-123",
|
||||
Labels: validSelector,
|
||||
},
|
||||
Spec: api.NodeSpec{
|
||||
ExternalID: "external",
|
||||
Capacity: api.ResourceList{
|
||||
api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
|
||||
api.ResourceName(api.ResourceMemory): resource.MustParse("-10G"),
|
||||
},
|
||||
},
|
||||
},
|
||||
"invalid-cpu": {
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "abc-123",
|
||||
Labels: validSelector,
|
||||
},
|
||||
Spec: api.NodeSpec{
|
||||
ExternalID: "external",
|
||||
Capacity: api.ResourceList{
|
||||
api.ResourceName(api.ResourceCPU): resource.MustParse("-10"),
|
||||
api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for k, v := range errorCases {
|
||||
@@ -1865,10 +1965,17 @@ func TestValidateMinion(t *testing.T) {
|
||||
}
|
||||
for i := range errs {
|
||||
field := errs[i].(*errors.ValidationError).Field
|
||||
if field != "metadata.name" &&
|
||||
field != "metadata.labels" &&
|
||||
field != "metadata.annotations" &&
|
||||
field != "metadata.namespace" {
|
||||
expectedFields := map[string]bool{
|
||||
"metadata.name": true,
|
||||
"metadata.labels": true,
|
||||
"metadata.annotations": true,
|
||||
"metadata.namespace": true,
|
||||
"spec.Capacity": true,
|
||||
"spec.Capacity[memory]": true,
|
||||
"spec.Capacity[cpu]": true,
|
||||
"spec.ExternalID": true,
|
||||
}
|
||||
if expectedFields[field] == false {
|
||||
t.Errorf("%s: missing prefix for: %v", k, errs[i])
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user