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

     1  //go:build windows
     2  // +build windows
     3  
     4  /*
     5  Copyright 2021 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package componentconfigs
    21  
    22  import (
    23  	"os"
    24  	"path/filepath"
    25  	"strings"
    26  
    27  	"github.com/pkg/errors"
    28  	"k8s.io/klog/v2"
    29  	kubeletconfig "k8s.io/kubelet/config/v1beta1"
    30  	"k8s.io/utils/ptr"
    31  )
    32  
    33  // Mutate modifies absolute path fields in the KubeletConfiguration to be Windows compatible absolute paths.
    34  func (kc *kubeletConfig) Mutate() error {
    35  	// When "kubeadm join" downloads the KubeletConfiguration from the cluster on Windows
    36  	// nodes, it would contain absolute paths that may lack drive letters, since the config
    37  	// could have been generated on a Linux control-plane node. On Windows the
    38  	// Golang filepath.IsAbs() function returns false unless the path contains a drive letter.
    39  	// This trips client-go and the kubelet, creating problems on Windows nodes.
    40  	// Fixing it in client-go or the kubelet is a breaking change to existing Windows
    41  	// users that rely on relative paths:
    42  	//   https://github.com/kubernetes/kubernetes/pull/77710#issuecomment-491989621
    43  	//
    44  	// Thus, a workaround here is to adapt the KubeletConfiguration paths for Windows.
    45  	// Note this is currently bound to KubeletConfiguration v1beta1.
    46  	klog.V(2).Infoln("[componentconfig] Adapting the paths in the KubeletConfiguration for Windows...")
    47  
    48  	// Get the drive from where the kubeadm binary was called.
    49  	exe, err := os.Executable()
    50  	if err != nil {
    51  		return errors.Wrap(err, "could not obtain information about the kubeadm executable")
    52  	}
    53  	drive := filepath.VolumeName(filepath.Dir(exe))
    54  	klog.V(2).Infof("[componentconfig] Assuming Windows drive %q", drive)
    55  
    56  	// Mutate the paths in the config.
    57  	mutatePaths(&kc.config, drive)
    58  	return nil
    59  }
    60  
    61  func mutatePaths(cfg *kubeletconfig.KubeletConfiguration, drive string) {
    62  	mutateStringField := func(name string, field *string) {
    63  		// filepath.IsAbs() is not reliable here in the Windows runtime, so check if the
    64  		// path starts with "/" instead. This means the path originated from a Unix node and
    65  		// is an absolute path.
    66  		if !strings.HasPrefix(*field, "/") {
    67  			return
    68  		}
    69  		// Prepend the drive letter to the path and update the field.
    70  		*field = filepath.Join(drive, *field)
    71  		klog.V(2).Infof("[componentconfig] kubelet/Windows: adapted path for field %q to %q", name, *field)
    72  	}
    73  
    74  	// Mutate the fields we care about.
    75  	klog.V(2).Infof("[componentconfig] kubelet/Windows: changing field \"resolverConfig\" to empty")
    76  	cfg.ResolverConfig = ptr.To("")
    77  	mutateStringField("staticPodPath", &cfg.StaticPodPath)
    78  	mutateStringField("authentication.x509.clientCAFile", &cfg.Authentication.X509.ClientCAFile)
    79  }