k8s.io/kubernetes@v1.29.3/test/e2e/framework/endpointslice/ports.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    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  // PortsByPodUID is a map that maps pod UID to container ports.
    26  type PortsByPodUID map[types.UID][]int
    27  
    28  // FullPortsByPodUID is a map that maps pod UID to container ports.
    29  type FullPortsByPodUID map[types.UID][]v1.ContainerPort
    30  
    31  // GetContainerPortsByPodUID returns a PortsByPodUID map on the given endpoints.
    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  // GetFullContainerPortsByPodUID returns a PortsByPodUID map on the given endpoints.
    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  }