deployment_controller.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*
  2. Copyright 2015 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 deployment contains all the logic for handling Kubernetes Deployments.
  14. // It implements a set of strategies (rolling, recreate) for deploying an application,
  15. // the means to rollback to previous versions, proportional scaling for mitigating
  16. // risk, cleanup policy, and other useful features of Deployments.
  17. package deployment
  18. import (
  19. "fmt"
  20. "reflect"
  21. "sort"
  22. "time"
  23. "github.com/golang/glog"
  24. "k8s.io/kubernetes/pkg/api"
  25. "k8s.io/kubernetes/pkg/api/unversioned"
  26. "k8s.io/kubernetes/pkg/apis/extensions"
  27. "k8s.io/kubernetes/pkg/client/cache"
  28. clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
  29. unversionedcore "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/unversioned"
  30. "k8s.io/kubernetes/pkg/client/record"
  31. "k8s.io/kubernetes/pkg/controller"
  32. "k8s.io/kubernetes/pkg/controller/deployment/util"
  33. "k8s.io/kubernetes/pkg/controller/framework"
  34. "k8s.io/kubernetes/pkg/labels"
  35. "k8s.io/kubernetes/pkg/runtime"
  36. "k8s.io/kubernetes/pkg/util/metrics"
  37. utilruntime "k8s.io/kubernetes/pkg/util/runtime"
  38. "k8s.io/kubernetes/pkg/util/wait"
  39. "k8s.io/kubernetes/pkg/util/workqueue"
  40. "k8s.io/kubernetes/pkg/watch"
  41. )
  42. const (
  43. // FullDeploymentResyncPeriod means we'll attempt to recompute the required replicas
  44. // of all deployments.
  45. // This recomputation happens based on contents in the local caches.
  46. FullDeploymentResyncPeriod = 30 * time.Second
  47. // We must avoid creating new replica set / counting pods until the replica set / pods store has synced.
  48. // If it hasn't synced, to avoid a hot loop, we'll wait this long between checks.
  49. StoreSyncedPollPeriod = 100 * time.Millisecond
  50. // MaxRetries is the number of times a deployment will be retried before it is dropped out of the queue.
  51. MaxRetries = 5
  52. )
  53. // DeploymentController is responsible for synchronizing Deployment objects stored
  54. // in the system with actual running replica sets and pods.
  55. type DeploymentController struct {
  56. client clientset.Interface
  57. eventRecorder record.EventRecorder
  58. // To allow injection of syncDeployment for testing.
  59. syncHandler func(dKey string) error
  60. // A store of deployments, populated by the dController
  61. dStore cache.StoreToDeploymentLister
  62. // Watches changes to all deployments
  63. dController *framework.Controller
  64. // A store of ReplicaSets, populated by the rsController
  65. rsStore cache.StoreToReplicaSetLister
  66. // Watches changes to all ReplicaSets
  67. rsController *framework.Controller
  68. // A store of pods, populated by the podController
  69. podStore cache.StoreToPodLister
  70. // Watches changes to all pods
  71. podController *framework.Controller
  72. // dStoreSynced returns true if the Deployment store has been synced at least once.
  73. // Added as a member to the struct to allow injection for testing.
  74. dStoreSynced func() bool
  75. // rsStoreSynced returns true if the ReplicaSet store has been synced at least once.
  76. // Added as a member to the struct to allow injection for testing.
  77. rsStoreSynced func() bool
  78. // podStoreSynced returns true if the pod store has been synced at least once.
  79. // Added as a member to the struct to allow injection for testing.
  80. podStoreSynced func() bool
  81. // Deployments that need to be synced
  82. queue workqueue.RateLimitingInterface
  83. }
  84. // NewDeploymentController creates a new DeploymentController.
  85. func NewDeploymentController(client clientset.Interface, resyncPeriod controller.ResyncPeriodFunc) *DeploymentController {
  86. eventBroadcaster := record.NewBroadcaster()
  87. eventBroadcaster.StartLogging(glog.Infof)
  88. // TODO: remove the wrapper when every clients have moved to use the clientset.
  89. eventBroadcaster.StartRecordingToSink(&unversionedcore.EventSinkImpl{Interface: client.Core().Events("")})
  90. if client != nil && client.Core().GetRESTClient().GetRateLimiter() != nil {
  91. metrics.RegisterMetricAndTrackRateLimiterUsage("deployment_controller", client.Core().GetRESTClient().GetRateLimiter())
  92. }
  93. dc := &DeploymentController{
  94. client: client,
  95. eventRecorder: eventBroadcaster.NewRecorder(api.EventSource{Component: "deployment-controller"}),
  96. queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "deployment"),
  97. }
  98. dc.dStore.Indexer, dc.dController = framework.NewIndexerInformer(
  99. &cache.ListWatch{
  100. ListFunc: func(options api.ListOptions) (runtime.Object, error) {
  101. return dc.client.Extensions().Deployments(api.NamespaceAll).List(options)
  102. },
  103. WatchFunc: func(options api.ListOptions) (watch.Interface, error) {
  104. return dc.client.Extensions().Deployments(api.NamespaceAll).Watch(options)
  105. },
  106. },
  107. &extensions.Deployment{},
  108. FullDeploymentResyncPeriod,
  109. framework.ResourceEventHandlerFuncs{
  110. AddFunc: dc.addDeploymentNotification,
  111. UpdateFunc: dc.updateDeploymentNotification,
  112. // This will enter the sync loop and no-op, because the deployment has been deleted from the store.
  113. DeleteFunc: dc.deleteDeploymentNotification,
  114. },
  115. cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
  116. )
  117. dc.rsStore.Store, dc.rsController = framework.NewInformer(
  118. &cache.ListWatch{
  119. ListFunc: func(options api.ListOptions) (runtime.Object, error) {
  120. return dc.client.Extensions().ReplicaSets(api.NamespaceAll).List(options)
  121. },
  122. WatchFunc: func(options api.ListOptions) (watch.Interface, error) {
  123. return dc.client.Extensions().ReplicaSets(api.NamespaceAll).Watch(options)
  124. },
  125. },
  126. &extensions.ReplicaSet{},
  127. resyncPeriod(),
  128. framework.ResourceEventHandlerFuncs{
  129. AddFunc: dc.addReplicaSet,
  130. UpdateFunc: dc.updateReplicaSet,
  131. DeleteFunc: dc.deleteReplicaSet,
  132. },
  133. )
  134. dc.podStore.Indexer, dc.podController = framework.NewIndexerInformer(
  135. &cache.ListWatch{
  136. ListFunc: func(options api.ListOptions) (runtime.Object, error) {
  137. return dc.client.Core().Pods(api.NamespaceAll).List(options)
  138. },
  139. WatchFunc: func(options api.ListOptions) (watch.Interface, error) {
  140. return dc.client.Core().Pods(api.NamespaceAll).Watch(options)
  141. },
  142. },
  143. &api.Pod{},
  144. resyncPeriod(),
  145. framework.ResourceEventHandlerFuncs{
  146. AddFunc: dc.addPod,
  147. UpdateFunc: dc.updatePod,
  148. DeleteFunc: dc.deletePod,
  149. },
  150. cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
  151. )
  152. dc.syncHandler = dc.syncDeployment
  153. dc.dStoreSynced = dc.dController.HasSynced
  154. dc.rsStoreSynced = dc.rsController.HasSynced
  155. dc.podStoreSynced = dc.podController.HasSynced
  156. return dc
  157. }
  158. // Run begins watching and syncing.
  159. func (dc *DeploymentController) Run(workers int, stopCh <-chan struct{}) {
  160. defer utilruntime.HandleCrash()
  161. go dc.dController.Run(stopCh)
  162. go dc.rsController.Run(stopCh)
  163. go dc.podController.Run(stopCh)
  164. // Wait for the rc and dc stores to sync before starting any work in this controller.
  165. ready := make(chan struct{})
  166. go dc.waitForSyncedStores(ready, stopCh)
  167. select {
  168. case <-ready:
  169. case <-stopCh:
  170. return
  171. }
  172. for i := 0; i < workers; i++ {
  173. go wait.Until(dc.worker, time.Second, stopCh)
  174. }
  175. <-stopCh
  176. glog.Infof("Shutting down deployment controller")
  177. dc.queue.ShutDown()
  178. }
  179. func (dc *DeploymentController) waitForSyncedStores(ready chan<- struct{}, stopCh <-chan struct{}) {
  180. defer utilruntime.HandleCrash()
  181. for !dc.dStoreSynced() || !dc.rsStoreSynced() || !dc.podStoreSynced() {
  182. select {
  183. case <-time.After(StoreSyncedPollPeriod):
  184. case <-stopCh:
  185. return
  186. }
  187. }
  188. close(ready)
  189. }
  190. func (dc *DeploymentController) addDeploymentNotification(obj interface{}) {
  191. d := obj.(*extensions.Deployment)
  192. glog.V(4).Infof("Adding deployment %s", d.Name)
  193. dc.enqueueDeployment(d)
  194. }
  195. func (dc *DeploymentController) updateDeploymentNotification(old, cur interface{}) {
  196. oldD := old.(*extensions.Deployment)
  197. glog.V(4).Infof("Updating deployment %s", oldD.Name)
  198. // Resync on deployment object relist.
  199. dc.enqueueDeployment(cur.(*extensions.Deployment))
  200. }
  201. func (dc *DeploymentController) deleteDeploymentNotification(obj interface{}) {
  202. d, ok := obj.(*extensions.Deployment)
  203. if !ok {
  204. tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
  205. if !ok {
  206. glog.Errorf("Couldn't get object from tombstone %#v", obj)
  207. return
  208. }
  209. d, ok = tombstone.Obj.(*extensions.Deployment)
  210. if !ok {
  211. glog.Errorf("Tombstone contained object that is not a Deployment %#v", obj)
  212. return
  213. }
  214. }
  215. glog.V(4).Infof("Deleting deployment %s", d.Name)
  216. dc.enqueueDeployment(d)
  217. }
  218. // addReplicaSet enqueues the deployment that manages a ReplicaSet when the ReplicaSet is created.
  219. func (dc *DeploymentController) addReplicaSet(obj interface{}) {
  220. rs := obj.(*extensions.ReplicaSet)
  221. glog.V(4).Infof("ReplicaSet %s added.", rs.Name)
  222. if d := dc.getDeploymentForReplicaSet(rs); d != nil {
  223. dc.enqueueDeployment(d)
  224. }
  225. }
  226. // getDeploymentForReplicaSet returns the deployment managing the given ReplicaSet.
  227. func (dc *DeploymentController) getDeploymentForReplicaSet(rs *extensions.ReplicaSet) *extensions.Deployment {
  228. deployments, err := dc.dStore.GetDeploymentsForReplicaSet(rs)
  229. if err != nil || len(deployments) == 0 {
  230. glog.V(4).Infof("Error: %v. No deployment found for ReplicaSet %v, deployment controller will avoid syncing.", err, rs.Name)
  231. return nil
  232. }
  233. // Because all ReplicaSet's belonging to a deployment should have a unique label key,
  234. // there should never be more than one deployment returned by the above method.
  235. // If that happens we should probably dynamically repair the situation by ultimately
  236. // trying to clean up one of the controllers, for now we just return the older one
  237. if len(deployments) > 1 {
  238. sort.Sort(util.BySelectorLastUpdateTime(deployments))
  239. glog.Errorf("user error! more than one deployment is selecting replica set %s/%s with labels: %#v, returning %s/%s", rs.Namespace, rs.Name, rs.Labels, deployments[0].Namespace, deployments[0].Name)
  240. }
  241. return &deployments[0]
  242. }
  243. // updateReplicaSet figures out what deployment(s) manage a ReplicaSet when the ReplicaSet
  244. // is updated and wake them up. If the anything of the ReplicaSets have changed, we need to
  245. // awaken both the old and new deployments. old and cur must be *extensions.ReplicaSet
  246. // types.
  247. func (dc *DeploymentController) updateReplicaSet(old, cur interface{}) {
  248. curRS := cur.(*extensions.ReplicaSet)
  249. oldRS := old.(*extensions.ReplicaSet)
  250. if curRS.ResourceVersion == oldRS.ResourceVersion {
  251. // Periodic resync will send update events for all known replica sets.
  252. // Two different versions of the same replica set will always have different RVs.
  253. return
  254. }
  255. // TODO: Write a unittest for this case
  256. glog.V(4).Infof("ReplicaSet %s updated.", curRS.Name)
  257. if d := dc.getDeploymentForReplicaSet(curRS); d != nil {
  258. dc.enqueueDeployment(d)
  259. }
  260. // A number of things could affect the old deployment: labels changing,
  261. // pod template changing, etc.
  262. if !api.Semantic.DeepEqual(oldRS, curRS) {
  263. if oldD := dc.getDeploymentForReplicaSet(oldRS); oldD != nil {
  264. dc.enqueueDeployment(oldD)
  265. }
  266. }
  267. }
  268. // deleteReplicaSet enqueues the deployment that manages a ReplicaSet when
  269. // the ReplicaSet is deleted. obj could be an *extensions.ReplicaSet, or
  270. // a DeletionFinalStateUnknown marker item.
  271. func (dc *DeploymentController) deleteReplicaSet(obj interface{}) {
  272. rs, ok := obj.(*extensions.ReplicaSet)
  273. // When a delete is dropped, the relist will notice a pod in the store not
  274. // in the list, leading to the insertion of a tombstone object which contains
  275. // the deleted key/value. Note that this value might be stale. If the ReplicaSet
  276. // changed labels the new deployment will not be woken up till the periodic resync.
  277. if !ok {
  278. tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
  279. if !ok {
  280. glog.Errorf("Couldn't get object from tombstone %#v, could take up to %v before a deployment recreates/updates replicasets", obj, FullDeploymentResyncPeriod)
  281. return
  282. }
  283. rs, ok = tombstone.Obj.(*extensions.ReplicaSet)
  284. if !ok {
  285. glog.Errorf("Tombstone contained object that is not a ReplicaSet %#v, could take up to %v before a deployment recreates/updates replicasets", obj, FullDeploymentResyncPeriod)
  286. return
  287. }
  288. }
  289. glog.V(4).Infof("ReplicaSet %s deleted.", rs.Name)
  290. if d := dc.getDeploymentForReplicaSet(rs); d != nil {
  291. dc.enqueueDeployment(d)
  292. }
  293. }
  294. // getDeploymentForPod returns the deployment that manages the given Pod.
  295. // If there are multiple deployments for a given Pod, only return the oldest one.
  296. func (dc *DeploymentController) getDeploymentForPod(pod *api.Pod) *extensions.Deployment {
  297. deployments, err := dc.dStore.GetDeploymentsForPod(pod)
  298. if err != nil || len(deployments) == 0 {
  299. glog.V(4).Infof("Error: %v. No deployment found for Pod %v, deployment controller will avoid syncing.", err, pod.Name)
  300. return nil
  301. }
  302. if len(deployments) > 1 {
  303. sort.Sort(util.BySelectorLastUpdateTime(deployments))
  304. glog.Errorf("user error! more than one deployment is selecting pod %s/%s with labels: %#v, returning %s/%s", pod.Namespace, pod.Name, pod.Labels, deployments[0].Namespace, deployments[0].Name)
  305. }
  306. return &deployments[0]
  307. }
  308. // When a pod is created, ensure its controller syncs
  309. func (dc *DeploymentController) addPod(obj interface{}) {
  310. pod, ok := obj.(*api.Pod)
  311. if !ok {
  312. return
  313. }
  314. glog.V(4).Infof("Pod %s created: %#v.", pod.Name, pod)
  315. if d := dc.getDeploymentForPod(pod); d != nil {
  316. dc.enqueueDeployment(d)
  317. }
  318. }
  319. // updatePod figures out what deployment(s) manage the ReplicaSet that manages the Pod when the Pod
  320. // is updated and wake them up. If anything of the Pods have changed, we need to awaken both
  321. // the old and new deployments. old and cur must be *api.Pod types.
  322. func (dc *DeploymentController) updatePod(old, cur interface{}) {
  323. curPod := cur.(*api.Pod)
  324. oldPod := old.(*api.Pod)
  325. if curPod.ResourceVersion == oldPod.ResourceVersion {
  326. // Periodic resync will send update events for all known pods.
  327. // Two different versions of the same pod will always have different RVs.
  328. return
  329. }
  330. glog.V(4).Infof("Pod %s updated %#v -> %#v.", curPod.Name, oldPod, curPod)
  331. if d := dc.getDeploymentForPod(curPod); d != nil {
  332. dc.enqueueDeployment(d)
  333. }
  334. if !api.Semantic.DeepEqual(oldPod, curPod) {
  335. if oldD := dc.getDeploymentForPod(oldPod); oldD != nil {
  336. dc.enqueueDeployment(oldD)
  337. }
  338. }
  339. }
  340. // When a pod is deleted, ensure its controller syncs.
  341. // obj could be an *api.Pod, or a DeletionFinalStateUnknown marker item.
  342. func (dc *DeploymentController) deletePod(obj interface{}) {
  343. pod, ok := obj.(*api.Pod)
  344. // When a delete is dropped, the relist will notice a pod in the store not
  345. // in the list, leading to the insertion of a tombstone object which contains
  346. // the deleted key/value. Note that this value might be stale. If the pod
  347. // changed labels the new ReplicaSet will not be woken up till the periodic
  348. // resync.
  349. if !ok {
  350. tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
  351. if !ok {
  352. glog.Errorf("Couldn't get object from tombstone %#v", obj)
  353. return
  354. }
  355. pod, ok = tombstone.Obj.(*api.Pod)
  356. if !ok {
  357. glog.Errorf("Tombstone contained object that is not a pod %#v", obj)
  358. return
  359. }
  360. }
  361. glog.V(4).Infof("Pod %s deleted: %#v.", pod.Name, pod)
  362. if d := dc.getDeploymentForPod(pod); d != nil {
  363. dc.enqueueDeployment(d)
  364. }
  365. }
  366. func (dc *DeploymentController) enqueueDeployment(deployment *extensions.Deployment) {
  367. key, err := controller.KeyFunc(deployment)
  368. if err != nil {
  369. glog.Errorf("Couldn't get key for object %#v: %v", deployment, err)
  370. return
  371. }
  372. dc.queue.Add(key)
  373. }
  374. func (dc *DeploymentController) markDeploymentOverlap(deployment *extensions.Deployment, withDeployment string) (*extensions.Deployment, error) {
  375. if deployment.Annotations[util.OverlapAnnotation] == withDeployment {
  376. return deployment, nil
  377. }
  378. if deployment.Annotations == nil {
  379. deployment.Annotations = make(map[string]string)
  380. }
  381. deployment.Annotations[util.OverlapAnnotation] = withDeployment
  382. return dc.client.Extensions().Deployments(deployment.Namespace).Update(deployment)
  383. }
  384. func (dc *DeploymentController) clearDeploymentOverlap(deployment *extensions.Deployment) (*extensions.Deployment, error) {
  385. if len(deployment.Annotations[util.OverlapAnnotation]) == 0 {
  386. return deployment, nil
  387. }
  388. delete(deployment.Annotations, util.OverlapAnnotation)
  389. return dc.client.Extensions().Deployments(deployment.Namespace).Update(deployment)
  390. }
  391. // worker runs a worker thread that just dequeues items, processes them, and marks them done.
  392. // It enforces that the syncHandler is never invoked concurrently with the same key.
  393. func (dc *DeploymentController) worker() {
  394. work := func() bool {
  395. key, quit := dc.queue.Get()
  396. if quit {
  397. return true
  398. }
  399. defer dc.queue.Done(key)
  400. err := dc.syncHandler(key.(string))
  401. dc.handleErr(err, key)
  402. return false
  403. }
  404. for {
  405. if quit := work(); quit {
  406. return
  407. }
  408. }
  409. }
  410. func (dc *DeploymentController) handleErr(err error, key interface{}) {
  411. if err == nil {
  412. dc.queue.Forget(key)
  413. return
  414. }
  415. if dc.queue.NumRequeues(key) < MaxRetries {
  416. glog.V(2).Infof("Error syncing deployment %v: %v", key, err)
  417. dc.queue.AddRateLimited(key)
  418. return
  419. }
  420. utilruntime.HandleError(err)
  421. dc.queue.Forget(key)
  422. }
  423. // syncDeployment will sync the deployment with the given key.
  424. // This function is not meant to be invoked concurrently with the same key.
  425. func (dc *DeploymentController) syncDeployment(key string) error {
  426. startTime := time.Now()
  427. defer func() {
  428. glog.V(4).Infof("Finished syncing deployment %q (%v)", key, time.Now().Sub(startTime))
  429. }()
  430. obj, exists, err := dc.dStore.Indexer.GetByKey(key)
  431. if err != nil {
  432. glog.Infof("Unable to retrieve deployment %v from store: %v", key, err)
  433. return err
  434. }
  435. if !exists {
  436. glog.Infof("Deployment has been deleted %v", key)
  437. return nil
  438. }
  439. deployment := obj.(*extensions.Deployment)
  440. everything := unversioned.LabelSelector{}
  441. if reflect.DeepEqual(deployment.Spec.Selector, &everything) {
  442. dc.eventRecorder.Eventf(deployment, api.EventTypeWarning, "SelectingAll", "This deployment is selecting all pods. A non-empty selector is required.")
  443. return nil
  444. }
  445. // Deep-copy otherwise we are mutating our cache.
  446. // TODO: Deep-copy only when needed.
  447. d, err := util.DeploymentDeepCopy(deployment)
  448. if err != nil {
  449. return err
  450. }
  451. if d.DeletionTimestamp != nil {
  452. return dc.syncStatusOnly(d)
  453. }
  454. // Handle overlapping deployments by deterministically avoid syncing deployments that fight over ReplicaSets.
  455. if err = dc.handleOverlap(d); err != nil {
  456. return err
  457. }
  458. if d.Spec.Paused {
  459. return dc.sync(d)
  460. }
  461. if d.Spec.RollbackTo != nil {
  462. revision := d.Spec.RollbackTo.Revision
  463. if d, err = dc.rollback(d, &revision); err != nil {
  464. return err
  465. }
  466. }
  467. scalingEvent, err := dc.isScalingEvent(d)
  468. if err != nil {
  469. return err
  470. }
  471. if scalingEvent {
  472. return dc.sync(d)
  473. }
  474. switch d.Spec.Strategy.Type {
  475. case extensions.RecreateDeploymentStrategyType:
  476. return dc.rolloutRecreate(d)
  477. case extensions.RollingUpdateDeploymentStrategyType:
  478. return dc.rolloutRolling(d)
  479. }
  480. return fmt.Errorf("unexpected deployment strategy type: %s", d.Spec.Strategy.Type)
  481. }
  482. // handleOverlap relists all deployment in the same namespace for overlaps, and avoid syncing
  483. // the newer overlapping ones (only sync the oldest one). New/old is determined by when the
  484. // deployment's selector is last updated.
  485. func (dc *DeploymentController) handleOverlap(d *extensions.Deployment) error {
  486. selector, err := unversioned.LabelSelectorAsSelector(d.Spec.Selector)
  487. if err != nil {
  488. return fmt.Errorf("deployment %s/%s has invalid label selector: %v", d.Namespace, d.Name, err)
  489. }
  490. deployments, err := dc.dStore.Deployments(d.Namespace).List(labels.Everything())
  491. if err != nil {
  492. return fmt.Errorf("error listing deployments in namespace %s: %v", d.Namespace, err)
  493. }
  494. overlapping := false
  495. for i := range deployments {
  496. other := &deployments[i]
  497. if !selector.Empty() && selector.Matches(labels.Set(other.Spec.Template.Labels)) && d.UID != other.UID {
  498. overlapping = true
  499. // We don't care if the overlapping annotation update failed or not (we don't make decision on it)
  500. d, _ = dc.markDeploymentOverlap(d, other.Name)
  501. other, _ = dc.markDeploymentOverlap(other, d.Name)
  502. // Skip syncing this one if older overlapping one is found
  503. // TODO: figure out a better way to determine which deployment to skip,
  504. // either with controller reference, or with validation.
  505. // Using oldest active replica set to determine which deployment to skip wouldn't make much difference,
  506. // since new replica set hasn't been created after selector update
  507. if util.SelectorUpdatedBefore(other, d) {
  508. return fmt.Errorf("found deployment %s/%s has overlapping selector with an older deployment %s/%s, skip syncing it", d.Namespace, d.Name, other.Namespace, other.Name)
  509. }
  510. }
  511. }
  512. if !overlapping {
  513. // We don't care if the overlapping annotation update failed or not (we don't make decision on it)
  514. d, _ = dc.clearDeploymentOverlap(d)
  515. }
  516. return nil
  517. }