container_gc.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 container
  14. import (
  15. "fmt"
  16. "time"
  17. "k8s.io/klog"
  18. )
  19. // Specified a policy for garbage collecting containers.
  20. type ContainerGCPolicy struct {
  21. // Minimum age at which a container can be garbage collected, zero for no limit.
  22. MinAge time.Duration
  23. // Max number of dead containers any single pod (UID, container name) pair is
  24. // allowed to have, less than zero for no limit.
  25. MaxPerPodContainer int
  26. // Max number of total dead containers, less than zero for no limit.
  27. MaxContainers int
  28. }
  29. // Manages garbage collection of dead containers.
  30. //
  31. // Implementation is thread-compatible.
  32. type ContainerGC interface {
  33. // Garbage collect containers.
  34. GarbageCollect() error
  35. // Deletes all unused containers, including containers belonging to pods that are terminated but not deleted
  36. DeleteAllUnusedContainers() error
  37. }
  38. // SourcesReadyProvider knows how to determine if configuration sources are ready
  39. type SourcesReadyProvider interface {
  40. // AllReady returns true if the currently configured sources have all been seen.
  41. AllReady() bool
  42. }
  43. // TODO(vmarmol): Preferentially remove pod infra containers.
  44. type realContainerGC struct {
  45. // Container runtime
  46. runtime Runtime
  47. // Policy for garbage collection.
  48. policy ContainerGCPolicy
  49. // sourcesReadyProvider provides the readiness of kubelet configuration sources.
  50. sourcesReadyProvider SourcesReadyProvider
  51. }
  52. // New ContainerGC instance with the specified policy.
  53. func NewContainerGC(runtime Runtime, policy ContainerGCPolicy, sourcesReadyProvider SourcesReadyProvider) (ContainerGC, error) {
  54. if policy.MinAge < 0 {
  55. return nil, fmt.Errorf("invalid minimum garbage collection age: %v", policy.MinAge)
  56. }
  57. return &realContainerGC{
  58. runtime: runtime,
  59. policy: policy,
  60. sourcesReadyProvider: sourcesReadyProvider,
  61. }, nil
  62. }
  63. func (cgc *realContainerGC) GarbageCollect() error {
  64. return cgc.runtime.GarbageCollect(cgc.policy, cgc.sourcesReadyProvider.AllReady(), false)
  65. }
  66. func (cgc *realContainerGC) DeleteAllUnusedContainers() error {
  67. klog.Infof("attempting to delete unused containers")
  68. return cgc.runtime.GarbageCollect(cgc.policy, cgc.sourcesReadyProvider.AllReady(), true)
  69. }