/
nginx.go
75 lines (68 loc) · 1.87 KB
/
nginx.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
package main
import (
"fmt"
"os"
"text/template"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
)
type ServerPort struct {
ServerAddress string
ServicePort int
}
type ServiceServers struct {
ServiceName string
ServerPorts []ServerPort
}
func main() {
config := &client.Config{
Host: "http://localhost:8080",
Username: "test",
Password: "password",
}
client, err := client.New(config)
if err != nil {
// handle error
}
//TODO: have namespace a flag for the script
ns := api.NamespaceDefault
list, err := client.Services(ns).List(labels.Everything())
if err != nil {
fmt.Println(err)
}
//TODO: allow selction of nodes by labels or fields
nodes, err := client.Nodes().List(labels.Everything(), fields.Everything())
if err != nil {
fmt.Println(err)
}
//TODO: this data structure is a mess. Maybe put in Kubernetes
// client helper methods for some of these data points
var templateServiceServers []ServiceServers
for _, service := range list.Items {
if service.Name == "kubernetes" {
// TODO: better way of filtering out the kubernetes service?
continue
}
s := &ServiceServers{ServiceName: service.Name}
sp := &ServerPort{}
for _, port := range service.Spec.Ports {
sp.ServicePort = port.NodePort
}
for _, node := range nodes.Items {
for _, address := range node.Status.Addresses {
sp.ServerAddress = address.Address
}
}
s.ServerPorts = append(s.ServerPorts, *sp)
templateServiceServers = append(templateServiceServers, *s)
}
// TODO: allow your own template file
t := template.New("Nginx config template")
t, err = t.ParseFiles("nginx.tmpl")
if err != nil {
fmt.Println(err)
}
t.ExecuteTemplate(os.Stdout, "nginx.tmpl", templateServiceServers)
}