github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/native/kubelet.go (about)

     1  /*
     2  Copyright 2022 The Katalyst 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 native
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"fmt"
    23  	"net/url"
    24  	"os"
    25  	"time"
    26  
    27  	"k8s.io/client-go/discovery"
    28  	"k8s.io/client-go/rest"
    29  )
    30  
    31  const (
    32  	defaultTimeout = time.Second * 10
    33  )
    34  
    35  // GetAndUnmarshalForHttps gets data from the given url and unmarshal it into the given struct.
    36  func GetAndUnmarshalForHttps(ctx context.Context, port int, nodeAddress, endpoint, authTokenFile string, v interface{}) error {
    37  	uri, err := generateURI(port, nodeAddress, endpoint)
    38  	if err != nil {
    39  		return err
    40  	}
    41  	restConfig, err := insecureConfig(uri, authTokenFile)
    42  	if err != nil {
    43  		return fmt.Errorf("failed to initialize rest config for kubelet config uri: %w", err)
    44  	}
    45  
    46  	discoveryClient, err := discovery.NewDiscoveryClientForConfig(restConfig)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	bytes, err := discoveryClient.RESTClient().
    52  		Get().
    53  		Timeout(defaultTimeout).
    54  		Do(ctx).
    55  		Raw()
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	if err = json.Unmarshal(bytes, v); err != nil {
    61  		return fmt.Errorf("failed to unmarshal json for kubelet config: %w", err)
    62  	}
    63  
    64  	return nil
    65  }
    66  
    67  func generateURI(port int, nodeAddress, endpoint string) (string, error) {
    68  	if nodeAddress == "" {
    69  		return "", fmt.Errorf("node address is empty")
    70  	}
    71  
    72  	u, err := url.ParseRequestURI(fmt.Sprintf("https://%s:%d%s", nodeAddress, port, endpoint))
    73  	if err != nil {
    74  		return "", fmt.Errorf("failed to parse -kubelet-config-uri: %w", err)
    75  	}
    76  
    77  	return u.String(), nil
    78  }
    79  
    80  func insecureConfig(host, tokenFile string) (*rest.Config, error) {
    81  	if tokenFile == "" {
    82  		return nil, fmt.Errorf("api auth token file must be defined")
    83  	}
    84  	if len(host) == 0 {
    85  		return nil, fmt.Errorf("kubelet host must be defined")
    86  	}
    87  
    88  	token, err := os.ReadFile(tokenFile)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  
    93  	tlsClientConfig := rest.TLSClientConfig{Insecure: true}
    94  
    95  	return &rest.Config{
    96  		Host:            host,
    97  		TLSClientConfig: tlsClientConfig,
    98  		BearerToken:     string(token),
    99  		BearerTokenFile: tokenFile,
   100  	}, nil
   101  }