...

Source file src/k8s.io/kubernetes/pkg/registry/flowcontrol/flowschema/strategy.go

Documentation: k8s.io/kubernetes/pkg/registry/flowcontrol/flowschema

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package flowschema
    18  
    19  import (
    20  	"context"
    21  
    22  	apiequality "k8s.io/apimachinery/pkg/api/equality"
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  	"k8s.io/apimachinery/pkg/util/validation/field"
    25  	"k8s.io/apiserver/pkg/storage/names"
    26  	"k8s.io/kubernetes/pkg/api/legacyscheme"
    27  	"k8s.io/kubernetes/pkg/apis/flowcontrol"
    28  	"k8s.io/kubernetes/pkg/apis/flowcontrol/validation"
    29  	"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
    30  )
    31  
    32  // flowSchemaStrategy implements verification logic for FlowSchema.
    33  type flowSchemaStrategy struct {
    34  	runtime.ObjectTyper
    35  	names.NameGenerator
    36  }
    37  
    38  // Strategy is the default logic that applies when creating and updating flow schema objects.
    39  var Strategy = flowSchemaStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
    40  
    41  // NamespaceScoped returns false because all PriorityClasses are global.
    42  func (flowSchemaStrategy) NamespaceScoped() bool {
    43  	return false
    44  }
    45  
    46  // GetResetFields returns the set of fields that get reset by the strategy
    47  // and should not be modified by the user.
    48  func (flowSchemaStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
    49  	fields := map[fieldpath.APIVersion]*fieldpath.Set{
    50  		"flowcontrol.apiserver.k8s.io/v1beta1": fieldpath.NewSet(
    51  			fieldpath.MakePathOrDie("status"),
    52  		),
    53  		"flowcontrol.apiserver.k8s.io/v1beta2": fieldpath.NewSet(
    54  			fieldpath.MakePathOrDie("status"),
    55  		),
    56  		"flowcontrol.apiserver.k8s.io/v1beta3": fieldpath.NewSet(
    57  			fieldpath.MakePathOrDie("status"),
    58  		),
    59  		"flowcontrol.apiserver.k8s.io/v1": fieldpath.NewSet(
    60  			fieldpath.MakePathOrDie("status"),
    61  		),
    62  	}
    63  
    64  	return fields
    65  }
    66  
    67  // PrepareForCreate clears the status of a flow-schema before creation.
    68  func (flowSchemaStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
    69  	fl := obj.(*flowcontrol.FlowSchema)
    70  	fl.Status = flowcontrol.FlowSchemaStatus{}
    71  	fl.Generation = 1
    72  }
    73  
    74  // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
    75  func (flowSchemaStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
    76  	newFlowSchema := obj.(*flowcontrol.FlowSchema)
    77  	oldFlowSchema := old.(*flowcontrol.FlowSchema)
    78  
    79  	// Spec updates bump the generation so that we can distinguish between status updates.
    80  	if !apiequality.Semantic.DeepEqual(newFlowSchema.Spec, oldFlowSchema.Spec) {
    81  		newFlowSchema.Generation = oldFlowSchema.Generation + 1
    82  	}
    83  	newFlowSchema.Status = oldFlowSchema.Status
    84  }
    85  
    86  // Validate validates a new flow-schema.
    87  func (flowSchemaStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
    88  	return validation.ValidateFlowSchema(obj.(*flowcontrol.FlowSchema))
    89  }
    90  
    91  // WarningsOnCreate returns warnings for the creation of the given object.
    92  func (flowSchemaStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
    93  	return nil
    94  }
    95  
    96  // Canonicalize normalizes the object after validation.
    97  func (flowSchemaStrategy) Canonicalize(obj runtime.Object) {
    98  }
    99  
   100  func (flowSchemaStrategy) AllowUnconditionalUpdate() bool {
   101  	return true
   102  }
   103  
   104  // AllowCreateOnUpdate is false for flow-schemas; this means a POST is needed to create one.
   105  func (flowSchemaStrategy) AllowCreateOnUpdate() bool {
   106  	return false
   107  }
   108  
   109  // ValidateUpdate is the default update validation for an end user.
   110  func (flowSchemaStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
   111  	return validation.ValidateFlowSchemaUpdate(old.(*flowcontrol.FlowSchema), obj.(*flowcontrol.FlowSchema))
   112  }
   113  
   114  // WarningsOnUpdate returns warnings for the given update.
   115  func (flowSchemaStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
   116  	return nil
   117  }
   118  
   119  type flowSchemaStatusStrategy struct {
   120  	flowSchemaStrategy
   121  }
   122  
   123  // StatusStrategy is the default logic that applies when updating flow-schema objects' status.
   124  var StatusStrategy = flowSchemaStatusStrategy{Strategy}
   125  
   126  // GetResetFields returns the set of fields that get reset by the strategy
   127  // and should not be modified by the user.
   128  func (flowSchemaStatusStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
   129  	fields := map[fieldpath.APIVersion]*fieldpath.Set{
   130  		"flowcontrol.apiserver.k8s.io/v1beta1": fieldpath.NewSet(
   131  			fieldpath.MakePathOrDie("metadata"),
   132  			fieldpath.MakePathOrDie("spec"),
   133  		),
   134  		"flowcontrol.apiserver.k8s.io/v1beta2": fieldpath.NewSet(
   135  			fieldpath.MakePathOrDie("metadata"),
   136  			fieldpath.MakePathOrDie("spec"),
   137  		),
   138  		"flowcontrol.apiserver.k8s.io/v1beta3": fieldpath.NewSet(
   139  			fieldpath.MakePathOrDie("metadata"),
   140  			fieldpath.MakePathOrDie("spec"),
   141  		),
   142  		"flowcontrol.apiserver.k8s.io/v1": fieldpath.NewSet(
   143  			fieldpath.MakePathOrDie("metadata"),
   144  			fieldpath.MakePathOrDie("spec"),
   145  		),
   146  	}
   147  
   148  	return fields
   149  }
   150  
   151  func (flowSchemaStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
   152  	newFlowSchema := obj.(*flowcontrol.FlowSchema)
   153  	oldFlowSchema := old.(*flowcontrol.FlowSchema)
   154  
   155  	// managedFields must be preserved since it's been modified to
   156  	// track changed fields in the status update.
   157  	managedFields := newFlowSchema.ManagedFields
   158  	newFlowSchema.ObjectMeta = oldFlowSchema.ObjectMeta
   159  	newFlowSchema.ManagedFields = managedFields
   160  	newFlowSchema.Spec = oldFlowSchema.Spec
   161  }
   162  
   163  func (flowSchemaStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
   164  	return validation.ValidateFlowSchemaStatusUpdate(old.(*flowcontrol.FlowSchema), obj.(*flowcontrol.FlowSchema))
   165  }
   166  
   167  // WarningsOnUpdate returns warnings for the given update.
   168  func (flowSchemaStatusStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
   169  	return nil
   170  }
   171  

View as plain text