mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-10 05:30:26 +00:00
Fix flag passing in CCM.
This commit is contained in:
@@ -19,14 +19,12 @@ limitations under the License.
|
||||
|
||||
// This file should be written by each cloud provider.
|
||||
// For an minimal working example, please refer to k8s.io/cloud-provider/sample/basic_main.go
|
||||
// For an advanced example, please refer to k8s.io/cloud-provider/sample/advanced_main.go
|
||||
// For more details, please refer to k8s.io/kubernetes/cmd/cloud-controller-manager/main.go
|
||||
// The current file demonstrate how other cloud provider should leverage CCM and it uses fake parameters. Please modify for your own use.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -57,47 +55,41 @@ const (
|
||||
func main() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
pflag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true
|
||||
_ = pflag.CommandLine.Parse(os.Args[1:])
|
||||
|
||||
// this is an example of allow-listing specific controller loops
|
||||
controllerList := []string{"cloud-node", "cloud-node-lifecycle", "service", "route"}
|
||||
|
||||
s, err := options.NewCloudControllerManagerOptions()
|
||||
ccmOptions, err := options.NewCloudControllerManagerOptions()
|
||||
if err != nil {
|
||||
klog.Fatalf("unable to initialize command options: %v", err)
|
||||
}
|
||||
c, err := s.Config(controllerList, app.ControllersDisabledByDefault.List())
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
cloud, err := cloudprovider.InitCloudProvider(cloudProviderName, c.ComponentConfig.KubeCloudShared.CloudProvider.CloudConfigFile)
|
||||
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 c.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")
|
||||
cloudInitializer := func(config *cloudcontrollerconfig.CompletedConfig) cloudprovider.Interface {
|
||||
cloudConfigFile := config.ComponentConfig.KubeCloudShared.CloudProvider.CloudConfigFile
|
||||
// initialize cloud provider with the cloud provider name and config file provided
|
||||
cloud, err := cloudprovider.InitCloudProvider(cloudProviderName, cloudConfigFile)
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize the cloud provider with a reference to the clientBuilder
|
||||
cloud.Initialize(config.ClientBuilder, make(chan struct{}))
|
||||
// Set the informer on the user cloud object
|
||||
if informerUserCloud, ok := cloud.(cloudprovider.InformerUser); ok {
|
||||
informerUserCloud.SetInformers(config.SharedInformers)
|
||||
}
|
||||
|
||||
return cloud
|
||||
}
|
||||
|
||||
// Initialize the cloud provider with a reference to the clientBuilder
|
||||
cloud.Initialize(c.ClientBuilder, make(chan struct{}))
|
||||
// Set the informer on the user cloud object
|
||||
if informerUserCloud, ok := cloud.(cloudprovider.InformerUser); ok {
|
||||
informerUserCloud.SetInformers(c.SharedInformers)
|
||||
}
|
||||
|
||||
controllerInitializers := app.DefaultControllerInitializers(c.Complete(), cloud)
|
||||
|
||||
controllerInitializers := app.DefaultInitFuncConstructors
|
||||
// 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")
|
||||
@@ -105,16 +97,9 @@ func main() {
|
||||
// Here is an example to add an controller(NodeIpamController) which will be used by cloud provider
|
||||
// generate nodeIPAMConfig. Here is an sample code. Please pass the right parameter in your code.
|
||||
// If you do not need additional controller, please ignore.
|
||||
nodeIPAMConfig := nodeipamconfig.NodeIPAMControllerConfiguration{
|
||||
ServiceCIDR: "sample",
|
||||
SecondaryServiceCIDR: "sample",
|
||||
NodeCIDRMaskSize: 11,
|
||||
NodeCIDRMaskSizeIPv4: 11,
|
||||
NodeCIDRMaskSizeIPv6: 111,
|
||||
}
|
||||
controllerInitializers["nodeipam"] = startNodeIpamControllerWrapper(c.Complete(), nodeIPAMConfig, cloud)
|
||||
controllerInitializers["nodeipam"] = startNodeIpamControllerWrapper
|
||||
|
||||
command := app.NewCloudControllerManagerCommand(s, c, controllerInitializers)
|
||||
command := app.NewCloudControllerManagerCommand(cloudProviderName, ccmOptions, cloudInitializer, controllerInitializers)
|
||||
|
||||
// TODO: once we switch everything over to Cobra commands, we can go back to calling
|
||||
// utilflag.InitFlags() (by removing its pflag.Parse() call). For now, we have to set the
|
||||
@@ -125,20 +110,20 @@ func main() {
|
||||
logs.InitLogs()
|
||||
defer logs.FlushLogs()
|
||||
|
||||
// the flags could be set before execute
|
||||
command.Flags().VisitAll(func(flag *pflag.Flag) {
|
||||
if flag.Name == "cloud-provider" {
|
||||
flag.Value.Set("SampleCloudProviderFlagValue")
|
||||
return
|
||||
}
|
||||
})
|
||||
if err := command.Execute(); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func startNodeIpamControllerWrapper(ccmconfig *cloudcontrollerconfig.CompletedConfig, nodeIPAMConfig nodeipamconfig.NodeIPAMControllerConfiguration, cloud cloudprovider.Interface) func(ctx genericcontrollermanager.ControllerContext) (http.Handler, bool, error) {
|
||||
func startNodeIpamControllerWrapper(completedConfig *cloudcontrollerconfig.CompletedConfig, cloud cloudprovider.Interface) app.InitFunc {
|
||||
nodeIPAMConfig := nodeipamconfig.NodeIPAMControllerConfiguration{
|
||||
ServiceCIDR: "sample",
|
||||
SecondaryServiceCIDR: "sample",
|
||||
NodeCIDRMaskSize: 11,
|
||||
NodeCIDRMaskSizeIPv4: 11,
|
||||
NodeCIDRMaskSizeIPv6: 111,
|
||||
}
|
||||
return func(ctx genericcontrollermanager.ControllerContext) (http.Handler, bool, error) {
|
||||
return startNodeIpamController(ccmconfig, nodeIPAMConfig, ctx, cloud)
|
||||
return startNodeIpamController(completedConfig, nodeIPAMConfig, ctx, cloud)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user