istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/kube/spdy.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 kube
    16  
    17  import (
    18  	"crypto/tls"
    19  	"fmt"
    20  	"net/http"
    21  
    22  	spdyStream "k8s.io/apimachinery/pkg/util/httpstream/spdy"
    23  	"k8s.io/client-go/rest"
    24  	"k8s.io/client-go/transport/spdy"
    25  )
    26  
    27  // roundTripperFor creates a SPDY upgrader that will work over custom transports.
    28  func roundTripperFor(restConfig *rest.Config) (http.RoundTripper, spdy.Upgrader, error) {
    29  	// Get the TLS config.
    30  	tlsConfig, err := rest.TLSConfigFor(restConfig)
    31  	if err != nil {
    32  		return nil, nil, fmt.Errorf("failed getting TLS config: %w", err)
    33  	}
    34  	if tlsConfig == nil && restConfig.Transport != nil {
    35  		// If using a custom transport, skip server verification on the upgrade.
    36  		// nolint: gosec
    37  		tlsConfig = &tls.Config{
    38  			InsecureSkipVerify: true,
    39  		}
    40  	}
    41  
    42  	var upgrader *spdyStream.SpdyRoundTripper
    43  	if restConfig.Proxy != nil {
    44  		upgrader, err = spdyStream.NewRoundTripperWithProxy(tlsConfig, restConfig.Proxy)
    45  		if err != nil {
    46  			return nil, nil, err
    47  		}
    48  	} else {
    49  		upgrader, err = spdyStream.NewRoundTripper(tlsConfig)
    50  		if err != nil {
    51  			return nil, nil, err
    52  		}
    53  	}
    54  	wrapper, err := rest.HTTPWrappersForConfig(restConfig, upgrader)
    55  	if err != nil {
    56  		return nil, nil, fmt.Errorf("failed creating SPDY upgrade wrapper: %w", err)
    57  	}
    58  	return wrapper, upgrader, nil
    59  }