...
1
16
17 package pods
18
19 import (
20 "fmt"
21
22 "k8s.io/apimachinery/pkg/util/validation/field"
23 api "k8s.io/kubernetes/pkg/apis/core"
24 "k8s.io/kubernetes/pkg/fieldpath"
25 )
26
27
28
29 type ContainerVisitorWithPath func(container *api.Container, path *field.Path) bool
30
31
32
33
34
35 func VisitContainersWithPath(podSpec *api.PodSpec, specPath *field.Path, visitor ContainerVisitorWithPath) bool {
36 fldPath := specPath.Child("initContainers")
37 for i := range podSpec.InitContainers {
38 if !visitor(&podSpec.InitContainers[i], fldPath.Index(i)) {
39 return false
40 }
41 }
42 fldPath = specPath.Child("containers")
43 for i := range podSpec.Containers {
44 if !visitor(&podSpec.Containers[i], fldPath.Index(i)) {
45 return false
46 }
47 }
48 fldPath = specPath.Child("ephemeralContainers")
49 for i := range podSpec.EphemeralContainers {
50 if !visitor((*api.Container)(&podSpec.EphemeralContainers[i].EphemeralContainerCommon), fldPath.Index(i)) {
51 return false
52 }
53 }
54 return true
55 }
56
57
58
59
60
61 func ConvertDownwardAPIFieldLabel(version, label, value string) (string, string, error) {
62 if version != "v1" {
63 return "", "", fmt.Errorf("unsupported pod version: %s", version)
64 }
65
66 if path, _, ok := fieldpath.SplitMaybeSubscriptedPath(label); ok {
67 switch path {
68 case "metadata.annotations", "metadata.labels":
69 return label, value, nil
70 default:
71 return "", "", fmt.Errorf("field label does not support subscript: %s", label)
72 }
73 }
74
75 switch label {
76 case "metadata.annotations",
77 "metadata.labels",
78 "metadata.name",
79 "metadata.namespace",
80 "metadata.uid",
81 "spec.nodeName",
82 "spec.restartPolicy",
83 "spec.serviceAccountName",
84 "spec.schedulerName",
85 "status.phase",
86 "status.hostIP",
87 "status.hostIPs",
88 "status.podIP",
89 "status.podIPs":
90 return label, value, nil
91
92 case "spec.host":
93 return "spec.nodeName", value, nil
94 default:
95 return "", "", fmt.Errorf("field label not supported: %s", label)
96 }
97 }
98
View as plain text