pod.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. Copyright 2016 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 pod
  14. import (
  15. "fmt"
  16. "hash/adler32"
  17. "time"
  18. "github.com/golang/glog"
  19. "k8s.io/kubernetes/pkg/api"
  20. "k8s.io/kubernetes/pkg/api/errors"
  21. unversionedcore "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/unversioned"
  22. errorsutil "k8s.io/kubernetes/pkg/util/errors"
  23. hashutil "k8s.io/kubernetes/pkg/util/hash"
  24. "k8s.io/kubernetes/pkg/util/wait"
  25. )
  26. func GetPodTemplateSpecHash(template api.PodTemplateSpec) uint32 {
  27. podTemplateSpecHasher := adler32.New()
  28. hashutil.DeepHashObject(podTemplateSpecHasher, template)
  29. return podTemplateSpecHasher.Sum32()
  30. }
  31. // TODO: use client library instead when it starts to support update retries
  32. // see https://github.com/kubernetes/kubernetes/issues/21479
  33. type updatePodFunc func(pod *api.Pod) error
  34. // UpdatePodWithRetries updates a pod with given applyUpdate function. Note that pod not found error is ignored.
  35. // The returned bool value can be used to tell if the pod is actually updated.
  36. func UpdatePodWithRetries(podClient unversionedcore.PodInterface, pod *api.Pod, applyUpdate updatePodFunc) (*api.Pod, bool, error) {
  37. var err error
  38. var podUpdated bool
  39. oldPod := pod
  40. if err = wait.Poll(10*time.Millisecond, 1*time.Minute, func() (bool, error) {
  41. pod, err = podClient.Get(oldPod.Name)
  42. if err != nil {
  43. return false, err
  44. }
  45. // Apply the update, then attempt to push it to the apiserver.
  46. if err = applyUpdate(pod); err != nil {
  47. return false, err
  48. }
  49. if pod, err = podClient.Update(pod); err == nil {
  50. // Update successful.
  51. return true, nil
  52. }
  53. // TODO: don't retry on perm-failed errors and handle them gracefully
  54. // Update could have failed due to conflict error. Try again.
  55. return false, nil
  56. }); err == nil {
  57. // When there's no error, we've updated this pod.
  58. podUpdated = true
  59. }
  60. // Handle returned error from wait poll
  61. if err == wait.ErrWaitTimeout {
  62. err = fmt.Errorf("timed out trying to update pod: %#v", oldPod)
  63. }
  64. // Ignore the pod not found error, but the pod isn't updated.
  65. if errors.IsNotFound(err) {
  66. glog.V(4).Infof("%s %s/%s is not found, skip updating it.", oldPod.Kind, oldPod.Namespace, oldPod.Name)
  67. err = nil
  68. }
  69. // Ignore the precondition violated error, but the pod isn't updated.
  70. if err == errorsutil.ErrPreconditionViolated {
  71. glog.V(4).Infof("%s %s/%s precondition doesn't hold, skip updating it.", oldPod.Kind, oldPod.Namespace, oldPod.Name)
  72. err = nil
  73. }
  74. // If the error is non-nil the returned pod cannot be trusted; if podUpdated is false, the pod isn't updated;
  75. // if the error is nil and podUpdated is true, the returned pod contains the applied update.
  76. return pod, podUpdated, err
  77. }
  78. // Filter uses the input function f to filter the given pod list, and return the filtered pods
  79. func Filter(podList *api.PodList, f func(api.Pod) bool) []api.Pod {
  80. pods := make([]api.Pod, 0)
  81. for _, p := range podList.Items {
  82. if f(p) {
  83. pods = append(pods, p)
  84. }
  85. }
  86. return pods
  87. }