2015-01-30 23:31:36 +00:00
/ *
2015-05-01 16:19:44 +00:00
Copyright 2014 The Kubernetes Authors All rights reserved .
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-02-08 04:07:00 +00:00
package app
2015-01-30 23:31:36 +00:00
import (
"net"
"net/http"
2015-03-13 15:44:11 +00:00
"net/http/pprof"
2015-01-30 23:31:36 +00:00
"strconv"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
2015-04-17 07:18:07 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd"
clientcmdapi "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd/api"
2015-01-30 23:31:36 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
2015-03-23 22:59:38 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider/nodecontroller"
2015-03-24 17:32:43 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider/servicecontroller"
2015-01-30 23:31:36 +00:00
replicationControllerPkg "github.com/GoogleCloudPlatform/kubernetes/pkg/controller"
2015-04-22 14:46:03 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/healthz"
2015-01-30 23:31:36 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/master/ports"
2015-03-20 16:49:03 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/namespace"
2015-01-30 23:31:36 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/resourcequota"
"github.com/GoogleCloudPlatform/kubernetes/pkg/service"
2015-04-21 03:25:56 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/serviceaccount"
2015-01-30 23:31:36 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
2015-04-16 17:28:45 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/volumeclaimbinder"
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-01-30 23:31:36 +00:00
"github.com/spf13/pflag"
)
2015-02-25 00:15:59 +00:00
// CMServer is the main context object for the controller manager.
2015-01-30 23:31:36 +00:00
type CMServer struct {
Port int
Address util . IP
CloudProvider string
CloudConfigFile string
2015-04-16 23:18:02 +00:00
ConcurrentEndpointSyncs int
2015-04-21 20:40:35 +00:00
ConcurrentRCSyncs int
2015-01-30 23:31:36 +00:00
MinionRegexp string
NodeSyncPeriod time . Duration
ResourceQuotaSyncPeriod time . Duration
2015-03-20 16:49:03 +00:00
NamespaceSyncPeriod time . Duration
2015-04-16 17:26:08 +00:00
PVClaimBinderSyncPeriod time . Duration
2015-01-30 23:31:36 +00:00
RegisterRetryCount int
MachineList util . StringList
2015-01-15 01:05:48 +00:00
SyncNodeList bool
2015-02-24 06:43:58 +00:00
SyncNodeStatus bool
2015-03-31 11:17:12 +00:00
NodeMonitorGracePeriod time . Duration
NodeStartupGracePeriod time . Duration
NodeMonitorPeriod time . Duration
NodeStatusUpdateRetry int
2015-02-07 19:53:42 +00:00
PodEvictionTimeout time . Duration
2015-04-02 15:13:13 +00:00
DeletingPodsQps float32
DeletingPodsBurst int
2015-05-01 16:02:38 +00:00
ServiceAccountKeyFile string
2015-01-30 23:31:36 +00:00
// TODO: Discover these by pinging the host machines, and rip out these params.
NodeMilliCPU int64
NodeMemory resource . Quantity
2015-04-28 15:02:45 +00:00
ClusterName string
2015-05-06 21:48:45 +00:00
ClusterCIDR util . IPNet
2015-04-28 15:02:45 +00:00
AllocateNodeCIDRs bool
EnableProfiling bool
2015-04-17 07:18:07 +00:00
Master string
Kubeconfig string
2015-01-30 23:31:36 +00:00
}
2015-02-07 19:53:42 +00:00
// NewCMServer creates a new CMServer with a default config.
2015-01-30 23:31:36 +00:00
func NewCMServer ( ) * CMServer {
s := CMServer {
Port : ports . ControllerManagerPort ,
Address : util . IP ( net . ParseIP ( "127.0.0.1" ) ) ,
2015-04-16 23:18:02 +00:00
ConcurrentEndpointSyncs : 5 ,
2015-04-21 20:40:35 +00:00
ConcurrentRCSyncs : 5 ,
2015-01-30 23:31:36 +00:00
NodeSyncPeriod : 10 * time . Second ,
ResourceQuotaSyncPeriod : 10 * time . Second ,
2015-04-13 17:15:27 +00:00
NamespaceSyncPeriod : 5 * time . Minute ,
2015-04-16 17:26:08 +00:00
PVClaimBinderSyncPeriod : 10 * time . Second ,
2015-01-30 23:31:36 +00:00
RegisterRetryCount : 10 ,
2015-02-07 19:53:42 +00:00
PodEvictionTimeout : 5 * time . Minute ,
2015-01-30 23:31:36 +00:00
NodeMilliCPU : 1000 ,
NodeMemory : resource . MustParse ( "3Gi" ) ,
2015-01-15 01:05:48 +00:00
SyncNodeList : true ,
2015-04-13 20:59:45 +00:00
ClusterName : "kubernetes" ,
2015-01-30 23:31:36 +00:00
}
return & s
}
// AddFlags adds flags for a specific CMServer to the specified FlagSet
func ( s * CMServer ) AddFlags ( fs * pflag . FlagSet ) {
fs . IntVar ( & s . Port , "port" , s . Port , "The port that the controller-manager's http service runs on" )
fs . Var ( & s . Address , "address" , "The IP address to serve on (set to 0.0.0.0 for all interfaces)" )
2015-04-24 06:10:33 +00:00
fs . StringVar ( & s . CloudProvider , "cloud-provider" , s . CloudProvider , "The provider for cloud services. Empty string for no provider." )
fs . StringVar ( & s . CloudConfigFile , "cloud-config" , s . CloudConfigFile , "The path to the cloud provider configuration file. Empty string for no configuration file." )
fs . IntVar ( & s . ConcurrentEndpointSyncs , "concurrent-endpoint-syncs" , s . ConcurrentEndpointSyncs , "The number of endpoint syncing operations that will be done concurrently. Larger number = faster endpoint updating, but more CPU (and network) load" )
2015-04-21 20:40:35 +00:00
fs . IntVar ( & s . ConcurrentRCSyncs , "concurrent_rc_syncs" , s . ConcurrentRCSyncs , "The number of replication controllers that are allowed to sync concurrently. Larger number = more reponsive replica management, but more CPU (and network) load" )
2015-04-24 06:10:33 +00:00
fs . StringVar ( & s . MinionRegexp , "minion-regexp" , s . MinionRegexp , "If non empty, and --cloud-provider is specified, a regular expression for matching minion VMs." )
fs . DurationVar ( & s . NodeSyncPeriod , "node-sync-period" , s . NodeSyncPeriod , "" +
2015-01-30 23:31:36 +00:00
"The period for syncing nodes from cloudprovider. Longer periods will result in " +
"fewer calls to cloud provider, but may delay addition of new nodes to cluster." )
2015-04-24 06:10:33 +00:00
fs . DurationVar ( & s . ResourceQuotaSyncPeriod , "resource-quota-sync-period" , s . ResourceQuotaSyncPeriod , "The period for syncing quota usage status in the system" )
fs . DurationVar ( & s . NamespaceSyncPeriod , "namespace-sync-period" , s . NamespaceSyncPeriod , "The period for syncing namespace life-cycle updates" )
fs . DurationVar ( & s . PVClaimBinderSyncPeriod , "pvclaimbinder-sync-period" , s . PVClaimBinderSyncPeriod , "The period for syncing persistent volumes and persistent volume claims" )
fs . DurationVar ( & s . PodEvictionTimeout , "pod-eviction-timeout" , s . PodEvictionTimeout , "The grace peroid for deleting pods on failed nodes." )
fs . Float32Var ( & s . DeletingPodsQps , "deleting-pods-qps" , 0.1 , "Number of nodes per second on which pods are deleted in case of node failure." )
fs . IntVar ( & s . DeletingPodsBurst , "deleting-pods-burst" , 10 , "Number of nodes on which pods are bursty deleted in case of node failure. For more details look into RateLimiter." )
fs . IntVar ( & s . RegisterRetryCount , "register-retry-count" , s . RegisterRetryCount , "" +
"The number of retries for initial node registration. Retry interval equals node-sync-period." )
2015-01-30 23:31:36 +00:00
fs . Var ( & s . MachineList , "machines" , "List of machines to schedule onto, comma separated." )
2015-04-24 06:10:33 +00:00
fs . BoolVar ( & s . SyncNodeList , "sync-nodes" , s . SyncNodeList , "If true, and --cloud-provider is specified, sync nodes from the cloud provider. Default true." )
fs . BoolVar ( & s . SyncNodeStatus , "sync-node-status" , s . SyncNodeStatus ,
2015-03-27 14:09:51 +00:00
"DEPRECATED. Does not have any effect now and it will be removed in a later release." )
2015-04-24 06:10:33 +00:00
fs . DurationVar ( & s . NodeMonitorGracePeriod , "node-monitor-grace-period" , 40 * time . Second ,
2015-03-27 14:09:51 +00:00
"Amount of time which we allow running Node to be unresponsive before marking it unhealty. " +
"Must be N times more than kubelet's nodeStatusUpdateFrequency, " +
"where N means number of retries allowed for kubelet to post node status." )
2015-04-24 06:10:33 +00:00
fs . DurationVar ( & s . NodeStartupGracePeriod , "node-startup-grace-period" , 60 * time . Second ,
2015-03-27 14:09:51 +00:00
"Amount of time which we allow starting Node to be unresponsive before marking it unhealty." )
2015-04-24 06:10:33 +00:00
fs . DurationVar ( & s . NodeMonitorPeriod , "node-monitor-period" , 5 * time . Second ,
2015-03-27 14:09:51 +00:00
"The period for syncing NodeStatus in NodeController." )
2015-05-01 16:02:38 +00:00
fs . StringVar ( & s . ServiceAccountKeyFile , "service-account-private-key-file" , s . ServiceAccountKeyFile , "Filename containing a PEM-encoded private RSA key used to sign service account tokens." )
2015-01-30 23:31:36 +00:00
// TODO: Discover these by pinging the host machines, and rip out these flags.
// TODO: in the meantime, use resource.QuantityFlag() instead of these
2015-04-24 06:10:33 +00:00
fs . Int64Var ( & s . NodeMilliCPU , "node-milli-cpu" , s . NodeMilliCPU , "The amount of MilliCPU provisioned on each node" )
fs . Var ( resource . NewQuantityFlagValue ( & s . NodeMemory ) , "node-memory" , "The amount of memory (in bytes) provisioned on each node" )
2015-05-04 11:22:25 +00:00
fs . BoolVar ( & s . EnableProfiling , "profiling" , true , "Enable profiling via web interface host:port/debug/pprof/" )
2015-05-06 21:48:45 +00:00
fs . Var ( & s . ClusterCIDR , "cluster-cidr" , "CIDR Range for Pods in cluster." )
2015-04-28 15:02:45 +00:00
fs . BoolVar ( & s . AllocateNodeCIDRs , "allocate-node-cidrs" , false , "Should CIDRs for Pods be allocated and set on the cloud provider." )
2015-04-17 07:18:07 +00:00
fs . StringVar ( & s . Master , "master" , s . Master , "The address of the Kubernetes API server (overrides any value in kubeconfig)" )
fs . StringVar ( & s . Kubeconfig , "kubeconfig" , s . Kubeconfig , "Path to kubeconfig file with authorization and master location information." )
2015-01-30 23:31:36 +00:00
}
func ( s * CMServer ) verifyMinionFlags ( ) {
2015-01-15 01:05:48 +00:00
if ! s . SyncNodeList && s . MinionRegexp != "" {
2015-04-24 06:10:33 +00:00
glog . Info ( "--minion-regexp is ignored by --sync-nodes=false" )
2015-01-15 01:05:48 +00:00
}
2015-01-30 23:31:36 +00:00
if s . CloudProvider == "" || s . MinionRegexp == "" {
if len ( s . MachineList ) == 0 {
glog . Info ( "No machines specified!" )
}
return
}
if len ( s . MachineList ) != 0 {
2015-04-24 06:10:33 +00:00
glog . Info ( "--machines is overwritten by --minion-regexp" )
2015-01-30 23:31:36 +00:00
}
}
// Run runs the CMServer. This should never exit.
func ( s * CMServer ) Run ( _ [ ] string ) error {
s . verifyMinionFlags ( )
2015-04-17 07:18:07 +00:00
if s . Kubeconfig == "" && s . Master == "" {
glog . Warningf ( "Neither --kubeconfig nor --master was specified. Using default API client. This might not work." )
2015-01-30 23:31:36 +00:00
}
2015-04-17 07:18:07 +00:00
// This creates a client, first loading any specified kubeconfig
// file, and then overriding the Master flag, if non-empty.
kubeconfig , err := clientcmd . NewNonInteractiveDeferredLoadingClientConfig (
& clientcmd . ClientConfigLoadingRules { ExplicitPath : s . Kubeconfig } ,
& clientcmd . ConfigOverrides { ClusterInfo : clientcmdapi . Cluster { Server : s . Master } } ) . ClientConfig ( )
if err != nil {
return err
}
kubeconfig . QPS = 20.0
kubeconfig . Burst = 30
kubeClient , err := client . New ( kubeconfig )
2015-01-30 23:31:36 +00:00
if err != nil {
glog . Fatalf ( "Invalid API configuration: %v" , err )
}
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 )
}
2015-04-22 14:46:03 +00:00
mux . Handle ( "/metrics" , prometheus . Handler ( ) )
server := & http . Server {
Addr : net . JoinHostPort ( s . Address . String ( ) , strconv . Itoa ( s . Port ) ) ,
Handler : mux ,
}
glog . Fatal ( server . ListenAndServe ( ) )
2015-03-13 15:44:11 +00:00
} ( )
2015-01-30 23:31:36 +00:00
endpoints := service . NewEndpointController ( kubeClient )
2015-04-16 23:18:02 +00:00
go endpoints . Run ( s . ConcurrentEndpointSyncs , util . NeverStop )
2015-01-30 23:31:36 +00:00
2015-05-06 21:39:14 +00:00
controllerManager := replicationControllerPkg . NewReplicationManager ( kubeClient , replicationControllerPkg . BurstReplicas )
2015-04-21 20:40:35 +00:00
go controllerManager . Run ( s . ConcurrentRCSyncs , util . NeverStop )
2015-01-30 23:31:36 +00:00
cloud := cloudprovider . InitCloudProvider ( s . CloudProvider , s . CloudConfigFile )
nodeResources := & api . NodeResources {
Capacity : api . ResourceList {
api . ResourceCPU : * resource . NewMilliQuantity ( s . NodeMilliCPU , resource . DecimalSI ) ,
api . ResourceMemory : s . NodeMemory ,
} ,
}
2015-02-07 19:53:42 +00:00
2015-03-27 14:09:51 +00:00
if s . SyncNodeStatus {
2015-04-24 06:10:33 +00:00
glog . Warning ( "DEPRECATION NOTICE: sync-node-status flag is being deprecated. It has no effect now and it will be removed in a future version." )
2015-03-27 14:09:51 +00:00
}
2015-03-23 22:59:38 +00:00
nodeController := nodecontroller . NewNodeController ( cloud , s . MinionRegexp , s . MachineList , nodeResources ,
2015-04-13 20:59:45 +00:00
kubeClient , s . RegisterRetryCount , s . PodEvictionTimeout , util . NewTokenBucketRateLimiter ( s . DeletingPodsQps , s . DeletingPodsBurst ) ,
2015-05-14 08:17:04 +00:00
s . NodeMonitorGracePeriod , s . NodeStartupGracePeriod , s . NodeMonitorPeriod , ( * net . IPNet ) ( & s . ClusterCIDR ) , s . AllocateNodeCIDRs )
2015-03-27 14:09:51 +00:00
nodeController . Run ( s . NodeSyncPeriod , s . SyncNodeList )
2015-01-30 23:31:36 +00:00
2015-03-24 17:32:43 +00:00
serviceController := servicecontroller . New ( cloud , kubeClient , s . ClusterName )
2015-04-22 20:54:44 +00:00
if err := serviceController . Run ( s . NodeSyncPeriod ) ; err != nil {
2015-03-24 17:32:43 +00:00
glog . Errorf ( "Failed to start service controller: %v" , err )
}
2015-01-30 23:31:36 +00:00
resourceQuotaManager := resourcequota . NewResourceQuotaManager ( kubeClient )
resourceQuotaManager . Run ( s . ResourceQuotaSyncPeriod )
2015-04-13 17:15:27 +00:00
namespaceManager := namespace . NewNamespaceManager ( kubeClient , s . NamespaceSyncPeriod )
namespaceManager . Run ( )
2015-03-20 16:49:03 +00:00
2015-05-13 00:44:29 +00:00
pvclaimBinder := volumeclaimbinder . NewPersistentVolumeClaimBinder ( kubeClient , s . PVClaimBinderSyncPeriod )
pvclaimBinder . Run ( )
2015-04-16 17:26:08 +00:00
2015-05-01 16:02:38 +00:00
if len ( s . ServiceAccountKeyFile ) > 0 {
privateKey , err := serviceaccount . ReadPrivateKey ( s . ServiceAccountKeyFile )
if err != nil {
glog . Errorf ( "Error reading key for service account token controller: %v" , err )
} else {
serviceaccount . NewTokensController (
kubeClient ,
serviceaccount . DefaultTokenControllerOptions (
serviceaccount . JWTTokenGenerator ( privateKey ) ,
) ,
) . Run ( )
}
}
serviceaccount . NewServiceAccountsController (
kubeClient ,
serviceaccount . DefaultServiceAccountControllerOptions ( ) ,
) . Run ( )
2015-04-21 03:25:56 +00:00
2015-01-30 23:31:36 +00:00
select { }
return nil
}