Skip to content

Commit e7538d4

Browse files
committed
Removed the debug endpoints, and cleaned up comments from Diogo, and Nathan
Signed-off-by: Ken Cochrane <[email protected]>
1 parent a2aed7c commit e7538d4

File tree

4 files changed

+41
-178
lines changed

4 files changed

+41
-178
lines changed

tools/metaserver/src/metaserver/aws.go

Lines changed: 26 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ type AWSWeb struct {
2626
func (a AWSWeb) TokenManager(w http.ResponseWriter, r *http.Request) {
2727
// get the swarm manager token, if they are a manager node,
2828
// and are not already in the swarm. Block otherwise
29-
GetRequestInfo(r)
30-
found := AlreadyInSwarm(r)
31-
isManager := IsManagerNode(r)
29+
RequestInfo(r)
30+
found := alreadyInSwarm(r)
31+
isManager := isManagerNode(r)
3232

3333
if found || !isManager {
3434
// they are either already in the swarm, or they are not a manager
3535
w.WriteHeader(http.StatusForbidden)
36-
fmt.Fprintf(w, "Access Denied\n")
36+
fmt.Fprintln(w, "Access Denied")
3737
return
3838
}
3939

4040
// They are not in the swarm, and they are a manager, so good to go.
41-
cli, ctx := GetDockerClient()
41+
cli, ctx := DockerClient()
4242
swarm, err := cli.SwarmInspect(ctx)
4343
if err != nil {
4444
w.WriteHeader(http.StatusInternalServerError)
@@ -51,20 +51,20 @@ func (a AWSWeb) TokenManager(w http.ResponseWriter, r *http.Request) {
5151
func (a AWSWeb) TokenWorker(w http.ResponseWriter, r *http.Request) {
5252
// get the swarm worker token, if they are a worker node,
5353
// and are not already in the swarm. block otherwise
54-
GetRequestInfo(r)
54+
RequestInfo(r)
5555

56-
found := AlreadyInSwarm(r)
57-
isWorker := IsWorkerNode(r)
56+
found := alreadyInSwarm(r)
57+
isWorker := isWorkerNode(r)
5858

5959
if found || !isWorker {
6060
// they are either already in the swarm, or they are not a worker
6161
w.WriteHeader(http.StatusForbidden)
62-
fmt.Fprintf(w, "Access Denied\n")
62+
fmt.Fprintln(w, "Access Denied")
6363
return
6464
}
6565

6666
// They are not in the swarm, and they are a worker, so good to go.
67-
cli, ctx := GetDockerClient()
67+
cli, ctx := DockerClient()
6868
swarm, err := cli.SwarmInspect(ctx)
6969
if err != nil {
7070
w.WriteHeader(http.StatusInternalServerError)
@@ -74,84 +74,23 @@ func (a AWSWeb) TokenWorker(w http.ResponseWriter, r *http.Request) {
7474
fmt.Fprintf(w, swarm.JoinTokens.Worker)
7575
}
7676

77-
func (a AWSWeb) CheckManager(w http.ResponseWriter, r *http.Request) {
78-
// check if this node is a manager and already in the swarm.
79-
GetRequestInfo(r)
80-
ip := GetRequestIP(r)
81-
found := AlreadyInSwarm(r)
82-
isManager := IsManagerNode(r)
83-
fmt.Fprintf(w, "IP:%s;InSwarm:%v;IsManager:%v;\n", ip, found, isManager)
84-
}
85-
86-
func (a AWSWeb) CheckWorker(w http.ResponseWriter, r *http.Request) {
87-
// check if this node is a worker, and already in the swarm.
88-
GetRequestInfo(r)
89-
ip := GetRequestIP(r)
90-
found := AlreadyInSwarm(r)
91-
isWorker := IsWorkerNode(r)
92-
fmt.Fprintf(w, "IP:%s;InSwarm:%v;IsWorker:%v;\n", ip, found, isWorker)
93-
}
94-
95-
func (a AWSWeb) Instances(w http.ResponseWriter, r *http.Request) {
96-
// show both manager and worker instances
97-
GetRequestInfo(r)
98-
fmt.Fprintf(w, "Managers: \n")
99-
instances := GetAWSManagers()
100-
for _, instance := range instances {
101-
fmt.Fprintf(w, "%s %s\n", instance.InstanceID, instance.PrivateIPAddress)
102-
}
103-
fmt.Fprintf(w, "Workers: \n")
104-
instances = GetAWSWorkers()
105-
for _, instance := range instances {
106-
fmt.Fprintf(w, "%s %s\n", instance.InstanceID, instance.PrivateIPAddress)
107-
}
108-
}
109-
110-
func (a AWSWeb) ManagerInstances(w http.ResponseWriter, r *http.Request) {
111-
// only show manager instances
112-
GetRequestInfo(r)
113-
instances := GetAWSManagers()
114-
for _, instance := range instances {
115-
fmt.Fprintf(w, "%s %s\n", instance.InstanceID, instance.PrivateIPAddress)
116-
}
117-
}
118-
119-
func (a AWSWeb) WorkerInstances(w http.ResponseWriter, r *http.Request) {
120-
// only show worker instances
121-
GetRequestInfo(r)
122-
instances := GetAWSWorkers()
123-
for _, instance := range instances {
124-
fmt.Fprintf(w, "%s %s\n", instance.InstanceID, instance.PrivateIPAddress)
125-
}
126-
}
127-
128-
func (a AWSWeb) Nodes(w http.ResponseWriter, r *http.Request) {
129-
// print the list of nodes in the swarm.
130-
GetRequestInfo(r)
131-
nodes := GetSwarmNodes()
132-
for _, node := range nodes {
133-
ip := ConvertAWSHostToIP(node.Description.Hostname)
134-
fmt.Fprintf(w, "%s %s %s\n", node.ID[:15], node.Description.Hostname, ip)
135-
}
136-
}
137-
138-
func AlreadyInSwarm(r *http.Request) bool {
77+
func alreadyInSwarm(r *http.Request) bool {
13978
// Is the node making the request, already in the swarm.
140-
ip := GetRequestIP(r)
141-
nodes := GetSwarmNodes()
79+
ip := RequestIP(r)
80+
nodes := SwarmNodes()
14281
for _, node := range nodes {
143-
nodeIP := ConvertAWSHostToIP(node.Description.Hostname)
82+
nodeIP := convertAWSHostToIP(node.Description.Hostname)
14483
if ip == nodeIP {
14584
return true
14685
}
14786
}
14887
return false
14988
}
15089

151-
func IsManagerNode(r *http.Request) bool {
90+
func isManagerNode(r *http.Request) bool {
15291
// Is the node making the request a manager node
153-
ip := GetRequestIP(r)
154-
instances := GetAWSManagers()
92+
ip := RequestIP(r)
93+
instances := awsManagers()
15594
for _, instance := range instances {
15695
if ip == instance.PrivateIPAddress {
15796
return true
@@ -160,10 +99,10 @@ func IsManagerNode(r *http.Request) bool {
16099
return false
161100
}
162101

163-
func IsWorkerNode(r *http.Request) bool {
102+
func isWorkerNode(r *http.Request) bool {
164103
// Is the node making the request a worker node
165-
ip := GetRequestIP(r)
166-
instances := GetAWSWorkers()
104+
ip := RequestIP(r)
105+
instances := awsWorkers()
167106
for _, instance := range instances {
168107
if ip == instance.PrivateIPAddress {
169108
return true
@@ -172,7 +111,7 @@ func IsWorkerNode(r *http.Request) bool {
172111
return false
173112
}
174113

175-
func GetAWSWorkers() []AwsInstance {
114+
func awsWorkers() []AwsInstance {
176115
// get the instances from AWS worker security group
177116

178117
customFilter := []*ec2.Filter{
@@ -188,10 +127,10 @@ func GetAWSWorkers() []AwsInstance {
188127
Values: []*string{aws.String(os.Getenv("WORKER_SECURITY_GROUP_ID"))},
189128
})
190129

191-
return getInstances(customFilter)
130+
return awsInstances(customFilter)
192131
}
193132

194-
func GetAWSManagers() []AwsInstance {
133+
func awsManagers() []AwsInstance {
195134
// get the instances from AWS Manager security group
196135

197136
customFilter := []*ec2.Filter{
@@ -207,10 +146,10 @@ func GetAWSManagers() []AwsInstance {
207146
Values: []*string{aws.String(os.Getenv("MANAGER_SECURITY_GROUP_ID"))},
208147
})
209148

210-
return getInstances(customFilter)
149+
return awsInstances(customFilter)
211150
}
212151

213-
func getInstances(customFilters []*ec2.Filter) []AwsInstance {
152+
func awsInstances(customFilters []*ec2.Filter) []AwsInstance {
214153
// get the instances from AWS, takes a filter to limit the results.
215154

216155
client := ec2.New(session.New(&aws.Config{}))
@@ -255,7 +194,7 @@ func getInstances(customFilters []*ec2.Filter) []AwsInstance {
255194
return instances
256195
}
257196

258-
func ConvertAWSHostToIP(hostStr string) string {
197+
func convertAWSHostToIP(hostStr string) string {
259198
// This is risky, this assumes the following formation for hosts in swarm node ls
260199
// ip-10-0-3-149.ec2.internal
261200
// there was one use case when someone had an old account, and their hostnames were not

tools/metaserver/src/metaserver/azure.go

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,67 +11,19 @@ type AzureWeb struct {
1111
//TODO: implement these using Azure specific API's
1212

1313
func (a AzureWeb) TokenManager(w http.ResponseWriter, r *http.Request) {
14-
GetRequestInfo(r)
14+
RequestInfo(r)
1515

1616
w.WriteHeader(http.StatusNotImplemented)
17-
fmt.Fprintf(w, "Not implmented Yet\n")
17+
fmt.Fprintf(w, "Not implmented Yet")
1818

1919
fmt.Println("Endpoint Hit: tokenManager")
2020
}
2121

2222
func (a AzureWeb) TokenWorker(w http.ResponseWriter, r *http.Request) {
23-
GetRequestInfo(r)
23+
RequestInfo(r)
2424

2525
w.WriteHeader(http.StatusNotImplemented)
26-
fmt.Fprintf(w, "Not implmented Yet\n")
26+
fmt.Fprintf(w, "Not implmented Yet")
2727

2828
fmt.Println("Endpoint Hit: tokenWorker")
2929
}
30-
31-
func (a AzureWeb) CheckManager(w http.ResponseWriter, r *http.Request) {
32-
GetRequestInfo(r)
33-
34-
w.WriteHeader(http.StatusNotImplemented)
35-
fmt.Fprintf(w, "Not implmented Yet\n")
36-
37-
}
38-
39-
func (a AzureWeb) CheckWorker(w http.ResponseWriter, r *http.Request) {
40-
GetRequestInfo(r)
41-
42-
w.WriteHeader(http.StatusNotImplemented)
43-
fmt.Fprintf(w, "Not implmented Yet\n")
44-
45-
}
46-
47-
func (a AzureWeb) Instances(w http.ResponseWriter, r *http.Request) {
48-
GetRequestInfo(r)
49-
50-
w.WriteHeader(http.StatusNotImplemented)
51-
fmt.Fprintf(w, "Not implmented Yet\n")
52-
53-
}
54-
55-
func (a AzureWeb) ManagerInstances(w http.ResponseWriter, r *http.Request) {
56-
GetRequestInfo(r)
57-
58-
w.WriteHeader(http.StatusNotImplemented)
59-
fmt.Fprintf(w, "Not implmented Yet\n")
60-
61-
}
62-
63-
func (a AzureWeb) WorkerInstances(w http.ResponseWriter, r *http.Request) {
64-
GetRequestInfo(r)
65-
66-
w.WriteHeader(http.StatusNotImplemented)
67-
fmt.Fprintf(w, "Not implmented Yet\n")
68-
69-
}
70-
71-
func (a AzureWeb) Nodes(w http.ResponseWriter, r *http.Request) {
72-
GetRequestInfo(r)
73-
74-
w.WriteHeader(http.StatusNotImplemented)
75-
fmt.Fprintf(w, "Not implmented Yet\n")
76-
77-
}

tools/metaserver/src/metaserver/main.go

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,24 @@ import (
1616
type Web interface {
1717
TokenManager(w http.ResponseWriter, r *http.Request)
1818
TokenWorker(w http.ResponseWriter, r *http.Request)
19-
CheckManager(w http.ResponseWriter, r *http.Request)
20-
CheckWorker(w http.ResponseWriter, r *http.Request)
21-
Instances(w http.ResponseWriter, r *http.Request)
22-
Nodes(w http.ResponseWriter, r *http.Request)
23-
ManagerInstances(w http.ResponseWriter, r *http.Request)
24-
WorkerInstances(w http.ResponseWriter, r *http.Request)
2519
}
2620

2721
func index(w http.ResponseWriter, r *http.Request) {
2822
// print the home index page.
29-
GetRequestInfo(r)
30-
fmt.Fprintf(w, "token/\ncheck/\nnodes/\ninstances/\n")
31-
}
32-
33-
func instanceIndex(w http.ResponseWriter, r *http.Request) {
34-
// print the instance index page.
35-
GetRequestInfo(r)
36-
fmt.Fprintf(w, "all/\nmanagers/\nworkers/\n")
23+
RequestInfo(r)
24+
fmt.Fprintln(w, "token/")
3725
}
3826

3927
func tokenIndex(w http.ResponseWriter, r *http.Request) {
4028
// print the token index page.
41-
GetRequestInfo(r)
42-
fmt.Fprintf(w, "manager/\nworker/\n")
43-
}
44-
45-
func check(w http.ResponseWriter, r *http.Request) {
46-
// print the check index page
47-
GetRequestInfo(r)
48-
fmt.Fprintf(w, "manager/\nworker/\n")
29+
RequestInfo(r)
30+
fmt.Fprintln(w, "manager/\nworker/")
4931
}
5032

5133
func errorHandler(w http.ResponseWriter, r *http.Request) {
5234
// handle the errors
5335
w.WriteHeader(http.StatusNotFound)
54-
fmt.Fprint(w, "404: Page not found")
36+
fmt.Fprintln(w, "404: Page not found")
5537
}
5638

5739
func handleRequests(flavor string) {
@@ -62,9 +44,7 @@ func handleRequests(flavor string) {
6244

6345
// common handlers
6446
r.HandleFunc("/", index)
65-
r.HandleFunc("/instances/", instanceIndex)
6647
r.HandleFunc("/token/", tokenIndex)
67-
r.HandleFunc("/check/", check)
6848

6949
// specific to the service
7050
var w Web
@@ -83,14 +63,6 @@ func handleRequests(flavor string) {
8363
r.HandleFunc("/token/manager/", w.TokenManager)
8464
r.HandleFunc("/token/worker/", w.TokenWorker)
8565

86-
// These are for debugging
87-
r.HandleFunc("/nodes/", w.Nodes) //swarm nodes
88-
r.HandleFunc("/instances/all/", w.Instances) //provider instances
89-
r.HandleFunc("/instances/managers/", w.ManagerInstances) //provider instances
90-
r.HandleFunc("/instances/workers/", w.WorkerInstances) //provider instances
91-
r.HandleFunc("/check/manager/", w.CheckManager) //would it pass manager check
92-
r.HandleFunc("/check/worker/", w.CheckWorker) //would it pass worker check
93-
9466
// setup the custom server.
9567
srv := &http.Server{
9668
Handler: r,

tools/metaserver/src/metaserver/utils.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const (
2121
clientVersion = "1.24" // docker client version
2222
)
2323

24-
func GetDockerClient() (client.APIClient, context.Context) {
24+
func DockerClient() (client.APIClient, context.Context) {
2525
// get the docker client
2626
tlsOptions := tlsconfig.Options{}
2727
host := "unix:///var/run/docker.sock"
@@ -33,7 +33,7 @@ func GetDockerClient() (client.APIClient, context.Context) {
3333
return dockerClient, ctx
3434
}
3535

36-
func GetRequestIP(r *http.Request) string {
36+
func RequestIP(r *http.Request) string {
3737
//given the request return the IP address of the requestor
3838
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
3939
return ip
@@ -89,7 +89,7 @@ func clientUserAgent() string {
8989
return fmt.Sprintf("Docker-Client/%s (%s)", clientVersion, runtime.GOOS)
9090
}
9191

92-
func GetRequestInfo(r *http.Request) {
92+
func RequestInfo(r *http.Request) {
9393
// Mostly used for debugging, we can cut this down,
9494
// since it isn't really used much anymore.
9595
fmt.Printf("Path:[%s] %s \n", r.Method, r.URL.Path)
@@ -105,11 +105,11 @@ func GetRequestInfo(r *http.Request) {
105105
fmt.Println(ip)
106106
}
107107
}
108-
fmt.Printf("\n")
108+
fmt.Println("")
109109
}
110110

111-
func GetSwarmNodes() []swarm.Node {
112-
cli, ctx := GetDockerClient()
111+
func SwarmNodes() []swarm.Node {
112+
cli, ctx := DockerClient()
113113

114114
// get the list of swarm nodes
115115
nodes, err := cli.NodeList(ctx, types.NodeListOptions{})

0 commit comments

Comments
 (0)