/
main.go
166 lines (130 loc) · 5.03 KB
/
main.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"log"
"k8s.io/kubernetes/pkg/api"
client "github.com/kubernetes/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/watch"
"github.com/sroze/kubernetes-load-balancer-proxifier/reverseproxy"
"encoding/json"
"os"
"strings"
)
func main() {
clusterAddress := os.Getenv("CLUSTER_ADDRESS")
rootDns := os.Getenv("ROOT_DNS_DOMAIN")
if rootDns == "" {
log.Fatalln("You need to precise your root DNS name with the `ROOT_DNS_DOMAIN` environment variable")
}
config := client.Config{
Host: clusterAddress,
Insecure: os.Getenv("INSECURE_CLUSTER") == "true",
}
c, err := client.New(&config)
if err != nil {
log.Fatalln("Can't connect to Kubernetes API:", err)
}
w, err := c.Services(api.NamespaceAll).Watch(labels.Everything(), fields.Everything(), api.ListOptions{})
if err != nil {
log.Fatalln("Unable to watch services:", err)
}
log.Println("Watching services")
for event := range w.ResultChan() {
service, ok := event.Object.(*api.Service)
if !ok {
log.Println("Got a non-service object")
continue
}
if event.Type == watch.Added || event.Type == watch.Modified {
err := ReviewService(c, service, rootDns)
if err != nil {
log.Println("An error occured while updating service")
}
}
}
}
func ReviewService(client *client.Client, service *api.Service, rootDns string) (error) {
if service.Spec.Type != api.ServiceTypeLoadBalancer {
log.Println("Skipping service", service.ObjectMeta.Name, "as it is not a LoadBalancer")
return nil
}
// If there's an IP and/or DNS address in the load balancer status, skip it
if ServiceHasLoadBalancerAddress(service) {
log.Println("Skipping service", service.ObjectMeta.Name, "as it already have a LoadBalancer address")
return nil
}
log.Println("Service", service.ObjectMeta.Name, "needs to be reviewed")
// Get existing proxy configuration
var proxyConfiguration reverseproxy.Configuration
if jsonConfiguration, found := service.ObjectMeta.Annotations["kubernetesReverseproxy"]; found {
proxyConfiguration = reverseproxy.Configuration{}
if err := json.Unmarshal([]byte(jsonConfiguration), &proxyConfiguration); err != nil {
log.Println("Unable to unmarshal the configuration, keep the empty one")
}
} else {
log.Println("No `kubernetesReverseproxy` annotation found")
proxyConfiguration = reverseproxy.Configuration{}
}
// If configuration found, skip it
if len(proxyConfiguration.Hosts) == 0 {
// Create the expected hostname of the service
host := strings.Join([]string{
service.ObjectMeta.Name,
service.ObjectMeta.Namespace,
rootDns,
}, ".")
// Append the new host to the configuration
proxyConfiguration.Hosts = append(proxyConfiguration.Hosts, reverseproxy.Host{
Host: host,
Port: 80,
})
jsonConfiguration, err := json.Marshal(proxyConfiguration)
if err != nil {
log.Println("Unable to JSON-encode the proxy configuration: ", err)
return err
}
if service.ObjectMeta.Annotations == nil {
service.ObjectMeta.Annotations = map[string]string{}
}
service.ObjectMeta.Annotations["kubernetesReverseproxy"] = string(jsonConfiguration)
// Update the service
log.Println("Adding the `kubernetesReverseproxy` annotation to service")
updated, err := client.Services(service.ObjectMeta.Namespace).Update(service)
if err != nil {
log.Println("Error while updated the service:", err)
return err
}
log.Println("Successfully added the reverse proxy configuration", updated)
} else {
// Updating service load-balancer status
log.Println("Updating the service load-balancer status")
service.Status = api.ServiceStatus{
LoadBalancer: api.LoadBalancerStatus{
Ingress: []api.LoadBalancerIngress{
api.LoadBalancerIngress{
Hostname: proxyConfiguration.Hosts[0].Host,
},
},
},
}
updated, err := client.Services(service.ObjectMeta.Namespace).Update(service)
if err != nil {
log.Println("Error while updated the service:", err)
return err
}
log.Println("Successfully updated the service status", updated)
}
return nil
}
func ServiceHasLoadBalancerAddress(service *api.Service) bool {
if len(service.Status.LoadBalancer.Ingress) == 0 {
return false
}
for _, ingress := range service.Status.LoadBalancer.Ingress {
if ingress.IP != "" || ingress.Hostname != "" {
return true
}
}
return false
}