core.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. "fmt"
  20. "net"
  21. "net/http"
  22. "strings"
  23. cloudprovider "k8s.io/cloud-provider"
  24. "k8s.io/klog"
  25. cloudcontrollerconfig "k8s.io/kubernetes/cmd/cloud-controller-manager/app/config"
  26. cloudcontrollers "k8s.io/kubernetes/pkg/controller/cloud"
  27. routecontroller "k8s.io/kubernetes/pkg/controller/route"
  28. servicecontroller "k8s.io/kubernetes/pkg/controller/service"
  29. netutils "k8s.io/utils/net"
  30. utilfeature "k8s.io/apiserver/pkg/util/feature"
  31. kubefeatures "k8s.io/kubernetes/pkg/features"
  32. )
  33. func startCloudNodeController(ctx *cloudcontrollerconfig.CompletedConfig, cloud cloudprovider.Interface, stopCh <-chan struct{}) (http.Handler, bool, error) {
  34. // Start the CloudNodeController
  35. nodeController, err := cloudcontrollers.NewCloudNodeController(
  36. ctx.SharedInformers.Core().V1().Nodes(),
  37. // cloud node controller uses existing cluster role from node-controller
  38. ctx.ClientBuilder.ClientOrDie("node-controller"),
  39. cloud,
  40. ctx.ComponentConfig.NodeStatusUpdateFrequency.Duration,
  41. )
  42. if err != nil {
  43. klog.Warningf("failed to start cloud node controller: %s", err)
  44. return nil, false, nil
  45. }
  46. go nodeController.Run(stopCh)
  47. return nil, true, nil
  48. }
  49. func startCloudNodeLifecycleController(ctx *cloudcontrollerconfig.CompletedConfig, cloud cloudprovider.Interface, stopCh <-chan struct{}) (http.Handler, bool, error) {
  50. // Start the cloudNodeLifecycleController
  51. cloudNodeLifecycleController, err := cloudcontrollers.NewCloudNodeLifecycleController(
  52. ctx.SharedInformers.Core().V1().Nodes(),
  53. // cloud node lifecycle controller uses existing cluster role from node-controller
  54. ctx.ClientBuilder.ClientOrDie("node-controller"),
  55. cloud,
  56. ctx.ComponentConfig.KubeCloudShared.NodeMonitorPeriod.Duration,
  57. )
  58. if err != nil {
  59. klog.Warningf("failed to start cloud node lifecycle controller: %s", err)
  60. return nil, false, nil
  61. }
  62. go cloudNodeLifecycleController.Run(stopCh)
  63. return nil, true, nil
  64. }
  65. func startServiceController(ctx *cloudcontrollerconfig.CompletedConfig, cloud cloudprovider.Interface, stopCh <-chan struct{}) (http.Handler, bool, error) {
  66. // Start the service controller
  67. serviceController, err := servicecontroller.New(
  68. cloud,
  69. ctx.ClientBuilder.ClientOrDie("service-controller"),
  70. ctx.SharedInformers.Core().V1().Services(),
  71. ctx.SharedInformers.Core().V1().Nodes(),
  72. ctx.ComponentConfig.KubeCloudShared.ClusterName,
  73. )
  74. if err != nil {
  75. // This error shouldn't fail. It lives like this as a legacy.
  76. klog.Errorf("Failed to start service controller: %v", err)
  77. return nil, false, nil
  78. }
  79. go serviceController.Run(stopCh, int(ctx.ComponentConfig.ServiceController.ConcurrentServiceSyncs))
  80. return nil, true, nil
  81. }
  82. func startRouteController(ctx *cloudcontrollerconfig.CompletedConfig, cloud cloudprovider.Interface, stopCh <-chan struct{}) (http.Handler, bool, error) {
  83. if !ctx.ComponentConfig.KubeCloudShared.AllocateNodeCIDRs || !ctx.ComponentConfig.KubeCloudShared.ConfigureCloudRoutes {
  84. 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)
  85. return nil, false, nil
  86. }
  87. // If CIDRs should be allocated for pods and set on the CloudProvider, then start the route controller
  88. routes, ok := cloud.Routes()
  89. if !ok {
  90. klog.Warning("configure-cloud-routes is set, but cloud provider does not support routes. Will not configure cloud provider routes.")
  91. return nil, false, nil
  92. }
  93. // failure: bad cidrs in config
  94. clusterCIDRs, dualStack, err := processCIDRs(ctx.ComponentConfig.KubeCloudShared.ClusterCIDR)
  95. if err != nil {
  96. return nil, false, err
  97. }
  98. // failure: more than one cidr and dual stack is not enabled
  99. if len(clusterCIDRs) > 1 && !utilfeature.DefaultFeatureGate.Enabled(kubefeatures.IPv6DualStack) {
  100. return nil, false, fmt.Errorf("len of ClusterCIDRs==%v and dualstack feature is not enabled", len(clusterCIDRs))
  101. }
  102. // failure: more than one cidr but they are not configured as dual stack
  103. if len(clusterCIDRs) > 1 && !dualStack {
  104. return nil, false, fmt.Errorf("len of ClusterCIDRs==%v and they are not configured as dual stack (at least one from each IPFamily", len(clusterCIDRs))
  105. }
  106. // failure: more than cidrs is not allowed even with dual stack
  107. if len(clusterCIDRs) > 2 {
  108. return nil, false, fmt.Errorf("length of clusterCIDRs is:%v more than max allowed of 2", len(clusterCIDRs))
  109. }
  110. routeController := routecontroller.New(
  111. routes,
  112. ctx.ClientBuilder.ClientOrDie("route-controller"),
  113. ctx.SharedInformers.Core().V1().Nodes(),
  114. ctx.ComponentConfig.KubeCloudShared.ClusterName,
  115. clusterCIDRs,
  116. )
  117. go routeController.Run(stopCh, ctx.ComponentConfig.KubeCloudShared.RouteReconciliationPeriod.Duration)
  118. return nil, true, nil
  119. }
  120. // processCIDRs is a helper function that works on a comma separated cidrs and returns
  121. // a list of typed cidrs
  122. // a flag if cidrs represents a dual stack
  123. // error if failed to parse any of the cidrs
  124. func processCIDRs(cidrsList string) ([]*net.IPNet, bool, error) {
  125. cidrsSplit := strings.Split(strings.TrimSpace(cidrsList), ",")
  126. cidrs, err := netutils.ParseCIDRs(cidrsSplit)
  127. if err != nil {
  128. return nil, false, err
  129. }
  130. // if cidrs has an error then the previous call will fail
  131. // safe to ignore error checking on next call
  132. dualstack, _ := netutils.IsDualStackCIDRs(cidrs)
  133. return cidrs, dualstack, nil
  134. }