mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-15 06:01:50 +00:00
controller-manager: switch to config/option struct pattern
This commit is contained in:
committed by
Dr. Stefan Schimanski
parent
ba791275ce
commit
0cbe0a6034
55
cmd/cloud-controller-manager/app/config/config.go
Normal file
55
cmd/cloud-controller-manager/app/config/config.go
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
Copyright 2018 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.
|
||||
*/
|
||||
|
||||
package app
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
genericcontrollermanager "k8s.io/kubernetes/cmd/controller-manager/app"
|
||||
)
|
||||
|
||||
// ExtraConfig are part of Config, also can place your custom config here.
|
||||
type ExtraConfig struct {
|
||||
NodeStatusUpdateFrequency time.Duration
|
||||
}
|
||||
|
||||
// Config is the main context object for the cloud controller manager.
|
||||
type Config struct {
|
||||
Generic genericcontrollermanager.Config
|
||||
Extra ExtraConfig
|
||||
}
|
||||
|
||||
type completedConfig struct {
|
||||
Generic genericcontrollermanager.CompletedConfig
|
||||
Extra *ExtraConfig
|
||||
}
|
||||
|
||||
// CompletedConfig same as Config, just to swap private object.
|
||||
type CompletedConfig struct {
|
||||
// Embed a private pointer that cannot be instantiated outside of this package.
|
||||
*completedConfig
|
||||
}
|
||||
|
||||
// Complete fills in any fields not set that are required to have valid data. It's mutating the receiver.
|
||||
func (c *Config) Complete() *CompletedConfig {
|
||||
cc := completedConfig{
|
||||
c.Generic.Complete(),
|
||||
&c.Extra,
|
||||
}
|
||||
|
||||
return &CompletedConfig{&cc}
|
||||
}
|
@@ -20,32 +20,24 @@ import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/pprof"
|
||||
"os"
|
||||
goruntime "runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/util/uuid"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
"k8s.io/apiserver/pkg/server/healthz"
|
||||
"k8s.io/client-go/informers"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
|
||||
restclient "k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
"k8s.io/client-go/tools/leaderelection"
|
||||
"k8s.io/client-go/tools/leaderelection/resourcelock"
|
||||
"k8s.io/client-go/tools/record"
|
||||
cloudcontrollerconfig "k8s.io/kubernetes/cmd/cloud-controller-manager/app/config"
|
||||
"k8s.io/kubernetes/cmd/cloud-controller-manager/app/options"
|
||||
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
||||
genericcontrollermanager "k8s.io/kubernetes/cmd/controller-manager/app"
|
||||
"k8s.io/kubernetes/pkg/cloudprovider"
|
||||
"k8s.io/kubernetes/pkg/controller"
|
||||
cloudcontrollers "k8s.io/kubernetes/pkg/controller/cloud"
|
||||
@@ -62,7 +54,7 @@ const (
|
||||
|
||||
// NewCloudControllerManagerCommand creates a *cobra.Command object with default parameters
|
||||
func NewCloudControllerManagerCommand() *cobra.Command {
|
||||
s := options.NewCloudControllerManagerServer()
|
||||
s := options.NewCloudControllerManagerOptions()
|
||||
cmd := &cobra.Command{
|
||||
Use: "cloud-controller-manager",
|
||||
Long: `The Cloud controller manager is a daemon that embeds
|
||||
@@ -70,7 +62,13 @@ the cloud specific control loops shipped with Kubernetes.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
verflag.PrintAndExitIfRequested()
|
||||
|
||||
if err := Run(s); err != nil {
|
||||
c, err := s.Config()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := Run(c.Complete()); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
@@ -83,84 +81,68 @@ the cloud specific control loops shipped with Kubernetes.`,
|
||||
}
|
||||
|
||||
// resyncPeriod computes the time interval a shared informer waits before resyncing with the api server
|
||||
func resyncPeriod(s *options.CloudControllerManagerServer) func() time.Duration {
|
||||
func resyncPeriod(c *cloudcontrollerconfig.CompletedConfig) func() time.Duration {
|
||||
return func() time.Duration {
|
||||
factor := rand.Float64() + 1
|
||||
return time.Duration(float64(s.MinResyncPeriod.Nanoseconds()) * factor)
|
||||
return time.Duration(float64(c.Generic.ComponentConfig.MinResyncPeriod.Nanoseconds()) * factor)
|
||||
}
|
||||
}
|
||||
|
||||
// Run runs the ExternalCMServer. This should never exit.
|
||||
func Run(s *options.CloudControllerManagerServer) error {
|
||||
if s.CloudProvider == "" {
|
||||
glog.Fatalf("--cloud-provider cannot be empty")
|
||||
}
|
||||
|
||||
cloud, err := cloudprovider.InitCloudProvider(s.CloudProvider, s.CloudConfigFile)
|
||||
func Run(c *cloudcontrollerconfig.CompletedConfig) error {
|
||||
cloud, err := cloudprovider.InitCloudProvider(c.Generic.ComponentConfig.CloudProvider, c.Generic.ComponentConfig.CloudConfigFile)
|
||||
if err != nil {
|
||||
glog.Fatalf("Cloud provider could not be initialized: %v", err)
|
||||
}
|
||||
|
||||
if cloud == nil {
|
||||
glog.Fatalf("cloud provider is nil")
|
||||
}
|
||||
|
||||
if cloud.HasClusterID() == false {
|
||||
if s.AllowUntaggedCloud == true {
|
||||
if c.Generic.ComponentConfig.AllowUntaggedCloud == true {
|
||||
glog.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 {
|
||||
glog.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")
|
||||
}
|
||||
}
|
||||
|
||||
if c, err := configz.New("componentconfig"); err == nil {
|
||||
c.Set(s.KubeControllerManagerConfiguration)
|
||||
// setup /configz endpoint
|
||||
if cz, err := configz.New("componentconfig"); err == nil {
|
||||
cz.Set(c.Generic.ComponentConfig)
|
||||
} else {
|
||||
glog.Errorf("unable to register configz: %s", err)
|
||||
}
|
||||
kubeconfig, err := clientcmd.BuildConfigFromFlags(s.Master, s.Kubeconfig)
|
||||
if err != nil {
|
||||
return err
|
||||
glog.Errorf("unable to register configz: %c", err)
|
||||
}
|
||||
|
||||
// Set the ContentType of the requests from kube client
|
||||
kubeconfig.ContentConfig.ContentType = s.ContentType
|
||||
// Override kubeconfig qps/burst settings from flags
|
||||
kubeconfig.QPS = s.KubeAPIQPS
|
||||
kubeconfig.Burst = int(s.KubeAPIBurst)
|
||||
kubeClient, err := kubernetes.NewForConfig(restclient.AddUserAgent(kubeconfig, "cloud-controller-manager"))
|
||||
if err != nil {
|
||||
glog.Fatalf("Invalid API configuration: %v", err)
|
||||
// Start the controller manager HTTP server
|
||||
stopCh := make(chan struct{})
|
||||
if c.Generic.InsecureServing != nil {
|
||||
if err := genericcontrollermanager.Serve(&c.Generic, c.Generic.InsecureServing.Serve, stopCh); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
leaderElectionClient := kubernetes.NewForConfigOrDie(restclient.AddUserAgent(kubeconfig, "leader-election"))
|
||||
|
||||
// Start the external controller manager server
|
||||
go startHTTP(s)
|
||||
|
||||
recorder := createRecorder(kubeClient)
|
||||
|
||||
run := func(stop <-chan struct{}) {
|
||||
rootClientBuilder := controller.SimpleControllerClientBuilder{
|
||||
ClientConfig: kubeconfig,
|
||||
ClientConfig: c.Generic.Kubeconfig,
|
||||
}
|
||||
var clientBuilder controller.ControllerClientBuilder
|
||||
if s.UseServiceAccountCredentials {
|
||||
if c.Generic.ComponentConfig.UseServiceAccountCredentials {
|
||||
clientBuilder = controller.SAControllerClientBuilder{
|
||||
ClientConfig: restclient.AnonymousClientConfig(kubeconfig),
|
||||
CoreClient: kubeClient.CoreV1(),
|
||||
AuthenticationClient: kubeClient.AuthenticationV1(),
|
||||
ClientConfig: restclient.AnonymousClientConfig(c.Generic.Kubeconfig),
|
||||
CoreClient: c.Generic.Client.CoreV1(),
|
||||
AuthenticationClient: c.Generic.Client.AuthenticationV1(),
|
||||
Namespace: "kube-system",
|
||||
}
|
||||
} else {
|
||||
clientBuilder = rootClientBuilder
|
||||
}
|
||||
|
||||
if err := StartControllers(s, kubeconfig, rootClientBuilder, clientBuilder, stop, recorder, cloud); err != nil {
|
||||
if err := startControllers(c, c.Generic.Kubeconfig, rootClientBuilder, clientBuilder, stop, c.Generic.EventRecorder, cloud); err != nil {
|
||||
glog.Fatalf("error running controllers: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if !s.LeaderElection.LeaderElect {
|
||||
if !c.Generic.ComponentConfig.LeaderElection.LeaderElect {
|
||||
run(nil)
|
||||
panic("unreachable")
|
||||
}
|
||||
@@ -174,13 +156,13 @@ func Run(s *options.CloudControllerManagerServer) error {
|
||||
id = id + "_" + string(uuid.NewUUID())
|
||||
|
||||
// Lock required for leader election
|
||||
rl, err := resourcelock.New(s.LeaderElection.ResourceLock,
|
||||
rl, err := resourcelock.New(c.Generic.ComponentConfig.LeaderElection.ResourceLock,
|
||||
"kube-system",
|
||||
"cloud-controller-manager",
|
||||
leaderElectionClient.CoreV1(),
|
||||
c.Generic.LeaderElectionClient.CoreV1(),
|
||||
resourcelock.ResourceLockConfig{
|
||||
Identity: id,
|
||||
EventRecorder: recorder,
|
||||
EventRecorder: c.Generic.EventRecorder,
|
||||
})
|
||||
if err != nil {
|
||||
glog.Fatalf("error creating lock: %v", err)
|
||||
@@ -189,9 +171,9 @@ func Run(s *options.CloudControllerManagerServer) error {
|
||||
// Try and become the leader and start cloud controller manager loops
|
||||
leaderelection.RunOrDie(leaderelection.LeaderElectionConfig{
|
||||
Lock: rl,
|
||||
LeaseDuration: s.LeaderElection.LeaseDuration.Duration,
|
||||
RenewDeadline: s.LeaderElection.RenewDeadline.Duration,
|
||||
RetryPeriod: s.LeaderElection.RetryPeriod.Duration,
|
||||
LeaseDuration: c.Generic.ComponentConfig.LeaderElection.LeaseDuration.Duration,
|
||||
RenewDeadline: c.Generic.ComponentConfig.LeaderElection.RenewDeadline.Duration,
|
||||
RetryPeriod: c.Generic.ComponentConfig.LeaderElection.RetryPeriod.Duration,
|
||||
Callbacks: leaderelection.LeaderCallbacks{
|
||||
OnStartedLeading: run,
|
||||
OnStoppedLeading: func() {
|
||||
@@ -202,36 +184,36 @@ func Run(s *options.CloudControllerManagerServer) error {
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
// StartControllers starts the cloud specific controller loops.
|
||||
func StartControllers(s *options.CloudControllerManagerServer, kubeconfig *restclient.Config, rootClientBuilder, clientBuilder controller.ControllerClientBuilder, stop <-chan struct{}, recorder record.EventRecorder, cloud cloudprovider.Interface) error {
|
||||
// startControllers starts the cloud specific controller loops.
|
||||
func startControllers(c *cloudcontrollerconfig.CompletedConfig, kubeconfig *restclient.Config, rootClientBuilder, clientBuilder controller.ControllerClientBuilder, stop <-chan struct{}, recorder record.EventRecorder, cloud cloudprovider.Interface) error {
|
||||
// Function to build the kube client object
|
||||
client := func(serviceAccountName string) kubernetes.Interface {
|
||||
return clientBuilder.ClientOrDie(serviceAccountName)
|
||||
}
|
||||
|
||||
if cloud != nil {
|
||||
// Initialize the cloud provider with a reference to the clientBuilder
|
||||
cloud.Initialize(clientBuilder)
|
||||
}
|
||||
|
||||
// TODO: move this setup into Config
|
||||
versionedClient := rootClientBuilder.ClientOrDie("shared-informers")
|
||||
sharedInformers := informers.NewSharedInformerFactory(versionedClient, resyncPeriod(s)())
|
||||
sharedInformers := informers.NewSharedInformerFactory(versionedClient, resyncPeriod(c)())
|
||||
|
||||
// Start the CloudNodeController
|
||||
nodeController := cloudcontrollers.NewCloudNodeController(
|
||||
sharedInformers.Core().V1().Nodes(),
|
||||
client("cloud-node-controller"), cloud,
|
||||
s.NodeMonitorPeriod.Duration,
|
||||
s.NodeStatusUpdateFrequency.Duration)
|
||||
c.Generic.ComponentConfig.NodeMonitorPeriod.Duration,
|
||||
c.Extra.NodeStatusUpdateFrequency)
|
||||
|
||||
nodeController.Run()
|
||||
time.Sleep(wait.Jitter(s.ControllerStartInterval.Duration, ControllerStartJitter))
|
||||
time.Sleep(wait.Jitter(c.Generic.ComponentConfig.ControllerStartInterval.Duration, ControllerStartJitter))
|
||||
|
||||
// Start the PersistentVolumeLabelController
|
||||
pvlController := cloudcontrollers.NewPersistentVolumeLabelController(client("pvl-controller"), cloud)
|
||||
threads := 5
|
||||
go pvlController.Run(threads, stop)
|
||||
time.Sleep(wait.Jitter(s.ControllerStartInterval.Duration, ControllerStartJitter))
|
||||
time.Sleep(wait.Jitter(c.Generic.ComponentConfig.ControllerStartInterval.Duration, ControllerStartJitter))
|
||||
|
||||
// Start the service controller
|
||||
serviceController, err := servicecontroller.New(
|
||||
@@ -239,34 +221,34 @@ func StartControllers(s *options.CloudControllerManagerServer, kubeconfig *restc
|
||||
client("service-controller"),
|
||||
sharedInformers.Core().V1().Services(),
|
||||
sharedInformers.Core().V1().Nodes(),
|
||||
s.ClusterName,
|
||||
c.Generic.ComponentConfig.ClusterName,
|
||||
)
|
||||
if err != nil {
|
||||
glog.Errorf("Failed to start service controller: %v", err)
|
||||
} else {
|
||||
go serviceController.Run(stop, int(s.ConcurrentServiceSyncs))
|
||||
time.Sleep(wait.Jitter(s.ControllerStartInterval.Duration, ControllerStartJitter))
|
||||
go serviceController.Run(stop, int(c.Generic.ComponentConfig.ConcurrentServiceSyncs))
|
||||
time.Sleep(wait.Jitter(c.Generic.ComponentConfig.ControllerStartInterval.Duration, ControllerStartJitter))
|
||||
}
|
||||
|
||||
// If CIDRs should be allocated for pods and set on the CloudProvider, then start the route controller
|
||||
if s.AllocateNodeCIDRs && s.ConfigureCloudRoutes {
|
||||
if c.Generic.ComponentConfig.AllocateNodeCIDRs && c.Generic.ComponentConfig.ConfigureCloudRoutes {
|
||||
if routes, ok := cloud.Routes(); !ok {
|
||||
glog.Warning("configure-cloud-routes is set, but cloud provider does not support routes. Will not configure cloud provider routes.")
|
||||
} else {
|
||||
var clusterCIDR *net.IPNet
|
||||
if len(strings.TrimSpace(s.ClusterCIDR)) != 0 {
|
||||
_, clusterCIDR, err = net.ParseCIDR(s.ClusterCIDR)
|
||||
if len(strings.TrimSpace(c.Generic.ComponentConfig.ClusterCIDR)) != 0 {
|
||||
_, clusterCIDR, err = net.ParseCIDR(c.Generic.ComponentConfig.ClusterCIDR)
|
||||
if err != nil {
|
||||
glog.Warningf("Unsuccessful parsing of cluster CIDR %v: %v", s.ClusterCIDR, err)
|
||||
glog.Warningf("Unsuccessful parsing of cluster CIDR %v: %v", c.Generic.ComponentConfig.ClusterCIDR, err)
|
||||
}
|
||||
}
|
||||
|
||||
routeController := routecontroller.New(routes, client("route-controller"), sharedInformers.Core().V1().Nodes(), s.ClusterName, clusterCIDR)
|
||||
go routeController.Run(stop, s.RouteReconciliationPeriod.Duration)
|
||||
time.Sleep(wait.Jitter(s.ControllerStartInterval.Duration, ControllerStartJitter))
|
||||
routeController := routecontroller.New(routes, client("route-controller"), sharedInformers.Core().V1().Nodes(), c.Generic.ComponentConfig.ClusterName, clusterCIDR)
|
||||
go routeController.Run(stop, c.Generic.ComponentConfig.RouteReconciliationPeriod.Duration)
|
||||
time.Sleep(wait.Jitter(c.Generic.ComponentConfig.ControllerStartInterval.Duration, ControllerStartJitter))
|
||||
}
|
||||
} else {
|
||||
glog.Infof("Will not configure cloud provider routes for allocate-node-cidrs: %v, configure-cloud-routes: %v.", s.AllocateNodeCIDRs, s.ConfigureCloudRoutes)
|
||||
glog.Infof("Will not configure cloud provider routes for allocate-node-cidrs: %v, configure-cloud-routes: %v.", c.Generic.ComponentConfig.AllocateNodeCIDRs, c.Generic.ComponentConfig.ConfigureCloudRoutes)
|
||||
}
|
||||
|
||||
// If apiserver is not running we should wait for some time and fail only then. This is particularly
|
||||
@@ -286,32 +268,3 @@ func StartControllers(s *options.CloudControllerManagerServer, kubeconfig *restc
|
||||
|
||||
select {}
|
||||
}
|
||||
|
||||
func startHTTP(s *options.CloudControllerManagerServer) {
|
||||
mux := http.NewServeMux()
|
||||
healthz.InstallHandler(mux)
|
||||
if s.EnableProfiling {
|
||||
mux.HandleFunc("/debug/pprof/", pprof.Index)
|
||||
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
|
||||
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
|
||||
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
|
||||
if s.EnableContentionProfiling {
|
||||
goruntime.SetBlockProfileRate(1)
|
||||
}
|
||||
}
|
||||
configz.InstallHandler(mux)
|
||||
mux.Handle("/metrics", prometheus.Handler())
|
||||
|
||||
server := &http.Server{
|
||||
Addr: net.JoinHostPort(s.Address, strconv.Itoa(int(s.Port))),
|
||||
Handler: mux,
|
||||
}
|
||||
glog.Fatal(server.ListenAndServe())
|
||||
}
|
||||
|
||||
func createRecorder(kubeClient *kubernetes.Clientset) record.EventRecorder {
|
||||
eventBroadcaster := record.NewBroadcaster()
|
||||
eventBroadcaster.StartLogging(glog.Infof)
|
||||
eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: v1core.New(kubeClient.CoreV1().RESTClient()).Events("")})
|
||||
return eventBroadcaster.NewRecorder(legacyscheme.Scheme, v1.EventSource{Component: "cloud-controller-manager"})
|
||||
}
|
||||
|
@@ -17,10 +17,13 @@ limitations under the License.
|
||||
package options
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||
cloudcontrollerconfig "k8s.io/kubernetes/cmd/cloud-controller-manager/app/config"
|
||||
cmoptions "k8s.io/kubernetes/cmd/controller-manager/app/options"
|
||||
"k8s.io/kubernetes/pkg/client/leaderelectionconfig"
|
||||
"k8s.io/kubernetes/pkg/master/ports"
|
||||
@@ -31,39 +34,78 @@ import (
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
// CloudControllerManagerServer is the main context object for the controller manager.
|
||||
type CloudControllerManagerServer struct {
|
||||
cmoptions.ControllerManagerServer
|
||||
// CloudControllerManagerOptions is the main context object for the controller manager.
|
||||
type CloudControllerManagerOptions struct {
|
||||
Generic cmoptions.GenericControllerManagerOptions
|
||||
|
||||
// NodeStatusUpdateFrequency is the frequency at which the controller updates nodes' status
|
||||
NodeStatusUpdateFrequency metav1.Duration
|
||||
}
|
||||
|
||||
// NewCloudControllerManagerServer creates a new ExternalCMServer with a default config.
|
||||
func NewCloudControllerManagerServer() *CloudControllerManagerServer {
|
||||
s := CloudControllerManagerServer{
|
||||
// NewCloudControllerManagerOptions creates a new ExternalCMServer with a default config.
|
||||
func NewCloudControllerManagerOptions() *CloudControllerManagerOptions {
|
||||
componentConfig := cmoptions.NewDefaultControllerManagerComponentConfig(ports.InsecureCloudControllerManagerPort)
|
||||
|
||||
s := CloudControllerManagerOptions{
|
||||
// The common/default are kept in 'cmd/kube-controller-manager/app/options/util.go'.
|
||||
// Please make common changes there and put anything cloud specific here.
|
||||
ControllerManagerServer: cmoptions.ControllerManagerServer{
|
||||
KubeControllerManagerConfiguration: cmoptions.GetDefaultControllerOptions(ports.CloudControllerManagerPort),
|
||||
},
|
||||
Generic: cmoptions.NewGenericControllerManagerOptions(componentConfig),
|
||||
NodeStatusUpdateFrequency: metav1.Duration{Duration: 5 * time.Minute},
|
||||
}
|
||||
s.LeaderElection.LeaderElect = true
|
||||
s.Generic.ComponentConfig.LeaderElection.LeaderElect = true
|
||||
|
||||
return &s
|
||||
}
|
||||
|
||||
// AddFlags adds flags for a specific ExternalCMServer to the specified FlagSet
|
||||
func (s *CloudControllerManagerServer) AddFlags(fs *pflag.FlagSet) {
|
||||
cmoptions.AddDefaultControllerFlags(&s.ControllerManagerServer, fs)
|
||||
fs.StringVar(&s.CloudProvider, "cloud-provider", s.CloudProvider, "The provider of cloud services. Cannot be empty.")
|
||||
fs.DurationVar(&s.NodeStatusUpdateFrequency.Duration, "node-status-update-frequency", s.NodeStatusUpdateFrequency.Duration, "Specifies how often the controller updates nodes' status.")
|
||||
// TODO: remove --service-account-private-key-file 6 months after 1.8 is released (~1.10)
|
||||
fs.StringVar(&s.ServiceAccountKeyFile, "service-account-private-key-file", s.ServiceAccountKeyFile, "Filename containing a PEM-encoded private RSA or ECDSA key used to sign service account tokens.")
|
||||
fs.MarkDeprecated("service-account-private-key-file", "This flag is currently no-op and will be deleted.")
|
||||
fs.Int32Var(&s.ConcurrentServiceSyncs, "concurrent-service-syncs", s.ConcurrentServiceSyncs, "The number of services that are allowed to sync concurrently. Larger number = more responsive service management, but more CPU (and network) load")
|
||||
func (o *CloudControllerManagerOptions) AddFlags(fs *pflag.FlagSet) {
|
||||
o.Generic.AddFlags(fs)
|
||||
|
||||
leaderelectionconfig.BindFlags(&s.LeaderElection, fs)
|
||||
fs.StringVar(&o.Generic.ComponentConfig.CloudProvider, "cloud-provider", o.Generic.ComponentConfig.CloudProvider, "The provider of cloud services. Cannot be empty.")
|
||||
fs.DurationVar(&o.NodeStatusUpdateFrequency.Duration, "node-status-update-frequency", o.NodeStatusUpdateFrequency.Duration, "Specifies how often the controller updates nodes' status.")
|
||||
// TODO: remove --service-account-private-key-file 6 months after 1.8 is released (~1.10)
|
||||
fs.StringVar(&o.Generic.ComponentConfig.ServiceAccountKeyFile, "service-account-private-key-file", o.Generic.ComponentConfig.ServiceAccountKeyFile, "Filename containing a PEM-encoded private RSA or ECDSA key used to sign service account tokens.")
|
||||
fs.MarkDeprecated("service-account-private-key-file", "This flag is currently no-op and will be deleted.")
|
||||
fs.Int32Var(&o.Generic.ComponentConfig.ConcurrentServiceSyncs, "concurrent-service-syncs", o.Generic.ComponentConfig.ConcurrentServiceSyncs, "The number of services that are allowed to sync concurrently. Larger number = more responsive service management, but more CPU (and network) load")
|
||||
|
||||
leaderelectionconfig.BindFlags(&o.Generic.ComponentConfig.LeaderElection, fs)
|
||||
|
||||
utilfeature.DefaultFeatureGate.AddFlag(fs)
|
||||
}
|
||||
|
||||
// ApplyTo fills up cloud controller manager config with options.
|
||||
func (o *CloudControllerManagerOptions) ApplyTo(c *cloudcontrollerconfig.Config) error {
|
||||
if err := o.Generic.ApplyTo(&c.Generic, "cloud-controller-manager"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.Extra.NodeStatusUpdateFrequency = o.NodeStatusUpdateFrequency.Duration
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate is used to validate config before launching the cloud controller manager
|
||||
func (o *CloudControllerManagerOptions) Validate() error {
|
||||
errors := []error{}
|
||||
errors = append(errors, o.Generic.Validate()...)
|
||||
|
||||
if len(o.Generic.ComponentConfig.CloudProvider) == 0 {
|
||||
errors = append(errors, fmt.Errorf("--cloud-provider cannot be empty"))
|
||||
}
|
||||
|
||||
return utilerrors.NewAggregate(errors)
|
||||
}
|
||||
|
||||
// Config return a cloud controller manager config objective
|
||||
func (o CloudControllerManagerOptions) Config() (*cloudcontrollerconfig.Config, error) {
|
||||
if err := o.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c := &cloudcontrollerconfig.Config{}
|
||||
if err := o.ApplyTo(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package options
|
||||
|
||||
import (
|
||||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -31,7 +32,7 @@ import (
|
||||
|
||||
func TestAddFlags(t *testing.T) {
|
||||
f := pflag.NewFlagSet("addflagstest", pflag.ContinueOnError)
|
||||
s := NewCloudControllerManagerServer()
|
||||
s := NewCloudControllerManagerOptions()
|
||||
s.AddFlags(f)
|
||||
|
||||
args := []string{
|
||||
@@ -65,13 +66,13 @@ func TestAddFlags(t *testing.T) {
|
||||
}
|
||||
f.Parse(args)
|
||||
|
||||
expected := &CloudControllerManagerServer{
|
||||
ControllerManagerServer: cmoptions.ControllerManagerServer{
|
||||
KubeControllerManagerConfiguration: componentconfig.KubeControllerManagerConfiguration{
|
||||
expected := &CloudControllerManagerOptions{
|
||||
Generic: cmoptions.GenericControllerManagerOptions{
|
||||
ComponentConfig: componentconfig.KubeControllerManagerConfiguration{
|
||||
CloudProvider: "gce",
|
||||
CloudConfigFile: "/cloud-config",
|
||||
Port: 10000,
|
||||
Address: "192.168.4.10",
|
||||
Port: 10253, // Note: InsecureServingOptions.ApplyTo will write the flag value back into the component config
|
||||
Address: "0.0.0.0", // Note: InsecureServingOptions.ApplyTo will write the flag value back into the component config
|
||||
ConcurrentEndpointSyncs: 5,
|
||||
ConcurrentRSSyncs: 5,
|
||||
ConcurrentResourceQuotaSyncs: 5,
|
||||
@@ -138,6 +139,11 @@ func TestAddFlags(t *testing.T) {
|
||||
CIDRAllocatorType: "RangeAllocator",
|
||||
Controllers: []string{"*"},
|
||||
},
|
||||
InsecureServing: &cmoptions.InsecureServingOptions{
|
||||
BindAddress: net.ParseIP("192.168.4.10"),
|
||||
BindPort: int(10000),
|
||||
BindNetwork: "tcp",
|
||||
},
|
||||
Kubeconfig: "/kubeconfig",
|
||||
Master: "192.168.4.20",
|
||||
},
|
||||
|
Reference in New Issue
Block a user