mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-05 23:47:50 +00:00
Migrate RuntimeClass to internal API
This commit is contained in:
38
pkg/apis/node/validation/BUILD
Normal file
38
pkg/apis/node/validation/BUILD
Normal file
@@ -0,0 +1,38 @@
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["validation.go"],
|
||||
importpath = "k8s.io/kubernetes/pkg/apis/node/validation",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//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",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["validation_test.go"],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//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",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
43
pkg/apis/node/validation/validation.go
Normal file
43
pkg/apis/node/validation/validation.go
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
Copyright 2019 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 (
|
||||
apivalidation "k8s.io/apimachinery/pkg/api/validation"
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
"k8s.io/kubernetes/pkg/apis/node"
|
||||
)
|
||||
|
||||
// ValidateRuntimeClass validates the RuntimeClass
|
||||
func ValidateRuntimeClass(rc *node.RuntimeClass) field.ErrorList {
|
||||
allErrs := apivalidation.ValidateObjectMeta(&rc.ObjectMeta, false, apivalidation.NameIsDNSSubdomain, field.NewPath("metadata"))
|
||||
|
||||
for _, msg := range apivalidation.NameIsDNSLabel(rc.Handler, false) {
|
||||
allErrs = append(allErrs, field.Invalid(field.NewPath("handler"), rc.Handler, msg))
|
||||
}
|
||||
|
||||
return allErrs
|
||||
}
|
||||
|
||||
// ValidateRuntimeClassUpdate validates an update to the object
|
||||
func ValidateRuntimeClassUpdate(new, old *node.RuntimeClass) field.ErrorList {
|
||||
allErrs := apivalidation.ValidateObjectMetaUpdate(&new.ObjectMeta, &old.ObjectMeta, field.NewPath("metadata"))
|
||||
|
||||
allErrs = append(allErrs, apivalidation.ValidateImmutableField(new.Handler, old.Handler, field.NewPath("handler"))...)
|
||||
|
||||
return allErrs
|
||||
}
|
||||
128
pkg/apis/node/validation/validation_test.go
Normal file
128
pkg/apis/node/validation/validation_test.go
Normal file
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
Copyright 2018 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"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/kubernetes/pkg/apis/node"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValidateRuntimeClass(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
rc node.RuntimeClass
|
||||
expectError bool
|
||||
}{{
|
||||
name: "invalid name",
|
||||
expectError: true,
|
||||
rc: node.RuntimeClass{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "&!@#"},
|
||||
Handler: "foo",
|
||||
},
|
||||
}, {
|
||||
name: "invalid Handler name",
|
||||
expectError: true,
|
||||
rc: node.RuntimeClass{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
|
||||
Handler: "&@#$",
|
||||
},
|
||||
}, {
|
||||
name: "invalid empty RuntimeClass",
|
||||
expectError: true,
|
||||
rc: node.RuntimeClass{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "empty"},
|
||||
},
|
||||
}, {
|
||||
name: "valid Handler",
|
||||
expectError: false,
|
||||
rc: node.RuntimeClass{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
|
||||
Handler: "bar-baz",
|
||||
},
|
||||
}}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
errs := ValidateRuntimeClass(&test.rc)
|
||||
if test.expectError {
|
||||
assert.NotEmpty(t, errs)
|
||||
} else {
|
||||
assert.Empty(t, errs)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRuntimeUpdate(t *testing.T) {
|
||||
old := node.RuntimeClass{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
|
||||
Handler: "bar",
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
expectError bool
|
||||
old, new node.RuntimeClass
|
||||
}{{
|
||||
name: "valid metadata update",
|
||||
old: old,
|
||||
new: node.RuntimeClass{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo",
|
||||
Labels: map[string]string{"foo": "bar"},
|
||||
},
|
||||
Handler: "bar",
|
||||
},
|
||||
}, {
|
||||
name: "invalid metadata update",
|
||||
expectError: true,
|
||||
old: old,
|
||||
new: node.RuntimeClass{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "empty",
|
||||
ClusterName: "somethingelse", // immutable
|
||||
},
|
||||
Handler: "bar",
|
||||
},
|
||||
}, {
|
||||
name: "invalid Handler update",
|
||||
expectError: true,
|
||||
old: old,
|
||||
new: node.RuntimeClass{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
|
||||
Handler: "somethingelse",
|
||||
},
|
||||
}}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
// So we don't need to write it in every test case...
|
||||
test.old.ObjectMeta.ResourceVersion = "1"
|
||||
test.new.ObjectMeta.ResourceVersion = "1"
|
||||
|
||||
errs := ValidateRuntimeClassUpdate(&test.new, &test.old)
|
||||
if test.expectError {
|
||||
assert.NotEmpty(t, errs)
|
||||
} else {
|
||||
assert.Empty(t, errs)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user