pod_container_deletor.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 kubelet
  14. import (
  15. "sort"
  16. "github.com/golang/glog"
  17. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  18. "k8s.io/kubernetes/pkg/util/wait"
  19. )
  20. const (
  21. // The limit on the number of buffered container deletion requests
  22. // This number is a bit arbitrary and may be adjusted in the future.
  23. containerDeletorBufferLimit = 50
  24. )
  25. type containerStatusbyCreatedList []*kubecontainer.ContainerStatus
  26. type podContainerDeletor struct {
  27. worker chan<- kubecontainer.ContainerID
  28. containersToKeep int
  29. }
  30. func (a containerStatusbyCreatedList) Len() int { return len(a) }
  31. func (a containerStatusbyCreatedList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  32. func (a containerStatusbyCreatedList) Less(i, j int) bool { return a[i].CreatedAt.After(a[j].CreatedAt) }
  33. func newPodContainerDeletor(runtime kubecontainer.Runtime, containersToKeep int) *podContainerDeletor {
  34. buffer := make(chan kubecontainer.ContainerID, containerDeletorBufferLimit)
  35. go wait.Until(func() {
  36. for {
  37. select {
  38. case id := <-buffer:
  39. runtime.DeleteContainer(id)
  40. }
  41. }
  42. }, 0, wait.NeverStop)
  43. return &podContainerDeletor{
  44. worker: buffer,
  45. containersToKeep: containersToKeep,
  46. }
  47. }
  48. // getContainersToDeleteInPod returns the exited containers in a pod whose name matches the name inferred from filterContainerId (if not empty), ordered by the creation time from the latest to the earliest.
  49. // If filterContainerId is empty, all dead containers in the pod are returned.
  50. func getContainersToDeleteInPod(filterContainerId string, podStatus *kubecontainer.PodStatus, containersToKeep int) containerStatusbyCreatedList {
  51. matchedContainer := func(filterContainerId string, podStatus *kubecontainer.PodStatus) *kubecontainer.ContainerStatus {
  52. if filterContainerId == "" {
  53. return nil
  54. }
  55. for _, containerStatus := range podStatus.ContainerStatuses {
  56. if containerStatus.ID.ID == filterContainerId {
  57. return containerStatus
  58. }
  59. }
  60. return nil
  61. }(filterContainerId, podStatus)
  62. if filterContainerId != "" && matchedContainer == nil {
  63. glog.Warningf("Container %q not found in pod's containers", filterContainerId)
  64. return containerStatusbyCreatedList{}
  65. }
  66. // Find the exited containers whose name matches the name of the container with id being filterContainerId
  67. var candidates containerStatusbyCreatedList
  68. for _, containerStatus := range podStatus.ContainerStatuses {
  69. if containerStatus.State != kubecontainer.ContainerStateExited {
  70. continue
  71. }
  72. if matchedContainer == nil || matchedContainer.Name == containerStatus.Name {
  73. candidates = append(candidates, containerStatus)
  74. }
  75. }
  76. if len(candidates) <= containersToKeep {
  77. return containerStatusbyCreatedList{}
  78. }
  79. sort.Sort(candidates)
  80. return candidates[containersToKeep:]
  81. }
  82. // deleteContainersInPod issues container deletion requests for containers selected by getContainersToDeleteInPod.
  83. func (p *podContainerDeletor) deleteContainersInPod(filterContainerId string, podStatus *kubecontainer.PodStatus, removeAll bool) {
  84. containersToKeep := p.containersToKeep
  85. if removeAll {
  86. containersToKeep = 0
  87. }
  88. for _, candidate := range getContainersToDeleteInPod(filterContainerId, podStatus, containersToKeep) {
  89. select {
  90. case p.worker <- candidate.ID:
  91. default:
  92. glog.Warningf("Failed to issue the request to remove container %v", candidate.ID)
  93. }
  94. }
  95. }