updatepodspec.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "fmt"
  16. appsv1 "k8s.io/api/apps/v1"
  17. appsv1beta1 "k8s.io/api/apps/v1beta1"
  18. appsv1beta2 "k8s.io/api/apps/v1beta2"
  19. batchv1 "k8s.io/api/batch/v1"
  20. batchv1beta1 "k8s.io/api/batch/v1beta1"
  21. batchv2alpha1 "k8s.io/api/batch/v2alpha1"
  22. "k8s.io/api/core/v1"
  23. extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
  24. "k8s.io/apimachinery/pkg/runtime"
  25. )
  26. func updatePodSpecForObject(obj runtime.Object, fn func(*v1.PodSpec) error) (bool, error) {
  27. switch t := obj.(type) {
  28. case *v1.Pod:
  29. return true, fn(&t.Spec)
  30. // ReplicationController
  31. case *v1.ReplicationController:
  32. if t.Spec.Template == nil {
  33. t.Spec.Template = &v1.PodTemplateSpec{}
  34. }
  35. return true, fn(&t.Spec.Template.Spec)
  36. // Deployment
  37. case *extensionsv1beta1.Deployment:
  38. return true, fn(&t.Spec.Template.Spec)
  39. case *appsv1beta1.Deployment:
  40. return true, fn(&t.Spec.Template.Spec)
  41. case *appsv1beta2.Deployment:
  42. return true, fn(&t.Spec.Template.Spec)
  43. case *appsv1.Deployment:
  44. return true, fn(&t.Spec.Template.Spec)
  45. // DaemonSet
  46. case *extensionsv1beta1.DaemonSet:
  47. return true, fn(&t.Spec.Template.Spec)
  48. case *appsv1beta2.DaemonSet:
  49. return true, fn(&t.Spec.Template.Spec)
  50. case *appsv1.DaemonSet:
  51. return true, fn(&t.Spec.Template.Spec)
  52. // ReplicaSet
  53. case *extensionsv1beta1.ReplicaSet:
  54. return true, fn(&t.Spec.Template.Spec)
  55. case *appsv1beta2.ReplicaSet:
  56. return true, fn(&t.Spec.Template.Spec)
  57. case *appsv1.ReplicaSet:
  58. return true, fn(&t.Spec.Template.Spec)
  59. // StatefulSet
  60. case *appsv1beta1.StatefulSet:
  61. return true, fn(&t.Spec.Template.Spec)
  62. case *appsv1beta2.StatefulSet:
  63. return true, fn(&t.Spec.Template.Spec)
  64. case *appsv1.StatefulSet:
  65. return true, fn(&t.Spec.Template.Spec)
  66. // Job
  67. case *batchv1.Job:
  68. return true, fn(&t.Spec.Template.Spec)
  69. // CronJob
  70. case *batchv1beta1.CronJob:
  71. return true, fn(&t.Spec.JobTemplate.Spec.Template.Spec)
  72. case *batchv2alpha1.CronJob:
  73. return true, fn(&t.Spec.JobTemplate.Spec.Template.Spec)
  74. default:
  75. return false, fmt.Errorf("the object is not a pod or does not have a pod template: %T", t)
  76. }
  77. }