...

Source file src/k8s.io/kubernetes/cmd/kubeadm/app/componentconfigs/kubelet_windows.go

Documentation: k8s.io/kubernetes/cmd/kubeadm/app/componentconfigs

     1  /*
     2  Copyright 2021 The Kubernetes 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 componentconfigs
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  	"strings"
    23  
    24  	"github.com/pkg/errors"
    25  	"k8s.io/klog/v2"
    26  	kubeletconfig "k8s.io/kubelet/config/v1beta1"
    27  	"k8s.io/utils/ptr"
    28  )
    29  
    30  // Mutate modifies absolute path fields in the KubeletConfiguration to be Windows compatible absolute paths.
    31  func (kc *kubeletConfig) Mutate() error {
    32  	// When "kubeadm join" downloads the KubeletConfiguration from the cluster on Windows
    33  	// nodes, it would contain absolute paths that may lack drive letters, since the config
    34  	// could have been generated on a Linux control-plane node. On Windows the
    35  	// Golang filepath.IsAbs() function returns false unless the path contains a drive letter.
    36  	// This trips client-go and the kubelet, creating problems on Windows nodes.
    37  	// Fixing it in client-go or the kubelet is a breaking change to existing Windows
    38  	// users that rely on relative paths:
    39  	//   https://github.com/kubernetes/kubernetes/pull/77710#issuecomment-491989621
    40  	//
    41  	// Thus, a workaround here is to adapt the KubeletConfiguration paths for Windows.
    42  	// Note this is currently bound to KubeletConfiguration v1beta1.
    43  	klog.V(2).Infoln("[componentconfig] Adapting the paths in the KubeletConfiguration for Windows...")
    44  
    45  	// Get the drive from where the kubeadm binary was called.
    46  	exe, err := os.Executable()
    47  	if err != nil {
    48  		return errors.Wrap(err, "could not obtain information about the kubeadm executable")
    49  	}
    50  	drive := filepath.VolumeName(filepath.Dir(exe))
    51  	klog.V(2).Infof("[componentconfig] Assuming Windows drive %q", drive)
    52  
    53  	// Mutate the paths in the config.
    54  	mutatePaths(&kc.config, drive)
    55  	return nil
    56  }
    57  
    58  func mutatePaths(cfg *kubeletconfig.KubeletConfiguration, drive string) {
    59  	mutateStringField := func(name string, field *string) {
    60  		// filepath.IsAbs() is not reliable here in the Windows runtime, so check if the
    61  		// path starts with "/" instead. This means the path originated from a Unix node and
    62  		// is an absolute path.
    63  		if !strings.HasPrefix(*field, "/") {
    64  			return
    65  		}
    66  		// Prepend the drive letter to the path and update the field.
    67  		*field = filepath.Join(drive, *field)
    68  		klog.V(2).Infof("[componentconfig] kubelet/Windows: adapted path for field %q to %q", name, *field)
    69  	}
    70  
    71  	// Mutate the fields we care about.
    72  	klog.V(2).Infof("[componentconfig] kubelet/Windows: changing field \"resolverConfig\" to empty")
    73  	cfg.ResolverConfig = ptr.To("")
    74  	mutateStringField("staticPodPath", &cfg.StaticPodPath)
    75  	mutateStringField("authentication.x509.clientCAFile", &cfg.Authentication.X509.ClientCAFile)
    76  }
    77  

View as plain text