strategy.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package event
  14. import (
  15. "context"
  16. "fmt"
  17. "k8s.io/apimachinery/pkg/fields"
  18. "k8s.io/apimachinery/pkg/labels"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/apimachinery/pkg/util/validation/field"
  21. "k8s.io/apiserver/pkg/registry/generic"
  22. "k8s.io/apiserver/pkg/registry/rest"
  23. "k8s.io/apiserver/pkg/storage"
  24. "k8s.io/apiserver/pkg/storage/names"
  25. "k8s.io/kubernetes/pkg/api/legacyscheme"
  26. api "k8s.io/kubernetes/pkg/apis/core"
  27. "k8s.io/kubernetes/pkg/apis/core/validation"
  28. )
  29. type eventStrategy struct {
  30. runtime.ObjectTyper
  31. names.NameGenerator
  32. }
  33. // Strategy is the default logic that pplies when creating and updating
  34. // Event objects via the REST API.
  35. var Strategy = eventStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  36. func (eventStrategy) DefaultGarbageCollectionPolicy(ctx context.Context) rest.GarbageCollectionPolicy {
  37. return rest.Unsupported
  38. }
  39. func (eventStrategy) NamespaceScoped() bool {
  40. return true
  41. }
  42. func (eventStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  43. }
  44. func (eventStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  45. }
  46. func (eventStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  47. event := obj.(*api.Event)
  48. return validation.ValidateEvent(event)
  49. }
  50. // Canonicalize normalizes the object after validation.
  51. func (eventStrategy) Canonicalize(obj runtime.Object) {
  52. }
  53. func (eventStrategy) AllowCreateOnUpdate() bool {
  54. return true
  55. }
  56. func (eventStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  57. event := obj.(*api.Event)
  58. return validation.ValidateEvent(event)
  59. }
  60. func (eventStrategy) AllowUnconditionalUpdate() bool {
  61. return true
  62. }
  63. // GetAttrs returns labels and fields of a given object for filtering purposes.
  64. func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
  65. event, ok := obj.(*api.Event)
  66. if !ok {
  67. return nil, nil, fmt.Errorf("not an event")
  68. }
  69. return labels.Set(event.Labels), EventToSelectableFields(event), nil
  70. }
  71. func MatchEvent(label labels.Selector, field fields.Selector) storage.SelectionPredicate {
  72. return storage.SelectionPredicate{
  73. Label: label,
  74. Field: field,
  75. GetAttrs: GetAttrs,
  76. }
  77. }
  78. // EventToSelectableFields returns a field set that represents the object
  79. func EventToSelectableFields(event *api.Event) fields.Set {
  80. objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&event.ObjectMeta, true)
  81. specificFieldsSet := fields.Set{
  82. "involvedObject.kind": event.InvolvedObject.Kind,
  83. "involvedObject.namespace": event.InvolvedObject.Namespace,
  84. "involvedObject.name": event.InvolvedObject.Name,
  85. "involvedObject.uid": string(event.InvolvedObject.UID),
  86. "involvedObject.apiVersion": event.InvolvedObject.APIVersion,
  87. "involvedObject.resourceVersion": event.InvolvedObject.ResourceVersion,
  88. "involvedObject.fieldPath": event.InvolvedObject.FieldPath,
  89. "reason": event.Reason,
  90. "source": event.Source.Component,
  91. "type": event.Type,
  92. }
  93. return generic.MergeFieldsSets(objectMetaFieldsSet, specificFieldsSet)
  94. }