controller.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. Copyright 2014 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 master
  14. import (
  15. "fmt"
  16. "net"
  17. "time"
  18. "github.com/golang/glog"
  19. "k8s.io/kubernetes/pkg/api"
  20. "k8s.io/kubernetes/pkg/api/endpoints"
  21. "k8s.io/kubernetes/pkg/api/errors"
  22. "k8s.io/kubernetes/pkg/api/rest"
  23. "k8s.io/kubernetes/pkg/registry/endpoint"
  24. "k8s.io/kubernetes/pkg/registry/namespace"
  25. "k8s.io/kubernetes/pkg/registry/rangeallocation"
  26. "k8s.io/kubernetes/pkg/registry/service"
  27. servicecontroller "k8s.io/kubernetes/pkg/registry/service/ipallocator/controller"
  28. portallocatorcontroller "k8s.io/kubernetes/pkg/registry/service/portallocator/controller"
  29. "k8s.io/kubernetes/pkg/util/async"
  30. "k8s.io/kubernetes/pkg/util/intstr"
  31. utilnet "k8s.io/kubernetes/pkg/util/net"
  32. "k8s.io/kubernetes/pkg/util/runtime"
  33. "k8s.io/kubernetes/pkg/util/wait"
  34. )
  35. // Controller is the controller manager for the core bootstrap Kubernetes controller
  36. // loops, which manage creating the "kubernetes" service, the "default" and "kube-system"
  37. // namespace, and provide the IP repair check on service IPs
  38. type Controller struct {
  39. NamespaceRegistry namespace.Registry
  40. ServiceRegistry service.Registry
  41. ServiceClusterIPRegistry rangeallocation.RangeRegistry
  42. ServiceClusterIPInterval time.Duration
  43. ServiceClusterIPRange *net.IPNet
  44. ServiceNodePortRegistry rangeallocation.RangeRegistry
  45. ServiceNodePortInterval time.Duration
  46. ServiceNodePortRange utilnet.PortRange
  47. EndpointReconciler EndpointReconciler
  48. EndpointInterval time.Duration
  49. SystemNamespaces []string
  50. SystemNamespacesInterval time.Duration
  51. PublicIP net.IP
  52. ServiceIP net.IP
  53. ServicePort int
  54. ExtraServicePorts []api.ServicePort
  55. ExtraEndpointPorts []api.EndpointPort
  56. PublicServicePort int
  57. KubernetesServiceNodePort int
  58. runner *async.Runner
  59. }
  60. // Start begins the core controller loops that must exist for bootstrapping
  61. // a cluster.
  62. func (c *Controller) Start() {
  63. if c.runner != nil {
  64. return
  65. }
  66. repairClusterIPs := servicecontroller.NewRepair(c.ServiceClusterIPInterval, c.ServiceRegistry, c.ServiceClusterIPRange, c.ServiceClusterIPRegistry)
  67. repairNodePorts := portallocatorcontroller.NewRepair(c.ServiceNodePortInterval, c.ServiceRegistry, c.ServiceNodePortRange, c.ServiceNodePortRegistry)
  68. // run all of the controllers once prior to returning from Start.
  69. if err := repairClusterIPs.RunOnce(); err != nil {
  70. // If we fail to repair cluster IPs apiserver is useless. We should restart and retry.
  71. glog.Fatalf("Unable to perform initial IP allocation check: %v", err)
  72. }
  73. if err := repairNodePorts.RunOnce(); err != nil {
  74. // If we fail to repair node ports apiserver is useless. We should restart and retry.
  75. glog.Fatalf("Unable to perform initial service nodePort check: %v", err)
  76. }
  77. // Service definition is reconciled during first run to correct port and type per expectations.
  78. if err := c.UpdateKubernetesService(true); err != nil {
  79. glog.Errorf("Unable to perform initial Kubernetes service initialization: %v", err)
  80. }
  81. c.runner = async.NewRunner(c.RunKubernetesNamespaces, c.RunKubernetesService, repairClusterIPs.RunUntil, repairNodePorts.RunUntil)
  82. c.runner.Start()
  83. }
  84. // RunKubernetesNamespaces periodically makes sure that all internal namespaces exist
  85. func (c *Controller) RunKubernetesNamespaces(ch chan struct{}) {
  86. wait.Until(func() {
  87. // Loop the system namespace list, and create them if they do not exist
  88. for _, ns := range c.SystemNamespaces {
  89. if err := c.CreateNamespaceIfNeeded(ns); err != nil {
  90. runtime.HandleError(fmt.Errorf("unable to create required kubernetes system namespace %s: %v", ns, err))
  91. }
  92. }
  93. }, c.SystemNamespacesInterval, ch)
  94. }
  95. // RunKubernetesService periodically updates the kubernetes service
  96. func (c *Controller) RunKubernetesService(ch chan struct{}) {
  97. wait.Until(func() {
  98. // Service definition is not reconciled after first
  99. // run, ports and type will be corrected only during
  100. // start.
  101. if err := c.UpdateKubernetesService(false); err != nil {
  102. runtime.HandleError(fmt.Errorf("unable to sync kubernetes service: %v", err))
  103. }
  104. }, c.EndpointInterval, ch)
  105. }
  106. // UpdateKubernetesService attempts to update the default Kube service.
  107. func (c *Controller) UpdateKubernetesService(reconcile bool) error {
  108. // Update service & endpoint records.
  109. // TODO: when it becomes possible to change this stuff,
  110. // stop polling and start watching.
  111. // TODO: add endpoints of all replicas, not just the elected master.
  112. if err := c.CreateNamespaceIfNeeded(api.NamespaceDefault); err != nil {
  113. return err
  114. }
  115. if c.ServiceIP != nil {
  116. servicePorts, serviceType := createPortAndServiceSpec(c.ServicePort, c.KubernetesServiceNodePort, "https", c.ExtraServicePorts)
  117. if err := c.CreateOrUpdateMasterServiceIfNeeded("kubernetes", c.ServiceIP, servicePorts, serviceType, reconcile); err != nil {
  118. return err
  119. }
  120. endpointPorts := createEndpointPortSpec(c.PublicServicePort, "https", c.ExtraEndpointPorts)
  121. if err := c.EndpointReconciler.ReconcileEndpoints("kubernetes", c.PublicIP, endpointPorts, reconcile); err != nil {
  122. return err
  123. }
  124. }
  125. return nil
  126. }
  127. // CreateNamespaceIfNeeded will create a namespace if it doesn't already exist
  128. func (c *Controller) CreateNamespaceIfNeeded(ns string) error {
  129. ctx := api.NewContext()
  130. if _, err := c.NamespaceRegistry.GetNamespace(ctx, ns); err == nil {
  131. // the namespace already exists
  132. return nil
  133. }
  134. newNs := &api.Namespace{
  135. ObjectMeta: api.ObjectMeta{
  136. Name: ns,
  137. Namespace: "",
  138. },
  139. }
  140. err := c.NamespaceRegistry.CreateNamespace(ctx, newNs)
  141. if err != nil && errors.IsAlreadyExists(err) {
  142. err = nil
  143. }
  144. return err
  145. }
  146. // createPortAndServiceSpec creates an array of service ports.
  147. // If the NodePort value is 0, just the servicePort is used, otherwise, a node port is exposed.
  148. func createPortAndServiceSpec(servicePort int, nodePort int, servicePortName string, extraServicePorts []api.ServicePort) ([]api.ServicePort, api.ServiceType) {
  149. //Use the Cluster IP type for the service port if NodePort isn't provided.
  150. //Otherwise, we will be binding the master service to a NodePort.
  151. servicePorts := []api.ServicePort{{Protocol: api.ProtocolTCP,
  152. Port: int32(servicePort),
  153. Name: servicePortName,
  154. TargetPort: intstr.FromInt(servicePort)}}
  155. serviceType := api.ServiceTypeClusterIP
  156. if nodePort > 0 {
  157. servicePorts[0].NodePort = int32(nodePort)
  158. serviceType = api.ServiceTypeNodePort
  159. }
  160. if extraServicePorts != nil {
  161. servicePorts = append(servicePorts, extraServicePorts...)
  162. }
  163. return servicePorts, serviceType
  164. }
  165. // createEndpointPortSpec creates an array of endpoint ports
  166. func createEndpointPortSpec(endpointPort int, endpointPortName string, extraEndpointPorts []api.EndpointPort) []api.EndpointPort {
  167. endpointPorts := []api.EndpointPort{{Protocol: api.ProtocolTCP,
  168. Port: int32(endpointPort),
  169. Name: endpointPortName,
  170. }}
  171. if extraEndpointPorts != nil {
  172. endpointPorts = append(endpointPorts, extraEndpointPorts...)
  173. }
  174. return endpointPorts
  175. }
  176. // CreateMasterServiceIfNeeded will create the specified service if it
  177. // doesn't already exist.
  178. func (c *Controller) CreateOrUpdateMasterServiceIfNeeded(serviceName string, serviceIP net.IP, servicePorts []api.ServicePort, serviceType api.ServiceType, reconcile bool) error {
  179. ctx := api.NewDefaultContext()
  180. if s, err := c.ServiceRegistry.GetService(ctx, serviceName); err == nil {
  181. // The service already exists.
  182. if reconcile {
  183. if svc, updated := getMasterServiceUpdateIfNeeded(s, servicePorts, serviceType); updated {
  184. glog.Warningf("Resetting master service %q to %#v", serviceName, svc)
  185. _, err := c.ServiceRegistry.UpdateService(ctx, svc)
  186. return err
  187. }
  188. }
  189. return nil
  190. }
  191. svc := &api.Service{
  192. ObjectMeta: api.ObjectMeta{
  193. Name: serviceName,
  194. Namespace: api.NamespaceDefault,
  195. Labels: map[string]string{"provider": "kubernetes", "component": "apiserver"},
  196. },
  197. Spec: api.ServiceSpec{
  198. Ports: servicePorts,
  199. // maintained by this code, not by the pod selector
  200. Selector: nil,
  201. ClusterIP: serviceIP.String(),
  202. SessionAffinity: api.ServiceAffinityClientIP,
  203. Type: serviceType,
  204. },
  205. }
  206. if err := rest.BeforeCreate(service.Strategy, ctx, svc); err != nil {
  207. return err
  208. }
  209. _, err := c.ServiceRegistry.CreateService(ctx, svc)
  210. if err != nil && errors.IsAlreadyExists(err) {
  211. err = nil
  212. }
  213. return err
  214. }
  215. // EndpointReconciler knows how to reconcile the endpoints for the apiserver service.
  216. type EndpointReconciler interface {
  217. // ReconcileEndpoints sets the endpoints for the given apiserver service (ro or rw).
  218. // ReconcileEndpoints expects that the endpoints objects it manages will all be
  219. // managed only by ReconcileEndpoints; therefore, to understand this, you need only
  220. // understand the requirements.
  221. //
  222. // Requirements:
  223. // * All apiservers MUST use the same ports for their {rw, ro} services.
  224. // * All apiservers MUST use ReconcileEndpoints and only ReconcileEndpoints to manage the
  225. // endpoints for their {rw, ro} services.
  226. // * ReconcileEndpoints is called periodically from all apiservers.
  227. ReconcileEndpoints(serviceName string, ip net.IP, endpointPorts []api.EndpointPort, reconcilePorts bool) error
  228. }
  229. // masterCountEndpointReconciler reconciles endpoints based on a specified expected number of
  230. // masters. masterCountEndpointReconciler implements EndpointReconciler.
  231. type masterCountEndpointReconciler struct {
  232. masterCount int
  233. endpointRegistry endpoint.Registry
  234. }
  235. var _ EndpointReconciler = &masterCountEndpointReconciler{}
  236. // NewMasterCountEndpointReconciler creates a new EndpointReconciler that reconciles based on a
  237. // specified expected number of masters.
  238. func NewMasterCountEndpointReconciler(masterCount int, endpointRegistry endpoint.Registry) *masterCountEndpointReconciler {
  239. return &masterCountEndpointReconciler{
  240. masterCount: masterCount,
  241. endpointRegistry: endpointRegistry,
  242. }
  243. }
  244. // ReconcileEndpoints sets the endpoints for the given apiserver service (ro or rw).
  245. // ReconcileEndpoints expects that the endpoints objects it manages will all be
  246. // managed only by ReconcileEndpoints; therefore, to understand this, you need only
  247. // understand the requirements and the body of this function.
  248. //
  249. // Requirements:
  250. // * All apiservers MUST use the same ports for their {rw, ro} services.
  251. // * All apiservers MUST use ReconcileEndpoints and only ReconcileEndpoints to manage the
  252. // endpoints for their {rw, ro} services.
  253. // * All apiservers MUST know and agree on the number of apiservers expected
  254. // to be running (c.masterCount).
  255. // * ReconcileEndpoints is called periodically from all apiservers.
  256. func (r *masterCountEndpointReconciler) ReconcileEndpoints(serviceName string, ip net.IP, endpointPorts []api.EndpointPort, reconcilePorts bool) error {
  257. ctx := api.NewDefaultContext()
  258. e, err := r.endpointRegistry.GetEndpoints(ctx, serviceName)
  259. if err != nil {
  260. e = &api.Endpoints{
  261. ObjectMeta: api.ObjectMeta{
  262. Name: serviceName,
  263. Namespace: api.NamespaceDefault,
  264. },
  265. }
  266. }
  267. if errors.IsNotFound(err) {
  268. // Simply create non-existing endpoints for the service.
  269. e.Subsets = []api.EndpointSubset{{
  270. Addresses: []api.EndpointAddress{{IP: ip.String()}},
  271. Ports: endpointPorts,
  272. }}
  273. return r.endpointRegistry.UpdateEndpoints(ctx, e)
  274. }
  275. // First, determine if the endpoint is in the format we expect (one
  276. // subset, ports matching endpointPorts, N IP addresses).
  277. formatCorrect, ipCorrect, portsCorrect := checkEndpointSubsetFormat(e, ip.String(), endpointPorts, r.masterCount, reconcilePorts)
  278. if !formatCorrect {
  279. // Something is egregiously wrong, just re-make the endpoints record.
  280. e.Subsets = []api.EndpointSubset{{
  281. Addresses: []api.EndpointAddress{{IP: ip.String()}},
  282. Ports: endpointPorts,
  283. }}
  284. glog.Warningf("Resetting endpoints for master service %q to %#v", serviceName, e)
  285. return r.endpointRegistry.UpdateEndpoints(ctx, e)
  286. }
  287. if ipCorrect && portsCorrect {
  288. return nil
  289. }
  290. if !ipCorrect {
  291. // We *always* add our own IP address.
  292. e.Subsets[0].Addresses = append(e.Subsets[0].Addresses, api.EndpointAddress{IP: ip.String()})
  293. // Lexicographic order is retained by this step.
  294. e.Subsets = endpoints.RepackSubsets(e.Subsets)
  295. // If too many IP addresses, remove the ones lexicographically after our
  296. // own IP address. Given the requirements stated at the top of
  297. // this function, this should cause the list of IP addresses to
  298. // become eventually correct.
  299. if addrs := &e.Subsets[0].Addresses; len(*addrs) > r.masterCount {
  300. // addrs is a pointer because we're going to mutate it.
  301. for i, addr := range *addrs {
  302. if addr.IP == ip.String() {
  303. for len(*addrs) > r.masterCount {
  304. // wrap around if necessary.
  305. remove := (i + 1) % len(*addrs)
  306. *addrs = append((*addrs)[:remove], (*addrs)[remove+1:]...)
  307. }
  308. break
  309. }
  310. }
  311. }
  312. }
  313. if !portsCorrect {
  314. // Reset ports.
  315. e.Subsets[0].Ports = endpointPorts
  316. }
  317. glog.Warningf("Resetting endpoints for master service %q to %v", serviceName, e)
  318. return r.endpointRegistry.UpdateEndpoints(ctx, e)
  319. }
  320. // Determine if the endpoint is in the format ReconcileEndpoints expects.
  321. //
  322. // Return values:
  323. // * formatCorrect is true if exactly one subset is found.
  324. // * ipCorrect is true when current master's IP is found and the number
  325. // of addresses is less than or equal to the master count.
  326. // * portsCorrect is true when endpoint ports exactly match provided ports.
  327. // portsCorrect is only evaluated when reconcilePorts is set to true.
  328. func checkEndpointSubsetFormat(e *api.Endpoints, ip string, ports []api.EndpointPort, count int, reconcilePorts bool) (formatCorrect bool, ipCorrect bool, portsCorrect bool) {
  329. if len(e.Subsets) != 1 {
  330. return false, false, false
  331. }
  332. sub := &e.Subsets[0]
  333. portsCorrect = true
  334. if reconcilePorts {
  335. if len(sub.Ports) != len(ports) {
  336. portsCorrect = false
  337. }
  338. for i, port := range ports {
  339. if len(sub.Ports) <= i || port != sub.Ports[i] {
  340. portsCorrect = false
  341. break
  342. }
  343. }
  344. }
  345. for _, addr := range sub.Addresses {
  346. if addr.IP == ip {
  347. ipCorrect = len(sub.Addresses) <= count
  348. break
  349. }
  350. }
  351. return true, ipCorrect, portsCorrect
  352. }
  353. // * getMasterServiceUpdateIfNeeded sets service attributes for the
  354. // given apiserver service.
  355. // * getMasterServiceUpdateIfNeeded expects that the service object it
  356. // manages will be managed only by getMasterServiceUpdateIfNeeded;
  357. // therefore, to understand this, you need only understand the
  358. // requirements and the body of this function.
  359. // * getMasterServiceUpdateIfNeeded ensures that the correct ports are
  360. // are set.
  361. //
  362. // Requirements:
  363. // * All apiservers MUST use getMasterServiceUpdateIfNeeded and only
  364. // getMasterServiceUpdateIfNeeded to manage service attributes
  365. // * updateMasterService is called periodically from all apiservers.
  366. func getMasterServiceUpdateIfNeeded(svc *api.Service, servicePorts []api.ServicePort, serviceType api.ServiceType) (s *api.Service, updated bool) {
  367. // Determine if the service is in the format we expect
  368. // (servicePorts are present and service type matches)
  369. formatCorrect := checkServiceFormat(svc, servicePorts, serviceType)
  370. if formatCorrect {
  371. return svc, false
  372. }
  373. svc.Spec.Ports = servicePorts
  374. svc.Spec.Type = serviceType
  375. return svc, true
  376. }
  377. // Determine if the service is in the correct format
  378. // getMasterServiceUpdateIfNeeded expects (servicePorts are correct
  379. // and service type matches).
  380. func checkServiceFormat(s *api.Service, ports []api.ServicePort, serviceType api.ServiceType) (formatCorrect bool) {
  381. if s.Spec.Type != serviceType {
  382. return false
  383. }
  384. if len(ports) != len(s.Spec.Ports) {
  385. return false
  386. }
  387. for i, port := range ports {
  388. if port != s.Spec.Ports[i] {
  389. return false
  390. }
  391. }
  392. return true
  393. }