strategy.go 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. Copyright 2017 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 networkpolicy
  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. "k8s.io/kubernetes/pkg/apis/networking"
  22. "k8s.io/kubernetes/pkg/apis/networking/validation"
  23. )
  24. // networkPolicyStrategy implements verification logic for NetworkPolicies
  25. type networkPolicyStrategy struct {
  26. runtime.ObjectTyper
  27. names.NameGenerator
  28. }
  29. // Strategy is the default logic that applies when creating and updating NetworkPolicy objects.
  30. var Strategy = networkPolicyStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  31. // NamespaceScoped returns true because all NetworkPolicies need to be within a namespace.
  32. func (networkPolicyStrategy) NamespaceScoped() bool {
  33. return true
  34. }
  35. // PrepareForCreate clears the status of a NetworkPolicy before creation.
  36. func (networkPolicyStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  37. networkPolicy := obj.(*networking.NetworkPolicy)
  38. networkPolicy.Generation = 1
  39. }
  40. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  41. func (networkPolicyStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  42. newNetworkPolicy := obj.(*networking.NetworkPolicy)
  43. oldNetworkPolicy := old.(*networking.NetworkPolicy)
  44. // Any changes to the spec increment the generation number, any changes to the
  45. // status should reflect the generation number of the corresponding object.
  46. // See metav1.ObjectMeta description for more information on Generation.
  47. if !reflect.DeepEqual(oldNetworkPolicy.Spec, newNetworkPolicy.Spec) {
  48. newNetworkPolicy.Generation = oldNetworkPolicy.Generation + 1
  49. }
  50. }
  51. // Validate validates a new NetworkPolicy.
  52. func (networkPolicyStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  53. networkPolicy := obj.(*networking.NetworkPolicy)
  54. allErrs := validation.ValidateNetworkPolicy(networkPolicy)
  55. allErrs = append(allErrs, validation.ValidateConditionalNetworkPolicy(networkPolicy, nil)...)
  56. return allErrs
  57. }
  58. // Canonicalize normalizes the object after validation.
  59. func (networkPolicyStrategy) Canonicalize(obj runtime.Object) {}
  60. // AllowCreateOnUpdate is false for NetworkPolicy; this means POST is needed to create one.
  61. func (networkPolicyStrategy) AllowCreateOnUpdate() bool {
  62. return false
  63. }
  64. // ValidateUpdate is the default update validation for an end user.
  65. func (networkPolicyStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  66. validationErrorList := validation.ValidateNetworkPolicy(obj.(*networking.NetworkPolicy))
  67. updateErrorList := validation.ValidateNetworkPolicyUpdate(obj.(*networking.NetworkPolicy), old.(*networking.NetworkPolicy))
  68. updateErrorList = append(updateErrorList, validation.ValidateConditionalNetworkPolicy(obj.(*networking.NetworkPolicy), old.(*networking.NetworkPolicy))...)
  69. return append(validationErrorList, updateErrorList...)
  70. }
  71. // AllowUnconditionalUpdate is the default update policy for NetworkPolicy objects.
  72. func (networkPolicyStrategy) AllowUnconditionalUpdate() bool {
  73. return true
  74. }