11package daemon
22
33import (
4- "bufio"
5- "context"
6- "fmt"
7- "net"
8- "os"
9- "path/filepath"
10- "strings"
11- "time"
12-
134 "github.com/AliyunContainerService/terway/pkg/tracing"
145
15- "github.com/containernetworking/plugins/plugins/ipam/host-local/backend/disk"
16- dockerTypes "github.com/docker/docker/api/types"
17- "github.com/docker/docker/api/types/filters"
18- "github.com/docker/docker/client"
19- log "github.com/sirupsen/logrus"
20-
216 "github.com/AliyunContainerService/terway/pkg/link"
227 "github.com/AliyunContainerService/terway/types"
238)
249
2510const (
26- defaultPrefix = "cali"
27- defaultIpamPath = "/var/lib/cni/networks/"
11+ defaultPrefix = "cali"
2812)
2913
3014type vethResourceManager struct {
31- runtimeAPI containerRuntime
3215}
3316
3417func (* vethResourceManager ) Allocate (context * networkContext , prefer string ) (types.NetworkResource , error ) {
@@ -43,72 +26,6 @@ func (*vethResourceManager) Release(context *networkContext, resItem types.Resou
4326}
4427
4528func (f * vethResourceManager ) GarbageCollection (inUseResSet map [string ]types.ResourceItem , expireResSet map [string ]types.ResourceItem ) error {
46- // fixme do gc on cni binary
47- lock , err := disk .NewFileLock (defaultIpamPath )
48- if err != nil {
49- return err
50- }
51- defer lock .Close ()
52- err = lock .Lock ()
53- if err != nil {
54- return err
55- }
56- sandboxList , err := f .runtimeAPI .GetRunningSandbox ()
57- if err != nil {
58- return err
59- }
60-
61- sandboxStubSet := make (map [string ]interface {})
62- for _ , sandbox := range sandboxList {
63- sandboxStubSet [sandbox ] = struct {}{}
64- }
65-
66- files , err := os .ReadDir (defaultIpamPath )
67- if err != nil {
68- log .Errorf ("Failed to list files in %q: %v" , defaultIpamPath , err )
69- return fmt .Errorf ("failed to list files in %q: %v" , defaultIpamPath , err )
70- }
71-
72- // gather containerIDs for allocated ips
73- ipContainerIDMap := make (map [string ]string )
74- for _ , file := range files {
75- // skip non checkpoint file
76- if ip := net .ParseIP (file .Name ()); ip == nil {
77- continue
78- }
79-
80- ipamFile , err := os .Open (filepath .Join (defaultIpamPath , file .Name ()))
81- if err != nil {
82- log .Errorf ("failed to open ipam file: %v, %v" , file , err )
83- continue
84- }
85-
86- r := bufio .NewReader (ipamFile )
87- content , _ , err := r .ReadLine ()
88- if err != nil {
89- log .Errorf ("Failed to read file %v: %v" , file , err )
90- err = ipamFile .Close ()
91- if err != nil {
92- log .Errorf ("Failed to close file %v: %v" , file , err )
93- }
94- continue
95- }
96- err = ipamFile .Close ()
97- if err != nil {
98- log .Errorf ("Failed to close file %v: %v" , file , err )
99- }
100- ipContainerIDMap [file .Name ()] = strings .TrimSpace (string (content ))
101- }
102-
103- for ip , containerID := range ipContainerIDMap {
104- if _ , ok := sandboxStubSet [containerID ]; ! ok && containerID != "" {
105- log .Warnf ("detect ip address leak: %s, removing" , ip )
106- err := os .Remove (filepath .Join (defaultIpamPath , ip ))
107- if err != nil {
108- log .Errorf ("error remove leak ip: %s, err: %v" , ip , err )
109- }
110- }
111- }
11229 return nil
11330}
11431
@@ -124,63 +41,6 @@ func (f *vethResourceManager) Stat(context *networkContext, resID string) (types
12441}
12542
12643func newVPCResourceManager () (ResourceManager , error ) {
127- mgr := & vethResourceManager {
128- runtimeAPI : dockerRuntime {},
129- }
130-
44+ mgr := & vethResourceManager {}
13145 return mgr , nil
13246}
133-
134- type containerRuntime interface {
135- GetRunningSandbox () ([]string , error )
136- }
137-
138- type dockerRuntime struct {}
139-
140- func (dockerRuntime ) GetRunningSandbox () ([]string , error ) {
141- var containerList []string
142- // use env DOCKER_API_VERSION=v1.21 to specify the version to v1.21,
143- // otherwise, client can negotiate an appropriate version with server.
144- dockerCli , err := client .NewClientWithOpts (
145- client .FromEnv ,
146- client .WithAPIVersionNegotiation (),
147- )
148- if err != nil {
149- return containerList , fmt .Errorf ("error init docker client to restore local lease: %+v" , err )
150- }
151- defer dockerCli .Close ()
152-
153- timeoutContext , cancel := context .WithTimeout (context .Background (), time .Minute )
154- defer cancel ()
155- listFilter := filters .NewArgs ()
156- listFilter .Add ("label" , fmt .Sprintf ("%s=%s" , "io.kubernetes.docker.type" , "podsandbox" ))
157- sandboxContainer , err := dockerCli .ContainerList (timeoutContext ,
158- dockerTypes.ContainerListOptions {
159- Filters : listFilter ,
160- },
161- )
162- if err != nil {
163- return containerList , fmt .Errorf ("error get docker containers to restore local lease: %+v" , err )
164- }
165-
166- for _ , container := range sandboxContainer {
167- timeoutContext , cancel := context .WithTimeout (context .Background (), time .Minute )
168- containerInfo , err := dockerCli .ContainerInspect (timeoutContext , container .ID )
169- cancel ()
170- if err != nil {
171- return containerList , fmt .Errorf ("error get container info to cleanup: %+v" , err )
172- }
173- if ! containerInfo .State .Running {
174- continue
175- }
176- if containerInfo .NetworkSettings == nil ||
177- containerInfo .NetworkSettings .SandboxKey == "" ||
178- containerInfo .NetworkSettings .SandboxKey == "/var/run/docker/netns/default" {
179- continue
180- }
181-
182- log .Debugf ("get container for ipam gc: %+v" , container .Labels )
183- containerList = append (containerList , container .ID )
184- }
185- return containerList , nil
186- }
0 commit comments