mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-03 19:58:17 +00:00 
			
		
		
		
	Add demonstration of wiring nodeIPAMController config object
This commit is contained in:
		@@ -1,6 +1,8 @@
 | 
			
		||||
rules:
 | 
			
		||||
  - selectorRegexp: k8s[.]io/kubernetes
 | 
			
		||||
    allowedPrefixes:
 | 
			
		||||
      - k8s.io/kubernetes/cmd/kube-controller-manager/app/options
 | 
			
		||||
      - k8s.io/kubernetes/cmd/kube-controller-manager/app/config
 | 
			
		||||
      - k8s.io/kubernetes/pkg/api/legacyscheme
 | 
			
		||||
      - k8s.io/kubernetes/pkg/api/service
 | 
			
		||||
      - k8s.io/kubernetes/pkg/api/v1/pod
 | 
			
		||||
@@ -35,3 +37,6 @@ rules:
 | 
			
		||||
      - k8s.io/kubernetes/pkg/util/node
 | 
			
		||||
      - k8s.io/kubernetes/pkg/util/parsers
 | 
			
		||||
      - k8s.io/kubernetes/pkg/util/taints
 | 
			
		||||
      - k8s.io/kubernetes/pkg/proxy/util
 | 
			
		||||
      - k8s.io/kubernetes/pkg/proxy/util/testing
 | 
			
		||||
      - k8s.io/kubernetes/pkg/util/sysctl
 | 
			
		||||
