Skip to content

Commit ec805e6

Browse files
author
French Ben
committed
Standardized node IP with 1.13 support
Signed-off-by: French Ben <[email protected]>
1 parent dcd1a96 commit ec805e6

File tree

4 files changed

+24
-30
lines changed

4 files changed

+24
-30
lines changed

tools/metaserver/src/metaserver/azure.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,17 @@ func (a AzureWeb) Managers() []WebInstance {
7979
"AZURE_SUBSCRIPTION_ID": os.Getenv("SUBSCRIPTION_ID"),
8080
"AZURE_TENANT_ID": os.Getenv("TENANT_ID"),
8181
"AZURE_GROUP_NAME": os.Getenv("GROUP_NAME"),
82-
"AZURE_PREFIX": os.Getenv("PREFIX")}
82+
"AZURE_VMSS_MGR": os.Getenv("VMSS_MGR"),
83+
"AZURE_VMSS_WRK": os.Getenv("VMSS_WRK")}
8384
nicClient, vmssClient := initClients(env)
8485
// Get list of VMSS Network Interfaces for Managers
85-
managerIPTable, err := getVMSSNic(nicClient, env, "managervmss")
86+
managerIPTable, err := getVMSSNic(nicClient, env, env["AZURE_VMSS_MGR"])
8687
if err != nil {
8788
fmt.Printf("Couldn't get Manager Nic for VMSS: %v", err)
8889
return []WebInstance{}
8990
}
9091
// Get list of VMSS for Managers
91-
managerVMs, err := getVMSSList(vmssClient, env, "managervmss", managerIPTable)
92+
managerVMs, err := getVMSSList(vmssClient, env, env["AZURE_VMSS_MGR"], managerIPTable)
9293
if err != nil {
9394
fmt.Printf("Couldn't get List of Manager VMSS: %v", err)
9495
}
@@ -104,16 +105,17 @@ func (a AzureWeb) Workers() []WebInstance {
104105
"AZURE_SUBSCRIPTION_ID": os.Getenv("SUBSCRIPTION_ID"),
105106
"AZURE_TENANT_ID": os.Getenv("TENANT_ID"),
106107
"AZURE_GROUP_NAME": os.Getenv("GROUP_NAME"),
107-
"AZURE_PREFIX": os.Getenv("PREFIX")}
108+
"AZURE_VMSS_MGR": os.Getenv("VMSS_MGR"),
109+
"AZURE_VMSS_WRK": os.Getenv("VMSS_WRK")}
108110
nicClient, vmssClient := initClients(env)
109111
// Get list of VMSS Network Interfaces for Managers
110-
workerIPTable, err := getVMSSNic(nicClient, env, "worker-vmss")
112+
workerIPTable, err := getVMSSNic(nicClient, env, env["AZURE_VMSS_WRK"])
111113
if err != nil {
112-
fmt.Errorf("Couldn't get Worker Nic for VMSS: %v", err)
114+
fmt.Printf("Couldn't get Worker Nic for VMSS: %v", err)
113115
return []WebInstance{}
114116
}
115117
// Get list of VMSS for Managers
116-
workerVMs, err := getVMSSList(vmssClient, env, "worker-vmss", workerIPTable)
118+
workerVMs, err := getVMSSList(vmssClient, env, env["AZURE_VMSS_WRK"], workerIPTable)
117119
if err != nil {
118120
fmt.Printf("Couldn't get List of Worker VMSS: %v", err)
119121
}
@@ -136,8 +138,7 @@ func initClients(env map[string]string) (network.InterfacesClient, compute.Virtu
136138
return nicClient, vmssClient
137139
}
138140

