mapbasedselectorforobject.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. corev1 "k8s.io/api/core/v1"
  20. extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
  21. "k8s.io/apimachinery/pkg/runtime"
  22. "k8s.io/kubernetes/pkg/kubectl/generate"
  23. )
  24. // mapBasedSelectorForObject returns the map-based selector associated with the provided object. If a
  25. // new set-based selector is provided, an error is returned if the selector cannot be converted to a
  26. // map-based selector
  27. func mapBasedSelectorForObject(object runtime.Object) (string, error) {
  28. // TODO: replace with a swagger schema based approach (identify pod selector via schema introspection)
  29. switch t := object.(type) {
  30. case *corev1.ReplicationController:
  31. return generate.MakeLabels(t.Spec.Selector), nil
  32. case *corev1.Pod:
  33. if len(t.Labels) == 0 {
  34. return "", fmt.Errorf("the pod has no labels and cannot be exposed")
  35. }
  36. return generate.MakeLabels(t.Labels), nil
  37. case *corev1.Service:
  38. if t.Spec.Selector == nil {
  39. return "", fmt.Errorf("the service has no pod selector set")
  40. }
  41. return generate.MakeLabels(t.Spec.Selector), nil
  42. case *extensionsv1beta1.Deployment:
  43. // "extensions" deployments use pod template labels if selector is not set.
  44. var labels map[string]string
  45. if t.Spec.Selector != nil {
  46. // TODO(madhusudancs): Make this smarter by admitting MatchExpressions with Equals
  47. // operator, DoubleEquals operator and In operator with only one element in the set.
  48. if len(t.Spec.Selector.MatchExpressions) > 0 {
  49. return "", fmt.Errorf("couldn't convert expressions - \"%+v\" to map-based selector format", t.Spec.Selector.MatchExpressions)
  50. }
  51. labels = t.Spec.Selector.MatchLabels
  52. } else {
  53. labels = t.Spec.Template.Labels
  54. }
  55. if len(labels) == 0 {
  56. return "", fmt.Errorf("the deployment has no labels or selectors and cannot be exposed")
  57. }
  58. return generate.MakeLabels(labels), nil
  59. case *appsv1.Deployment:
  60. // "apps" deployments must have the selector set.
  61. if t.Spec.Selector == nil || len(t.Spec.Selector.MatchLabels) == 0 {
  62. return "", fmt.Errorf("invalid deployment: no selectors, therefore cannot be exposed")
  63. }
  64. // TODO(madhusudancs): Make this smarter by admitting MatchExpressions with Equals
  65. // operator, DoubleEquals operator and In operator with only one element in the set.
  66. if len(t.Spec.Selector.MatchExpressions) > 0 {
  67. return "", fmt.Errorf("couldn't convert expressions - \"%+v\" to map-based selector format", t.Spec.Selector.MatchExpressions)
  68. }
  69. return generate.MakeLabels(t.Spec.Selector.MatchLabels), nil
  70. case *appsv1beta2.Deployment:
  71. // "apps" deployments must have the selector set.
  72. if t.Spec.Selector == nil || len(t.Spec.Selector.MatchLabels) == 0 {
  73. return "", fmt.Errorf("invalid deployment: no selectors, therefore cannot be exposed")
  74. }
  75. // TODO(madhusudancs): Make this smarter by admitting MatchExpressions with Equals
  76. // operator, DoubleEquals operator and In operator with only one element in the set.
  77. if len(t.Spec.Selector.MatchExpressions) > 0 {
  78. return "", fmt.Errorf("couldn't convert expressions - \"%+v\" to map-based selector format", t.Spec.Selector.MatchExpressions)
  79. }
  80. return generate.MakeLabels(t.Spec.Selector.MatchLabels), nil
  81. case *appsv1beta1.Deployment:
  82. // "apps" deployments must have the selector set.
  83. if t.Spec.Selector == nil || len(t.Spec.Selector.MatchLabels) == 0 {
  84. return "", fmt.Errorf("invalid deployment: no selectors, therefore cannot be exposed")
  85. }
  86. // TODO(madhusudancs): Make this smarter by admitting MatchExpressions with Equals
  87. // operator, DoubleEquals operator and In operator with only one element in the set.
  88. if len(t.Spec.Selector.MatchExpressions) > 0 {
  89. return "", fmt.Errorf("couldn't convert expressions - \"%+v\" to map-based selector format", t.Spec.Selector.MatchExpressions)
  90. }
  91. return generate.MakeLabels(t.Spec.Selector.MatchLabels), nil
  92. case *extensionsv1beta1.ReplicaSet:
  93. // "extensions" replicasets use pod template labels if selector is not set.
  94. var labels map[string]string
  95. if t.Spec.Selector != nil {
  96. // TODO(madhusudancs): Make this smarter by admitting MatchExpressions with Equals
  97. // operator, DoubleEquals operator and In operator with only one element in the set.
  98. if len(t.Spec.Selector.MatchExpressions) > 0 {
  99. return "", fmt.Errorf("couldn't convert expressions - \"%+v\" to map-based selector format", t.Spec.Selector.MatchExpressions)
  100. }
  101. labels = t.Spec.Selector.MatchLabels
  102. } else {
  103. labels = t.Spec.Template.Labels
  104. }
  105. if len(labels) == 0 {
  106. return "", fmt.Errorf("the replica set has no labels or selectors and cannot be exposed")
  107. }
  108. return generate.MakeLabels(labels), nil
  109. case *appsv1.ReplicaSet:
  110. // "apps" replicasets must have the selector set.
  111. if t.Spec.Selector == nil || len(t.Spec.Selector.MatchLabels) == 0 {
  112. return "", fmt.Errorf("invalid replicaset: no selectors, therefore cannot be exposed")
  113. }
  114. // TODO(madhusudancs): Make this smarter by admitting MatchExpressions with Equals
  115. // operator, DoubleEquals operator and In operator with only one element in the set.
  116. if len(t.Spec.Selector.MatchExpressions) > 0 {
  117. return "", fmt.Errorf("couldn't convert expressions - \"%+v\" to map-based selector format", t.Spec.Selector.MatchExpressions)
  118. }
  119. return generate.MakeLabels(t.Spec.Selector.MatchLabels), nil
  120. case *appsv1beta2.ReplicaSet:
  121. // "apps" replicasets must have the selector set.
  122. if t.Spec.Selector == nil || len(t.Spec.Selector.MatchLabels) == 0 {
  123. return "", fmt.Errorf("invalid replicaset: no selectors, therefore cannot be exposed")
  124. }
  125. // TODO(madhusudancs): Make this smarter by admitting MatchExpressions with Equals
  126. // operator, DoubleEquals operator and In operator with only one element in the set.
  127. if len(t.Spec.Selector.MatchExpressions) > 0 {
  128. return "", fmt.Errorf("couldn't convert expressions - \"%+v\" to map-based selector format", t.Spec.Selector.MatchExpressions)
  129. }
  130. return generate.MakeLabels(t.Spec.Selector.MatchLabels), nil
  131. default:
  132. return "", fmt.Errorf("cannot extract pod selector from %T", object)
  133. }
  134. }