...

Source file src/k8s.io/kubernetes/pkg/probe/http/request.go

Documentation: k8s.io/kubernetes/pkg/probe/http

     1  /*
     2  Copyright 2022 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 http
    18  
    19  import (
    20  	"fmt"
    21  	"net"
    22  	"net/http"
    23  	"net/url"
    24  	"strconv"
    25  	"strings"
    26  
    27  	v1 "k8s.io/api/core/v1"
    28  	"k8s.io/component-base/version"
    29  	"k8s.io/kubernetes/pkg/probe"
    30  )
    31  
    32  // NewProbeRequest returns an http.Request suitable for use as a request for a
    33  // probe.
    34  func NewProbeRequest(url *url.URL, headers http.Header) (*http.Request, error) {
    35  	return newProbeRequest(url, headers, "probe")
    36  }
    37  
    38  // NewRequestForHTTPGetAction returns an http.Request derived from httpGet.
    39  // When httpGet.Host is empty, podIP will be used instead.
    40  func NewRequestForHTTPGetAction(httpGet *v1.HTTPGetAction, container *v1.Container, podIP string, userAgentFragment string) (*http.Request, error) {
    41  	scheme := strings.ToLower(string(httpGet.Scheme))
    42  	if scheme == "" {
    43  		scheme = "http"
    44  	}
    45  
    46  	host := httpGet.Host
    47  	if host == "" {
    48  		host = podIP
    49  	}
    50  
    51  	port, err := probe.ResolveContainerPort(httpGet.Port, container)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	path := httpGet.Path
    57  	url := formatURL(scheme, host, port, path)
    58  	headers := v1HeaderToHTTPHeader(httpGet.HTTPHeaders)
    59  
    60  	return newProbeRequest(url, headers, userAgentFragment)
    61  }
    62  
    63  func newProbeRequest(url *url.URL, headers http.Header, userAgentFragment string) (*http.Request, error) {
    64  	req, err := http.NewRequest("GET", url.String(), nil)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	if headers == nil {
    70  		headers = http.Header{}
    71  	}
    72  	if _, ok := headers["User-Agent"]; !ok {
    73  		// User-Agent header was not defined, set it
    74  		headers.Set("User-Agent", userAgent(userAgentFragment))
    75  	}
    76  	if _, ok := headers["Accept"]; !ok {
    77  		// Accept header was not defined. accept all
    78  		headers.Set("Accept", "*/*")
    79  	} else if headers.Get("Accept") == "" {
    80  		// Accept header was overridden but is empty. removing
    81  		headers.Del("Accept")
    82  	}
    83  	req.Header = headers
    84  	req.Host = headers.Get("Host")
    85  
    86  	return req, nil
    87  }
    88  
    89  func userAgent(purpose string) string {
    90  	v := version.Get()
    91  	return fmt.Sprintf("kube-%s/%s.%s", purpose, v.Major, v.Minor)
    92  }
    93  
    94  // formatURL formats a URL from args.  For testability.
    95  func formatURL(scheme string, host string, port int, path string) *url.URL {
    96  	u, err := url.Parse(path)
    97  	// Something is busted with the path, but it's too late to reject it. Pass it along as is.
    98  	//
    99  	// This construction of a URL may be wrong in some cases, but it preserves
   100  	// legacy prober behavior.
   101  	if err != nil {
   102  		u = &url.URL{
   103  			Path: path,
   104  		}
   105  	}
   106  	u.Scheme = scheme
   107  	u.Host = net.JoinHostPort(host, strconv.Itoa(port))
   108  	return u
   109  }
   110  
   111  // v1HeaderToHTTPHeader takes a list of HTTPHeader <name, value> string pairs
   112  // and returns a populated string->[]string http.Header map.
   113  func v1HeaderToHTTPHeader(headerList []v1.HTTPHeader) http.Header {
   114  	headers := make(http.Header)
   115  	for _, header := range headerList {
   116  		headers.Add(header.Name, header.Value)
   117  	}
   118  	return headers
   119  }
   120  

View as plain text