Skip to content

Commit

Permalink
Read Kubelet configuration the same way as Kubelet to apply default v…
Browse files Browse the repository at this point in the history
…alues

Signed-off-by: PiotrProkop <[email protected]>
  • Loading branch information
PiotrProkop committed Feb 10, 2023
1 parent 698bce2 commit e621e13
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions pkg/utils/kubeconf/kubelet_config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 %v"

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
}

0 comments on commit e621e13

Please sign in to comment.