2016-10-07 20:39:21 -04:00
/ *
Copyright 2016 The Kubernetes Authors .
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
// The external controller manager is responsible for running controller loops that
2016-12-17 12:27:48 -05:00
// are cloud provider dependent. It uses the API to listen to new events on resources.
2016-10-07 20:39:21 -04:00
2020-08-03 06:27:45 -04:00
// This file should be written by each cloud provider.
2020-11-09 14:58:09 -05:00
// For an minimal working example, please refer to k8s.io/cloud-provider/sample/basic_main.go
// For more details, please refer to k8s.io/kubernetes/cmd/cloud-controller-manager/main.go
2020-08-03 06:27:45 -04:00
// The current file demonstrate how other cloud provider should leverage CCM and it uses fake parameters. Please modify for your own use.
2016-10-07 20:39:21 -04:00
package main
import (
"os"
2021-02-10 18:00:44 -05:00
"k8s.io/apimachinery/pkg/util/wait"
2021-06-25 03:33:31 -04:00
cloudprovider "k8s.io/cloud-provider"
2020-10-20 13:28:15 -04:00
"k8s.io/cloud-provider/app"
2020-08-03 06:27:45 -04:00
cloudcontrollerconfig "k8s.io/cloud-provider/app/config"
"k8s.io/cloud-provider/options"
2021-09-16 12:18:35 -04:00
"k8s.io/component-base/cli"
2021-03-02 13:26:51 -05:00
cliflag "k8s.io/component-base/cli/flag"
2019-08-22 20:40:36 -04:00
_ "k8s.io/component-base/metrics/prometheus/clientgo" // load all the prometheus client-go plugins
2020-07-30 11:58:54 -04:00
_ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration
2020-08-03 06:27:45 -04:00
"k8s.io/klog/v2"
2020-11-09 14:58:09 -05:00
// For existing cloud providers, the option to import legacy providers is still available.
// e.g. _"k8s.io/legacy-cloud-providers/<provider>"
2020-08-03 06:27:45 -04:00
)
2016-10-07 20:39:21 -04:00
func main ( ) {
2021-01-14 16:07:53 -05:00
ccmOptions , err := options . NewCloudControllerManagerOptions ( )
2020-08-03 06:27:45 -04:00
if err != nil {
klog . Fatalf ( "unable to initialize command options: %v" , err )
}
2021-01-14 16:07:53 -05:00
controllerInitializers := app . DefaultInitFuncConstructors
2020-08-03 06:27:45 -04:00
// Here is an example to remove the controller which is not needed.
// e.g. remove the cloud-node-lifecycle controller which current cloud provider does not need.
//delete(controllerInitializers, "cloud-node-lifecycle")
// Here is an example to add an controller(NodeIpamController) which will be used by cloud provider
2021-02-28 04:50:52 -05:00
// generate nodeIPAMConfig. Here is an sample code.
2020-08-03 06:27:45 -04:00
// If you do not need additional controller, please ignore.
2021-03-01 19:14:24 -05:00
nodeIpamController := nodeIPAMController { }
nodeIpamController . nodeIPAMControllerOptions . NodeIPAMControllerConfiguration = & nodeIpamController . nodeIPAMControllerConfiguration
2021-03-02 13:26:51 -05:00
fss := cliflag . NamedFlagSets { }
nodeIpamController . nodeIPAMControllerOptions . AddFlags ( fss . FlagSet ( "nodeipam controller" ) )
2021-06-25 03:33:31 -04:00
controllerInitializers [ "nodeipam" ] = app . ControllerInitFuncConstructor {
// "node-controller" is the shared identity of all node controllers, including node, node lifecycle, and node ipam.
// See https://github.com/kubernetes/kubernetes/pull/72764#issuecomment-453300990 for more context.
InitContext : app . ControllerInitContext {
ClientName : "node-controller" ,
} ,
Constructor : nodeIpamController . StartNodeIpamControllerWrapper ,
}
2020-08-03 06:27:45 -04:00
2021-03-02 13:26:51 -05:00
command := app . NewCloudControllerManagerCommand ( ccmOptions , cloudInitializer , controllerInitializers , fss , wait . NeverStop )
2021-09-16 12:18:35 -04:00
code := cli . Run ( command )
os . Exit ( code )
2016-10-07 20:39:21 -04:00
}
2020-08-03 06:27:45 -04:00
2021-01-20 15:09:46 -05:00
func cloudInitializer ( config * cloudcontrollerconfig . CompletedConfig ) cloudprovider . Interface {
2021-02-10 18:00:44 -05:00
cloudConfig := config . ComponentConfig . KubeCloudShared . CloudProvider
2021-01-20 15:09:46 -05:00
// initialize cloud provider with the cloud provider name and config file provided
2021-02-10 18:00:44 -05:00
cloud , err := cloudprovider . InitCloudProvider ( cloudConfig . Name , cloudConfig . CloudConfigFile )
2021-01-20 15:09:46 -05:00
if err != nil {
klog . Fatalf ( "Cloud provider could not be initialized: %v" , err )
}
if cloud == nil {
klog . Fatalf ( "Cloud provider is nil" )
}
if ! cloud . HasClusterID ( ) {
if config . ComponentConfig . KubeCloudShared . AllowUntaggedCloud {
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" )
} else {
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" )
}
}
return cloud
}