strategy.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Copyright 2018 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 auditsink
  14. import (
  15. "context"
  16. "reflect"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "k8s.io/apimachinery/pkg/util/validation/field"
  19. "k8s.io/apiserver/pkg/storage/names"
  20. "k8s.io/kubernetes/pkg/api/legacyscheme"
  21. audit "k8s.io/kubernetes/pkg/apis/auditregistration"
  22. "k8s.io/kubernetes/pkg/apis/auditregistration/validation"
  23. )
  24. // auditSinkStrategy implements verification logic for AuditSink.
  25. type auditSinkStrategy struct {
  26. runtime.ObjectTyper
  27. names.NameGenerator
  28. }
  29. // Strategy is the default logic that applies when creating and updating AuditSink objects.
  30. var Strategy = auditSinkStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  31. // NamespaceScoped returns false because all AuditSink's need to be cluster scoped
  32. func (auditSinkStrategy) NamespaceScoped() bool {
  33. return false
  34. }
  35. // PrepareForCreate clears the status of an AuditSink before creation.
  36. func (auditSinkStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  37. ic := obj.(*audit.AuditSink)
  38. ic.Generation = 1
  39. }
  40. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  41. func (auditSinkStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  42. newIC := obj.(*audit.AuditSink)
  43. oldIC := old.(*audit.AuditSink)
  44. // Any changes to the policy or backend increment the generation number
  45. // See metav1.ObjectMeta description for more information on Generation.
  46. if !reflect.DeepEqual(oldIC.Spec, newIC.Spec) {
  47. newIC.Generation = oldIC.Generation + 1
  48. }
  49. }
  50. // Validate validates a new auditSink.
  51. func (auditSinkStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  52. ic := obj.(*audit.AuditSink)
  53. return validation.ValidateAuditSink(ic)
  54. }
  55. // Canonicalize normalizes the object after validation.
  56. func (auditSinkStrategy) Canonicalize(obj runtime.Object) {
  57. }
  58. // AllowCreateOnUpdate is true for auditSink; this means you may create one with a PUT request.
  59. func (auditSinkStrategy) AllowCreateOnUpdate() bool {
  60. return false
  61. }
  62. // ValidateUpdate is the default update validation for an end user.
  63. func (auditSinkStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  64. validationErrorList := validation.ValidateAuditSink(obj.(*audit.AuditSink))
  65. updateErrorList := validation.ValidateAuditSinkUpdate(obj.(*audit.AuditSink), old.(*audit.AuditSink))
  66. return append(validationErrorList, updateErrorList...)
  67. }
  68. // AllowUnconditionalUpdate is the default update policy for auditSink objects. Status update should
  69. // only be allowed if version match.
  70. func (auditSinkStrategy) AllowUnconditionalUpdate() bool {
  71. return false
  72. }