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.
95 lines
3.2 KiB
Go
95 lines
3.2 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 resourceclaimtemplate
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/apimachinery/pkg/util/validation/field"
|
|
"k8s.io/apiserver/pkg/registry/generic"
|
|
"k8s.io/apiserver/pkg/storage/names"
|
|
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
|
"k8s.io/kubernetes/pkg/apis/resource"
|
|
"k8s.io/kubernetes/pkg/apis/resource/validation"
|
|
)
|
|
|
|
// resourceClaimTemplateStrategy implements behavior for ResourceClaimTemplate objects
|
|
type resourceClaimTemplateStrategy struct {
|
|
runtime.ObjectTyper
|
|
names.NameGenerator
|
|
}
|
|
|
|
var Strategy = resourceClaimTemplateStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
|
|
|
|
func (resourceClaimTemplateStrategy) NamespaceScoped() bool {
|
|
return true
|
|
}
|
|
|
|
func (resourceClaimTemplateStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
|
|
}
|
|
|
|
func (resourceClaimTemplateStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
|
|
resourceClaimTemplate := obj.(*resource.ResourceClaimTemplate)
|
|
return validation.ValidateResourceClaimTemplate(resourceClaimTemplate)
|
|
}
|
|
|
|
func (resourceClaimTemplateStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
|
|
return nil
|
|
}
|
|
|
|
func (resourceClaimTemplateStrategy) Canonicalize(obj runtime.Object) {
|
|
}
|
|
|
|
func (resourceClaimTemplateStrategy) AllowCreateOnUpdate() bool {
|
|
return false
|
|
}
|
|
|
|
func (resourceClaimTemplateStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
|
|
}
|
|
|
|
func (resourceClaimTemplateStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
|
|
errorList := validation.ValidateResourceClaimTemplate(obj.(*resource.ResourceClaimTemplate))
|
|
return append(errorList, validation.ValidateResourceClaimTemplateUpdate(obj.(*resource.ResourceClaimTemplate), old.(*resource.ResourceClaimTemplate))...)
|
|
}
|
|
|
|
func (resourceClaimTemplateStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
|
|
return nil
|
|
}
|
|
|
|
func (resourceClaimTemplateStrategy) AllowUnconditionalUpdate() bool {
|
|
return true
|
|
}
|
|
|
|
// GetAttrs returns labels and fields of a given object for filtering purposes.
|
|
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
|
|
template, ok := obj.(*resource.ResourceClaimTemplate)
|
|
if !ok {
|
|
return nil, nil, errors.New("not a resourceclaimtemplate")
|
|
}
|
|
return labels.Set(template.Labels), toSelectableFields(template), nil
|
|
}
|
|
|
|
// toSelectableFields returns a field set that represents the object
|
|
func toSelectableFields(template *resource.ResourceClaimTemplate) fields.Set {
|
|
fields := generic.ObjectMetaFieldsSet(&template.ObjectMeta, true)
|
|
return fields
|
|
}
|