container_gc.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. )
  18. // Specified a policy for garbage collecting containers.
  19. type ContainerGCPolicy struct {
  20. // Minimum age at which a container can be garbage collected, zero for no limit.
  21. MinAge time.Duration
  22. // Max number of dead containers any single pod (UID, container name) pair is
  23. // allowed to have, less than zero for no limit.
  24. MaxPerPodContainer int
  25. // Max number of total dead containers, less than zero for no limit.
  26. MaxContainers int
  27. }
  28. // Manages garbage collection of dead containers.
  29. //
  30. // Implementation is thread-compatible.
  31. type ContainerGC interface {
  32. // Garbage collect containers.
  33. GarbageCollect(allSourcesReady bool) error
  34. }
  35. // TODO(vmarmol): Preferentially remove pod infra containers.
  36. type realContainerGC struct {
  37. // Container runtime
  38. runtime Runtime
  39. // Policy for garbage collection.
  40. policy ContainerGCPolicy
  41. }
  42. // New ContainerGC instance with the specified policy.
  43. func NewContainerGC(runtime Runtime, policy ContainerGCPolicy) (ContainerGC, error) {
  44. if policy.MinAge < 0 {
  45. return nil, fmt.Errorf("invalid minimum garbage collection age: %v", policy.MinAge)
  46. }
  47. return &realContainerGC{
  48. runtime: runtime,
  49. policy: policy,
  50. }, nil
  51. }
  52. func (cgc *realContainerGC) GarbageCollect(allSourcesReady bool) error {
  53. return cgc.runtime.GarbageCollect(cgc.policy, allSourcesReady)
  54. }