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