github.com/kubewharf/katalyst-core@v0.5.3/pkg/metaserver/agent/kubeletconfig/kubeletconfig.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 kubeletconfig
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	kubeletconfigv1beta1 "k8s.io/kubelet/config/v1beta1"
    24  
    25  	"github.com/kubewharf/katalyst-core/pkg/config/agent/global"
    26  	"github.com/kubewharf/katalyst-core/pkg/metrics"
    27  	"github.com/kubewharf/katalyst-core/pkg/util/native"
    28  )
    29  
    30  // KubeletConfigFetcher is used to get the configuration of kubelet.
    31  type KubeletConfigFetcher interface {
    32  	// GetKubeletConfig returns the configuration of kubelet.
    33  	GetKubeletConfig(ctx context.Context) (*kubeletconfigv1beta1.KubeletConfiguration, error)
    34  }
    35  
    36  // NewKubeletConfigFetcher returns a KubeletConfigFetcher
    37  func NewKubeletConfigFetcher(baseConf *global.BaseConfiguration, emitter metrics.MetricEmitter) KubeletConfigFetcher {
    38  	return &kubeletConfigFetcherImpl{
    39  		emitter:  emitter,
    40  		baseConf: baseConf,
    41  	}
    42  }
    43  
    44  // kubeletConfigFetcherImpl use kubelet 10255 pods interface to get pod directly without cache.
    45  type kubeletConfigFetcherImpl struct {
    46  	emitter  metrics.MetricEmitter
    47  	baseConf *global.BaseConfiguration
    48  }
    49  
    50  // GetKubeletConfig gets kubelet config from kubelet 10250/configz api
    51  func (k *kubeletConfigFetcherImpl) GetKubeletConfig(ctx context.Context) (*kubeletconfigv1beta1.KubeletConfiguration, error) {
    52  	if !k.baseConf.KubeletSecurePortEnabled {
    53  		return nil, fmt.Errorf("it is not enabled to get contents from kubelet secure port")
    54  	}
    55  
    56  	type configzWrapper struct {
    57  		ComponentConfig kubeletconfigv1beta1.KubeletConfiguration `json:"kubeletconfig"`
    58  	}
    59  	configz := configzWrapper{}
    60  
    61  	if err := native.GetAndUnmarshalForHttps(ctx, k.baseConf.KubeletSecurePort, k.baseConf.NodeAddress,
    62  		k.baseConf.KubeletConfigEndpoint, k.baseConf.APIAuthTokenFile, &configz); err != nil {
    63  		return nil, fmt.Errorf("failed to get kubelet config, error: %v", err)
    64  	}
    65  
    66  	return &configz.ComponentConfig, nil
    67  }