runtimeobj.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Copyright 2019 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 resource
  14. import (
  15. "context"
  16. "fmt"
  17. appsv1 "k8s.io/api/apps/v1"
  18. batchv1 "k8s.io/api/batch/v1"
  19. v1 "k8s.io/api/core/v1"
  20. extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/labels"
  23. "k8s.io/apimachinery/pkg/runtime"
  24. "k8s.io/apimachinery/pkg/runtime/schema"
  25. clientset "k8s.io/client-go/kubernetes"
  26. appsinternal "k8s.io/kubernetes/pkg/apis/apps"
  27. batchinternal "k8s.io/kubernetes/pkg/apis/batch"
  28. api "k8s.io/kubernetes/pkg/apis/core"
  29. extensionsinternal "k8s.io/kubernetes/pkg/apis/extensions"
  30. )
  31. // GetRuntimeObjectForKind returns a runtime.Object based on its GroupKind,
  32. // namespace and name.
  33. func GetRuntimeObjectForKind(c clientset.Interface, kind schema.GroupKind, ns, name string) (runtime.Object, error) {
  34. switch kind {
  35. case api.Kind("ReplicationController"):
  36. return c.CoreV1().ReplicationControllers(ns).Get(context.TODO(), name, metav1.GetOptions{})
  37. case extensionsinternal.Kind("ReplicaSet"), appsinternal.Kind("ReplicaSet"):
  38. return c.AppsV1().ReplicaSets(ns).Get(context.TODO(), name, metav1.GetOptions{})
  39. case extensionsinternal.Kind("Deployment"), appsinternal.Kind("Deployment"):
  40. return c.AppsV1().Deployments(ns).Get(context.TODO(), name, metav1.GetOptions{})
  41. case extensionsinternal.Kind("DaemonSet"):
  42. return c.AppsV1().DaemonSets(ns).Get(context.TODO(), name, metav1.GetOptions{})
  43. case batchinternal.Kind("Job"):
  44. return c.BatchV1().Jobs(ns).Get(context.TODO(), name, metav1.GetOptions{})
  45. default:
  46. return nil, fmt.Errorf("Unsupported kind when getting runtime object: %v", kind)
  47. }
  48. }
  49. // GetSelectorFromRuntimeObject returns the labels for the given object.
  50. func GetSelectorFromRuntimeObject(obj runtime.Object) (labels.Selector, error) {
  51. switch typed := obj.(type) {
  52. case *v1.ReplicationController:
  53. return labels.SelectorFromSet(typed.Spec.Selector), nil
  54. case *extensionsv1beta1.ReplicaSet:
  55. return metav1.LabelSelectorAsSelector(typed.Spec.Selector)
  56. case *appsv1.ReplicaSet:
  57. return metav1.LabelSelectorAsSelector(typed.Spec.Selector)
  58. case *extensionsv1beta1.Deployment:
  59. return metav1.LabelSelectorAsSelector(typed.Spec.Selector)
  60. case *appsv1.Deployment:
  61. return metav1.LabelSelectorAsSelector(typed.Spec.Selector)
  62. case *extensionsv1beta1.DaemonSet:
  63. return metav1.LabelSelectorAsSelector(typed.Spec.Selector)
  64. case *appsv1.DaemonSet:
  65. return metav1.LabelSelectorAsSelector(typed.Spec.Selector)
  66. case *batchv1.Job:
  67. return metav1.LabelSelectorAsSelector(typed.Spec.Selector)
  68. default:
  69. return nil, fmt.Errorf("Unsupported kind when getting selector: %v", obj)
  70. }
  71. }
  72. // GetReplicasFromRuntimeObject returns the number of replicas for the given
  73. // object.
  74. func GetReplicasFromRuntimeObject(obj runtime.Object) (int32, error) {
  75. switch typed := obj.(type) {
  76. case *v1.ReplicationController:
  77. if typed.Spec.Replicas != nil {
  78. return *typed.Spec.Replicas, nil
  79. }
  80. return 0, nil
  81. case *extensionsv1beta1.ReplicaSet:
  82. if typed.Spec.Replicas != nil {
  83. return *typed.Spec.Replicas, nil
  84. }
  85. return 0, nil
  86. case *appsv1.ReplicaSet:
  87. if typed.Spec.Replicas != nil {
  88. return *typed.Spec.Replicas, nil
  89. }
  90. return 0, nil
  91. case *extensionsv1beta1.Deployment:
  92. if typed.Spec.Replicas != nil {
  93. return *typed.Spec.Replicas, nil
  94. }
  95. return 0, nil
  96. case *appsv1.Deployment:
  97. if typed.Spec.Replicas != nil {
  98. return *typed.Spec.Replicas, nil
  99. }
  100. return 0, nil
  101. case *extensionsv1beta1.DaemonSet:
  102. return 0, nil
  103. case *appsv1.DaemonSet:
  104. return 0, nil
  105. case *batchv1.Job:
  106. // TODO: currently we use pause pods so that's OK. When we'll want to switch to Pods
  107. // that actually finish we need a better way to do this.
  108. if typed.Spec.Parallelism != nil {
  109. return *typed.Spec.Parallelism, nil
  110. }
  111. return 0, nil
  112. default:
  113. return -1, fmt.Errorf("Unsupported kind when getting number of replicas: %v", obj)
  114. }
  115. }