@@ -13,10 +13,6 @@ func LocatePod(pod *corev1.Pod) string {
1313 return fmt .Sprintf ("ns/%s pod/%s node/%s uid/%s" , pod .Namespace , pod .Name , pod .Spec .NodeName , pod .UID )
1414}
1515
16- func LocatePodContainer (pod * corev1.Pod , containerName string ) string {
17- return fmt .Sprintf ("ns/%s pod/%s node/%s uid/%s container/%s" , pod .Namespace , pod .Name , pod .Spec .NodeName , pod .UID , containerName )
18- }
19-
2016// NonUniquePodLocatorFrom produces an inexact locator based on namespace and name. This is useful when dealing with events
2117// that are produced that do not contain UIDs. Ultimately, we should use UIDs everywhere, but this is will keep some our
2218// matching working until then.
@@ -26,6 +22,7 @@ func NonUniquePodLocatorFrom(locator string) string {
2622 return fmt .Sprintf ("ns/%s pod/%s" , namespace , parts ["pod" ])
2723}
2824
25+ // TODO: all callers should eventuall be using structured locator variant below:
2926func PodFrom (locator string ) PodReference {
3027 parts := LocatorParts (locator )
3128 namespace := NamespaceFrom (parts )
@@ -43,10 +40,27 @@ func PodFrom(locator string) PodReference {
4340 }
4441}
4542
46- func ContainerFrom (locator string ) ContainerReference {
47- pod := PodFrom (locator )
48- parts := LocatorParts (locator )
49- name := parts ["container" ]
43+ // PodFromLocator is used to strip down a locator to just a pod. (as it may contain additional keys like container or node)
44+ // that we do not want for some uses.
45+ func PodFromLocator (locator Locator ) PodReference {
46+ namespace := locator .Keys [LocatorNamespaceKey ]
47+ name := locator .Keys [LocatorPodKey ]
48+ uid := locator .Keys [LocatorUIDKey ]
49+ if len (namespace ) == 0 || len (name ) == 0 {
50+ return PodReference {}
51+ }
52+ return PodReference {
53+ NamespacedReference : NamespacedReference {
54+ Namespace : namespace ,
55+ Name : name ,
56+ UID : uid ,
57+ },
58+ }
59+ }
60+
61+ func ContainerFrom (locator Locator ) ContainerReference {
62+ pod := PodFrom (locator .OldLocator ())
63+ name := locator .Keys [LocatorContainerKey ]
5064 if len (name ) == 0 || len (pod .UID ) == 0 {
5165 return ContainerReference {}
5266 }
@@ -61,17 +75,17 @@ type PodReference struct {
6175 NamespacedReference
6276}
6377
64- func (r PodReference ) ToLocator () string {
65- return fmt . Sprintf ( "ns/%s pod/%s uid/%s" , r .Namespace , r .Name , r .UID )
78+ func (r PodReference ) ToLocator () Locator {
79+ return NewLocator (). PodFromNames ( r .Namespace , r .Name , r .UID )
6680}
6781
6882type ContainerReference struct {
6983 Pod PodReference
7084 ContainerName string
7185}
7286
73- func (r ContainerReference ) ToLocator () string {
74- return fmt . Sprintf ( "ns/%s pod/%s uid/%s container/%s" , r .Pod .Namespace , r .Pod .Name , r .Pod .UID , r .ContainerName )
87+ func (r ContainerReference ) ToLocator () Locator {
88+ return NewLocator (). ContainerFromNames ( r .Pod .Namespace , r .Pod .Name , r .Pod .UID , r .ContainerName )
7589}
7690
7791func AnnotationsFromMessage (message string ) map [AnnotationKey ]string {
0 commit comments