...

Source file src/k8s.io/kubernetes/cmd/cloud-controller-manager/main.go

Documentation: k8s.io/kubernetes/cmd/cloud-controller-manager

     1  /*
     2  Copyright 2016 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  // The external controller manager is responsible for running controller loops that
    18  // are cloud provider dependent. It uses the API to listen to new events on resources.
    19  
    20  // This file should be written by each cloud provider.
    21  // For a minimal working example, please refer to k8s.io/cloud-provider/sample/basic_main.go
    22  // For more details, please refer to k8s.io/kubernetes/cmd/cloud-controller-manager/main.go
    23  // The current file demonstrate how other cloud provider should leverage CCM and it uses fake parameters. Please modify for your own use.
    24  
    25  package main
    26  
    27  import (
    28  	"os"
    29  
    30  	"k8s.io/apimachinery/pkg/util/wait"
    31  	cloudprovider "k8s.io/cloud-provider"
    32  	"k8s.io/cloud-provider/app"
    33  	cloudcontrollerconfig "k8s.io/cloud-provider/app/config"
    34  	"k8s.io/cloud-provider/names"
    35  	"k8s.io/cloud-provider/options"
    36  	"k8s.io/component-base/cli"
    37  	cliflag "k8s.io/component-base/cli/flag"
    38  	_ "k8s.io/component-base/metrics/prometheus/clientgo" // load all the prometheus client-go plugins
    39  	_ "k8s.io/component-base/metrics/prometheus/version"  // for version metric registration
    40  	"k8s.io/klog/v2"
    41  	kcmnames "k8s.io/kubernetes/cmd/kube-controller-manager/names"
    42  	// For existing cloud providers, the option to import legacy providers is still available.
    43  	// e.g. _"k8s.io/legacy-cloud-providers/<provider>"
    44  )
    45  
    46  func main() {
    47  	ccmOptions, err := options.NewCloudControllerManagerOptions()
    48  	if err != nil {
    49  		klog.Fatalf("unable to initialize command options: %v", err)
    50  	}
    51  
    52  	controllerInitializers := app.DefaultInitFuncConstructors
    53  	controllerAliases := names.CCMControllerAliases()
    54  	// Here is an example to remove the controller which is not needed.
    55  	// e.g. remove the cloud-node-lifecycle controller which current cloud provider does not need.
    56  	//delete(controllerInitializers, "cloud-node-lifecycle")
    57  
    58  	// Here is an example to add an controller(NodeIpamController) which will be used by cloud provider
    59  	// generate nodeIPAMConfig. Here is an sample code.
    60  	// If you do not need additional controller, please ignore.
    61  
    62  	nodeIpamController := nodeIPAMController{}
    63  	nodeIpamController.nodeIPAMControllerOptions.NodeIPAMControllerConfiguration = &nodeIpamController.nodeIPAMControllerConfiguration
    64  	fss := cliflag.NamedFlagSets{}
    65  	nodeIpamController.nodeIPAMControllerOptions.AddFlags(fss.FlagSet(kcmnames.NodeIpamController))
    66  
    67  	controllerInitializers[kcmnames.NodeIpamController] = app.ControllerInitFuncConstructor{
    68  		// "node-controller" is the shared identity of all node controllers, including node, node lifecycle, and node ipam.
    69  		// See https://github.com/kubernetes/kubernetes/pull/72764#issuecomment-453300990 for more context.
    70  		InitContext: app.ControllerInitContext{
    71  			ClientName: "node-controller",
    72  		},
    73  		Constructor: nodeIpamController.StartNodeIpamControllerWrapper,
    74  	}
    75  	controllerAliases["nodeipam"] = kcmnames.NodeIpamController
    76  
    77  	command := app.NewCloudControllerManagerCommand(ccmOptions, cloudInitializer, controllerInitializers, controllerAliases, fss, wait.NeverStop)
    78  	code := cli.Run(command)
    79  	os.Exit(code)
    80  }
    81  
    82  func cloudInitializer(config *cloudcontrollerconfig.CompletedConfig) cloudprovider.Interface {
    83  	cloudConfig := config.ComponentConfig.KubeCloudShared.CloudProvider
    84  	// initialize cloud provider with the cloud provider name and config file provided
    85  	cloud, err := cloudprovider.InitCloudProvider(cloudConfig.Name, cloudConfig.CloudConfigFile)
    86  	if err != nil {
    87  		klog.Fatalf("Cloud provider could not be initialized: %v", err)
    88  	}
    89  	if cloud == nil {
    90  		klog.Fatalf("Cloud provider is nil")
    91  	}
    92  
    93  	if !cloud.HasClusterID() {
    94  		if config.ComponentConfig.KubeCloudShared.AllowUntaggedCloud {
    95  			klog.Warning("detected a cluster without a ClusterID.  A ClusterID will be required in the future.  Please tag your cluster to avoid any future issues")
    96  		} else {
    97  			klog.Fatalf("no ClusterID found.  A ClusterID is required for the cloud provider to function properly.  This check can be bypassed by setting the allow-untagged-cloud option")
    98  		}
    99  	}
   100  
   101  	return cloud
   102  }
   103  

View as plain text