strategy.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 serviceaccount
  14. import (
  15. "context"
  16. "k8s.io/apimachinery/pkg/runtime"
  17. "k8s.io/apimachinery/pkg/util/validation/field"
  18. "k8s.io/apiserver/pkg/storage/names"
  19. "k8s.io/kubernetes/pkg/api/legacyscheme"
  20. api "k8s.io/kubernetes/pkg/apis/core"
  21. "k8s.io/kubernetes/pkg/apis/core/validation"
  22. )
  23. // strategy implements behavior for ServiceAccount objects
  24. type strategy struct {
  25. runtime.ObjectTyper
  26. names.NameGenerator
  27. }
  28. // Strategy is the default logic that applies when creating and updating ServiceAccount
  29. // objects via the REST API.
  30. var Strategy = strategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  31. func (strategy) NamespaceScoped() bool {
  32. return true
  33. }
  34. func (strategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  35. cleanSecretReferences(obj.(*api.ServiceAccount))
  36. }
  37. func (strategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  38. return validation.ValidateServiceAccount(obj.(*api.ServiceAccount))
  39. }
  40. // Canonicalize normalizes the object after validation.
  41. func (strategy) Canonicalize(obj runtime.Object) {
  42. }
  43. func (strategy) AllowCreateOnUpdate() bool {
  44. return false
  45. }
  46. func (strategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  47. cleanSecretReferences(obj.(*api.ServiceAccount))
  48. }
  49. func cleanSecretReferences(serviceAccount *api.ServiceAccount) {
  50. for i, secret := range serviceAccount.Secrets {
  51. serviceAccount.Secrets[i] = api.ObjectReference{Name: secret.Name}
  52. }
  53. }
  54. func (strategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  55. return validation.ValidateServiceAccountUpdate(obj.(*api.ServiceAccount), old.(*api.ServiceAccount))
  56. }
  57. func (strategy) AllowUnconditionalUpdate() bool {
  58. return true
  59. }