-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes.go
137 lines (126 loc) · 3.44 KB
/
kubernetes.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/url"
"os"
"time"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/cache"
"github.com/GoogleCloudPlatform/kubernetes/pkg/controller/framework"
kcontrollerFramework "github.com/GoogleCloudPlatform/kubernetes/pkg/controller/framework"
kSelector "github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"golang.org/x/net/context"
)
const (
// Resync period for the kube controller loop.
resyncPeriod = 5 * time.Minute
)
type SyncHandler interface {
SetHost(host string, target *url.URL)
RemoveHost(host string)
}
type kubernetesServiceConfig struct {
Config struct {
} `json:"config,omitempty"`
Hosts []string `json:"hosts,omitempty"`
}
type kubernetesDataStore struct {
kubClient *kclient.Client
srvCache []*kapi.Service
}
func NewKubernetesDataStore() (*kubernetesDataStore, error) {
masterHost := os.Getenv("KUBERNETES_RO_SERVICE_HOST")
if masterHost == "" {
log.Fatalf("KUBERNETES_RO_SERVICE_HOST is not defined")
}
masterPort := os.Getenv("KUBERNETES_RO_SERVICE_PORT")
if masterPort == "" {
log.Fatalf("KUBERNETES_RO_SERVICE_PORT is not defined")
}
config := &kclient.Config{
Host: fmt.Sprintf("http://%s:%s", masterHost, masterPort),
Version: "v1beta3",
}
kubClient, err := kclient.New(config)
if err != nil {
return nil, err
}
return &kubernetesDataStore{
kubClient: kubClient,
}, nil
}
func (ds *kubernetesDataStore) createServiceLW() *cache.ListWatch {
return cache.NewListWatchFromClient(ds.kubClient, "services", kapi.NamespaceAll, kSelector.Everything())
}
func (ds *kubernetesDataStore) Watch(ctx context.Context, sh SyncHandler) {
var serviceController *kcontrollerFramework.Controller
_, serviceController = framework.NewInformer(
ds.createServiceLW(),
&kapi.Service{},
resyncPeriod,
framework.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if s, ok := obj.(*kapi.Service); ok {
ds.setService(s, sh)
}
},
DeleteFunc: func(obj interface{}) {
if s, ok := obj.(*kapi.Service); ok {
ds.removeService(s, sh)
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
if s, ok := newObj.(*kapi.Service); ok {
ds.setService(s, sh)
}
},
},
)
serviceController.Run(ctx.Done())
}
func (ds *kubernetesDataStore) setService(service *kapi.Service, sh SyncHandler) error {
if _, ok := service.Annotations["router"]; !ok {
return nil
}
var res kubernetesServiceConfig
if err := json.Unmarshal([]byte(service.Annotations["router"]), &res); err != nil {
return nil
}
for i := range res.Hosts {
host := res.Hosts[i]
if len(service.Spec.Ports) == 0 {
continue
}
target, err := url.Parse(
fmt.Sprintf(
"http://%s:%d",
service.Spec.PortalIP,
service.Spec.Ports[0].Port,
),
)
if err != nil {
return fmt.Errorf("cannot parse URL: %s", err)
}
sh.SetHost(host, target)
}
log.Printf("watcher: added %s", service.Name)
return nil
}
func (ds *kubernetesDataStore) removeService(service *kapi.Service, sh SyncHandler) error {
if _, ok := service.Annotations["router"]; !ok {
return nil
}
var res kubernetesServiceConfig
if err := json.Unmarshal([]byte(service.Annotations["router"]), &res); err != nil {
return nil
}
for i := range res.Hosts {
host := res.Hosts[i]
sh.RemoveHost(host)
}
log.Printf("watcher: removed %s", service.Name)
return nil
}