Files
kubernetes/pkg/registry/resource/deviceclass/strategy.go
Patrick Ohly 9f36c8d718 DRA: add DRAControlPlaneController feature gate for "classic DRA"
In the API, the effect of the feature gate is that alpha fields get dropped on
create. They get preserved during updates if already set. The
PodSchedulingContext registration is *not* restricted by the feature gate.
This enables deleting stale PodSchedulingContext objects after disabling
the feature gate.

The scheduler checks the new feature gate before setting up an informer for
PodSchedulingContext objects and when deciding whether it can schedule a
pod. If any claim depends on a control plane controller, the scheduler bails
out, leading to:

    Status:       Pending
    ...
      Warning  FailedScheduling             73s   default-scheduler  0/1 nodes are available: resourceclaim depends on disabled DRAControlPlaneController feature. no new claims to deallocate, preemption: 0/1 nodes are available: 1 Preemption is not helpful for scheduling.

The rest of the changes prepare for testing the new feature separately from
"structured parameters". The goal is to have base "dra" jobs which just enable
and test those, then "classic-dra" jobs which add DRAControlPlaneController.
2024-07-22 18:09:34 +02:00

110 lines
3.3 KiB
Go

/*
Copyright 2024 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 deviceclass
import (
"context"
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apiserver/pkg/storage/names"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/kubernetes/pkg/api/legacyscheme"
"k8s.io/kubernetes/pkg/apis/resource"
"k8s.io/kubernetes/pkg/apis/resource/validation"
"k8s.io/kubernetes/pkg/features"
)
// deviceClassStrategy implements behavior for DeviceClass objects
type deviceClassStrategy struct {
runtime.ObjectTyper
names.NameGenerator
}
var Strategy = deviceClassStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
func (deviceClassStrategy) NamespaceScoped() bool {
return false
}
func (deviceClassStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
class := obj.(*resource.DeviceClass)
class.Generation = 1
dropDisabledFields(class, nil)
}
func (deviceClassStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
deviceClass := obj.(*resource.DeviceClass)
return validation.ValidateDeviceClass(deviceClass)
}
func (deviceClassStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
return nil
}
func (deviceClassStrategy) Canonicalize(obj runtime.Object) {
}
func (deviceClassStrategy) AllowCreateOnUpdate() bool {
return false
}
func (deviceClassStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
class := obj.(*resource.DeviceClass)
oldClass := old.(*resource.DeviceClass)
dropDisabledFields(class, oldClass)
// Any changes to the spec increment the generation number.
if !apiequality.Semantic.DeepEqual(oldClass.Spec, class.Spec) {
class.Generation = oldClass.Generation + 1
}
}
func (deviceClassStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
errorList := validation.ValidateDeviceClass(obj.(*resource.DeviceClass))
return append(errorList, validation.ValidateDeviceClassUpdate(obj.(*resource.DeviceClass), old.(*resource.DeviceClass))...)
}
func (deviceClassStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
return nil
}
func (deviceClassStrategy) AllowUnconditionalUpdate() bool {
return true
}
// dropDisabledFields removes fields which are covered by the optional DRAControlPlaneController feature gate.
func dropDisabledFields(newClass, oldClass *resource.DeviceClass) {
if utilfeature.DefaultFeatureGate.Enabled(features.DRAControlPlaneController) {
// No need to drop anything.
return
}
if oldClass == nil {
// Always drop on create.
newClass.Spec.SuitableNodes = nil
return
}
// Drop on update only if not already set.
if oldClass.Spec.SuitableNodes == nil {
newClass.Spec.SuitableNodes = nil
}
}