objectpauser.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 polymorphichelpers
  14. import (
  15. "errors"
  16. "fmt"
  17. appsv1 "k8s.io/api/apps/v1"
  18. appsv1beta1 "k8s.io/api/apps/v1beta1"
  19. appsv1beta2 "k8s.io/api/apps/v1beta2"
  20. extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
  21. "k8s.io/apimachinery/pkg/runtime"
  22. "k8s.io/kubernetes/pkg/kubectl/scheme"
  23. )
  24. // Currently only supports Deployments.
  25. func defaultObjectPauser(obj runtime.Object) ([]byte, error) {
  26. switch obj := obj.(type) {
  27. case *extensionsv1beta1.Deployment:
  28. if obj.Spec.Paused {
  29. return nil, errors.New("is already paused")
  30. }
  31. obj.Spec.Paused = true
  32. return runtime.Encode(scheme.Codecs.LegacyCodec(extensionsv1beta1.SchemeGroupVersion), obj)
  33. case *appsv1.Deployment:
  34. if obj.Spec.Paused {
  35. return nil, errors.New("is already paused")
  36. }
  37. obj.Spec.Paused = true
  38. return runtime.Encode(scheme.Codecs.LegacyCodec(appsv1.SchemeGroupVersion), obj)
  39. case *appsv1beta2.Deployment:
  40. if obj.Spec.Paused {
  41. return nil, errors.New("is already paused")
  42. }
  43. obj.Spec.Paused = true
  44. return runtime.Encode(scheme.Codecs.LegacyCodec(appsv1beta2.SchemeGroupVersion), obj)
  45. case *appsv1beta1.Deployment:
  46. if obj.Spec.Paused {
  47. return nil, errors.New("is already paused")
  48. }
  49. obj.Spec.Paused = true
  50. return runtime.Encode(scheme.Codecs.LegacyCodec(appsv1beta1.SchemeGroupVersion), obj)
  51. default:
  52. return nil, fmt.Errorf("pausing is not supported")
  53. }
  54. }