helpers.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright 2017 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 pods
  14. import (
  15. "fmt"
  16. "k8s.io/apimachinery/pkg/util/validation/field"
  17. utilfeature "k8s.io/apiserver/pkg/util/feature"
  18. api "k8s.io/kubernetes/pkg/apis/core"
  19. "k8s.io/kubernetes/pkg/features"
  20. "k8s.io/kubernetes/pkg/fieldpath"
  21. )
  22. // ContainerVisitorWithPath is called with each container and the field.Path to that container,
  23. // and returns true if visiting should continue.
  24. type ContainerVisitorWithPath func(container *api.Container, path *field.Path) bool
  25. // VisitContainersWithPath invokes the visitor function with a pointer to the spec
  26. // of every container in the given pod spec and the field.Path to that container.
  27. // If visitor returns false, visiting is short-circuited. VisitContainersWithPath returns true if visiting completes,
  28. // false if visiting was short-circuited.
  29. func VisitContainersWithPath(podSpec *api.PodSpec, visitor ContainerVisitorWithPath) bool {
  30. path := field.NewPath("spec", "initContainers")
  31. for i := range podSpec.InitContainers {
  32. if !visitor(&podSpec.InitContainers[i], path.Index(i)) {
  33. return false
  34. }
  35. }
  36. path = field.NewPath("spec", "containers")
  37. for i := range podSpec.Containers {
  38. if !visitor(&podSpec.Containers[i], path.Index(i)) {
  39. return false
  40. }
  41. }
  42. if utilfeature.DefaultFeatureGate.Enabled(features.EphemeralContainers) {
  43. path = field.NewPath("spec", "ephemeralContainers")
  44. for i := range podSpec.EphemeralContainers {
  45. if !visitor((*api.Container)(&podSpec.EphemeralContainers[i].EphemeralContainerCommon), path.Index(i)) {
  46. return false
  47. }
  48. }
  49. }
  50. return true
  51. }
  52. // ConvertDownwardAPIFieldLabel converts the specified downward API field label
  53. // and its value in the pod of the specified version to the internal version,
  54. // and returns the converted label and value. This function returns an error if
  55. // the conversion fails.
  56. func ConvertDownwardAPIFieldLabel(version, label, value string) (string, string, error) {
  57. if version != "v1" {
  58. return "", "", fmt.Errorf("unsupported pod version: %s", version)
  59. }
  60. if path, _, ok := fieldpath.SplitMaybeSubscriptedPath(label); ok {
  61. switch path {
  62. case "metadata.annotations", "metadata.labels":
  63. return label, value, nil
  64. default:
  65. return "", "", fmt.Errorf("field label does not support subscript: %s", label)
  66. }
  67. }
  68. switch label {
  69. case "metadata.annotations",
  70. "metadata.labels",
  71. "metadata.name",
  72. "metadata.namespace",
  73. "metadata.uid",
  74. "spec.nodeName",
  75. "spec.restartPolicy",
  76. "spec.serviceAccountName",
  77. "spec.schedulerName",
  78. "status.phase",
  79. "status.hostIP",
  80. "status.podIP",
  81. "status.podIPs":
  82. return label, value, nil
  83. // This is for backwards compatibility with old v1 clients which send spec.host
  84. case "spec.host":
  85. return "spec.nodeName", value, nil
  86. default:
  87. return "", "", fmt.Errorf("field label not supported: %s", label)
  88. }
  89. }