core.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package app implements a server that runs a set of active
  14. // components. This includes node controllers, service and
  15. // route controller, and so on.
  16. //
  17. package app
  18. import (
  19. "net"
  20. "net/http"
  21. "strings"
  22. cloudprovider "k8s.io/cloud-provider"
  23. "k8s.io/klog"
  24. cloudcontrollerconfig "k8s.io/kubernetes/cmd/cloud-controller-manager/app/config"
  25. cloudcontrollers "k8s.io/kubernetes/pkg/controller/cloud"
  26. routecontroller "k8s.io/kubernetes/pkg/controller/route"
  27. servicecontroller "k8s.io/kubernetes/pkg/controller/service"
  28. )
  29. func startCloudNodeController(ctx *cloudcontrollerconfig.CompletedConfig, cloud cloudprovider.Interface, stopCh <-chan struct{}) (http.Handler, bool, error) {
  30. // Start the CloudNodeController
  31. nodeController := cloudcontrollers.NewCloudNodeController(
  32. ctx.SharedInformers.Core().V1().Nodes(),
  33. // cloud node controller uses existing cluster role from node-controller
  34. ctx.ClientBuilder.ClientOrDie("node-controller"),
  35. cloud,
  36. ctx.ComponentConfig.NodeStatusUpdateFrequency.Duration)
  37. go nodeController.Run(stopCh)
  38. return nil, true, nil
  39. }
  40. func startCloudNodeLifecycleController(ctx *cloudcontrollerconfig.CompletedConfig, cloud cloudprovider.Interface, stopCh <-chan struct{}) (http.Handler, bool, error) {
  41. // Start the cloudNodeLifecycleController
  42. cloudNodeLifecycleController, err := cloudcontrollers.NewCloudNodeLifecycleController(
  43. ctx.SharedInformers.Core().V1().Nodes(),
  44. // cloud node lifecycle controller uses existing cluster role from node-controller
  45. ctx.ClientBuilder.ClientOrDie("node-controller"),
  46. cloud,
  47. ctx.ComponentConfig.KubeCloudShared.NodeMonitorPeriod.Duration,
  48. )
  49. if err != nil {
  50. klog.Warningf("failed to start cloud node lifecycle controller: %s", err)
  51. return nil, false, nil
  52. }
  53. go cloudNodeLifecycleController.Run(stopCh)
  54. return nil, true, nil
  55. }
  56. func startServiceController(ctx *cloudcontrollerconfig.CompletedConfig, cloud cloudprovider.Interface, stopCh <-chan struct{}) (http.Handler, bool, error) {
  57. // Start the service controller
  58. serviceController, err := servicecontroller.New(
  59. cloud,
  60. ctx.ClientBuilder.ClientOrDie("service-controller"),
  61. ctx.SharedInformers.Core().V1().Services(),
  62. ctx.SharedInformers.Core().V1().Nodes(),
  63. ctx.ComponentConfig.KubeCloudShared.ClusterName,
  64. )
  65. if err != nil {
  66. // This error shouldn't fail. It lives like this as a legacy.
  67. klog.Errorf("Failed to start service controller: %v", err)
  68. return nil, false, nil
  69. }
  70. go serviceController.Run(stopCh, int(ctx.ComponentConfig.ServiceController.ConcurrentServiceSyncs))
  71. return nil, true, nil
  72. }
  73. func startRouteController(ctx *cloudcontrollerconfig.CompletedConfig, cloud cloudprovider.Interface, stopCh <-chan struct{}) (http.Handler, bool, error) {
  74. if !ctx.ComponentConfig.KubeCloudShared.AllocateNodeCIDRs || !ctx.ComponentConfig.KubeCloudShared.ConfigureCloudRoutes {
  75. klog.Infof("Will not configure cloud provider routes for allocate-node-cidrs: %v, configure-cloud-routes: %v.", ctx.ComponentConfig.KubeCloudShared.AllocateNodeCIDRs, ctx.ComponentConfig.KubeCloudShared.ConfigureCloudRoutes)
  76. return nil, false, nil
  77. }
  78. // If CIDRs should be allocated for pods and set on the CloudProvider, then start the route controller
  79. routes, ok := cloud.Routes()
  80. if !ok {
  81. klog.Warning("configure-cloud-routes is set, but cloud provider does not support routes. Will not configure cloud provider routes.")
  82. return nil, false, nil
  83. }
  84. var clusterCIDR *net.IPNet
  85. var err error
  86. if len(strings.TrimSpace(ctx.ComponentConfig.KubeCloudShared.ClusterCIDR)) != 0 {
  87. _, clusterCIDR, err = net.ParseCIDR(ctx.ComponentConfig.KubeCloudShared.ClusterCIDR)
  88. if err != nil {
  89. klog.Warningf("Unsuccessful parsing of cluster CIDR %v: %v", ctx.ComponentConfig.KubeCloudShared.ClusterCIDR, err)
  90. }
  91. }
  92. routeController := routecontroller.New(
  93. routes,
  94. ctx.ClientBuilder.ClientOrDie("route-controller"),
  95. ctx.SharedInformers.Core().V1().Nodes(),
  96. ctx.ComponentConfig.KubeCloudShared.ClusterName,
  97. clusterCIDR,
  98. )
  99. go routeController.Run(stopCh, ctx.ComponentConfig.KubeCloudShared.RouteReconciliationPeriod.Duration)
  100. return nil, true, nil
  101. }