controller_utils.go 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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 controller
  14. import (
  15. "encoding/binary"
  16. "encoding/json"
  17. "fmt"
  18. "hash/fnv"
  19. "sync"
  20. "sync/atomic"
  21. "time"
  22. apps "k8s.io/api/apps/v1"
  23. v1 "k8s.io/api/core/v1"
  24. apierrors "k8s.io/apimachinery/pkg/api/errors"
  25. "k8s.io/apimachinery/pkg/api/meta"
  26. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  27. "k8s.io/apimachinery/pkg/labels"
  28. "k8s.io/apimachinery/pkg/runtime"
  29. "k8s.io/apimachinery/pkg/types"
  30. "k8s.io/apimachinery/pkg/util/clock"
  31. "k8s.io/apimachinery/pkg/util/rand"
  32. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  33. "k8s.io/apimachinery/pkg/util/sets"
  34. "k8s.io/apimachinery/pkg/util/strategicpatch"
  35. "k8s.io/apimachinery/pkg/util/wait"
  36. clientset "k8s.io/client-go/kubernetes"
  37. v1core "k8s.io/client-go/kubernetes/typed/core/v1"
  38. "k8s.io/client-go/tools/cache"
  39. "k8s.io/client-go/tools/record"
  40. clientretry "k8s.io/client-go/util/retry"
  41. podutil "k8s.io/kubernetes/pkg/api/v1/pod"
  42. _ "k8s.io/kubernetes/pkg/apis/core/install"
  43. "k8s.io/kubernetes/pkg/apis/core/validation"
  44. hashutil "k8s.io/kubernetes/pkg/util/hash"
  45. taintutils "k8s.io/kubernetes/pkg/util/taints"
  46. "k8s.io/utils/integer"
  47. "k8s.io/klog"
  48. )
  49. const (
  50. // If a watch drops a delete event for a pod, it'll take this long
  51. // before a dormant controller waiting for those packets is woken up anyway. It is
  52. // specifically targeted at the case where some problem prevents an update
  53. // of expectations, without it the controller could stay asleep forever. This should
  54. // be set based on the expected latency of watch events.
  55. //
  56. // Currently a controller can service (create *and* observe the watch events for said
  57. // creation) about 10 pods a second, so it takes about 1 min to service
  58. // 500 pods. Just creation is limited to 20qps, and watching happens with ~10-30s
  59. // latency/pod at the scale of 3000 pods over 100 nodes.
  60. ExpectationsTimeout = 5 * time.Minute
  61. // When batching pod creates, SlowStartInitialBatchSize is the size of the
  62. // initial batch. The size of each successive batch is twice the size of
  63. // the previous batch. For example, for a value of 1, batch sizes would be
  64. // 1, 2, 4, 8, ... and for a value of 10, batch sizes would be
  65. // 10, 20, 40, 80, ... Setting the value higher means that quota denials
  66. // will result in more doomed API calls and associated event spam. Setting
  67. // the value lower will result in more API call round trip periods for
  68. // large batches.
  69. //
  70. // Given a number of pods to start "N":
  71. // The number of doomed calls per sync once quota is exceeded is given by:
  72. // min(N,SlowStartInitialBatchSize)
  73. // The number of batches is given by:
  74. // 1+floor(log_2(ceil(N/SlowStartInitialBatchSize)))
  75. SlowStartInitialBatchSize = 1
  76. )
  77. var UpdateTaintBackoff = wait.Backoff{
  78. Steps: 5,
  79. Duration: 100 * time.Millisecond,
  80. Jitter: 1.0,
  81. }
  82. var UpdateLabelBackoff = wait.Backoff{
  83. Steps: 5,
  84. Duration: 100 * time.Millisecond,
  85. Jitter: 1.0,
  86. }
  87. var (
  88. KeyFunc = cache.DeletionHandlingMetaNamespaceKeyFunc
  89. )
  90. type ResyncPeriodFunc func() time.Duration
  91. // Returns 0 for resyncPeriod in case resyncing is not needed.
  92. func NoResyncPeriodFunc() time.Duration {
  93. return 0
  94. }
  95. // StaticResyncPeriodFunc returns the resync period specified
  96. func StaticResyncPeriodFunc(resyncPeriod time.Duration) ResyncPeriodFunc {
  97. return func() time.Duration {
  98. return resyncPeriod
  99. }
  100. }
  101. // Expectations are a way for controllers to tell the controller manager what they expect. eg:
  102. // ControllerExpectations: {
  103. // controller1: expects 2 adds in 2 minutes
  104. // controller2: expects 2 dels in 2 minutes
  105. // controller3: expects -1 adds in 2 minutes => controller3's expectations have already been met
  106. // }
  107. //
  108. // Implementation:
  109. // ControlleeExpectation = pair of atomic counters to track controllee's creation/deletion
  110. // ControllerExpectationsStore = TTLStore + a ControlleeExpectation per controller
  111. //
  112. // * Once set expectations can only be lowered
  113. // * A controller isn't synced till its expectations are either fulfilled, or expire
  114. // * Controllers that don't set expectations will get woken up for every matching controllee
  115. // ExpKeyFunc to parse out the key from a ControlleeExpectation
  116. var ExpKeyFunc = func(obj interface{}) (string, error) {
  117. if e, ok := obj.(*ControlleeExpectations); ok {
  118. return e.key, nil
  119. }
  120. return "", fmt.Errorf("could not find key for obj %#v", obj)
  121. }
  122. // ControllerExpectationsInterface is an interface that allows users to set and wait on expectations.
  123. // Only abstracted out for testing.
  124. // Warning: if using KeyFunc it is not safe to use a single ControllerExpectationsInterface with different
  125. // types of controllers, because the keys might conflict across types.
  126. type ControllerExpectationsInterface interface {
  127. GetExpectations(controllerKey string) (*ControlleeExpectations, bool, error)
  128. SatisfiedExpectations(controllerKey string) bool
  129. DeleteExpectations(controllerKey string)
  130. SetExpectations(controllerKey string, add, del int) error
  131. ExpectCreations(controllerKey string, adds int) error
  132. ExpectDeletions(controllerKey string, dels int) error
  133. CreationObserved(controllerKey string)
  134. DeletionObserved(controllerKey string)
  135. RaiseExpectations(controllerKey string, add, del int)
  136. LowerExpectations(controllerKey string, add, del int)
  137. }
  138. // ControllerExpectations is a cache mapping controllers to what they expect to see before being woken up for a sync.
  139. type ControllerExpectations struct {
  140. cache.Store
  141. }
  142. // GetExpectations returns the ControlleeExpectations of the given controller.
  143. func (r *ControllerExpectations) GetExpectations(controllerKey string) (*ControlleeExpectations, bool, error) {
  144. exp, exists, err := r.GetByKey(controllerKey)
  145. if err == nil && exists {
  146. return exp.(*ControlleeExpectations), true, nil
  147. }
  148. return nil, false, err
  149. }
  150. // DeleteExpectations deletes the expectations of the given controller from the TTLStore.
  151. func (r *ControllerExpectations) DeleteExpectations(controllerKey string) {
  152. if exp, exists, err := r.GetByKey(controllerKey); err == nil && exists {
  153. if err := r.Delete(exp); err != nil {
  154. klog.V(2).Infof("Error deleting expectations for controller %v: %v", controllerKey, err)
  155. }
  156. }
  157. }
  158. // SatisfiedExpectations returns true if the required adds/dels for the given controller have been observed.
  159. // Add/del counts are established by the controller at sync time, and updated as controllees are observed by the controller
  160. // manager.
  161. func (r *ControllerExpectations) SatisfiedExpectations(controllerKey string) bool {
  162. if exp, exists, err := r.GetExpectations(controllerKey); exists {
  163. if exp.Fulfilled() {
  164. klog.V(4).Infof("Controller expectations fulfilled %#v", exp)
  165. return true
  166. } else if exp.isExpired() {
  167. klog.V(4).Infof("Controller expectations expired %#v", exp)
  168. return true
  169. } else {
  170. klog.V(4).Infof("Controller still waiting on expectations %#v", exp)
  171. return false
  172. }
  173. } else if err != nil {
  174. klog.V(2).Infof("Error encountered while checking expectations %#v, forcing sync", err)
  175. } else {
  176. // When a new controller is created, it doesn't have expectations.
  177. // When it doesn't see expected watch events for > TTL, the expectations expire.
  178. // - In this case it wakes up, creates/deletes controllees, and sets expectations again.
  179. // When it has satisfied expectations and no controllees need to be created/destroyed > TTL, the expectations expire.
  180. // - In this case it continues without setting expectations till it needs to create/delete controllees.
  181. klog.V(4).Infof("Controller %v either never recorded expectations, or the ttl expired.", controllerKey)
  182. }
  183. // Trigger a sync if we either encountered and error (which shouldn't happen since we're
  184. // getting from local store) or this controller hasn't established expectations.
  185. return true
  186. }
  187. // TODO: Extend ExpirationCache to support explicit expiration.
  188. // TODO: Make this possible to disable in tests.
  189. // TODO: Support injection of clock.
  190. func (exp *ControlleeExpectations) isExpired() bool {
  191. return clock.RealClock{}.Since(exp.timestamp) > ExpectationsTimeout
  192. }
  193. // SetExpectations registers new expectations for the given controller. Forgets existing expectations.
  194. func (r *ControllerExpectations) SetExpectations(controllerKey string, add, del int) error {
  195. exp := &ControlleeExpectations{add: int64(add), del: int64(del), key: controllerKey, timestamp: clock.RealClock{}.Now()}
  196. klog.V(4).Infof("Setting expectations %#v", exp)
  197. return r.Add(exp)
  198. }
  199. func (r *ControllerExpectations) ExpectCreations(controllerKey string, adds int) error {
  200. return r.SetExpectations(controllerKey, adds, 0)
  201. }
  202. func (r *ControllerExpectations) ExpectDeletions(controllerKey string, dels int) error {
  203. return r.SetExpectations(controllerKey, 0, dels)
  204. }
  205. // Decrements the expectation counts of the given controller.
  206. func (r *ControllerExpectations) LowerExpectations(controllerKey string, add, del int) {
  207. if exp, exists, err := r.GetExpectations(controllerKey); err == nil && exists {
  208. exp.Add(int64(-add), int64(-del))
  209. // The expectations might've been modified since the update on the previous line.
  210. klog.V(4).Infof("Lowered expectations %#v", exp)
  211. }
  212. }
  213. // Increments the expectation counts of the given controller.
  214. func (r *ControllerExpectations) RaiseExpectations(controllerKey string, add, del int) {
  215. if exp, exists, err := r.GetExpectations(controllerKey); err == nil && exists {
  216. exp.Add(int64(add), int64(del))
  217. // The expectations might've been modified since the update on the previous line.
  218. klog.V(4).Infof("Raised expectations %#v", exp)
  219. }
  220. }
  221. // CreationObserved atomically decrements the `add` expectation count of the given controller.
  222. func (r *ControllerExpectations) CreationObserved(controllerKey string) {
  223. r.LowerExpectations(controllerKey, 1, 0)
  224. }
  225. // DeletionObserved atomically decrements the `del` expectation count of the given controller.
  226. func (r *ControllerExpectations) DeletionObserved(controllerKey string) {
  227. r.LowerExpectations(controllerKey, 0, 1)
  228. }
  229. // Expectations are either fulfilled, or expire naturally.
  230. type Expectations interface {
  231. Fulfilled() bool
  232. }
  233. // ControlleeExpectations track controllee creates/deletes.
  234. type ControlleeExpectations struct {
  235. // Important: Since these two int64 fields are using sync/atomic, they have to be at the top of the struct due to a bug on 32-bit platforms
  236. // See: https://golang.org/pkg/sync/atomic/ for more information
  237. add int64
  238. del int64
  239. key string
  240. timestamp time.Time
  241. }
  242. // Add increments the add and del counters.
  243. func (e *ControlleeExpectations) Add(add, del int64) {
  244. atomic.AddInt64(&e.add, add)
  245. atomic.AddInt64(&e.del, del)
  246. }
  247. // Fulfilled returns true if this expectation has been fulfilled.
  248. func (e *ControlleeExpectations) Fulfilled() bool {
  249. // TODO: think about why this line being atomic doesn't matter
  250. return atomic.LoadInt64(&e.add) <= 0 && atomic.LoadInt64(&e.del) <= 0
  251. }
  252. // GetExpectations returns the add and del expectations of the controllee.
  253. func (e *ControlleeExpectations) GetExpectations() (int64, int64) {
  254. return atomic.LoadInt64(&e.add), atomic.LoadInt64(&e.del)
  255. }
  256. // NewControllerExpectations returns a store for ControllerExpectations.
  257. func NewControllerExpectations() *ControllerExpectations {
  258. return &ControllerExpectations{cache.NewStore(ExpKeyFunc)}
  259. }
  260. // UIDSetKeyFunc to parse out the key from a UIDSet.
  261. var UIDSetKeyFunc = func(obj interface{}) (string, error) {
  262. if u, ok := obj.(*UIDSet); ok {
  263. return u.key, nil
  264. }
  265. return "", fmt.Errorf("could not find key for obj %#v", obj)
  266. }
  267. // UIDSet holds a key and a set of UIDs. Used by the
  268. // UIDTrackingControllerExpectations to remember which UID it has seen/still
  269. // waiting for.
  270. type UIDSet struct {
  271. sets.String
  272. key string
  273. }
  274. // UIDTrackingControllerExpectations tracks the UID of the pods it deletes.
  275. // This cache is needed over plain old expectations to safely handle graceful
  276. // deletion. The desired behavior is to treat an update that sets the
  277. // DeletionTimestamp on an object as a delete. To do so consistently, one needs
  278. // to remember the expected deletes so they aren't double counted.
  279. // TODO: Track creates as well (#22599)
  280. type UIDTrackingControllerExpectations struct {
  281. ControllerExpectationsInterface
  282. // TODO: There is a much nicer way to do this that involves a single store,
  283. // a lock per entry, and a ControlleeExpectationsInterface type.
  284. uidStoreLock sync.Mutex
  285. // Store used for the UIDs associated with any expectation tracked via the
  286. // ControllerExpectationsInterface.
  287. uidStore cache.Store
  288. }
  289. // GetUIDs is a convenience method to avoid exposing the set of expected uids.
  290. // The returned set is not thread safe, all modifications must be made holding
  291. // the uidStoreLock.
  292. func (u *UIDTrackingControllerExpectations) GetUIDs(controllerKey string) sets.String {
  293. if uid, exists, err := u.uidStore.GetByKey(controllerKey); err == nil && exists {
  294. return uid.(*UIDSet).String
  295. }
  296. return nil
  297. }
  298. // ExpectDeletions records expectations for the given deleteKeys, against the given controller.
  299. func (u *UIDTrackingControllerExpectations) ExpectDeletions(rcKey string, deletedKeys []string) error {
  300. u.uidStoreLock.Lock()
  301. defer u.uidStoreLock.Unlock()
  302. if existing := u.GetUIDs(rcKey); existing != nil && existing.Len() != 0 {
  303. klog.Errorf("Clobbering existing delete keys: %+v", existing)
  304. }
  305. expectedUIDs := sets.NewString()
  306. for _, k := range deletedKeys {
  307. expectedUIDs.Insert(k)
  308. }
  309. klog.V(4).Infof("Controller %v waiting on deletions for: %+v", rcKey, deletedKeys)
  310. if err := u.uidStore.Add(&UIDSet{expectedUIDs, rcKey}); err != nil {
  311. return err
  312. }
  313. return u.ControllerExpectationsInterface.ExpectDeletions(rcKey, expectedUIDs.Len())
  314. }
  315. // DeletionObserved records the given deleteKey as a deletion, for the given rc.
  316. func (u *UIDTrackingControllerExpectations) DeletionObserved(rcKey, deleteKey string) {
  317. u.uidStoreLock.Lock()
  318. defer u.uidStoreLock.Unlock()
  319. uids := u.GetUIDs(rcKey)
  320. if uids != nil && uids.Has(deleteKey) {
  321. klog.V(4).Infof("Controller %v received delete for pod %v", rcKey, deleteKey)
  322. u.ControllerExpectationsInterface.DeletionObserved(rcKey)
  323. uids.Delete(deleteKey)
  324. }
  325. }
  326. // DeleteExpectations deletes the UID set and invokes DeleteExpectations on the
  327. // underlying ControllerExpectationsInterface.
  328. func (u *UIDTrackingControllerExpectations) DeleteExpectations(rcKey string) {
  329. u.uidStoreLock.Lock()
  330. defer u.uidStoreLock.Unlock()
  331. u.ControllerExpectationsInterface.DeleteExpectations(rcKey)
  332. if uidExp, exists, err := u.uidStore.GetByKey(rcKey); err == nil && exists {
  333. if err := u.uidStore.Delete(uidExp); err != nil {
  334. klog.V(2).Infof("Error deleting uid expectations for controller %v: %v", rcKey, err)
  335. }
  336. }
  337. }
  338. // NewUIDTrackingControllerExpectations returns a wrapper around
  339. // ControllerExpectations that is aware of deleteKeys.
  340. func NewUIDTrackingControllerExpectations(ce ControllerExpectationsInterface) *UIDTrackingControllerExpectations {
  341. return &UIDTrackingControllerExpectations{ControllerExpectationsInterface: ce, uidStore: cache.NewStore(UIDSetKeyFunc)}
  342. }
  343. // Reasons for pod events
  344. const (
  345. // FailedCreatePodReason is added in an event and in a replica set condition
  346. // when a pod for a replica set is failed to be created.
  347. FailedCreatePodReason = "FailedCreate"
  348. // SuccessfulCreatePodReason is added in an event when a pod for a replica set
  349. // is successfully created.
  350. SuccessfulCreatePodReason = "SuccessfulCreate"
  351. // FailedDeletePodReason is added in an event and in a replica set condition
  352. // when a pod for a replica set is failed to be deleted.
  353. FailedDeletePodReason = "FailedDelete"
  354. // SuccessfulDeletePodReason is added in an event when a pod for a replica set
  355. // is successfully deleted.
  356. SuccessfulDeletePodReason = "SuccessfulDelete"
  357. )
  358. // RSControlInterface is an interface that knows how to add or delete
  359. // ReplicaSets, as well as increment or decrement them. It is used
  360. // by the deployment controller to ease testing of actions that it takes.
  361. type RSControlInterface interface {
  362. PatchReplicaSet(namespace, name string, data []byte) error
  363. }
  364. // RealRSControl is the default implementation of RSControllerInterface.
  365. type RealRSControl struct {
  366. KubeClient clientset.Interface
  367. Recorder record.EventRecorder
  368. }
  369. var _ RSControlInterface = &RealRSControl{}
  370. func (r RealRSControl) PatchReplicaSet(namespace, name string, data []byte) error {
  371. _, err := r.KubeClient.AppsV1().ReplicaSets(namespace).Patch(name, types.StrategicMergePatchType, data)
  372. return err
  373. }
  374. // TODO: merge the controller revision interface in controller_history.go with this one
  375. // ControllerRevisionControlInterface is an interface that knows how to patch
  376. // ControllerRevisions, as well as increment or decrement them. It is used
  377. // by the daemonset controller to ease testing of actions that it takes.
  378. type ControllerRevisionControlInterface interface {
  379. PatchControllerRevision(namespace, name string, data []byte) error
  380. }
  381. // RealControllerRevisionControl is the default implementation of ControllerRevisionControlInterface.
  382. type RealControllerRevisionControl struct {
  383. KubeClient clientset.Interface
  384. }
  385. var _ ControllerRevisionControlInterface = &RealControllerRevisionControl{}
  386. func (r RealControllerRevisionControl) PatchControllerRevision(namespace, name string, data []byte) error {
  387. _, err := r.KubeClient.AppsV1().ControllerRevisions(namespace).Patch(name, types.StrategicMergePatchType, data)
  388. return err
  389. }
  390. // PodControlInterface is an interface that knows how to add or delete pods
  391. // created as an interface to allow testing.
  392. type PodControlInterface interface {
  393. // CreatePods creates new pods according to the spec.
  394. CreatePods(namespace string, template *v1.PodTemplateSpec, object runtime.Object) error
  395. // CreatePodsOnNode creates a new pod according to the spec on the specified node,
  396. // and sets the ControllerRef.
  397. CreatePodsOnNode(nodeName, namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error
  398. // CreatePodsWithControllerRef creates new pods according to the spec, and sets object as the pod's controller.
  399. CreatePodsWithControllerRef(namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error
  400. // DeletePod deletes the pod identified by podID.
  401. DeletePod(namespace string, podID string, object runtime.Object) error
  402. // PatchPod patches the pod.
  403. PatchPod(namespace, name string, data []byte) error
  404. }
  405. // RealPodControl is the default implementation of PodControlInterface.
  406. type RealPodControl struct {
  407. KubeClient clientset.Interface
  408. Recorder record.EventRecorder
  409. }
  410. var _ PodControlInterface = &RealPodControl{}
  411. func getPodsLabelSet(template *v1.PodTemplateSpec) labels.Set {
  412. desiredLabels := make(labels.Set)
  413. for k, v := range template.Labels {
  414. desiredLabels[k] = v
  415. }
  416. return desiredLabels
  417. }
  418. func getPodsFinalizers(template *v1.PodTemplateSpec) []string {
  419. desiredFinalizers := make([]string, len(template.Finalizers))
  420. copy(desiredFinalizers, template.Finalizers)
  421. return desiredFinalizers
  422. }
  423. func getPodsAnnotationSet(template *v1.PodTemplateSpec) labels.Set {
  424. desiredAnnotations := make(labels.Set)
  425. for k, v := range template.Annotations {
  426. desiredAnnotations[k] = v
  427. }
  428. return desiredAnnotations
  429. }
  430. func getPodsPrefix(controllerName string) string {
  431. // use the dash (if the name isn't too long) to make the pod name a bit prettier
  432. prefix := fmt.Sprintf("%s-", controllerName)
  433. if len(validation.ValidatePodName(prefix, true)) != 0 {
  434. prefix = controllerName
  435. }
  436. return prefix
  437. }
  438. func validateControllerRef(controllerRef *metav1.OwnerReference) error {
  439. if controllerRef == nil {
  440. return fmt.Errorf("controllerRef is nil")
  441. }
  442. if len(controllerRef.APIVersion) == 0 {
  443. return fmt.Errorf("controllerRef has empty APIVersion")
  444. }
  445. if len(controllerRef.Kind) == 0 {
  446. return fmt.Errorf("controllerRef has empty Kind")
  447. }
  448. if controllerRef.Controller == nil || *controllerRef.Controller != true {
  449. return fmt.Errorf("controllerRef.Controller is not set to true")
  450. }
  451. if controllerRef.BlockOwnerDeletion == nil || *controllerRef.BlockOwnerDeletion != true {
  452. return fmt.Errorf("controllerRef.BlockOwnerDeletion is not set")
  453. }
  454. return nil
  455. }
  456. func (r RealPodControl) CreatePods(namespace string, template *v1.PodTemplateSpec, object runtime.Object) error {
  457. return r.createPods("", namespace, template, object, nil)
  458. }
  459. func (r RealPodControl) CreatePodsWithControllerRef(namespace string, template *v1.PodTemplateSpec, controllerObject runtime.Object, controllerRef *metav1.OwnerReference) error {
  460. if err := validateControllerRef(controllerRef); err != nil {
  461. return err
  462. }
  463. return r.createPods("", namespace, template, controllerObject, controllerRef)
  464. }
  465. func (r RealPodControl) CreatePodsOnNode(nodeName, namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error {
  466. if err := validateControllerRef(controllerRef); err != nil {
  467. return err
  468. }
  469. return r.createPods(nodeName, namespace, template, object, controllerRef)
  470. }
  471. func (r RealPodControl) PatchPod(namespace, name string, data []byte) error {
  472. _, err := r.KubeClient.CoreV1().Pods(namespace).Patch(name, types.StrategicMergePatchType, data)
  473. return err
  474. }
  475. func GetPodFromTemplate(template *v1.PodTemplateSpec, parentObject runtime.Object, controllerRef *metav1.OwnerReference) (*v1.Pod, error) {
  476. desiredLabels := getPodsLabelSet(template)
  477. desiredFinalizers := getPodsFinalizers(template)
  478. desiredAnnotations := getPodsAnnotationSet(template)
  479. accessor, err := meta.Accessor(parentObject)
  480. if err != nil {
  481. return nil, fmt.Errorf("parentObject does not have ObjectMeta, %v", err)
  482. }
  483. prefix := getPodsPrefix(accessor.GetName())
  484. pod := &v1.Pod{
  485. ObjectMeta: metav1.ObjectMeta{
  486. Labels: desiredLabels,
  487. Annotations: desiredAnnotations,
  488. GenerateName: prefix,
  489. Finalizers: desiredFinalizers,
  490. },
  491. }
  492. if controllerRef != nil {
  493. pod.OwnerReferences = append(pod.OwnerReferences, *controllerRef)
  494. }
  495. pod.Spec = *template.Spec.DeepCopy()
  496. return pod, nil
  497. }
  498. func (r RealPodControl) createPods(nodeName, namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error {
  499. pod, err := GetPodFromTemplate(template, object, controllerRef)
  500. if err != nil {
  501. return err
  502. }
  503. if len(nodeName) != 0 {
  504. pod.Spec.NodeName = nodeName
  505. }
  506. if labels.Set(pod.Labels).AsSelectorPreValidated().Empty() {
  507. return fmt.Errorf("unable to create pods, no labels")
  508. }
  509. newPod, err := r.KubeClient.CoreV1().Pods(namespace).Create(pod)
  510. if err != nil {
  511. r.Recorder.Eventf(object, v1.EventTypeWarning, FailedCreatePodReason, "Error creating: %v", err)
  512. return err
  513. }
  514. accessor, err := meta.Accessor(object)
  515. if err != nil {
  516. klog.Errorf("parentObject does not have ObjectMeta, %v", err)
  517. return nil
  518. }
  519. klog.V(4).Infof("Controller %v created pod %v", accessor.GetName(), newPod.Name)
  520. r.Recorder.Eventf(object, v1.EventTypeNormal, SuccessfulCreatePodReason, "Created pod: %v", newPod.Name)
  521. return nil
  522. }
  523. func (r RealPodControl) DeletePod(namespace string, podID string, object runtime.Object) error {
  524. accessor, err := meta.Accessor(object)
  525. if err != nil {
  526. return fmt.Errorf("object does not have ObjectMeta, %v", err)
  527. }
  528. klog.V(2).Infof("Controller %v deleting pod %v/%v", accessor.GetName(), namespace, podID)
  529. if err := r.KubeClient.CoreV1().Pods(namespace).Delete(podID, nil); err != nil && !apierrors.IsNotFound(err) {
  530. r.Recorder.Eventf(object, v1.EventTypeWarning, FailedDeletePodReason, "Error deleting: %v", err)
  531. return fmt.Errorf("unable to delete pods: %v", err)
  532. }
  533. r.Recorder.Eventf(object, v1.EventTypeNormal, SuccessfulDeletePodReason, "Deleted pod: %v", podID)
  534. return nil
  535. }
  536. type FakePodControl struct {
  537. sync.Mutex
  538. Templates []v1.PodTemplateSpec
  539. ControllerRefs []metav1.OwnerReference
  540. DeletePodName []string
  541. Patches [][]byte
  542. Err error
  543. CreateLimit int
  544. CreateCallCount int
  545. }
  546. var _ PodControlInterface = &FakePodControl{}
  547. func (f *FakePodControl) PatchPod(namespace, name string, data []byte) error {
  548. f.Lock()
  549. defer f.Unlock()
  550. f.Patches = append(f.Patches, data)
  551. if f.Err != nil {
  552. return f.Err
  553. }
  554. return nil
  555. }
  556. func (f *FakePodControl) CreatePods(namespace string, spec *v1.PodTemplateSpec, object runtime.Object) error {
  557. f.Lock()
  558. defer f.Unlock()
  559. f.CreateCallCount++
  560. if f.CreateLimit != 0 && f.CreateCallCount > f.CreateLimit {
  561. return fmt.Errorf("not creating pod, limit %d already reached (create call %d)", f.CreateLimit, f.CreateCallCount)
  562. }
  563. f.Templates = append(f.Templates, *spec)
  564. if f.Err != nil {
  565. return f.Err
  566. }
  567. return nil
  568. }
  569. func (f *FakePodControl) CreatePodsWithControllerRef(namespace string, spec *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error {
  570. f.Lock()
  571. defer f.Unlock()
  572. f.CreateCallCount++
  573. if f.CreateLimit != 0 && f.CreateCallCount > f.CreateLimit {
  574. return fmt.Errorf("not creating pod, limit %d already reached (create call %d)", f.CreateLimit, f.CreateCallCount)
  575. }
  576. f.Templates = append(f.Templates, *spec)
  577. f.ControllerRefs = append(f.ControllerRefs, *controllerRef)
  578. if f.Err != nil {
  579. return f.Err
  580. }
  581. return nil
  582. }
  583. func (f *FakePodControl) CreatePodsOnNode(nodeName, namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error {
  584. f.Lock()
  585. defer f.Unlock()
  586. f.CreateCallCount++
  587. if f.CreateLimit != 0 && f.CreateCallCount > f.CreateLimit {
  588. return fmt.Errorf("not creating pod, limit %d already reached (create call %d)", f.CreateLimit, f.CreateCallCount)
  589. }
  590. f.Templates = append(f.Templates, *template)
  591. f.ControllerRefs = append(f.ControllerRefs, *controllerRef)
  592. if f.Err != nil {
  593. return f.Err
  594. }
  595. return nil
  596. }
  597. func (f *FakePodControl) DeletePod(namespace string, podID string, object runtime.Object) error {
  598. f.Lock()
  599. defer f.Unlock()
  600. f.DeletePodName = append(f.DeletePodName, podID)
  601. if f.Err != nil {
  602. return f.Err
  603. }
  604. return nil
  605. }
  606. func (f *FakePodControl) Clear() {
  607. f.Lock()
  608. defer f.Unlock()
  609. f.DeletePodName = []string{}
  610. f.Templates = []v1.PodTemplateSpec{}
  611. f.ControllerRefs = []metav1.OwnerReference{}
  612. f.Patches = [][]byte{}
  613. f.CreateLimit = 0
  614. f.CreateCallCount = 0
  615. }
  616. // ByLogging allows custom sorting of pods so the best one can be picked for getting its logs.
  617. type ByLogging []*v1.Pod
  618. func (s ByLogging) Len() int { return len(s) }
  619. func (s ByLogging) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  620. func (s ByLogging) Less(i, j int) bool {
  621. // 1. assigned < unassigned
  622. if s[i].Spec.NodeName != s[j].Spec.NodeName && (len(s[i].Spec.NodeName) == 0 || len(s[j].Spec.NodeName) == 0) {
  623. return len(s[i].Spec.NodeName) > 0
  624. }
  625. // 2. PodRunning < PodUnknown < PodPending
  626. m := map[v1.PodPhase]int{v1.PodRunning: 0, v1.PodUnknown: 1, v1.PodPending: 2}
  627. if m[s[i].Status.Phase] != m[s[j].Status.Phase] {
  628. return m[s[i].Status.Phase] < m[s[j].Status.Phase]
  629. }
  630. // 3. ready < not ready
  631. if podutil.IsPodReady(s[i]) != podutil.IsPodReady(s[j]) {
  632. return podutil.IsPodReady(s[i])
  633. }
  634. // TODO: take availability into account when we push minReadySeconds information from deployment into pods,
  635. // see https://github.com/kubernetes/kubernetes/issues/22065
  636. // 4. Been ready for more time < less time < empty time
  637. if podutil.IsPodReady(s[i]) && podutil.IsPodReady(s[j]) && !podReadyTime(s[i]).Equal(podReadyTime(s[j])) {
  638. return afterOrZero(podReadyTime(s[j]), podReadyTime(s[i]))
  639. }
  640. // 5. Pods with containers with higher restart counts < lower restart counts
  641. if maxContainerRestarts(s[i]) != maxContainerRestarts(s[j]) {
  642. return maxContainerRestarts(s[i]) > maxContainerRestarts(s[j])
  643. }
  644. // 6. older pods < newer pods < empty timestamp pods
  645. if !s[i].CreationTimestamp.Equal(&s[j].CreationTimestamp) {
  646. return afterOrZero(&s[j].CreationTimestamp, &s[i].CreationTimestamp)
  647. }
  648. return false
  649. }
  650. // ActivePods type allows custom sorting of pods so a controller can pick the best ones to delete.
  651. type ActivePods []*v1.Pod
  652. func (s ActivePods) Len() int { return len(s) }
  653. func (s ActivePods) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  654. func (s ActivePods) Less(i, j int) bool {
  655. // 1. Unassigned < assigned
  656. // If only one of the pods is unassigned, the unassigned one is smaller
  657. if s[i].Spec.NodeName != s[j].Spec.NodeName && (len(s[i].Spec.NodeName) == 0 || len(s[j].Spec.NodeName) == 0) {
  658. return len(s[i].Spec.NodeName) == 0
  659. }
  660. // 2. PodPending < PodUnknown < PodRunning
  661. m := map[v1.PodPhase]int{v1.PodPending: 0, v1.PodUnknown: 1, v1.PodRunning: 2}
  662. if m[s[i].Status.Phase] != m[s[j].Status.Phase] {
  663. return m[s[i].Status.Phase] < m[s[j].Status.Phase]
  664. }
  665. // 3. Not ready < ready
  666. // If only one of the pods is not ready, the not ready one is smaller
  667. if podutil.IsPodReady(s[i]) != podutil.IsPodReady(s[j]) {
  668. return !podutil.IsPodReady(s[i])
  669. }
  670. // TODO: take availability into account when we push minReadySeconds information from deployment into pods,
  671. // see https://github.com/kubernetes/kubernetes/issues/22065
  672. // 4. Been ready for empty time < less time < more time
  673. // If both pods are ready, the latest ready one is smaller
  674. if podutil.IsPodReady(s[i]) && podutil.IsPodReady(s[j]) && !podReadyTime(s[i]).Equal(podReadyTime(s[j])) {
  675. return afterOrZero(podReadyTime(s[i]), podReadyTime(s[j]))
  676. }
  677. // 5. Pods with containers with higher restart counts < lower restart counts
  678. if maxContainerRestarts(s[i]) != maxContainerRestarts(s[j]) {
  679. return maxContainerRestarts(s[i]) > maxContainerRestarts(s[j])
  680. }
  681. // 6. Empty creation time pods < newer pods < older pods
  682. if !s[i].CreationTimestamp.Equal(&s[j].CreationTimestamp) {
  683. return afterOrZero(&s[i].CreationTimestamp, &s[j].CreationTimestamp)
  684. }
  685. return false
  686. }
  687. // afterOrZero checks if time t1 is after time t2; if one of them
  688. // is zero, the zero time is seen as after non-zero time.
  689. func afterOrZero(t1, t2 *metav1.Time) bool {
  690. if t1.Time.IsZero() || t2.Time.IsZero() {
  691. return t1.Time.IsZero()
  692. }
  693. return t1.After(t2.Time)
  694. }
  695. func podReadyTime(pod *v1.Pod) *metav1.Time {
  696. if podutil.IsPodReady(pod) {
  697. for _, c := range pod.Status.Conditions {
  698. // we only care about pod ready conditions
  699. if c.Type == v1.PodReady && c.Status == v1.ConditionTrue {
  700. return &c.LastTransitionTime
  701. }
  702. }
  703. }
  704. return &metav1.Time{}
  705. }
  706. func maxContainerRestarts(pod *v1.Pod) int {
  707. maxRestarts := 0
  708. for _, c := range pod.Status.ContainerStatuses {
  709. maxRestarts = integer.IntMax(maxRestarts, int(c.RestartCount))
  710. }
  711. return maxRestarts
  712. }
  713. // FilterActivePods returns pods that have not terminated.
  714. func FilterActivePods(pods []*v1.Pod) []*v1.Pod {
  715. var result []*v1.Pod
  716. for _, p := range pods {
  717. if IsPodActive(p) {
  718. result = append(result, p)
  719. } else {
  720. klog.V(4).Infof("Ignoring inactive pod %v/%v in state %v, deletion time %v",
  721. p.Namespace, p.Name, p.Status.Phase, p.DeletionTimestamp)
  722. }
  723. }
  724. return result
  725. }
  726. func IsPodActive(p *v1.Pod) bool {
  727. return v1.PodSucceeded != p.Status.Phase &&
  728. v1.PodFailed != p.Status.Phase &&
  729. p.DeletionTimestamp == nil
  730. }
  731. // FilterActiveReplicaSets returns replica sets that have (or at least ought to have) pods.
  732. func FilterActiveReplicaSets(replicaSets []*apps.ReplicaSet) []*apps.ReplicaSet {
  733. activeFilter := func(rs *apps.ReplicaSet) bool {
  734. return rs != nil && *(rs.Spec.Replicas) > 0
  735. }
  736. return FilterReplicaSets(replicaSets, activeFilter)
  737. }
  738. type filterRS func(rs *apps.ReplicaSet) bool
  739. // FilterReplicaSets returns replica sets that are filtered by filterFn (all returned ones should match filterFn).
  740. func FilterReplicaSets(RSes []*apps.ReplicaSet, filterFn filterRS) []*apps.ReplicaSet {
  741. var filtered []*apps.ReplicaSet
  742. for i := range RSes {
  743. if filterFn(RSes[i]) {
  744. filtered = append(filtered, RSes[i])
  745. }
  746. }
  747. return filtered
  748. }
  749. // PodKey returns a key unique to the given pod within a cluster.
  750. // It's used so we consistently use the same key scheme in this module.
  751. // It does exactly what cache.MetaNamespaceKeyFunc would have done
  752. // except there's not possibility for error since we know the exact type.
  753. func PodKey(pod *v1.Pod) string {
  754. return fmt.Sprintf("%v/%v", pod.Namespace, pod.Name)
  755. }
  756. // ControllersByCreationTimestamp sorts a list of ReplicationControllers by creation timestamp, using their names as a tie breaker.
  757. type ControllersByCreationTimestamp []*v1.ReplicationController
  758. func (o ControllersByCreationTimestamp) Len() int { return len(o) }
  759. func (o ControllersByCreationTimestamp) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
  760. func (o ControllersByCreationTimestamp) Less(i, j int) bool {
  761. if o[i].CreationTimestamp.Equal(&o[j].CreationTimestamp) {
  762. return o[i].Name < o[j].Name
  763. }
  764. return o[i].CreationTimestamp.Before(&o[j].CreationTimestamp)
  765. }
  766. // ReplicaSetsByCreationTimestamp sorts a list of ReplicaSet by creation timestamp, using their names as a tie breaker.
  767. type ReplicaSetsByCreationTimestamp []*apps.ReplicaSet
  768. func (o ReplicaSetsByCreationTimestamp) Len() int { return len(o) }
  769. func (o ReplicaSetsByCreationTimestamp) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
  770. func (o ReplicaSetsByCreationTimestamp) Less(i, j int) bool {
  771. if o[i].CreationTimestamp.Equal(&o[j].CreationTimestamp) {
  772. return o[i].Name < o[j].Name
  773. }
  774. return o[i].CreationTimestamp.Before(&o[j].CreationTimestamp)
  775. }
  776. // ReplicaSetsBySizeOlder sorts a list of ReplicaSet by size in descending order, using their creation timestamp or name as a tie breaker.
  777. // By using the creation timestamp, this sorts from old to new replica sets.
  778. type ReplicaSetsBySizeOlder []*apps.ReplicaSet
  779. func (o ReplicaSetsBySizeOlder) Len() int { return len(o) }
  780. func (o ReplicaSetsBySizeOlder) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
  781. func (o ReplicaSetsBySizeOlder) Less(i, j int) bool {
  782. if *(o[i].Spec.Replicas) == *(o[j].Spec.Replicas) {
  783. return ReplicaSetsByCreationTimestamp(o).Less(i, j)
  784. }
  785. return *(o[i].Spec.Replicas) > *(o[j].Spec.Replicas)
  786. }
  787. // ReplicaSetsBySizeNewer sorts a list of ReplicaSet by size in descending order, using their creation timestamp or name as a tie breaker.
  788. // By using the creation timestamp, this sorts from new to old replica sets.
  789. type ReplicaSetsBySizeNewer []*apps.ReplicaSet
  790. func (o ReplicaSetsBySizeNewer) Len() int { return len(o) }
  791. func (o ReplicaSetsBySizeNewer) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
  792. func (o ReplicaSetsBySizeNewer) Less(i, j int) bool {
  793. if *(o[i].Spec.Replicas) == *(o[j].Spec.Replicas) {
  794. return ReplicaSetsByCreationTimestamp(o).Less(j, i)
  795. }
  796. return *(o[i].Spec.Replicas) > *(o[j].Spec.Replicas)
  797. }
  798. // AddOrUpdateTaintOnNode add taints to the node. If taint was added into node, it'll issue API calls
  799. // to update nodes; otherwise, no API calls. Return error if any.
  800. func AddOrUpdateTaintOnNode(c clientset.Interface, nodeName string, taints ...*v1.Taint) error {
  801. if len(taints) == 0 {
  802. return nil
  803. }
  804. firstTry := true
  805. return clientretry.RetryOnConflict(UpdateTaintBackoff, func() error {
  806. var err error
  807. var oldNode *v1.Node
  808. // First we try getting node from the API server cache, as it's cheaper. If it fails
  809. // we get it from etcd to be sure to have fresh data.
  810. if firstTry {
  811. oldNode, err = c.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{ResourceVersion: "0"})
  812. firstTry = false
  813. } else {
  814. oldNode, err = c.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{})
  815. }
  816. if err != nil {
  817. return err
  818. }
  819. var newNode *v1.Node
  820. oldNodeCopy := oldNode
  821. updated := false
  822. for _, taint := range taints {
  823. curNewNode, ok, err := taintutils.AddOrUpdateTaint(oldNodeCopy, taint)
  824. if err != nil {
  825. return fmt.Errorf("failed to update taint of node")
  826. }
  827. updated = updated || ok
  828. newNode = curNewNode
  829. oldNodeCopy = curNewNode
  830. }
  831. if !updated {
  832. return nil
  833. }
  834. return PatchNodeTaints(c, nodeName, oldNode, newNode)
  835. })
  836. }
  837. // RemoveTaintOffNode is for cleaning up taints temporarily added to node,
  838. // won't fail if target taint doesn't exist or has been removed.
  839. // If passed a node it'll check if there's anything to be done, if taint is not present it won't issue
  840. // any API calls.
  841. func RemoveTaintOffNode(c clientset.Interface, nodeName string, node *v1.Node, taints ...*v1.Taint) error {
  842. if len(taints) == 0 {
  843. return nil
  844. }
  845. // Short circuit for limiting amount of API calls.
  846. if node != nil {
  847. match := false
  848. for _, taint := range taints {
  849. if taintutils.TaintExists(node.Spec.Taints, taint) {
  850. match = true
  851. break
  852. }
  853. }
  854. if !match {
  855. return nil
  856. }
  857. }
  858. firstTry := true
  859. return clientretry.RetryOnConflict(UpdateTaintBackoff, func() error {
  860. var err error
  861. var oldNode *v1.Node
  862. // First we try getting node from the API server cache, as it's cheaper. If it fails
  863. // we get it from etcd to be sure to have fresh data.
  864. if firstTry {
  865. oldNode, err = c.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{ResourceVersion: "0"})
  866. firstTry = false
  867. } else {
  868. oldNode, err = c.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{})
  869. }
  870. if err != nil {
  871. return err
  872. }
  873. var newNode *v1.Node
  874. oldNodeCopy := oldNode
  875. updated := false
  876. for _, taint := range taints {
  877. curNewNode, ok, err := taintutils.RemoveTaint(oldNodeCopy, taint)
  878. if err != nil {
  879. return fmt.Errorf("failed to remove taint of node")
  880. }
  881. updated = updated || ok
  882. newNode = curNewNode
  883. oldNodeCopy = curNewNode
  884. }
  885. if !updated {
  886. return nil
  887. }
  888. return PatchNodeTaints(c, nodeName, oldNode, newNode)
  889. })
  890. }
  891. // PatchNodeTaints patches node's taints.
  892. func PatchNodeTaints(c clientset.Interface, nodeName string, oldNode *v1.Node, newNode *v1.Node) error {
  893. oldData, err := json.Marshal(oldNode)
  894. if err != nil {
  895. return fmt.Errorf("failed to marshal old node %#v for node %q: %v", oldNode, nodeName, err)
  896. }
  897. newTaints := newNode.Spec.Taints
  898. newNodeClone := oldNode.DeepCopy()
  899. newNodeClone.Spec.Taints = newTaints
  900. newData, err := json.Marshal(newNodeClone)
  901. if err != nil {
  902. return fmt.Errorf("failed to marshal new node %#v for node %q: %v", newNodeClone, nodeName, err)
  903. }
  904. patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, v1.Node{})
  905. if err != nil {
  906. return fmt.Errorf("failed to create patch for node %q: %v", nodeName, err)
  907. }
  908. _, err = c.CoreV1().Nodes().Patch(nodeName, types.StrategicMergePatchType, patchBytes)
  909. return err
  910. }
  911. // WaitForCacheSync is a wrapper around cache.WaitForCacheSync that generates log messages
  912. // indicating that the controller identified by controllerName is waiting for syncs, followed by
  913. // either a successful or failed sync.
  914. func WaitForCacheSync(controllerName string, stopCh <-chan struct{}, cacheSyncs ...cache.InformerSynced) bool {
  915. klog.Infof("Waiting for caches to sync for %s controller", controllerName)
  916. if !cache.WaitForCacheSync(stopCh, cacheSyncs...) {
  917. utilruntime.HandleError(fmt.Errorf("unable to sync caches for %s controller", controllerName))
  918. return false
  919. }
  920. klog.Infof("Caches are synced for %s controller", controllerName)
  921. return true
  922. }
  923. // ComputeHash returns a hash value calculated from pod template and
  924. // a collisionCount to avoid hash collision. The hash will be safe encoded to
  925. // avoid bad words.
  926. func ComputeHash(template *v1.PodTemplateSpec, collisionCount *int32) string {
  927. podTemplateSpecHasher := fnv.New32a()
  928. hashutil.DeepHashObject(podTemplateSpecHasher, *template)
  929. // Add collisionCount in the hash if it exists.
  930. if collisionCount != nil {
  931. collisionCountBytes := make([]byte, 8)
  932. binary.LittleEndian.PutUint32(collisionCountBytes, uint32(*collisionCount))
  933. podTemplateSpecHasher.Write(collisionCountBytes)
  934. }
  935. return rand.SafeEncodeString(fmt.Sprint(podTemplateSpecHasher.Sum32()))
  936. }
  937. func AddOrUpdateLabelsOnNode(kubeClient clientset.Interface, nodeName string, labelsToUpdate map[string]string) error {
  938. firstTry := true
  939. return clientretry.RetryOnConflict(UpdateLabelBackoff, func() error {
  940. var err error
  941. var node *v1.Node
  942. // First we try getting node from the API server cache, as it's cheaper. If it fails
  943. // we get it from etcd to be sure to have fresh data.
  944. if firstTry {
  945. node, err = kubeClient.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{ResourceVersion: "0"})
  946. firstTry = false
  947. } else {
  948. node, err = kubeClient.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{})
  949. }
  950. if err != nil {
  951. return err
  952. }
  953. // Make a copy of the node and update the labels.
  954. newNode := node.DeepCopy()
  955. if newNode.Labels == nil {
  956. newNode.Labels = make(map[string]string)
  957. }
  958. for key, value := range labelsToUpdate {
  959. newNode.Labels[key] = value
  960. }
  961. oldData, err := json.Marshal(node)
  962. if err != nil {
  963. return fmt.Errorf("failed to marshal the existing node %#v: %v", node, err)
  964. }
  965. newData, err := json.Marshal(newNode)
  966. if err != nil {
  967. return fmt.Errorf("failed to marshal the new node %#v: %v", newNode, err)
  968. }
  969. patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, &v1.Node{})
  970. if err != nil {
  971. return fmt.Errorf("failed to create a two-way merge patch: %v", err)
  972. }
  973. if _, err := kubeClient.CoreV1().Nodes().Patch(node.Name, types.StrategicMergePatchType, patchBytes); err != nil {
  974. return fmt.Errorf("failed to patch the node: %v", err)
  975. }
  976. return nil
  977. })
  978. }
  979. func getOrCreateServiceAccount(coreClient v1core.CoreV1Interface, namespace, name string) (*v1.ServiceAccount, error) {
  980. sa, err := coreClient.ServiceAccounts(namespace).Get(name, metav1.GetOptions{})
  981. if err == nil {
  982. return sa, nil
  983. }
  984. if !apierrors.IsNotFound(err) {
  985. return nil, err
  986. }
  987. // Create the namespace if we can't verify it exists.
  988. // Tolerate errors, since we don't know whether this component has namespace creation permissions.
  989. if _, err := coreClient.Namespaces().Get(namespace, metav1.GetOptions{}); apierrors.IsNotFound(err) {
  990. if _, err = coreClient.Namespaces().Create(&v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}}); err != nil && !apierrors.IsAlreadyExists(err) {
  991. klog.Warningf("create non-exist namespace %s failed:%v", namespace, err)
  992. }
  993. }
  994. // Create the service account
  995. sa, err = coreClient.ServiceAccounts(namespace).Create(&v1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: name}})
  996. if apierrors.IsAlreadyExists(err) {
  997. // If we're racing to init and someone else already created it, re-fetch
  998. return coreClient.ServiceAccounts(namespace).Get(name, metav1.GetOptions{})
  999. }
  1000. return sa, err
  1001. }