// Translates service port to target port
// It rewrites ports as needed if the Service port declares targetPort.
// It returns an error when a named targetPort can't find a match in the pod, or the Service did not declare
// the port.
Translates service port to target port
It rewrites ports as needed if the Service port declares targetPort.
It returns an error when a named targetPort can't find a match in the pod, or the Service did not declare
the port.
// Translates service port to target port // It rewrites ports as needed if the Service port declares targetPort . // It returns an error when a named targetPort can ' t find a match in the pod or the Service did not declare // the port .
func translateServicePortToTargetPort(port string, svc corev1.Service, pod corev1.Pod) (string, error) {
	localPort, remotePort := splitPort(port)
	portnum, err := strconv.Atoi(remotePort)
	if err != nil {
		svcPort, err := util.LookupServicePortNumberByName(svc, remotePort)
		if err != nil {
			return "", err
		}
		portnum = int(svcPort)
		if localPort == remotePort {
			localPort = strconv.Itoa(portnum)
		}
	}
	containerPort, err := util.LookupContainerPortNumberByServicePort(svc, pod, int32(portnum))
	if err != nil {
		return "", err
	}
	remotePort = strconv.Itoa(int(containerPort))
	if localPort != remotePort {
		return fmt.Sprintf("%s:%s", localPort, remotePort), nil
	}
	return remotePort, nil
}
func translateServicePortToTargetPort ( port string , svc corev1 . Service , pod corev1 . Pod ) ( string , error ) { localPort , remotePort := splitPort ( port ) portnum , err := strconv . Atoi ( remotePort ) if err != nil { svcPort , err := util . LookupServicePortNumberByName ( svc , remotePort ) if err != nil { return " " , err } portnum = int ( svcPort ) if localPort == remotePort { localPort = strconv . Itoa ( portnum ) } } containerPort , err := util . LookupContainerPortNumberByServicePort ( svc , pod , int32 ( portnum ) ) if err != nil { return " " , err } remotePort = strconv . Itoa ( int ( containerPort ) ) if localPort != remotePort { return fmt . Sprintf ( " " , localPort , remotePort ) , nil } return remotePort , nil }
Translates service port to target port
It rewrites ports as needed if the Service port declares targetPort.
Translates service port to target port It rewrites ports as needed if the Service port declares targetPort .
// nolint:gosec // can't resolve a named port, or Service did not declare this port, return an error // convert the resolved target port back to a string