...

Source file src/k8s.io/kubernetes/pkg/registry/resource/resourceclaim/strategy.go

Documentation: k8s.io/kubernetes/pkg/registry/resource/resourceclaim

     1  /*
     2  Copyright 2022 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 resourceclaim
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/fields"
    25  	"k8s.io/apimachinery/pkg/labels"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	"k8s.io/apimachinery/pkg/util/validation/field"
    28  	"k8s.io/apiserver/pkg/registry/generic"
    29  	"k8s.io/apiserver/pkg/storage"
    30  	"k8s.io/apiserver/pkg/storage/names"
    31  	"k8s.io/kubernetes/pkg/api/legacyscheme"
    32  	"k8s.io/kubernetes/pkg/apis/resource"
    33  	"k8s.io/kubernetes/pkg/apis/resource/validation"
    34  	"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
    35  )
    36  
    37  // resourceclaimStrategy implements behavior for ResourceClaim objects
    38  type resourceclaimStrategy struct {
    39  	runtime.ObjectTyper
    40  	names.NameGenerator
    41  }
    42  
    43  // Strategy is the default logic that applies when creating and updating
    44  // ResourceClaim objects via the REST API.
    45  var Strategy = resourceclaimStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
    46  
    47  func (resourceclaimStrategy) NamespaceScoped() bool {
    48  	return true
    49  }
    50  
    51  // GetResetFields returns the set of fields that get reset by the strategy and
    52  // should not be modified by the user. For a new ResourceClaim that is the
    53  // status.
    54  func (resourceclaimStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
    55  	fields := map[fieldpath.APIVersion]*fieldpath.Set{
    56  		"resource.k8s.io/v1alpha2": fieldpath.NewSet(
    57  			fieldpath.MakePathOrDie("status"),
    58  		),
    59  	}
    60  
    61  	return fields
    62  }
    63  
    64  func (resourceclaimStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
    65  	claim := obj.(*resource.ResourceClaim)
    66  	// Status must not be set by user on create.
    67  	claim.Status = resource.ResourceClaimStatus{}
    68  }
    69  
    70  func (resourceclaimStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
    71  	claim := obj.(*resource.ResourceClaim)
    72  	return validation.ValidateClaim(claim)
    73  }
    74  
    75  func (resourceclaimStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
    76  	return nil
    77  }
    78  
    79  func (resourceclaimStrategy) Canonicalize(obj runtime.Object) {
    80  }
    81  
    82  func (resourceclaimStrategy) AllowCreateOnUpdate() bool {
    83  	return false
    84  }
    85  
    86  func (resourceclaimStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
    87  	newClaim := obj.(*resource.ResourceClaim)
    88  	oldClaim := old.(*resource.ResourceClaim)
    89  	newClaim.Status = oldClaim.Status
    90  }
    91  
    92  func (resourceclaimStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
    93  	newClaim := obj.(*resource.ResourceClaim)
    94  	oldClaim := old.(*resource.ResourceClaim)
    95  	errorList := validation.ValidateClaim(newClaim)
    96  	return append(errorList, validation.ValidateClaimUpdate(newClaim, oldClaim)...)
    97  }
    98  
    99  func (resourceclaimStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
   100  	return nil
   101  }
   102  
   103  func (resourceclaimStrategy) AllowUnconditionalUpdate() bool {
   104  	return true
   105  }
   106  
   107  type resourceclaimStatusStrategy struct {
   108  	resourceclaimStrategy
   109  }
   110  
   111  var StatusStrategy = resourceclaimStatusStrategy{Strategy}
   112  
   113  // GetResetFields returns the set of fields that get reset by the strategy and
   114  // should not be modified by the user. For a status update that is the spec.
   115  func (resourceclaimStatusStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
   116  	fields := map[fieldpath.APIVersion]*fieldpath.Set{
   117  		"resource.k8s.io/v1alpha2": fieldpath.NewSet(
   118  			fieldpath.MakePathOrDie("spec"),
   119  		),
   120  	}
   121  
   122  	return fields
   123  }
   124  
   125  func (resourceclaimStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
   126  	newClaim := obj.(*resource.ResourceClaim)
   127  	oldClaim := old.(*resource.ResourceClaim)
   128  	newClaim.Spec = oldClaim.Spec
   129  	metav1.ResetObjectMetaForStatus(&newClaim.ObjectMeta, &oldClaim.ObjectMeta)
   130  }
   131  
   132  func (resourceclaimStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
   133  	newClaim := obj.(*resource.ResourceClaim)
   134  	oldClaim := old.(*resource.ResourceClaim)
   135  	return validation.ValidateClaimStatusUpdate(newClaim, oldClaim)
   136  }
   137  
   138  // WarningsOnUpdate returns warnings for the given update.
   139  func (resourceclaimStatusStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
   140  	return nil
   141  }
   142  
   143  // Match returns a generic matcher for a given label and field selector.
   144  func Match(label labels.Selector, field fields.Selector) storage.SelectionPredicate {
   145  	return storage.SelectionPredicate{
   146  		Label:    label,
   147  		Field:    field,
   148  		GetAttrs: GetAttrs,
   149  	}
   150  }
   151  
   152  // GetAttrs returns labels and fields of a given object for filtering purposes.
   153  func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
   154  	claim, ok := obj.(*resource.ResourceClaim)
   155  	if !ok {
   156  		return nil, nil, errors.New("not a resourceclaim")
   157  	}
   158  	return labels.Set(claim.Labels), toSelectableFields(claim), nil
   159  }
   160  
   161  // toSelectableFields returns a field set that represents the object
   162  func toSelectableFields(claim *resource.ResourceClaim) fields.Set {
   163  	fields := generic.ObjectMetaFieldsSet(&claim.ObjectMeta, true)
   164  	return fields
   165  }
   166  

View as plain text