-
Notifications
You must be signed in to change notification settings - Fork 599
/
common.go
86 lines (72 loc) · 2.17 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
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package platform
import (
"context"
"time"
"github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/ecscni"
"github.com/containernetworking/cni/pkg/types"
)
const (
// Identifiers for each platform we support.
WarmpoolDebugPlatform = "ec2-debug-warmpool"
FirecrackerDebugPlatform = "ec2-debug-firecracker"
WarmpoolPlatform = "warmpool"
FirecrackerPlatform = "firecracker"
)
// executeCNIPlugin executes CNI plugins with the given network configs and a timeout context.
func (c *common) executeCNIPlugin(
ctx context.Context,
add bool,
cniNetConf ...ecscni.PluginConfig,
) ([]*types.Result, error) {
var timeout time.Duration
var results []*types.Result
var err error
if add {
timeout = nsSetupTimeoutDuration
} else {
timeout = nsCleanupTimeoutDuration
}
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
for _, cfg := range cniNetConf {
if add {
var addResult types.Result
addResult, err = c.cniClient.Add(ctx, cfg)
if addResult != nil {
results = append(results, &addResult)
}
} else {
err = c.cniClient.Del(ctx, cfg)
}
if err != nil {
break
}
}
return results, err
}
// interfacesMACToName lists all network interfaces on the host inside the default
// netns and returns a mac address to device name map.
func (c *common) interfacesMACToName() (map[string]string, error) {
links, err := c.net.Interfaces()
if err != nil {
return nil, err
}
// Build a map of interface MAC address to name on the host.
macToName := make(map[string]string)
for _, link := range links {
macToName[link.HardwareAddr.String()] = link.Name
}
return macToName, nil
}