k8s.io/kubernetes@v1.29.3/test/e2e/framework/endpoints/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 endpoints
    18  
    19  import (
    20  	v1 "k8s.io/api/core/v1"
    21  	"k8s.io/apimachinery/pkg/types"
    22  )
    23  
    24  // PortsByPodUID is a map that maps pod UID to container ports.
    25  type PortsByPodUID map[types.UID][]int
    26  
    27  // FullPortsByPodUID is a map that maps pod UID to container ports.
    28  type FullPortsByPodUID map[types.UID][]v1.ContainerPort
    29  
    30  // GetContainerPortsByPodUID returns a PortsByPodUID map on the given endpoints.
    31  func GetContainerPortsByPodUID(ep *v1.Endpoints) PortsByPodUID {
    32  	m := PortsByPodUID{}
    33  	for _, ss := range ep.Subsets {
    34  		for _, port := range ss.Ports {
    35  			for _, addr := range ss.Addresses {
    36  				containerPort := port.Port
    37  				if _, ok := m[addr.TargetRef.UID]; !ok {
    38  					m[addr.TargetRef.UID] = make([]int, 0)
    39  				}
    40  				m[addr.TargetRef.UID] = append(m[addr.TargetRef.UID], int(containerPort))
    41  			}
    42  		}
    43  	}
    44  	return m
    45  }
    46  
    47  // GetFullContainerPortsByPodUID returns a FullPortsByPodUID map on the given endpoints with all the port data.
    48  func GetFullContainerPortsByPodUID(ep *v1.Endpoints) FullPortsByPodUID {
    49  	m := FullPortsByPodUID{}
    50  	for _, ss := range ep.Subsets {
    51  		for _, port := range ss.Ports {
    52  			containerPort := v1.ContainerPort{
    53  				Name:          port.Name,
    54  				ContainerPort: port.Port,
    55  				Protocol:      port.Protocol,
    56  			}
    57  			for _, addr := range ss.Addresses {
    58  				if _, ok := m[addr.TargetRef.UID]; !ok {
    59  					m[addr.TargetRef.UID] = make([]v1.ContainerPort, 0)
    60  				}
    61  				m[addr.TargetRef.UID] = append(m[addr.TargetRef.UID], containerPort)
    62  			}
    63  		}
    64  	}
    65  	return m
    66  }