istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/networking/core/tunnelingconfig/apply.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tunnelingconfig
    16  
    17  import (
    18  	"net"
    19  	"strconv"
    20  
    21  	tcp "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/tcp_proxy/v3"
    22  
    23  	networking "istio.io/api/networking/v1alpha3"
    24  )
    25  
    26  type ApplyFunc = func(tcpProxy *tcp.TcpProxy, destinationRule *networking.DestinationRule, subsetName string)
    27  
    28  // Apply configures tunneling_config in a given TcpProxy depending on the destination rule and the destination hosts
    29  var Apply ApplyFunc = func(tcpProxy *tcp.TcpProxy, destinationRule *networking.DestinationRule, subsetName string) {
    30  	var tunnelSettings *networking.TrafficPolicy_TunnelSettings
    31  	if subsetName != "" {
    32  		for _, s := range destinationRule.GetSubsets() {
    33  			if s.Name == subsetName {
    34  				tunnelSettings = s.GetTrafficPolicy().GetTunnel()
    35  				break
    36  			}
    37  		}
    38  	} else {
    39  		tunnelSettings = destinationRule.GetTrafficPolicy().GetTunnel()
    40  	}
    41  
    42  	if tunnelSettings == nil {
    43  		return
    44  	}
    45  
    46  	tcpProxy.TunnelingConfig = &tcp.TcpProxy_TunnelingConfig{
    47  		Hostname: net.JoinHostPort(tunnelSettings.GetTargetHost(), strconv.Itoa(int(tunnelSettings.GetTargetPort()))),
    48  		UsePost:  tunnelSettings.Protocol == "POST",
    49  	}
    50  }
    51  
    52  // Skip has no effect; its only purpose is to avoid passing nil values for ApplyFunc arguments
    53  // when it is not desired to apply `tunneling_config` to a listener, e.g. AUTO_PASSTHROUGH
    54  var Skip ApplyFunc = func(_ *tcp.TcpProxy, _ *networking.DestinationRule, _ string) {}