k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cmd/kubeadm/app/discovery/https/https.go (about)

     1  /*
     2  Copyright 2016 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 https
    18  
    19  import (
    20  	"io"
    21  	"net/http"
    22  	"time"
    23  
    24  	netutil "k8s.io/apimachinery/pkg/util/net"
    25  	"k8s.io/client-go/tools/clientcmd"
    26  	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
    27  
    28  	"k8s.io/kubernetes/cmd/kubeadm/app/discovery/file"
    29  )
    30  
    31  // RetrieveValidatedConfigInfo connects to the API Server and makes sure it can talk
    32  // securely to the API Server using the provided CA cert and
    33  // optionally refreshes the cluster-info information from the cluster-info ConfigMap
    34  func RetrieveValidatedConfigInfo(httpsURL string, discoveryTimeout time.Duration) (*clientcmdapi.Config, error) {
    35  	client := &http.Client{Transport: netutil.SetOldTransportDefaults(&http.Transport{})}
    36  	response, err := client.Get(httpsURL)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	defer response.Body.Close()
    41  
    42  	kubeconfig, err := io.ReadAll(response.Body)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	config, err := clientcmd.Load(kubeconfig)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	return file.ValidateConfigInfo(config, discoveryTimeout)
    52  }