mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-10-09 00:26:58 +00:00
This is a complete revamp of the original API. Some of the key differences: - refocused on structured parameters and allocating devices - support for constraints across devices - support for allocating "all" or a fixed amount of similar devices in a single request - no class for ResourceClaims, instead individual device requests are associated with a mandatory DeviceClass For the sake of simplicity, optional basic types (ints, strings) where the null value is the default are represented as values in the API types. This makes Go code simpler because it doesn't have to check for nil (consumers) and values can be set directly (producers). The effect is that in protobuf, these fields always get encoded because `opt` only has an effect for pointers. The roundtrip test data for v1.29.0 and v1.30.0 changes because of the new "request" field. This is considered acceptable because the entire `claims` field in the pod spec is still alpha. The implementation is complete enough to bring up the apiserver. Adapting other components follows.
269 lines
10 KiB
Go
269 lines
10 KiB
Go
/*
|
|
Copyright 2022 The Kubernetes 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 validation
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/util/validation/field"
|
|
"k8s.io/kubernetes/pkg/apis/resource"
|
|
"k8s.io/utils/ptr"
|
|
)
|
|
|
|
func testResourceSlice(name, nodeName, driverName string) *resource.ResourceSlice {
|
|
return &resource.ResourceSlice{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: name,
|
|
},
|
|
Spec: resource.ResourceSliceSpec{
|
|
NodeName: nodeName,
|
|
Driver: driverName,
|
|
Pool: resource.ResourcePool{
|
|
Name: nodeName,
|
|
ResourceSliceCount: 1,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestValidateResourceSlice(t *testing.T) {
|
|
goodName := "foo"
|
|
badName := "!@#$%^"
|
|
driverName := "test.example.com"
|
|
now := metav1.Now()
|
|
badValue := "spaces not allowed"
|
|
|
|
scenarios := map[string]struct {
|
|
slice *resource.ResourceSlice
|
|
wantFailures field.ErrorList
|
|
}{
|
|
"good": {
|
|
slice: testResourceSlice(goodName, goodName, driverName),
|
|
},
|
|
"missing-name": {
|
|
wantFailures: field.ErrorList{field.Required(field.NewPath("metadata", "name"), "name or generateName is required")},
|
|
slice: testResourceSlice("", goodName, driverName),
|
|
},
|
|
"bad-name": {
|
|
wantFailures: field.ErrorList{field.Invalid(field.NewPath("metadata", "name"), badName, "a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')")},
|
|
slice: testResourceSlice(badName, goodName, driverName),
|
|
},
|
|
"generate-name": {
|
|
slice: func() *resource.ResourceSlice {
|
|
slice := testResourceSlice(goodName, goodName, driverName)
|
|
slice.GenerateName = "prefix-"
|
|
return slice
|
|
}(),
|
|
},
|
|
"uid": {
|
|
slice: func() *resource.ResourceSlice {
|
|
slice := testResourceSlice(goodName, goodName, driverName)
|
|
slice.UID = "ac051fac-2ead-46d9-b8b4-4e0fbeb7455d"
|
|
return slice
|
|
}(),
|
|
},
|
|
"resource-version": {
|
|
slice: func() *resource.ResourceSlice {
|
|
slice := testResourceSlice(goodName, goodName, driverName)
|
|
slice.ResourceVersion = "1"
|
|
return slice
|
|
}(),
|
|
},
|
|
"generation": {
|
|
slice: func() *resource.ResourceSlice {
|
|
slice := testResourceSlice(goodName, goodName, driverName)
|
|
slice.Generation = 100
|
|
return slice
|
|
}(),
|
|
},
|
|
"creation-timestamp": {
|
|
slice: func() *resource.ResourceSlice {
|
|
slice := testResourceSlice(goodName, goodName, driverName)
|
|
slice.CreationTimestamp = now
|
|
return slice
|
|
}(),
|
|
},
|
|
"deletion-grace-period-seconds": {
|
|
slice: func() *resource.ResourceSlice {
|
|
slice := testResourceSlice(goodName, goodName, driverName)
|
|
slice.DeletionGracePeriodSeconds = ptr.To[int64](10)
|
|
return slice
|
|
}(),
|
|
},
|
|
"owner-references": {
|
|
slice: func() *resource.ResourceSlice {
|
|
slice := testResourceSlice(goodName, goodName, driverName)
|
|
slice.OwnerReferences = []metav1.OwnerReference{
|
|
{
|
|
APIVersion: "v1",
|
|
Kind: "pod",
|
|
Name: "foo",
|
|
UID: "ac051fac-2ead-46d9-b8b4-4e0fbeb7455d",
|
|
},
|
|
}
|
|
return slice
|
|
}(),
|
|
},
|
|
"finalizers": {
|
|
slice: func() *resource.ResourceSlice {
|
|
slice := testResourceSlice(goodName, goodName, driverName)
|
|
slice.Finalizers = []string{
|
|
"example.com/foo",
|
|
}
|
|
return slice
|
|
}(),
|
|
},
|
|
"managed-fields": {
|
|
slice: func() *resource.ResourceSlice {
|
|
slice := testResourceSlice(goodName, goodName, driverName)
|
|
slice.ManagedFields = []metav1.ManagedFieldsEntry{
|
|
{
|
|
FieldsType: "FieldsV1",
|
|
Operation: "Apply",
|
|
APIVersion: "apps/v1",
|
|
Manager: "foo",
|
|
},
|
|
}
|
|
return slice
|
|
}(),
|
|
},
|
|
"good-labels": {
|
|
slice: func() *resource.ResourceSlice {
|
|
slice := testResourceSlice(goodName, goodName, driverName)
|
|
slice.Labels = map[string]string{
|
|
"apps.kubernetes.io/name": "test",
|
|
}
|
|
return slice
|
|
}(),
|
|
},
|
|
"bad-labels": {
|
|
wantFailures: field.ErrorList{field.Invalid(field.NewPath("metadata", "labels"), badValue, "a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyValue', or 'my_value', or '12345', regex used for validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?')")},
|
|
slice: func() *resource.ResourceSlice {
|
|
slice := testResourceSlice(goodName, goodName, driverName)
|
|
slice.Labels = map[string]string{
|
|
"hello-world": badValue,
|
|
}
|
|
return slice
|
|
}(),
|
|
},
|
|
"good-annotations": {
|
|
slice: func() *resource.ResourceSlice {
|
|
slice := testResourceSlice(goodName, goodName, driverName)
|
|
slice.Annotations = map[string]string{
|
|
"foo": "bar",
|
|
}
|
|
return slice
|
|
}(),
|
|
},
|
|
"bad-annotations": {
|
|
wantFailures: field.ErrorList{field.Invalid(field.NewPath("metadata", "annotations"), badName, "name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')")},
|
|
slice: func() *resource.ResourceSlice {
|
|
slice := testResourceSlice(goodName, goodName, driverName)
|
|
slice.Annotations = map[string]string{
|
|
badName: "hello world",
|
|
}
|
|
return slice
|
|
}(),
|
|
},
|
|
"bad-nodename": {
|
|
wantFailures: field.ErrorList{
|
|
field.Invalid(field.NewPath("spec", "pool", "name"), badName, "a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')"),
|
|
field.Invalid(field.NewPath("spec", "nodeName"), badName, "a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')"),
|
|
},
|
|
slice: testResourceSlice(goodName, badName, driverName),
|
|
},
|
|
"bad-multi-pool-name": {
|
|
wantFailures: field.ErrorList{
|
|
field.Invalid(field.NewPath("spec", "pool", "name"), badName, "a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')"),
|
|
field.Invalid(field.NewPath("spec", "pool", "name"), badName, "a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')"),
|
|
field.Invalid(field.NewPath("spec", "nodeName"), badName+"/"+badName, "a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')"),
|
|
},
|
|
slice: testResourceSlice(goodName, badName+"/"+badName, driverName),
|
|
},
|
|
"bad-drivername": {
|
|
wantFailures: field.ErrorList{field.Invalid(field.NewPath("spec", "driver"), badName, "a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')")},
|
|
slice: testResourceSlice(goodName, goodName, badName),
|
|
},
|
|
}
|
|
|
|
for name, scenario := range scenarios {
|
|
t.Run(name, func(t *testing.T) {
|
|
errs := ValidateResourceSlice(scenario.slice)
|
|
assert.Equal(t, scenario.wantFailures, errs)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidateResourceSliceUpdate(t *testing.T) {
|
|
name := "valid"
|
|
validResourceSlice := testResourceSlice(name, name, name)
|
|
|
|
scenarios := map[string]struct {
|
|
oldResourceSlice *resource.ResourceSlice
|
|
update func(slice *resource.ResourceSlice) *resource.ResourceSlice
|
|
wantFailures field.ErrorList
|
|
}{
|
|
"valid-no-op-update": {
|
|
oldResourceSlice: validResourceSlice,
|
|
update: func(slice *resource.ResourceSlice) *resource.ResourceSlice { return slice },
|
|
},
|
|
"invalid-name-update": {
|
|
oldResourceSlice: validResourceSlice,
|
|
update: func(slice *resource.ResourceSlice) *resource.ResourceSlice {
|
|
slice.Name += "-update"
|
|
return slice
|
|
},
|
|
wantFailures: field.ErrorList{field.Invalid(field.NewPath("metadata", "name"), name+"-update", "field is immutable")},
|
|
},
|
|
"invalid-update-nodename": {
|
|
wantFailures: field.ErrorList{field.Invalid(field.NewPath("spec", "nodeName"), name+"-updated", "field is immutable")},
|
|
oldResourceSlice: validResourceSlice,
|
|
update: func(slice *resource.ResourceSlice) *resource.ResourceSlice {
|
|
slice.Spec.NodeName += "-updated"
|
|
return slice
|
|
},
|
|
},
|
|
"invalid-update-drivername": {
|
|
wantFailures: field.ErrorList{field.Invalid(field.NewPath("spec", "driver"), name+"-updated", "field is immutable")},
|
|
oldResourceSlice: validResourceSlice,
|
|
update: func(slice *resource.ResourceSlice) *resource.ResourceSlice {
|
|
slice.Spec.Driver += "-updated"
|
|
return slice
|
|
},
|
|
},
|
|
"invalid-update-pool": {
|
|
wantFailures: field.ErrorList{field.Invalid(field.NewPath("spec", "pool", "name"), validResourceSlice.Spec.Pool.Name+"-updated", "field is immutable")},
|
|
oldResourceSlice: validResourceSlice,
|
|
update: func(slice *resource.ResourceSlice) *resource.ResourceSlice {
|
|
slice.Spec.Pool.Name += "-updated"
|
|
return slice
|
|
},
|
|
},
|
|
}
|
|
|
|
for name, scenario := range scenarios {
|
|
t.Run(name, func(t *testing.T) {
|
|
scenario.oldResourceSlice.ResourceVersion = "1"
|
|
errs := ValidateResourceSliceUpdate(scenario.update(scenario.oldResourceSlice.DeepCopy()), scenario.oldResourceSlice)
|
|
assert.Equal(t, scenario.wantFailures, errs)
|
|
})
|
|
}
|
|
}
|