2015-01-30 23:31:36 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2015-01-30 23:31:36 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2015-02-08 04:07:00 +00:00
|
|
|
// Package app implements a server that runs a set of active
|
2015-01-30 23:31:36 +00:00
|
|
|
// components. This includes replication controllers, service endpoints and
|
|
|
|
// nodes.
|
2015-06-11 13:13:19 +00:00
|
|
|
//
|
2015-02-08 04:07:00 +00:00
|
|
|
package app
|
2015-01-30 23:31:36 +00:00
|
|
|
|
|
|
|
import (
|
2015-06-23 22:43:59 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2015-10-06 09:12:00 +00:00
|
|
|
"math/rand"
|
2015-01-30 23:31:36 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2015-03-13 15:44:11 +00:00
|
|
|
"net/http/pprof"
|
2015-11-04 23:22:11 +00:00
|
|
|
"os"
|
2017-02-28 18:43:08 +00:00
|
|
|
goruntime "runtime"
|
2015-01-30 23:31:36 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2017-01-17 03:38:19 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2017-01-10 20:11:25 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
2017-03-08 07:03:57 +00:00
|
|
|
|
2017-01-17 10:38:25 +00:00
|
|
|
"k8s.io/apiserver/pkg/server/healthz"
|
2017-03-08 07:03:57 +00:00
|
|
|
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
|
|
|
|
2017-01-25 19:00:30 +00:00
|
|
|
"k8s.io/client-go/discovery"
|
2017-01-30 18:39:54 +00:00
|
|
|
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
|
|
|
|
clientv1 "k8s.io/client-go/pkg/api/v1"
|
2017-01-19 18:27:59 +00:00
|
|
|
restclient "k8s.io/client-go/rest"
|
2017-01-20 18:06:17 +00:00
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
2017-01-30 18:39:54 +00:00
|
|
|
"k8s.io/client-go/tools/record"
|
2017-01-23 18:37:22 +00:00
|
|
|
certutil "k8s.io/client-go/util/cert"
|
2017-03-08 07:03:57 +00:00
|
|
|
|
2015-12-24 23:59:05 +00:00
|
|
|
"k8s.io/kubernetes/cmd/kube-controller-manager/app/options"
|
2017-01-30 18:39:54 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2017-01-10 08:49:34 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
|
2017-02-24 14:52:43 +00:00
|
|
|
informers "k8s.io/kubernetes/pkg/client/informers/informers_generated/externalversions"
|
2015-11-04 23:22:11 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/leaderelection"
|
2016-09-15 19:17:18 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/leaderelection/resourcelock"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/cloudprovider"
|
2015-11-11 21:19:39 +00:00
|
|
|
"k8s.io/kubernetes/pkg/controller"
|
2015-10-10 03:58:57 +00:00
|
|
|
nodecontroller "k8s.io/kubernetes/pkg/controller/node"
|
|
|
|
routecontroller "k8s.io/kubernetes/pkg/controller/route"
|
|
|
|
servicecontroller "k8s.io/kubernetes/pkg/controller/service"
|
2015-12-24 21:54:40 +00:00
|
|
|
serviceaccountcontroller "k8s.io/kubernetes/pkg/controller/serviceaccount"
|
2016-07-02 01:50:25 +00:00
|
|
|
"k8s.io/kubernetes/pkg/controller/volume/attachdetach"
|
|
|
|
persistentvolumecontroller "k8s.io/kubernetes/pkg/controller/volume/persistentvolume"
|
2017-03-08 07:03:57 +00:00
|
|
|
"k8s.io/kubernetes/pkg/features"
|
2015-12-24 21:54:40 +00:00
|
|
|
"k8s.io/kubernetes/pkg/serviceaccount"
|
2016-02-02 02:09:02 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/configz"
|
2015-04-18 13:31:24 +00:00
|
|
|
|
2015-01-30 23:31:36 +00:00
|
|
|
"github.com/golang/glog"
|
2015-04-22 14:46:03 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-10-12 14:33:39 +00:00
|
|
|
"github.com/spf13/cobra"
|
2015-01-30 23:31:36 +00:00
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
2016-03-30 14:07:30 +00:00
|
|
|
const (
|
|
|
|
// Jitter used when starting controller managers
|
|
|
|
ControllerStartJitter = 1.0
|
|
|
|
)
|
|
|
|
|
2015-10-12 14:33:39 +00:00
|
|
|
// NewControllerManagerCommand creates a *cobra.Command object with default parameters
|
|
|
|
func NewControllerManagerCommand() *cobra.Command {
|
2015-12-24 23:59:05 +00:00
|
|
|
s := options.NewCMServer()
|
2017-01-10 20:11:25 +00:00
|
|
|
s.AddFlags(pflag.CommandLine, KnownControllers(), ControllersDisabledByDefault.List())
|
2015-10-12 14:33:39 +00:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "kube-controller-manager",
|
|
|
|
Long: `The Kubernetes controller manager is a daemon that embeds
|
|
|
|
the core control loops shipped with Kubernetes. In applications of robotics and
|
|
|
|
automation, a control loop is a non-terminating loop that regulates the state of
|
|
|
|
the system. In Kubernetes, a controller is a control loop that watches the shared
|
|
|
|
state of the cluster through the apiserver and makes changes attempting to move the
|
|
|
|
current state towards the desired state. Examples of controllers that ship with
|
|
|
|
Kubernetes today are the replication controller, endpoints controller, namespace
|
|
|
|
controller, and serviceaccounts controller.`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-02-09 02:02:55 +00:00
|
|
|
// ResyncPeriod returns a function which generates a duration each time it is
|
|
|
|
// invoked; this is so that multiple controllers don't get into lock-step and all
|
|
|
|
// hammer the apiserver with list requests simultaneously.
|
2015-12-24 23:59:05 +00:00
|
|
|
func ResyncPeriod(s *options.CMServer) func() time.Duration {
|
|
|
|
return func() time.Duration {
|
|
|
|
factor := rand.Float64() + 1
|
|
|
|
return time.Duration(float64(s.MinResyncPeriod.Nanoseconds()) * factor)
|
|
|
|
}
|
2015-10-06 09:12:00 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 23:31:36 +00:00
|
|
|
// Run runs the CMServer. This should never exit.
|
2015-12-24 23:59:05 +00:00
|
|
|
func Run(s *options.CMServer) error {
|
2017-01-10 20:11:25 +00:00
|
|
|
if err := s.Validate(KnownControllers(), ControllersDisabledByDefault.List()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-02 02:09:02 +00:00
|
|
|
if c, err := configz.New("componentconfig"); err == nil {
|
|
|
|
c.Set(s.KubeControllerManagerConfiguration)
|
|
|
|
} else {
|
|
|
|
glog.Errorf("unable to register configz: %s", err)
|
|
|
|
}
|
2015-11-06 18:34:49 +00:00
|
|
|
kubeconfig, err := clientcmd.BuildConfigFromFlags(s.Master, s.Kubeconfig)
|
2015-04-17 07:18:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-19 07:35:32 +00:00
|
|
|
kubeconfig.ContentConfig.ContentType = s.ContentType
|
2015-10-12 15:56:15 +00:00
|
|
|
// Override kubeconfig qps/burst settings from flags
|
2015-10-20 12:33:48 +00:00
|
|
|
kubeconfig.QPS = s.KubeAPIQPS
|
2016-04-27 04:35:14 +00:00
|
|
|
kubeconfig.Burst = int(s.KubeAPIBurst)
|
2016-09-20 13:43:11 +00:00
|
|
|
kubeClient, err := clientset.NewForConfig(restclient.AddUserAgent(kubeconfig, "controller-manager"))
|
2015-01-30 23:31:36 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("Invalid API configuration: %v", err)
|
|
|
|
}
|
2016-09-26 07:46:59 +00:00
|
|
|
leaderElectionClient := clientset.NewForConfigOrDie(restclient.AddUserAgent(kubeconfig, "leader-election"))
|
2015-01-30 23:31:36 +00:00
|
|
|
|
2015-03-13 15:44:11 +00:00
|
|
|
go func() {
|
2015-04-22 14:46:03 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
healthz.InstallHandler(mux)
|
2015-03-13 15:44:11 +00:00
|
|
|
if s.EnableProfiling {
|
|
|
|
mux.HandleFunc("/debug/pprof/", pprof.Index)
|
|
|
|
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
|
|
|
|
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
|
2017-02-28 18:43:08 +00:00
|
|
|
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
|
|
|
|
if s.EnableContentionProfiling {
|
|
|
|
goruntime.SetBlockProfileRate(1)
|
|
|
|
}
|
2015-03-13 15:44:11 +00:00
|
|
|
}
|
2016-07-21 11:27:09 +00:00
|
|
|
configz.InstallHandler(mux)
|
2015-04-22 14:46:03 +00:00
|
|
|
mux.Handle("/metrics", prometheus.Handler())
|
|
|
|
|
|
|
|
server := &http.Server{
|
2016-04-27 04:35:14 +00:00
|
|
|
Addr: net.JoinHostPort(s.Address, strconv.Itoa(int(s.Port))),
|
2015-04-22 14:46:03 +00:00
|
|
|
Handler: mux,
|
|
|
|
}
|
|
|
|
glog.Fatal(server.ListenAndServe())
|
2015-03-13 15:44:11 +00:00
|
|
|
}()
|
2015-01-30 23:31:36 +00:00
|
|
|
|
2016-06-21 16:13:23 +00:00
|
|
|
eventBroadcaster := record.NewBroadcaster()
|
|
|
|
eventBroadcaster.StartLogging(glog.Infof)
|
2017-01-30 18:39:54 +00:00
|
|
|
eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: v1core.New(kubeClient.Core().RESTClient()).Events("")})
|
|
|
|
recorder := eventBroadcaster.NewRecorder(api.Scheme, clientv1.EventSource{Component: "controller-manager"})
|
2016-06-21 16:13:23 +00:00
|
|
|
|
2015-11-04 23:22:11 +00:00
|
|
|
run := func(stop <-chan struct{}) {
|
2016-09-22 19:53:08 +00:00
|
|
|
rootClientBuilder := controller.SimpleControllerClientBuilder{
|
|
|
|
ClientConfig: kubeconfig,
|
|
|
|
}
|
|
|
|
var clientBuilder controller.ControllerClientBuilder
|
2016-11-01 13:27:35 +00:00
|
|
|
if len(s.ServiceAccountKeyFile) > 0 && s.UseServiceAccountCredentials {
|
2016-09-22 19:53:08 +00:00
|
|
|
clientBuilder = controller.SAControllerClientBuilder{
|
2017-02-17 20:48:22 +00:00
|
|
|
ClientConfig: restclient.AnonymousClientConfig(kubeconfig),
|
|
|
|
CoreClient: kubeClient.Core(),
|
|
|
|
AuthenticationClient: kubeClient.Authentication(),
|
|
|
|
Namespace: "kube-system",
|
2016-09-22 19:53:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
clientBuilder = rootClientBuilder
|
|
|
|
}
|
|
|
|
|
2017-05-08 18:16:05 +00:00
|
|
|
err := StartControllers(NewControllerInitializers(), s, rootClientBuilder, clientBuilder, stop)
|
2015-11-04 23:22:11 +00:00
|
|
|
glog.Fatalf("error running controllers: %v", err)
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.LeaderElection.LeaderElect {
|
|
|
|
run(nil)
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-15 19:17:18 +00:00
|
|
|
// TODO: enable other lock types
|
|
|
|
rl := resourcelock.EndpointsLock{
|
2017-01-17 03:38:19 +00:00
|
|
|
EndpointsMeta: metav1.ObjectMeta{
|
2015-11-04 23:22:11 +00:00
|
|
|
Namespace: "kube-system",
|
|
|
|
Name: "kube-controller-manager",
|
|
|
|
},
|
2016-09-15 19:17:18 +00:00
|
|
|
Client: leaderElectionClient,
|
|
|
|
LockConfig: resourcelock.ResourceLockConfig{
|
|
|
|
Identity: id,
|
|
|
|
EventRecorder: recorder,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
leaderelection.RunOrDie(leaderelection.LeaderElectionConfig{
|
|
|
|
Lock: &rl,
|
2016-09-26 07:46:59 +00:00
|
|
|
LeaseDuration: s.LeaderElection.LeaseDuration.Duration,
|
|
|
|
RenewDeadline: s.LeaderElection.RenewDeadline.Duration,
|
|
|
|
RetryPeriod: s.LeaderElection.RetryPeriod.Duration,
|
2015-11-04 23:22:11 +00:00
|
|
|
Callbacks: leaderelection.LeaderCallbacks{
|
|
|
|
OnStartedLeading: run,
|
|
|
|
OnStoppedLeading: func() {
|
|
|
|
glog.Fatalf("leaderelection lost")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
2016-12-02 19:18:16 +00:00
|
|
|
type ControllerContext struct {
|
|
|
|
// ClientBuilder will provide a client for this controller to use
|
|
|
|
ClientBuilder controller.ControllerClientBuilder
|
|
|
|
|
2017-02-06 18:35:50 +00:00
|
|
|
// InformerFactory gives access to informers for the controller.
|
2016-12-02 19:18:16 +00:00
|
|
|
InformerFactory informers.SharedInformerFactory
|
|
|
|
|
|
|
|
// Options provides access to init options for a given controller
|
|
|
|
Options options.CMServer
|
|
|
|
|
|
|
|
// AvailableResources is a map listing currently available resources
|
|
|
|
AvailableResources map[schema.GroupVersionResource]bool
|
|
|
|
|
|
|
|
// Stop is the stop channel
|
|
|
|
Stop <-chan struct{}
|
|
|
|
}
|
|
|
|
|
2017-01-10 20:11:25 +00:00
|
|
|
func (c ControllerContext) IsControllerEnabled(name string) bool {
|
|
|
|
return IsControllerEnabled(name, ControllersDisabledByDefault, c.Options.Controllers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsControllerEnabled(name string, disabledByDefaultControllers sets.String, controllers ...string) bool {
|
|
|
|
hasStar := false
|
2017-03-17 06:25:10 +00:00
|
|
|
for _, ctrl := range controllers {
|
|
|
|
if ctrl == name {
|
2017-01-10 20:11:25 +00:00
|
|
|
return true
|
|
|
|
}
|
2017-03-17 06:25:10 +00:00
|
|
|
if ctrl == "-"+name {
|
2017-01-10 20:11:25 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-03-17 06:25:10 +00:00
|
|
|
if ctrl == "*" {
|
2017-01-10 20:11:25 +00:00
|
|
|
hasStar = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if we get here, there was no explicit choice
|
|
|
|
if !hasStar {
|
|
|
|
// nothing on by default
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if disabledByDefaultControllers.Has(name) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-12-02 19:18:16 +00:00
|
|
|
// 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`
|
2016-12-05 14:04:32 +00:00
|
|
|
// The bool indicates whether the controller was enabled.
|
2016-12-02 19:18:16 +00:00
|
|
|
type InitFunc func(ctx ControllerContext) (bool, error)
|
|
|
|
|
2017-01-10 20:11:25 +00:00
|
|
|
func KnownControllers() []string {
|
2017-05-08 18:16:05 +00:00
|
|
|
ret := sets.StringKeySet(NewControllerInitializers())
|
2017-03-06 20:58:08 +00:00
|
|
|
|
|
|
|
ret.Insert(
|
|
|
|
saTokenControllerName,
|
|
|
|
nodeControllerName,
|
|
|
|
serviceControllerName,
|
|
|
|
routeControllerName,
|
|
|
|
pvBinderControllerName,
|
2017-04-08 05:32:22 +00:00
|
|
|
attachDetachControllerName,
|
2017-03-06 20:58:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// add "special" controllers that aren't initialized normally
|
|
|
|
return ret.List()
|
2017-01-10 20:11:25 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 20:17:26 +00:00
|
|
|
var ControllersDisabledByDefault = sets.NewString(
|
|
|
|
"bootstrapsigner",
|
|
|
|
"tokencleaner",
|
|
|
|
)
|
2017-01-10 20:11:25 +00:00
|
|
|
|
2017-05-08 18:16:05 +00:00
|
|
|
// NewControllerInitializers is a public map of named controller groups (you can start more than one in an init func)
|
|
|
|
// paired to their InitFunc. This allows for structured downstream composition and subdivision.
|
|
|
|
func NewControllerInitializers() map[string]InitFunc {
|
2016-12-02 19:18:16 +00:00
|
|
|
controllers := map[string]InitFunc{}
|
|
|
|
controllers["endpoint"] = startEndpointController
|
|
|
|
controllers["replicationcontroller"] = startReplicationController
|
2016-12-05 14:04:32 +00:00
|
|
|
controllers["podgc"] = startPodGCController
|
2016-12-02 19:18:16 +00:00
|
|
|
controllers["resourcequota"] = startResourceQuotaController
|
|
|
|
controllers["namespace"] = startNamespaceController
|
2016-12-05 14:04:32 +00:00
|
|
|
controllers["serviceaccount"] = startServiceAccountController
|
|
|
|
controllers["garbagecollector"] = startGarbageCollectorController
|
|
|
|
controllers["daemonset"] = startDaemonSetController
|
|
|
|
controllers["job"] = startJobController
|
|
|
|
controllers["deployment"] = startDeploymentController
|
|
|
|
controllers["replicaset"] = startReplicaSetController
|
|
|
|
controllers["horizontalpodautoscaling"] = startHPAController
|
|
|
|
controllers["disruption"] = startDisruptionController
|
|
|
|
controllers["statefuleset"] = startStatefulSetController
|
|
|
|
controllers["cronjob"] = startCronJobController
|
|
|
|
controllers["certificatesigningrequests"] = startCSRController
|
2017-01-30 10:48:15 +00:00
|
|
|
controllers["ttl"] = startTTLController
|
2016-12-12 20:17:26 +00:00
|
|
|
controllers["bootstrapsigner"] = startBootstrapSignerController
|
|
|
|
controllers["tokencleaner"] = startTokenCleanerController
|
2016-12-02 19:18:16 +00:00
|
|
|
|
|
|
|
return controllers
|
|
|
|
}
|
|
|
|
|
2016-11-21 20:00:20 +00:00
|
|
|
// TODO: In general, any controller checking this needs to be dynamic so
|
|
|
|
// users don't have to restart their controller manager if they change the apiserver.
|
2017-05-08 18:16:05 +00:00
|
|
|
// Until we get there, the structure here needs to be exposed for the construction of a proper ControllerContext.
|
|
|
|
func GetAvailableResources(clientBuilder controller.ControllerClientBuilder) (map[schema.GroupVersionResource]bool, error) {
|
2016-11-21 20:00:20 +00:00
|
|
|
var discoveryClient discovery.DiscoveryInterface
|
|
|
|
|
|
|
|
// If apiserver is not running we should wait for some time and fail only then. This is particularly
|
|
|
|
// important when we start apiserver and controller manager at the same time.
|
|
|
|
err := wait.PollImmediate(time.Second, 10*time.Second, func() (bool, error) {
|
|
|
|
client, err := clientBuilder.Client("controller-discovery")
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Failed to get api versions from server: %v", err)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2017-03-16 15:05:03 +00:00
|
|
|
healthStatus := 0
|
|
|
|
client.Discovery().RESTClient().Get().AbsPath("/healthz").Do().StatusCode(&healthStatus)
|
|
|
|
if healthStatus != http.StatusOK {
|
|
|
|
glog.Errorf("Server isn't healthy yet. Waiting a little while.")
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2016-11-21 20:00:20 +00:00
|
|
|
discoveryClient = client.Discovery()
|
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-11-21 20:10:59 +00:00
|
|
|
return nil, fmt.Errorf("failed to get api versions from server: %v", err)
|
2016-11-21 20:00:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resourceMap, err := discoveryClient.ServerResources()
|
|
|
|
if err != nil {
|
2016-11-21 20:10:59 +00:00
|
|
|
return nil, fmt.Errorf("failed to get supported resources from server: %v", err)
|
2016-11-21 20:00:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
allResources := map[schema.GroupVersionResource]bool{}
|
|
|
|
for _, apiResourceList := range resourceMap {
|
|
|
|
version, err := schema.ParseGroupVersion(apiResourceList.GroupVersion)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, apiResource := range apiResourceList.APIResources {
|
|
|
|
allResources[version.WithResource(apiResource.Name)] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return allResources, nil
|
|
|
|
}
|
|
|
|
|
2017-03-06 20:58:08 +00:00
|
|
|
const (
|
2017-04-08 05:32:22 +00:00
|
|
|
saTokenControllerName = "serviceaccount-token"
|
|
|
|
nodeControllerName = "node"
|
|
|
|
serviceControllerName = "service"
|
|
|
|
routeControllerName = "route"
|
|
|
|
pvBinderControllerName = "persistentvolume-binder"
|
|
|
|
attachDetachControllerName = "attachdetach"
|
2017-03-06 20:58:08 +00:00
|
|
|
)
|
|
|
|
|
2016-12-02 19:18:16 +00:00
|
|
|
func StartControllers(controllers map[string]InitFunc, s *options.CMServer, rootClientBuilder, clientBuilder controller.ControllerClientBuilder, stop <-chan struct{}) error {
|
2017-02-06 18:35:50 +00:00
|
|
|
versionedClient := rootClientBuilder.ClientOrDie("shared-informers")
|
2017-02-24 14:52:43 +00:00
|
|
|
sharedInformers := informers.NewSharedInformerFactory(versionedClient, ResyncPeriod(s)())
|
2016-08-04 07:06:29 +00:00
|
|
|
|
2016-09-22 19:53:08 +00:00
|
|
|
// always start the SA token controller first using a full-power client, since it needs to mint tokens for the rest
|
2017-03-06 20:58:08 +00:00
|
|
|
if len(s.ServiceAccountKeyFile) > 0 && IsControllerEnabled(saTokenControllerName, ControllersDisabledByDefault, s.Controllers...) {
|
2016-09-22 19:53:08 +00:00
|
|
|
privateKey, err := serviceaccount.ReadPrivateKey(s.ServiceAccountKeyFile)
|
|
|
|
if err != nil {
|
2016-11-21 20:10:59 +00:00
|
|
|
return fmt.Errorf("error reading key for service account token controller: %v", err)
|
2016-09-22 19:53:08 +00:00
|
|
|
} else {
|
|
|
|
var rootCA []byte
|
|
|
|
if s.RootCAFile != "" {
|
|
|
|
rootCA, err = ioutil.ReadFile(s.RootCAFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error reading root-ca-file at %s: %v", s.RootCAFile, err)
|
|
|
|
}
|
|
|
|
if _, err := certutil.ParseCertsPEM(rootCA); err != nil {
|
|
|
|
return fmt.Errorf("error parsing root-ca-file at %s: %v", s.RootCAFile, err)
|
|
|
|
}
|
|
|
|
} else {
|
2016-11-21 20:09:12 +00:00
|
|
|
rootCA = rootClientBuilder.ConfigOrDie("tokens-controller").CAData
|
2016-09-22 19:53:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go serviceaccountcontroller.NewTokensController(
|
|
|
|
rootClientBuilder.ClientOrDie("tokens-controller"),
|
|
|
|
serviceaccountcontroller.TokensControllerOptions{
|
|
|
|
TokenGenerator: serviceaccount.JWTTokenGenerator(privateKey),
|
|
|
|
RootCA: rootCA,
|
|
|
|
},
|
2016-11-21 19:51:14 +00:00
|
|
|
).Run(int(s.ConcurrentSATokenSyncs), stop)
|
2016-09-22 19:53:08 +00:00
|
|
|
time.Sleep(wait.Jitter(s.ControllerStartInterval.Duration, ControllerStartJitter))
|
|
|
|
}
|
2017-03-06 20:58:08 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
glog.Warningf("%q is disabled", saTokenControllerName)
|
2016-09-22 19:53:08 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 18:16:05 +00:00
|
|
|
availableResources, err := GetAvailableResources(clientBuilder)
|
2016-11-21 20:00:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-02 19:18:16 +00:00
|
|
|
ctx := ControllerContext{
|
|
|
|
ClientBuilder: clientBuilder,
|
|
|
|
InformerFactory: sharedInformers,
|
|
|
|
Options: *s,
|
|
|
|
AvailableResources: availableResources,
|
|
|
|
Stop: stop,
|
|
|
|
}
|
2015-01-30 23:31:36 +00:00
|
|
|
|
2016-12-02 19:18:16 +00:00
|
|
|
for controllerName, initFn := range controllers {
|
2017-01-10 20:11:25 +00:00
|
|
|
if !ctx.IsControllerEnabled(controllerName) {
|
|
|
|
glog.Warningf("%q is disabled", controllerName)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-12-02 19:18:16 +00:00
|
|
|
time.Sleep(wait.Jitter(s.ControllerStartInterval.Duration, ControllerStartJitter))
|
2015-01-30 23:31:36 +00:00
|
|
|
|
2016-12-02 19:18:16 +00:00
|
|
|
glog.V(1).Infof("Starting %q", controllerName)
|
|
|
|
started, err := initFn(ctx)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Error starting %q", controllerName)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !started {
|
|
|
|
glog.Warningf("Skipping %q", controllerName)
|
2017-01-14 00:27:00 +00:00
|
|
|
continue
|
2016-12-02 19:18:16 +00:00
|
|
|
}
|
|
|
|
glog.Infof("Started %q", controllerName)
|
|
|
|
}
|
2015-09-21 22:51:27 +00:00
|
|
|
|
2017-04-08 05:32:22 +00:00
|
|
|
// all the remaining plugins want this cloud variable
|
2015-07-01 18:41:49 +00:00
|
|
|
cloud, err := cloudprovider.InitCloudProvider(s.CloudProvider, s.CloudConfigFile)
|
|
|
|
if err != nil {
|
2016-11-21 20:10:59 +00:00
|
|
|
return fmt.Errorf("cloud provider could not be initialized: %v", err)
|
2015-07-01 18:41:49 +00:00
|
|
|
}
|
2015-02-07 19:53:42 +00:00
|
|
|
|
2017-03-06 20:58:08 +00:00
|
|
|
if ctx.IsControllerEnabled(nodeControllerName) {
|
|
|
|
_, clusterCIDR, err := net.ParseCIDR(s.ClusterCIDR)
|
|
|
|
if err != nil {
|
|
|
|
glog.Warningf("Unsuccessful parsing of cluster CIDR %v: %v", s.ClusterCIDR, err)
|
|
|
|
}
|
|
|
|
_, serviceCIDR, err := net.ParseCIDR(s.ServiceCIDR)
|
|
|
|
if err != nil {
|
|
|
|
glog.Warningf("Unsuccessful parsing of service CIDR %v: %v", s.ServiceCIDR, err)
|
|
|
|
}
|
|
|
|
nodeController, err := nodecontroller.NewNodeController(
|
|
|
|
sharedInformers.Core().V1().Pods(),
|
|
|
|
sharedInformers.Core().V1().Nodes(),
|
|
|
|
sharedInformers.Extensions().V1beta1().DaemonSets(),
|
|
|
|
cloud,
|
|
|
|
clientBuilder.ClientOrDie("node-controller"),
|
|
|
|
s.PodEvictionTimeout.Duration,
|
|
|
|
s.NodeEvictionRate,
|
|
|
|
s.SecondaryNodeEvictionRate,
|
|
|
|
s.LargeClusterSizeThreshold,
|
|
|
|
s.UnhealthyZoneThreshold,
|
|
|
|
s.NodeMonitorGracePeriod.Duration,
|
|
|
|
s.NodeStartupGracePeriod.Duration,
|
|
|
|
s.NodeMonitorPeriod.Duration,
|
|
|
|
clusterCIDR,
|
|
|
|
serviceCIDR,
|
|
|
|
int(s.NodeCIDRMaskSize),
|
|
|
|
s.AllocateNodeCIDRs,
|
2017-02-27 08:33:55 +00:00
|
|
|
nodecontroller.CIDRAllocatorType(s.CIDRAllocatorType),
|
2017-03-06 20:58:08 +00:00
|
|
|
s.EnableTaintManager,
|
2017-03-08 07:03:57 +00:00
|
|
|
utilfeature.DefaultFeatureGate.Enabled(features.TaintBasedEvictions),
|
2017-03-06 20:58:08 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to initialize nodecontroller: %v", err)
|
|
|
|
}
|
2017-04-12 19:49:17 +00:00
|
|
|
go nodeController.Run(stop)
|
2017-03-06 20:58:08 +00:00
|
|
|
time.Sleep(wait.Jitter(s.ControllerStartInterval.Duration, ControllerStartJitter))
|
2016-07-26 05:35:40 +00:00
|
|
|
} else {
|
2017-03-06 20:58:08 +00:00
|
|
|
glog.Warningf("%q is disabled", nodeControllerName)
|
2015-03-24 17:32:43 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 20:58:08 +00:00
|
|
|
if ctx.IsControllerEnabled(serviceControllerName) {
|
|
|
|
serviceController, err := servicecontroller.New(
|
|
|
|
cloud,
|
|
|
|
clientBuilder.ClientOrDie("service-controller"),
|
|
|
|
sharedInformers.Core().V1().Services(),
|
|
|
|
sharedInformers.Core().V1().Nodes(),
|
|
|
|
s.ClusterName,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Failed to start service controller: %v", err)
|
2015-07-26 00:02:23 +00:00
|
|
|
} else {
|
2017-03-06 20:58:08 +00:00
|
|
|
go serviceController.Run(stop, int(s.ConcurrentServiceSyncs))
|
2015-05-15 21:49:26 +00:00
|
|
|
}
|
2017-03-06 20:58:08 +00:00
|
|
|
time.Sleep(wait.Jitter(s.ControllerStartInterval.Duration, ControllerStartJitter))
|
2016-07-21 10:31:28 +00:00
|
|
|
} else {
|
2017-03-06 20:58:08 +00:00
|
|
|
glog.Warningf("%q is disabled", serviceControllerName)
|
2015-05-15 21:49:26 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 20:58:08 +00:00
|
|
|
if ctx.IsControllerEnabled(routeControllerName) {
|
|
|
|
_, clusterCIDR, err := net.ParseCIDR(s.ClusterCIDR)
|
|
|
|
if err != nil {
|
|
|
|
glog.Warningf("Unsuccessful parsing of cluster CIDR %v: %v", s.ClusterCIDR, err)
|
|
|
|
}
|
|
|
|
if s.AllocateNodeCIDRs && s.ConfigureCloudRoutes {
|
|
|
|
if cloud == nil {
|
|
|
|
glog.Warning("configure-cloud-routes is set, but no cloud provider specified. Will not configure cloud provider routes.")
|
|
|
|
} else 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 {
|
|
|
|
routeController := routecontroller.New(routes, clientBuilder.ClientOrDie("route-controller"), sharedInformers.Core().V1().Nodes(), s.ClusterName, clusterCIDR)
|
|
|
|
go routeController.Run(stop, s.RouteReconciliationPeriod.Duration)
|
|
|
|
time.Sleep(wait.Jitter(s.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)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
glog.Warningf("%q is disabled", routeControllerName)
|
2016-09-26 12:15:25 +00:00
|
|
|
}
|
2015-04-16 17:26:08 +00:00
|
|
|
|
2017-03-06 20:58:08 +00:00
|
|
|
if ctx.IsControllerEnabled(pvBinderControllerName) {
|
|
|
|
params := persistentvolumecontroller.ControllerParameters{
|
|
|
|
KubeClient: clientBuilder.ClientOrDie("persistent-volume-binder"),
|
|
|
|
SyncPeriod: s.PVClaimBinderSyncPeriod.Duration,
|
|
|
|
VolumePlugins: ProbeControllerVolumePlugins(cloud, s.VolumeConfiguration),
|
|
|
|
Cloud: cloud,
|
|
|
|
ClusterName: s.ClusterName,
|
|
|
|
VolumeInformer: sharedInformers.Core().V1().PersistentVolumes(),
|
|
|
|
ClaimInformer: sharedInformers.Core().V1().PersistentVolumeClaims(),
|
2017-04-25 08:13:38 +00:00
|
|
|
ClassInformer: sharedInformers.Storage().V1().StorageClasses(),
|
2017-03-06 20:58:08 +00:00
|
|
|
EnableDynamicProvisioning: s.VolumeConfiguration.EnableDynamicProvisioning,
|
|
|
|
}
|
2017-03-13 07:41:59 +00:00
|
|
|
volumeController, volumeControllerErr := persistentvolumecontroller.NewController(params)
|
|
|
|
if volumeControllerErr != nil {
|
|
|
|
return fmt.Errorf("failed to construct persistentvolume controller: %v", volumeControllerErr)
|
|
|
|
}
|
2017-03-06 20:58:08 +00:00
|
|
|
go volumeController.Run(stop)
|
|
|
|
time.Sleep(wait.Jitter(s.ControllerStartInterval.Duration, ControllerStartJitter))
|
|
|
|
} else {
|
|
|
|
glog.Warningf("%q is disabled", pvBinderControllerName)
|
2017-01-06 22:24:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 05:32:22 +00:00
|
|
|
if ctx.IsControllerEnabled(attachDetachControllerName) {
|
2017-03-06 20:58:08 +00:00
|
|
|
if s.ReconcilerSyncLoopPeriod.Duration < time.Second {
|
|
|
|
return fmt.Errorf("Duration time must be greater than one second as set via command line option reconcile-sync-loop-period.")
|
|
|
|
}
|
|
|
|
attachDetachController, attachDetachControllerErr :=
|
|
|
|
attachdetach.NewAttachDetachController(
|
|
|
|
clientBuilder.ClientOrDie("attachdetach-controller"),
|
|
|
|
sharedInformers.Core().V1().Pods(),
|
|
|
|
sharedInformers.Core().V1().Nodes(),
|
|
|
|
sharedInformers.Core().V1().PersistentVolumeClaims(),
|
|
|
|
sharedInformers.Core().V1().PersistentVolumes(),
|
|
|
|
cloud,
|
|
|
|
ProbeAttachableVolumePlugins(s.VolumeConfiguration),
|
|
|
|
s.DisableAttachDetachReconcilerSync,
|
|
|
|
s.ReconcilerSyncLoopPeriod.Duration,
|
|
|
|
)
|
|
|
|
if attachDetachControllerErr != nil {
|
|
|
|
return fmt.Errorf("failed to start attach/detach controller: %v", attachDetachControllerErr)
|
|
|
|
}
|
|
|
|
go attachDetachController.Run(stop)
|
|
|
|
time.Sleep(wait.Jitter(s.ControllerStartInterval.Duration, ControllerStartJitter))
|
|
|
|
} else {
|
2017-04-08 05:32:22 +00:00
|
|
|
glog.Warningf("%q is disabled", attachDetachControllerName)
|
2016-04-30 06:36:27 +00:00
|
|
|
}
|
2016-04-30 06:36:27 +00:00
|
|
|
|
2016-08-04 07:06:29 +00:00
|
|
|
sharedInformers.Start(stop)
|
2016-04-14 18:00:52 +00:00
|
|
|
|
2015-01-30 23:31:36 +00:00
|
|
|
select {}
|
|
|
|
}
|