...
1
16
17 package container
18
19 import (
20 "context"
21 "fmt"
22 "time"
23
24 "k8s.io/klog/v2"
25 )
26
27
28 type GCPolicy struct {
29
30 MinAge time.Duration
31
32
33
34 MaxPerPodContainer int
35
36
37 MaxContainers int
38 }
39
40
41
42
43 type GC interface {
44
45 GarbageCollect(ctx context.Context) error
46
47 DeleteAllUnusedContainers(ctx context.Context) error
48
49 IsContainerFsSeparateFromImageFs(ctx context.Context) bool
50 }
51
52
53 type SourcesReadyProvider interface {
54
55 AllReady() bool
56 }
57
58
59 type realContainerGC struct {
60
61 runtime Runtime
62
63
64 policy GCPolicy
65
66
67 sourcesReadyProvider SourcesReadyProvider
68 }
69
70
71 func NewContainerGC(runtime Runtime, policy GCPolicy, sourcesReadyProvider SourcesReadyProvider) (GC, error) {
72 if policy.MinAge < 0 {
73 return nil, fmt.Errorf("invalid minimum garbage collection age: %v", policy.MinAge)
74 }
75
76 return &realContainerGC{
77 runtime: runtime,
78 policy: policy,
79 sourcesReadyProvider: sourcesReadyProvider,
80 }, nil
81 }
82
83 func (cgc *realContainerGC) GarbageCollect(ctx context.Context) error {
84 return cgc.runtime.GarbageCollect(ctx, cgc.policy, cgc.sourcesReadyProvider.AllReady(), false)
85 }
86
87 func (cgc *realContainerGC) DeleteAllUnusedContainers(ctx context.Context) error {
88 klog.InfoS("Attempting to delete unused containers")
89 return cgc.runtime.GarbageCollect(ctx, cgc.policy, cgc.sourcesReadyProvider.AllReady(), true)
90 }
91
92 func (cgc *realContainerGC) IsContainerFsSeparateFromImageFs(ctx context.Context) bool {
93 resp, err := cgc.runtime.ImageFsInfo(ctx)
94 if err != nil {
95 return false
96 }
97
98 if resp.ContainerFilesystems == nil || resp.ImageFilesystems == nil || len(resp.ContainerFilesystems) == 0 || len(resp.ImageFilesystems) == 0 {
99 return false
100 }
101
102
103
104
105 if resp.ContainerFilesystems[0].FsId != nil && resp.ImageFilesystems[0].FsId != nil {
106 return resp.ContainerFilesystems[0].FsId.Mountpoint != resp.ImageFilesystems[0].FsId.Mountpoint
107 }
108 return false
109 }
110
View as plain text