From f76fc5bf6b1d0d67cbbd1e2162de977c2e086be6 Mon Sep 17 00:00:00 2001 From: PiotrProkop Date: Fri, 10 Feb 2023 11:59:44 +0100 Subject: [PATCH] Read Kubelet configuration the same way as Kubelet to apply default values Signed-off-by: PiotrProkop --- pkg/utils/kubeconf/kubelet_config_file.go | 27 ++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/pkg/utils/kubeconf/kubelet_config_file.go b/pkg/utils/kubeconf/kubelet_config_file.go index ede0d12737..f863090b08 100644 --- a/pkg/utils/kubeconf/kubelet_config_file.go +++ b/pkg/utils/kubeconf/kubelet_config_file.go @@ -17,23 +17,40 @@ limitations under the License. package kubeconf import ( - "os" - - "github.com/ghodss/yaml" + "fmt" kubeletconfigv1beta1 "k8s.io/kubelet/config/v1beta1" + kubeletconfigscheme "k8s.io/kubernetes/pkg/kubelet/apis/config/scheme" + "k8s.io/kubernetes/pkg/kubelet/kubeletconfig/configfiles" + utilfs "k8s.io/kubernetes/pkg/util/filesystem" ) // GetKubeletConfigFromLocalFile returns KubeletConfiguration loaded from the node local config +// based on https://github.com/kubernetes/kubernetes/blob/master/cmd/kubelet/app/server.go#L337 +// it fills empty fields with default values func GetKubeletConfigFromLocalFile(kubeletConfigPath string) (*kubeletconfigv1beta1.KubeletConfiguration, error) { - kubeletBytes, err := os.ReadFile(kubeletConfigPath) + const errFmt = "failed to load Kubelet config file %s, error %w" + + loader, err := configfiles.NewFsLoader(&utilfs.DefaultFs{}, kubeletConfigPath) + if err != nil { + return nil, fmt.Errorf(errFmt, kubeletConfigPath, err) + } + + kc, err := loader.Load() + if err != nil { + return nil, fmt.Errorf(errFmt, kubeletConfigPath, err) + } + + scheme, _, err := kubeletconfigscheme.NewSchemeAndCodecs() if err != nil { return nil, err } kubeletConfig := &kubeletconfigv1beta1.KubeletConfiguration{} - if err := yaml.Unmarshal(kubeletBytes, kubeletConfig); err != nil { + err = scheme.Convert(kc, kubeletConfig, nil) + if err != nil { return nil, err } + return kubeletConfig, nil }