forked from aws/amazon-vpc-cni-k8s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify-aws.go
152 lines (121 loc) · 3.38 KB
/
verify-aws.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
package main
import (
"fmt"
"time"
"github.com/aws/amazon-ecs-cni-plugins/pkg/logger"
"github.com/aws/amazon-vpc-cni-k8s/pkg/awsutils"
log "github.com/cihub/seelog"
"math/rand"
)
var testEC2Instance *awsutils.EC2InstanceMetadataCache
const (
defaultLogFilePath = "/var/log/verify-aws.log"
)
func main() {
var testOK = true
defer log.Flush()
logger.SetupLogger(logger.GetLogFileLocation(defaultLogFilePath))
// verify API: GetInstanceMetadata
testEC2Instance, _ = awsutils.New()
testENIs, _ := testEC2Instance.GetAttachedENIs()
log.Infof("Number of current attached interface is %d \n", len(testENIs))
// detach and delete all interfaces except primary interface
for _, eni := range testENIs {
log.Infof("*eni %s, primary eni %s\n", eni.ENIID, testEC2Instance.GetPrimaryENI())
if eni.ENIID == testEC2Instance.GetPrimaryENI() {
log.Infof("skip deleting primary ENI %s\n", eni.ENIID)
continue
}
testEC2Instance.FreeENI(eni.ENIID)
}
// verify only primary interface left
testENIs, err := testEC2Instance.GetAttachedENIs()
if err != nil {
fmt.Print(" err: GetAttachedENIs()", err.Error())
testOK = false
}
log.Infof("Number of current attached interface is %d \n", len(testENIs))
if len(testENIs) != 1 {
fmt.Println("=== Failed: GetInstanceMetadata, expected only primary interface")
testOK = false
} else {
fmt.Println("=== Passed OK: GetInstanceMetadata")
}
// verify allocate IP address
testEC2Instance.AllocAllIPAddress(testEC2Instance.GetPrimaryENI())
//verify allocate MAX of ENI
if verifyFreeENIAWSAllocENI() != true {
testOK = false
fmt.Println("==== Failed: verifyFreeENIAWSAllocENI")
} else {
fmt.Println("===== Passed OK: verifyFreeENIAWSAllocENI")
}
log.Infof("DONE: verify-aws testOK %v", testOK)
}
func verifyFreeENIAWSAllocENI() bool {
var enis []string
var result = true
//verify allocate MAX number of ENI
for {
result, eni := verifyAllocENI(false)
if !result {
break
}
fmt.Println("eni", eni)
enis = append(enis, eni)
}
if len(enis) > 0 {
//pick radom eni to delete
eniIdx := rand.Intn(len(enis))
log.Debugf("Verify free eniIdx %d, eni : %s", eniIdx, enis[eniIdx])
testEC2Instance.FreeENI(enis[eniIdx])
result, _ = verifyAllocENI(true)
}
return result
}
func verifyAllocENI(printResult bool) (bool, string) {
// verify API: AllocENI
eni, err := testEC2Instance.AllocENI()
if err != nil {
log.Errorf("verifyAllocENI: error from AllocENI %s", err)
}
// will retry maximum of 5 * 5 seconds and see if the newly created ENI showing up on the instance
result := false
if eni != "" && err == nil {
err = testEC2Instance.AllocAllIPAddress(eni)
if err != nil {
log.Error("error on AllocAllIPAddress", err)
}
retry := 0
for {
retry++
if retry > 5 {
log.Info("exceed retry limit on GetInstanceMetaData")
break
}
testENIs, _ := testEC2Instance.GetAttachedENIs()
// verify eni is in the returned eni list
for _, returnedENI := range testENIs {
if eni == returnedENI.ENIID {
addrs, _, _ := testEC2Instance.DescribeENI(eni)
log.Infof("Number of address %d on eni %s", len(addrs), eni)
result = true
break
}
}
if result != true {
time.Sleep(5 * time.Second)
continue
}
break
}
}
if printResult {
if result == true {
log.Info("=== Passed OK: AllocENI")
} else {
log.Info("=== Failed: AllocENI %v", err)
}
}
return result, eni
}