...

Source file src/k8s.io/kubernetes/pkg/registry/core/event/strategy.go

Documentation: k8s.io/kubernetes/pkg/registry/core/event

     1  /*
     2  Copyright 2014 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 event
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"k8s.io/apimachinery/pkg/fields"
    24  	"k8s.io/apimachinery/pkg/labels"
    25  	"k8s.io/apimachinery/pkg/runtime"
    26  	"k8s.io/apimachinery/pkg/runtime/schema"
    27  	"k8s.io/apimachinery/pkg/util/validation/field"
    28  	genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
    29  	"k8s.io/apiserver/pkg/registry/generic"
    30  	"k8s.io/apiserver/pkg/registry/rest"
    31  	"k8s.io/apiserver/pkg/storage"
    32  	"k8s.io/apiserver/pkg/storage/names"
    33  	"k8s.io/kubernetes/pkg/api/legacyscheme"
    34  	api "k8s.io/kubernetes/pkg/apis/core"
    35  	"k8s.io/kubernetes/pkg/apis/core/validation"
    36  )
    37  
    38  type eventStrategy struct {
    39  	runtime.ObjectTyper
    40  	names.NameGenerator
    41  }
    42  
    43  // Strategy is the default logic that applies when creating and updating
    44  // Event objects via the REST API.
    45  var Strategy = eventStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
    46  
    47  func (eventStrategy) DefaultGarbageCollectionPolicy(ctx context.Context) rest.GarbageCollectionPolicy {
    48  	return rest.Unsupported
    49  }
    50  
    51  func (eventStrategy) NamespaceScoped() bool {
    52  	return true
    53  }
    54  
    55  func (eventStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
    56  }
    57  
    58  func (eventStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
    59  }
    60  
    61  func (eventStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
    62  	groupVersion := requestGroupVersion(ctx)
    63  	event := obj.(*api.Event)
    64  	return validation.ValidateEventCreate(event, groupVersion)
    65  }
    66  
    67  // WarningsOnCreate returns warnings for the creation of the given object.
    68  func (eventStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { return nil }
    69  
    70  // Canonicalize normalizes the object after validation.
    71  func (eventStrategy) Canonicalize(obj runtime.Object) {
    72  }
    73  
    74  func (eventStrategy) AllowCreateOnUpdate() bool {
    75  	return true
    76  }
    77  
    78  func (eventStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
    79  	groupVersion := requestGroupVersion(ctx)
    80  	event := obj.(*api.Event)
    81  	oldEvent := old.(*api.Event)
    82  	return validation.ValidateEventUpdate(event, oldEvent, groupVersion)
    83  }
    84  
    85  // WarningsOnUpdate returns warnings for the given update.
    86  func (eventStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
    87  	return nil
    88  }
    89  
    90  func (eventStrategy) AllowUnconditionalUpdate() bool {
    91  	return true
    92  }
    93  
    94  // GetAttrs returns labels and fields of a given object for filtering purposes.
    95  func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
    96  	event, ok := obj.(*api.Event)
    97  	if !ok {
    98  		return nil, nil, fmt.Errorf("not an event")
    99  	}
   100  	return labels.Set(event.Labels), ToSelectableFields(event), nil
   101  }
   102  
   103  // Matcher returns a selection predicate for a given label and field selector.
   104  func Matcher(label labels.Selector, field fields.Selector) storage.SelectionPredicate {
   105  	return storage.SelectionPredicate{
   106  		Label:    label,
   107  		Field:    field,
   108  		GetAttrs: GetAttrs,
   109  	}
   110  }
   111  
   112  // ToSelectableFields returns a field set that represents the object.
   113  func ToSelectableFields(event *api.Event) fields.Set {
   114  	objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&event.ObjectMeta, true)
   115  	source := event.Source.Component
   116  	if source == "" {
   117  		source = event.ReportingController
   118  	}
   119  	specificFieldsSet := fields.Set{
   120  		"involvedObject.kind":            event.InvolvedObject.Kind,
   121  		"involvedObject.namespace":       event.InvolvedObject.Namespace,
   122  		"involvedObject.name":            event.InvolvedObject.Name,
   123  		"involvedObject.uid":             string(event.InvolvedObject.UID),
   124  		"involvedObject.apiVersion":      event.InvolvedObject.APIVersion,
   125  		"involvedObject.resourceVersion": event.InvolvedObject.ResourceVersion,
   126  		"involvedObject.fieldPath":       event.InvolvedObject.FieldPath,
   127  		"reason":                         event.Reason,
   128  		"reportingComponent":             event.ReportingController, // use the core/v1 field name
   129  		"source":                         source,
   130  		"type":                           event.Type,
   131  	}
   132  	return generic.MergeFieldsSets(specificFieldsSet, objectMetaFieldsSet)
   133  }
   134  
   135  // requestGroupVersion returns the group/version associated with the given context, or a zero-value group/version.
   136  func requestGroupVersion(ctx context.Context) schema.GroupVersion {
   137  	if requestInfo, found := genericapirequest.RequestInfoFrom(ctx); found {
   138  		return schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}
   139  	}
   140  	return schema.GroupVersion{}
   141  }
   142  

View as plain text