strategy.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright 2015 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 podsecuritypolicy
  14. import (
  15. "fmt"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/api/rest"
  18. "k8s.io/kubernetes/pkg/apis/extensions"
  19. "k8s.io/kubernetes/pkg/apis/extensions/validation"
  20. "k8s.io/kubernetes/pkg/fields"
  21. "k8s.io/kubernetes/pkg/labels"
  22. "k8s.io/kubernetes/pkg/registry/generic"
  23. "k8s.io/kubernetes/pkg/runtime"
  24. "k8s.io/kubernetes/pkg/util/validation/field"
  25. )
  26. // strategy implements behavior for PodSecurityPolicy objects
  27. type strategy struct {
  28. runtime.ObjectTyper
  29. api.NameGenerator
  30. }
  31. // Strategy is the default logic that applies when creating and updating PodSecurityPolicy
  32. // objects via the REST API.
  33. var Strategy = strategy{api.Scheme, api.SimpleNameGenerator}
  34. var _ = rest.RESTCreateStrategy(Strategy)
  35. var _ = rest.RESTUpdateStrategy(Strategy)
  36. func (strategy) NamespaceScoped() bool {
  37. return false
  38. }
  39. func (strategy) AllowCreateOnUpdate() bool {
  40. return false
  41. }
  42. func (strategy) AllowUnconditionalUpdate() bool {
  43. return true
  44. }
  45. func (strategy) PrepareForCreate(ctx api.Context, obj runtime.Object) {
  46. }
  47. func (strategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
  48. }
  49. func (strategy) Canonicalize(obj runtime.Object) {
  50. }
  51. func (strategy) Validate(ctx api.Context, obj runtime.Object) field.ErrorList {
  52. return validation.ValidatePodSecurityPolicy(obj.(*extensions.PodSecurityPolicy))
  53. }
  54. func (strategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) field.ErrorList {
  55. return validation.ValidatePodSecurityPolicyUpdate(old.(*extensions.PodSecurityPolicy), obj.(*extensions.PodSecurityPolicy))
  56. }
  57. // Matcher returns a generic matcher for a given label and field selector.
  58. func MatchPodSecurityPolicy(label labels.Selector, field fields.Selector) *generic.SelectionPredicate {
  59. return &generic.SelectionPredicate{
  60. Label: label,
  61. Field: field,
  62. GetAttrs: func(obj runtime.Object) (labels.Set, fields.Set, error) {
  63. psp, ok := obj.(*extensions.PodSecurityPolicy)
  64. if !ok {
  65. return nil, nil, fmt.Errorf("given object is not a pod security policy.")
  66. }
  67. return labels.Set(psp.ObjectMeta.Labels), PodSecurityPolicyToSelectableFields(psp), nil
  68. },
  69. }
  70. }
  71. // PodSecurityPolicyToSelectableFields returns a label set that represents the object
  72. func PodSecurityPolicyToSelectableFields(obj *extensions.PodSecurityPolicy) fields.Set {
  73. return generic.ObjectMetaFieldsSet(&obj.ObjectMeta, false)
  74. }