@@ -42,6 +42,7 @@ import (
 | 
			
		||||
	_ "k8s.io/component-base/metrics/prometheus/version"  // for version metric registration
 | 
			
		||||
	genericcontrollermanager "k8s.io/controller-manager/app"
 | 
			
		||||
	"k8s.io/klog/v2"
 | 
			
		||||
	nodeipamcontrolleroptions "k8s.io/kubernetes/cmd/kube-controller-manager/app/options"
 | 
			
		||||
	nodeipamconfig "k8s.io/kubernetes/pkg/controller/nodeipam/config"
 | 
			
		||||
	// For existing cloud providers, the option to import legacy providers is still available.
 | 
			
		||||
	// e.g. _"k8s.io/legacy-cloud-providers/<provider>"
 | 
			
		||||
@@ -60,7 +61,33 @@ func main() {
 | 
			
		||||
		klog.Fatalf("unable to initialize command options: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	cloudInitializer := func(config *cloudcontrollerconfig.CompletedConfig) cloudprovider.Interface {
 | 
			
		||||
	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")
 | 
			
		||||
 | 
			
		||||
	// 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.
 | 
			
		||||
	controllerInitializers["nodeipam"] = startNodeIpamControllerWrapper
 | 
			
		||||
 | 
			
		||||
	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
 | 
			
		||||
	// normalize func and add the go flag set by hand.
 | 
			
		||||
	// Here is an sample
 | 
			
		||||
	pflag.CommandLine.SetNormalizeFunc(flag.WordSepNormalizeFunc)
 | 
			
		||||
	// utilflag.InitFlags()
 | 
			
		||||
	logs.InitLogs()
 | 
			
		||||
	defer logs.FlushLogs()
 | 
			
		||||
 | 
			
		||||
	if err := command.Execute(); err != nil {
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func cloudInitializer(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)
 | 
			
		||||
@@ -87,42 +114,19 @@ func main() {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 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")
 | 
			
		||||
 | 
			
		||||
	// 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.
 | 
			
		||||
	controllerInitializers["nodeipam"] = startNodeIpamControllerWrapper
 | 
			
		||||
 | 
			
		||||
	command := app.NewCloudControllerManagerCommand(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
 | 
			
		||||
	// normalize func and add the go flag set by hand.
 | 
			
		||||
	// Here is an sample
 | 
			
		||||
	pflag.CommandLine.SetNormalizeFunc(flag.WordSepNormalizeFunc)
 | 
			
		||||
	// utilflag.InitFlags()
 | 
			
		||||
	logs.InitLogs()
 | 
			
		||||
	defer logs.FlushLogs()
 | 
			
		||||
 | 
			
		||||
	if err := command.Execute(); err != nil {
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func startNodeIpamControllerWrapper(completedConfig *cloudcontrollerconfig.CompletedConfig, cloud cloudprovider.Interface) app.InitFunc {
 | 
			
		||||
	nodeIPAMConfig := nodeipamconfig.NodeIPAMControllerConfiguration{
 | 
			
		||||
		ServiceCIDR:          "sample",
 | 
			
		||||
		SecondaryServiceCIDR: "sample",
 | 
			
		||||
		NodeCIDRMaskSize:     11,
 | 
			
		||||
		NodeCIDRMaskSizeIPv4: 11,
 | 
			
		||||
		NodeCIDRMaskSizeIPv6: 111,
 | 
			
		||||
	fs := pflag.NewFlagSet("fs", pflag.ContinueOnError)
 | 
			
		||||
	var nodeIPAMControllerOptions nodeipamcontrolleroptions.NodeIPAMControllerOptions
 | 
			
		||||
	nodeIPAMControllerOptions.AddFlags(fs)
 | 
			
		||||
	errors := nodeIPAMControllerOptions.Validate()
 | 
			
		||||
	if len(errors) > 0 {
 | 
			
		||||
		klog.Fatal("NodeIPAM controller values are not properly.")
 | 
			
		||||
	}
 | 
			
		||||
	var nodeIPAMConfig nodeipamconfig.NodeIPAMControllerConfiguration
 | 
			
		||||
	nodeIPAMControllerOptions.ApplyTo(&nodeIPAMConfig)
 | 
			
		||||
 | 
			
		||||
	return func(ctx genericcontrollermanager.ControllerContext) (http.Handler, bool, error) {
 | 
			
		||||
		return startNodeIpamController(completedConfig, nodeIPAMConfig, ctx, cloud)
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -64,7 +64,7 @@ const (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// NewCloudControllerManagerCommand creates a *cobra.Command object with default parameters
 | 
			
		||||
func NewCloudControllerManagerCommand(s *options.CloudControllerManagerOptions, cloudInitializer InitCloudFunc, initFuncConstructor map[string]InitFuncConstructor) *cobra.Command {
 | 
			
		||||
func NewCloudControllerManagerCommand(cloudProviderName string, s *options.CloudControllerManagerOptions, cloudInitializer InitCloudFunc, initFuncConstructor map[string]InitFuncConstructor) *cobra.Command {
 | 
			
		||||
 | 
			
		||||
	cmd := &cobra.Command{
 | 
			
		||||
		Use: "cloud-controller-manager",
 | 
			
		||||
@@ -72,6 +72,11 @@ func NewCloudControllerManagerCommand(s *options.CloudControllerManagerOptions,
 | 
			
		||||
the cloud specific control loops shipped with Kubernetes.`,
 | 
			
		||||
		Run: func(cmd *cobra.Command, args []string) {
 | 
			
		||||
			verflag.PrintAndExitIfRequested()
 | 
			
		||||
 | 
			
		||||
			cloudProviderFlag := cmd.Flags().Lookup("cloud-provider")
 | 
			
		||||
			if cloudProviderFlag.Value.String() == "" {
 | 
			
		||||
				cloudProviderFlag.Value.Set(cloudProviderName)
 | 
			
		||||
			}
 | 
			
		||||
			cliflag.PrintFlags(cmd.Flags())
 | 
			
		||||
 | 
			
		||||
			c, err := s.Config(ControllerNames(initFuncConstructor), ControllersDisabledByDefault.List())
 | 
			
		||||
@@ -130,8 +135,6 @@ the cloud specific control loops shipped with Kubernetes.`,
 | 
			
		||||
	return cmd
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type InitCloudFunc func(config *cloudcontrollerconfig.CompletedConfig) cloudprovider.Interface
 | 
			
		||||
 | 
			
		||||
// Run runs the ExternalCMServer.  This should never exit.
 | 
			
		||||
func Run(c *cloudcontrollerconfig.CompletedConfig, controllerInitializers map[string]InitFunc, stopCh <-chan struct{}) error {
 | 
			
		||||
	// To help debugging, immediately log version
 | 
			
		||||
@@ -262,11 +265,15 @@ func startControllers(ctx genericcontrollermanager.ControllerContext, c *cloudco
 | 
			
		||||
	select {}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// InitCloudFunc is used to initialize cloud
 | 
			
		||||
type InitCloudFunc func(config *cloudcontrollerconfig.CompletedConfig) cloudprovider.Interface
 | 
			
		||||
 | 
			
		||||
// InitFunc is used to launch a particular controller.  It may run additional "should I activate checks".
 | 
			
		||||
// Any error returned will cause the controller process to `Fatal`
 | 
			
		||||
// The bool indicates whether the controller was enabled.
 | 
			
		||||
type InitFunc func(ctx genericcontrollermanager.ControllerContext) (debuggingHandler http.Handler, enabled bool, err error)
 | 
			
		||||
 | 
			
		||||
// InitFuncConstructor is used to construct InitFunc
 | 
			
		||||
type InitFuncConstructor func(completedConfig *cloudcontrollerconfig.CompletedConfig, cloud cloudprovider.Interface) InitFunc
 | 
			
		||||
 | 
			
		||||
// ControllerNames indicate the default controller we are known.
 | 
			
		||||
@@ -316,6 +323,7 @@ func startRouteControllerWrapper(completedConfig *cloudcontrollerconfig.Complete
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DefaultInitFuncConstructors is a map of default named controller groups paired with InitFuncConstructor
 | 
			
		||||
var DefaultInitFuncConstructors = map[string]InitFuncConstructor{
 | 
			
		||||
	"cloud-node":           StartCloudNodeControllerWrapper,
 | 
			
		||||
	"cloud-node-lifecycle": startCloudNodeLifecycleControllerWrapper,
 | 
			
		||||
 
 | 
			
		||||
@@ -18,7 +18,7 @@ limitations under the License.
 | 
			
		||||
// 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
 | 
			
		||||
 | 
			
		||||
package sample
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"math/rand"
 | 
			
		||||
@@ -54,7 +54,23 @@ func main() {
 | 
			
		||||
		klog.Fatalf("unable to initialize command options: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	cloudInitializer := func(config *config.CompletedConfig) cloudprovider.Interface {
 | 
			
		||||
	command := app.NewCloudControllerManagerCommand(sampleCloudProviderName, ccmOptions, cloudInitializer, app.DefaultInitFuncConstructors)
 | 
			
		||||
 | 
			
		||||
	// 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
 | 
			
		||||
	// normalize func and add the go flag set by hand.
 | 
			
		||||
	// Here is an sample
 | 
			
		||||
	pflag.CommandLine.SetNormalizeFunc(flag.WordSepNormalizeFunc)
 | 
			
		||||
	// utilflag.InitFlags()
 | 
			
		||||
	logs.InitLogs()
 | 
			
		||||
	defer logs.FlushLogs()
 | 
			
		||||
 | 
			
		||||
	if err := command.Execute(); err != nil {
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func cloudInitializer(config *config.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(sampleCloudProviderName, cloudConfigFile)
 | 
			
		||||
@@ -81,20 +97,4 @@ func main() {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return cloud
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	command := app.NewCloudControllerManagerCommand(ccmOptions, cloudInitializer, app.DefaultInitFuncConstructors)
 | 
			
		||||
 | 
			
		||||
	// 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
 | 
			
		||||
	// normalize func and add the go flag set by hand.
 | 
			
		||||
	// Here is an sample
 | 
			
		||||
	pflag.CommandLine.SetNormalizeFunc(flag.WordSepNormalizeFunc)
 | 
			
		||||
	// utilflag.InitFlags()
 | 
			
		||||
	logs.InitLogs()
 | 
			
		||||
	defer logs.FlushLogs()
 | 
			
		||||
 | 
			
		||||
	if err := command.Execute(); err != nil {
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user