/
cli.go
144 lines (115 loc) · 3.07 KB
/
cli.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
package cnsclient
import (
"fmt"
"os"
"sort"
"strings"
"time"
"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/restserver"
)
const (
getCmdArg = "get"
getAvailableArg = "Available"
getAllocatedArg = "Allocated"
getAllArg = "All"
getPendingReleaseArg = "PendingRelease"
getPodCmdArg = "getPodContexts"
getInMemoryData = "getInMemory"
releaseArg = "release"
eth0InterfaceName = "eth0"
azure0InterfaceName = "azure0"
envCNSIPAddress = "CNSIpAddress"
envCNSPort = "CNSPort"
)
var (
availableCmds = []string{
getCmdArg,
getPodCmdArg,
getInMemoryData,
}
getFlags = []string{
getAvailableArg,
getAllocatedArg,
getAllocatedArg,
}
)
func HandleCNSClientCommands(cmd, arg string) error {
cnsIPAddress := os.Getenv(envCNSIPAddress)
cnsPort := os.Getenv(envCNSPort)
cnsClient, err := InitCnsClient("http://"+cnsIPAddress+":"+cnsPort, 5*time.Second)
if err != nil {
return err
}
switch {
case strings.EqualFold(getCmdArg, cmd):
return getCmd(cnsClient, arg)
case strings.EqualFold(getPodCmdArg, cmd):
return getPodCmd(cnsClient)
case strings.EqualFold(getInMemoryData, cmd):
return getInMemory(cnsClient)
default:
return fmt.Errorf("No debug cmd supplied, options are: %v", getCmdArg)
}
}
func getCmd(client *CNSClient, arg string) error {
var states []string
switch arg {
case cns.Available:
states = append(states, cns.Available)
case cns.Allocated:
states = append(states, cns.Allocated)
case cns.PendingRelease:
states = append(states, cns.PendingRelease)
case cns.PendingProgramming:
states = append(states, cns.PendingProgramming)
default:
states = append(states, cns.Allocated)
states = append(states, cns.Available)
states = append(states, cns.PendingRelease)
states = append(states, cns.PendingProgramming)
}
addr, err := client.GetIPAddressesMatchingStates(states...)
if err != nil {
return err
}
printIPAddresses(addr)
return nil
}
// Sort the addresses based on IP, then write to stdout
func printIPAddresses(addrSlice []cns.IPConfigurationStatus) {
sort.Slice(addrSlice, func(i, j int) bool {
return addrSlice[i].IPAddress < addrSlice[j].IPAddress
})
for _, addr := range addrSlice {
cns.IPConfigurationStatus.String(addr)
}
}
func getPodCmd(client *CNSClient) error {
resp, err := client.GetPodOrchestratorContext()
if err != nil {
return err
}
printPodContext(resp)
return nil
}
func printPodContext(podContext map[string]string) {
var i = 1
for orchContext, podID := range podContext {
fmt.Println(i, " ", orchContext, " : ", podID)
i++
}
}
func getInMemory(client *CNSClient) error {
inmemoryData, err := client.GetHTTPServiceData()
if err != nil {
return err
}
printInMemoryStruct(inmemoryData.HttpRestServiceData)
return nil
}
func printInMemoryStruct(data restserver.HttpRestServiceData) {
fmt.Println("PodIPIDByOrchestratorContext: ", data.PodIPIDByOrchestratorContext)
fmt.Println("PodIPConfigState: ", data.PodIPConfigState)
fmt.Println("IPAMPoolMonitor: ", data.IPAMPoolMonitor)
}