ref.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. Copyright 2015 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 container
  14. import (
  15. "fmt"
  16. "k8s.io/api/core/v1"
  17. utilfeature "k8s.io/apiserver/pkg/util/feature"
  18. ref "k8s.io/client-go/tools/reference"
  19. "k8s.io/kubernetes/pkg/api/legacyscheme"
  20. "k8s.io/kubernetes/pkg/features"
  21. )
  22. var ImplicitContainerPrefix string = "implicitly required container "
  23. // GenerateContainerRef returns an *v1.ObjectReference which references the given container
  24. // within the given pod. Returns an error if the reference can't be constructed or the
  25. // container doesn't actually belong to the pod.
  26. //
  27. // This function will return an error if the provided Pod does not have a selfLink,
  28. // but we expect selfLink to be populated at all call sites for the function.
  29. func GenerateContainerRef(pod *v1.Pod, container *v1.Container) (*v1.ObjectReference, error) {
  30. fieldPath, err := fieldPath(pod, container)
  31. if err != nil {
  32. // TODO: figure out intelligent way to refer to containers that we implicitly
  33. // start (like the pod infra container). This is not a good way, ugh.
  34. fieldPath = ImplicitContainerPrefix + container.Name
  35. }
  36. ref, err := ref.GetPartialReference(legacyscheme.Scheme, pod, fieldPath)
  37. if err != nil {
  38. return nil, err
  39. }
  40. return ref, nil
  41. }
  42. // fieldPath returns a fieldPath locating container within pod.
  43. // Returns an error if the container isn't part of the pod.
  44. func fieldPath(pod *v1.Pod, container *v1.Container) (string, error) {
  45. for i := range pod.Spec.Containers {
  46. here := &pod.Spec.Containers[i]
  47. if here.Name == container.Name {
  48. if here.Name == "" {
  49. return fmt.Sprintf("spec.containers[%d]", i), nil
  50. }
  51. return fmt.Sprintf("spec.containers{%s}", here.Name), nil
  52. }
  53. }
  54. for i := range pod.Spec.InitContainers {
  55. here := &pod.Spec.InitContainers[i]
  56. if here.Name == container.Name {
  57. if here.Name == "" {
  58. return fmt.Sprintf("spec.initContainers[%d]", i), nil
  59. }
  60. return fmt.Sprintf("spec.initContainers{%s}", here.Name), nil
  61. }
  62. }
  63. if utilfeature.DefaultFeatureGate.Enabled(features.EphemeralContainers) {
  64. for i := range pod.Spec.EphemeralContainers {
  65. here := &pod.Spec.EphemeralContainers[i]
  66. if here.Name == container.Name {
  67. if here.Name == "" {
  68. return fmt.Sprintf("spec.ephemeralContainers[%d]", i), nil
  69. }
  70. return fmt.Sprintf("spec.ephemeralContainers{%s}", here.Name), nil
  71. }
  72. }
  73. }
  74. return "", fmt.Errorf("container %q not found in pod %s/%s", container.Name, pod.Namespace, pod.Name)
  75. }