...
1
16
17 package endpointslice
18
19 import (
20 v1 "k8s.io/api/core/v1"
21 discoveryv1 "k8s.io/api/discovery/v1"
22 "k8s.io/apimachinery/pkg/types"
23 )
24
25
26 type PortsByPodUID map[types.UID][]int
27
28
29 type FullPortsByPodUID map[types.UID][]v1.ContainerPort
30
31
32 func GetContainerPortsByPodUID(eps []discoveryv1.EndpointSlice) PortsByPodUID {
33 m := PortsByPodUID{}
34
35 for _, es := range eps {
36 for _, port := range es.Ports {
37 if port.Port == nil {
38 continue
39 }
40 for _, ep := range es.Endpoints {
41 containerPort := *port.Port
42 if _, ok := m[ep.TargetRef.UID]; !ok {
43 m[ep.TargetRef.UID] = make([]int, 0)
44 }
45 m[ep.TargetRef.UID] = append(m[ep.TargetRef.UID], int(containerPort))
46 }
47 }
48 }
49 return m
50 }
51
52
53 func GetFullContainerPortsByPodUID(eps []discoveryv1.EndpointSlice) FullPortsByPodUID {
54 m := FullPortsByPodUID{}
55
56 for _, es := range eps {
57 for _, port := range es.Ports {
58 if port.Port == nil {
59 continue
60 }
61 containerPort := v1.ContainerPort{
62 Name: *port.Name,
63 ContainerPort: *port.Port,
64 Protocol: *port.Protocol,
65 }
66 for _, ep := range es.Endpoints {
67 if _, ok := m[ep.TargetRef.UID]; !ok {
68 m[ep.TargetRef.UID] = make([]v1.ContainerPort, 0)
69 }
70 m[ep.TargetRef.UID] = append(m[ep.TargetRef.UID], containerPort)
71 }
72 }
73 }
74 return m
75 }
76
View as plain text