...

Source file src/k8s.io/kubernetes/cmd/kube-controller-manager/app/plugins.go

Documentation: k8s.io/kubernetes/cmd/kube-controller-manager/app

     1  /*
     2  Copyright 2014 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 app
    18  
    19  import (
    20  	// This file exists to force the desired plugin implementations to be linked.
    21  	// This should probably be part of some configuration fed into the build for a
    22  	// given binary target.
    23  
    24  	"fmt"
    25  
    26  	"k8s.io/klog/v2"
    27  
    28  	// ensure the cloud providers are installed
    29  	_ "k8s.io/kubernetes/pkg/cloudprovider/providers"
    30  	// Volume plugins
    31  	"k8s.io/kubernetes/pkg/volume"
    32  	"k8s.io/kubernetes/pkg/volume/csi"
    33  	"k8s.io/kubernetes/pkg/volume/fc"
    34  	"k8s.io/kubernetes/pkg/volume/flexvolume"
    35  	"k8s.io/kubernetes/pkg/volume/hostpath"
    36  	"k8s.io/kubernetes/pkg/volume/iscsi"
    37  	"k8s.io/kubernetes/pkg/volume/local"
    38  	"k8s.io/kubernetes/pkg/volume/nfs"
    39  	volumeutil "k8s.io/kubernetes/pkg/volume/util"
    40  
    41  	utilfeature "k8s.io/apiserver/pkg/util/feature"
    42  	persistentvolumeconfig "k8s.io/kubernetes/pkg/controller/volume/persistentvolume/config"
    43  	"k8s.io/utils/exec"
    44  )
    45  
    46  // ProbeAttachableVolumePlugins collects all volume plugins for the attach/
    47  // detach controller.
    48  // The list of plugins is manually compiled. This code and the plugin
    49  // initialization code for kubelet really, really need a through refactor.
    50  func ProbeAttachableVolumePlugins(logger klog.Logger) ([]volume.VolumePlugin, error) {
    51  	var err error
    52  	allPlugins := []volume.VolumePlugin{}
    53  	allPlugins, err = appendAttachableLegacyProviderVolumes(logger, allPlugins, utilfeature.DefaultFeatureGate)
    54  	if err != nil {
    55  		return allPlugins, err
    56  	}
    57  	allPlugins = append(allPlugins, fc.ProbeVolumePlugins()...)
    58  	allPlugins = append(allPlugins, iscsi.ProbeVolumePlugins()...)
    59  	allPlugins = append(allPlugins, csi.ProbeVolumePlugins()...)
    60  	return allPlugins, nil
    61  }
    62  
    63  // GetDynamicPluginProber gets the probers of dynamically discoverable plugins
    64  // for the attach/detach controller.
    65  // Currently only Flexvolume plugins are dynamically discoverable.
    66  func GetDynamicPluginProber(config persistentvolumeconfig.VolumeConfiguration) volume.DynamicPluginProber {
    67  	return flexvolume.GetDynamicPluginProber(config.FlexVolumePluginDir, exec.New() /*exec.Interface*/)
    68  }
    69  
    70  // ProbeExpandableVolumePlugins returns volume plugins which are expandable
    71  func ProbeExpandableVolumePlugins(logger klog.Logger, config persistentvolumeconfig.VolumeConfiguration) ([]volume.VolumePlugin, error) {
    72  	var err error
    73  	allPlugins := []volume.VolumePlugin{}
    74  	allPlugins, err = appendExpandableLegacyProviderVolumes(logger, allPlugins, utilfeature.DefaultFeatureGate)
    75  	if err != nil {
    76  		return allPlugins, err
    77  	}
    78  	allPlugins = append(allPlugins, fc.ProbeVolumePlugins()...)
    79  	return allPlugins, nil
    80  }
    81  
    82  // ProbeControllerVolumePlugins collects all persistent volume plugins into an
    83  // easy to use list. Only volume plugins that implement any of
    84  // provisioner/recycler/deleter interface should be returned.
    85  func ProbeControllerVolumePlugins(logger klog.Logger, config persistentvolumeconfig.VolumeConfiguration) ([]volume.VolumePlugin, error) {
    86  	allPlugins := []volume.VolumePlugin{}
    87  
    88  	// The list of plugins to probe is decided by this binary, not
    89  	// by dynamic linking or other "magic".  Plugins will be analyzed and
    90  	// initialized later.
    91  
    92  	// Each plugin can make use of VolumeConfig.  The single arg to this func contains *all* enumerated
    93  	// options meant to configure volume plugins.  From that single config, create an instance of volume.VolumeConfig
    94  	// for a specific plugin and pass that instance to the plugin's ProbeVolumePlugins(config) func.
    95  
    96  	// HostPath recycling is for testing and development purposes only!
    97  	hostPathConfig := volume.VolumeConfig{
    98  		RecyclerMinimumTimeout:   int(config.PersistentVolumeRecyclerConfiguration.MinimumTimeoutHostPath),
    99  		RecyclerTimeoutIncrement: int(config.PersistentVolumeRecyclerConfiguration.IncrementTimeoutHostPath),
   100  		RecyclerPodTemplate:      volume.NewPersistentVolumeRecyclerPodTemplate(),
   101  		ProvisioningEnabled:      config.EnableHostPathProvisioning,
   102  	}
   103  	if err := AttemptToLoadRecycler(config.PersistentVolumeRecyclerConfiguration.PodTemplateFilePathHostPath, &hostPathConfig); err != nil {
   104  		logger.Error(err, "Could not create hostpath recycler pod from file", "path", config.PersistentVolumeRecyclerConfiguration.PodTemplateFilePathHostPath)
   105  		klog.FlushAndExit(klog.ExitFlushTimeout, 1)
   106  	}
   107  	allPlugins = append(allPlugins, hostpath.ProbeVolumePlugins(hostPathConfig)...)
   108  
   109  	nfsConfig := volume.VolumeConfig{
   110  		RecyclerMinimumTimeout:   int(config.PersistentVolumeRecyclerConfiguration.MinimumTimeoutNFS),
   111  		RecyclerTimeoutIncrement: int(config.PersistentVolumeRecyclerConfiguration.IncrementTimeoutNFS),
   112  		RecyclerPodTemplate:      volume.NewPersistentVolumeRecyclerPodTemplate(),
   113  	}
   114  	if err := AttemptToLoadRecycler(config.PersistentVolumeRecyclerConfiguration.PodTemplateFilePathNFS, &nfsConfig); err != nil {
   115  		logger.Error(err, "Could not create NFS recycler pod from file", "path", config.PersistentVolumeRecyclerConfiguration.PodTemplateFilePathNFS)
   116  		klog.FlushAndExit(klog.ExitFlushTimeout, 1)
   117  	}
   118  	allPlugins = append(allPlugins, nfs.ProbeVolumePlugins(nfsConfig)...)
   119  
   120  	var err error
   121  	allPlugins, err = appendExpandableLegacyProviderVolumes(logger, allPlugins, utilfeature.DefaultFeatureGate)
   122  	if err != nil {
   123  		return allPlugins, err
   124  	}
   125  
   126  	allPlugins = append(allPlugins, local.ProbeVolumePlugins()...)
   127  	allPlugins = append(allPlugins, csi.ProbeVolumePlugins()...)
   128  
   129  	return allPlugins, nil
   130  }
   131  
   132  // AttemptToLoadRecycler tries decoding a pod from a filepath for use as a recycler for a volume.
   133  // If successful, this method will set the recycler on the config.
   134  // If unsuccessful, an error is returned. Function is exported for reuse downstream.
   135  func AttemptToLoadRecycler(path string, config *volume.VolumeConfig) error {
   136  	if path != "" {
   137  		recyclerPod, err := volumeutil.LoadPodFromFile(path)
   138  		if err != nil {
   139  			return err
   140  		}
   141  		if err = volume.ValidateRecyclerPodTemplate(recyclerPod); err != nil {
   142  			return fmt.Errorf("pod specification (%v): %v", path, err)
   143  		}
   144  		config.RecyclerPodTemplate = recyclerPod
   145  	}
   146  	return nil
   147  }
   148  

View as plain text