139-
func getVMSSNic(client network.InterfacesClient, env map[string]string, vmType string) (IPTable map[string]string, err error) {
140-
vmss := fmt.Sprintf("%s-%s", env["AZURE_PREFIX"], vmType)
141+
func getVMSSNic(client network.InterfacesClient, env map[string]string, vmss string) (IPTable map[string]string, err error) {
141142
result, err := client.ListVirtualMachineScaleSetNetworkInterfaces(env["AZURE_GROUP_NAME"], vmss)
142143
if err != nil {
143144
// Message from an error.
@@ -151,7 +152,6 @@ func getVMSSNic(client network.InterfacesClient, env map[string]string, vmType s
151152
if *nic.Properties.Primary {
152153
for _, ipConfig := range *nic.Properties.IPConfigurations {
153154
if *ipConfig.Properties.Primary {
154-
fmt.Printf("Adding: %s to table at index: %s\n\n", *ipConfig.Properties.PrivateIPAddress, *nic.ID)
155155
IPTable[*nic.ID] = *ipConfig.Properties.PrivateIPAddress
156156
}
157157
}
@@ -160,8 +160,7 @@ func getVMSSNic(client network.InterfacesClient, env map[string]string, vmType s
160160
return IPTable, nil
161161
}
162162

163-
func getVMSSList(client compute.VirtualMachineScaleSetVMsClient, env map[string]string, vmType string, nicIPTable map[string]string) ([]WebInstance, error) {
164-
vmss := fmt.Sprintf("%s-%s", env["AZURE_PREFIX"], vmType)
163+
func getVMSSList(client compute.VirtualMachineScaleSetVMsClient, env map[string]string, vmss string, nicIPTable map[string]string) ([]WebInstance, error) {
165164
vms := []WebInstance{}
166165

167166
result, err := client.List(env["AZURE_GROUP_NAME"], vmss, "", "", "")

tools/metaserver/src/metaserver/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ func validate(flavor string) {
104104
os.Exit(1)
105105
}
106106
} else if flavor == "azure" {
107-
// add azure validation code here.
108-
if !checkEnvVars("APP_ID", "APP_SECRET", "SUBSCRIPTION_ID", "TENANT_ID", "GROUP_NAME", "PREFIX") {
107+
// make sure our required ENV variables are available, if not fail.
108+
if !checkEnvVars("APP_ID", "APP_SECRET", "SUBSCRIPTION_ID", "TENANT_ID", "GROUP_NAME", "VMSS_MGR", "VMSS_WRK") {
109109
os.Exit(1)
110110
}
111111
} else {

tools/metaserver/src/metaserver/utils.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,33 +123,28 @@ func SwarmNodes() []swarm.Node {
123123
return nodes
124124
}
125125

126+
func nodeIP(id string) string {
127+
cli, ctx := DockerClient()
128+
// inspect the node ID provided
129+
node, _, err := cli.NodeInspectWithRaw(ctx, id)
130+
if err != nil {
131+
return ""
132+
}
133+
return node.Status.Addr
134+
}
135+
126136
func alreadyInSwarm(ip string) bool {
127137
// Is the node making the request, already in the swarm.
128138
nodes := SwarmNodes()
129139
for _, node := range nodes {
130-
nodeIP := convertHostToIP(node.Description.Hostname)
140+
nodeIP := nodeIP(node.ID)
131141
if ip == nodeIP {
132142
return true
133143
}
134144
}
135145
return false
136146
}
137147

138-
func convertHostToIP(hostStr string) string {
139-
// This is risky, this assumes the following formation for hosts in swarm node ls
140-
// ip-10-0-3-149.ec2.internal
141-
// there was one use case when someone had an old account, and their hostnames were not
142-
// in this format. they just had
143-
// ip-192-168-33-67
144-
// not sure how many other formats there are.
145-
// This will work for both formats above.
146-
hostSplit := strings.Split(hostStr, ".")
147-
host := hostSplit[0]
148-
host = strings.Replace(host, "ip-", "", -1)
149-
ip := strings.Replace(host, "-", ".", -1)
150-
return ip
151-
}
152-
153148
func isNodeInList(ip string, instances []WebInstance) bool {
154149
// given an IP, find out if it is in the instance list.
155150
for _, instance := range instances {

0 commit comments

Comments
 (0)