/
common.go
262 lines (230 loc) · 7.62 KB
/
common.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package container
import (
"sort"
"strconv"
"strings"
"syscall"
"time"
"github.com/neuvector/neuvector/share"
"github.com/neuvector/neuvector/share/system"
)
// const defaultContainerLogPath = "/var/lib/docker/containers/%s/%s-json.log"
const (
DockerUCPInstanceIDKey string = "com.docker.ucp.InstanceID"
DockerUCPVersionKey string = "com.docker.ucp.version"
DockerUCPCollectionKey string = "com.docker.ucp.collection"
DockerSwarmServiceKey string = "com.docker.swarm.service.name"
DockerSwarmTaskName string = "com.docker.swarm.task.name"
DockerSwarmTaskID string = "com.docker.swarm.task.id"
DockerComposeProjectKey string = "com.docker.compose.project"
DockerComposeServiceKey string = "com.docker.compose.service"
DockerIngressNetworkName string = "ingress"
DockerIngressSandboxName string = "ingress-sbox"
DockerIngressEPName string = "ingress-endpoint"
)
const (
RancherCNINetwork string = "io.rancher.cni.network"
RancherContainerNetwork string = "io.rancher.container.network"
RancherKeyContainerIP string = "io.rancher.container.ip"
RancherKeyContainerSystem string = "io.rancher.container.system"
RancherKeyContainerName string = "io.rancher.container.name"
RancherKeyStackName string = "io.rancher.stack.name"
RancherKeyStackServiceName string = "io.rancher.stack_service.name"
)
const (
RancherOverlayNetworkName string = "rancher"
)
const (
KubeKeyPodNamespace string = "io.kubernetes.pod.namespace"
KubeKeyPodName string = "io.kubernetes.pod.name"
KubeKeyDeployConfig string = "deploymentconfig"
KubeKeyPodHash string = "pod-template-hash"
KubeKeyContainerName string = "io.kubernetes.container.name"
KubeNamespaceSystem string = "kube-system"
KubeNamespaceCatalog string = "kube-service-catalog"
KubeContainerNamePod string = "POD"
KubeContainerNameProxy string = "kube-proxy"
KubePodNamePrefixProxy string = "kube-proxy"
)
const (
KubeLinkerdProxyName string = "linkerd-proxy"
KubeLinkerdSysPodNamespace string = "linkerd"
KubeIstioProxyName string = "istio-proxy"
KubeProxyMeshLoMacStr string = "6c:6b:73:74:%02x:%02x"
KubeProxyMeshLoMacPrefix string = "6c:6b:73:74"
KubeIstioSystemPodNamespace string = "istio-system"
KubeIstioSystemIngGwPrefix string = "istio-ingressgateway"
KubeIstioSystemEgGwPrefix string = "istio-egressgateway"
KubeAwsProxyName string = "envoy"
)
const (
OpenShiftPodImage string = "openshift/origin-pod"
OpenShiftPodTag string = "io.openshift.tags"
)
const (
KubeRancherPodNamespace string = "cattle-system"
KubeRancherKeyStackName string = "annotation.io.rancher.stack.name"
KubeRancherIngressNamespace string = "ingress-nginx"
)
const (
AliyunSystem string = "aliyun.system"
AliyunAddon string = "aliyun.addon"
)
const (
ECSAgentImagePrefix string = "amazon/amazon-ecs-agent:"
ECSTaskDefinition string = "com.amazonaws.ecs.task-definition-family"
ECSContainerName string = "com.amazonaws.ecs.container-name"
ECSCluster string = "com.amazonaws.ecs.cluster"
)
const (
PlatformContainerNone = ""
PlatformContainerNeuVector = "NeuVector"
PlatformContainerDockerUCPCtrl = "Docker-UCP-Controller"
PlatformContainerDockerUCPSwarm = "Docker-UCP-Swarm"
PlatformContainerDockerUCPOther = "Docker-UCP-Other"
PlatformContainerDockerDTR = "Docker-DTR"
PlatformContainerDockerSystem = "Docker-System" // generic
PlatformContainerRancherInfra = "Rancher"
PlatformContainerRancherAgent = "Rancher-Agent"
PlatformContainerRancherNetworkAgent = "Rancher-Network-Agent"
PlatformContainerOpenshift = "Openshift"
PlatformContainerKubeInfra = "Kubernetes"
PlatformContainerKubeInfraPause = "Kubernetes-System-POD"
PlatformContainerAliyunAgent = "Aliyu-Agent"
PlatformContainerAliyunAddon = "Aliyu-Addon"
PlatformContainerECSAgent = "ECS-Agent"
PlatformContainerIstioInfra = "Istio-System-POD"
PlatformContainerLinkerdInfra = "Linkerd-System-POD"
)
// var rt Runtime
const shortContainerIDLength int = 12
const clientConnectTimeout time.Duration = time.Duration(5 * time.Second)
func parsePortString(s string) (uint8, uint16) {
tokens := strings.Split(s, "/")
if len(tokens) < 2 {
return 0, 0
}
port, _ := strconv.Atoi(tokens[0])
if tokens[1] == "tcp" {
return syscall.IPPROTO_TCP, uint16(port)
} else {
return syscall.IPPROTO_UDP, uint16(port)
}
}
func trimContainerName(name string) string {
if len(name) > 0 && name[0] == '/' {
return name[1:]
} else {
return name
}
}
func ShortContainerId(id string) string {
if len(id) <= shortContainerIDLength {
return id
} else {
return id[:shortContainerIDLength]
}
}
func TrimImageID(in string) string {
return strings.TrimPrefix(in, "sha256:")
}
func TrimContainerImageVersion(image string) string {
var colon, slash int
if colon = strings.LastIndexByte(image, ':'); colon == -1 {
return image
}
if slash = strings.LastIndexByte(image, '/'); slash == -1 {
return image[:colon]
}
if colon > slash {
return image[:colon]
} else {
return image
}
}
func TrimContainerImageRepo(image string) string {
var slash int
if slash = strings.IndexByte(image, '/'); slash == -1 {
return image
}
// If string before the first / has ':' or '.', consider it as registry name
if s := strings.IndexAny(image[:slash], ":."); s == -1 {
return image
} else {
return image[slash+1:]
}
}
func SortContainers(cs []*ContainerMetaExtra) []*ContainerMetaExtra {
sort.Slice(cs, func(i, j int) bool {
return !cs[i].isChild && cs[j].isChild
})
return cs
}
func getDevice(id string, rt Runtime, sys *system.SystemTools) (*share.CLUSDevice, *ContainerMetaExtra, error) {
if info, err := rt.GetContainer(id); err == nil {
dev := &share.CLUSDevice{
ID: info.ID,
Name: info.Name,
Labels: info.Labels,
Pid: info.Pid,
SelfHostname: info.Hostname,
MemoryLimit: info.MemoryLimit,
CPUs: info.CPUs,
NetworkMode: info.NetMode,
PidMode: info.PidMode,
CreatedAt: info.CreatedAt,
StartedAt: info.StartedAt,
}
// Read address
ifaces := sys.GetGlobalAddrs(false)
dev.Ifaces = make(map[string][]share.CLUSIPAddr)
for name, addrs := range ifaces {
dev.Ifaces[name] = make([]share.CLUSIPAddr, len(addrs))
for i, addr := range addrs {
dev.Ifaces[name][i] = share.CLUSIPAddr{
IPNet: addr,
Scope: share.CLUSIPAddrScopeLocalhost,
}
}
}
return dev, info, nil
} else {
return nil, nil, err
}
}
/*
func (s *DockerDriver) GetContainerLogs(fromDocker bool, id string, start, limit int) ([]byte, error) {
log.WithFields(log.Fields{"fromdocker": fromDocker, "limit": limit, "id": id}).Debug("")
if fromDocker {
options := dockerclient.LogOptions{
Stdout: true,
Stderr: true,
Tail: int64(limit),
}
if r, err := s.Client.ContainerLogs(id, &options); err != nil {
return nil, fmt.Errorf("Get container log from docker fail:%v", err)
} else {
defer r.Close()
if dat, err := ioutil.ReadAll(r); err != nil {
return nil, fmt.Errorf("Read container log fail:%v", err)
} else {
return dat, nil
}
}
} else {
meta, err := s.GetContainerMeta(id)
if err != nil {
return nil, fmt.Errorf("Get container log path fail:%v", err)
}
// some version docker has no logpath field
if meta.LogPath == "" {
meta.LogPath = fmt.Sprintf(defaultContainerLogPath, id, id)
}
data, err := s.sys.ReadContainerFile(meta.LogPath, 1, start, limit)
if err != nil {
return nil, fmt.Errorf("ReadContainerFile return error:%v", err)
}
return data, nil
}
}
*/