-
Notifications
You must be signed in to change notification settings - Fork 1
/
updater.go
151 lines (136 loc) · 3.84 KB
/
updater.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
package main
import (
"fmt"
"time"
log "github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
)
const (
eventCreate = "create"
eventUpdate = "update"
eventDelete = "delete"
serviceAnnotationPath = "service.announser/zookeeper-path"
serviceAnnotationPortName = "service.announser/portname"
)
func checkRequiredServiceFieldsExists(service *v1.Service) error {
annotations := service.GetAnnotations()
if _, ok := annotations[serviceAnnotationPortName]; !ok {
return fmt.Errorf("missing annotation %v", serviceAnnotationPortName)
}
if _, ok := annotations[serviceAnnotationPath]; !ok {
return fmt.Errorf("missing annotation %v", serviceAnnotationPath)
}
if service.Spec.Type != "LoadBalancer" {
return fmt.Errorf("only type LoadBalancer supported. %v not supported yet", service.Spec.Type)
}
return nil
}
func getServicePortByName(name string, service *v1.Service) *v1.ServicePort {
for _, port := range service.Spec.Ports {
if port.Name == name {
return &port
}
}
return nil
}
func getServiceAddr(service *v1.Service) string {
for _, val := range service.Status.LoadBalancer.Ingress {
if val.Hostname != "" {
return val.Hostname
} else if val.IP != "" {
return val.IP
}
}
return ""
}
func newUpdaterEvent(eventType string, service *v1.Service) (*UpdaterEvent, error) {
err := checkRequiredServiceFieldsExists(service)
if err != nil {
return nil, fmt.Errorf("error service %v, err: %v", service.GetName(), err.Error())
}
annotations := service.GetAnnotations()
member := newZKMember()
member.path = annotations[serviceAnnotationPath]
member.name = service.GetName()
member.prefix = service.GetResourceVersion()
portname := annotations[serviceAnnotationPortName]
port := getServicePortByName(portname, service)
if port == nil {
return nil, fmt.Errorf("service named missing port")
}
serviceAddr := getServiceAddr(service)
if serviceAddr == "" {
return nil, fmt.Errorf("missing LoadBalancer hostname or ip will retry")
}
member.addServiceEndpoint(
portname,
serviceAddr,
int(port.Port),
)
event := UpdaterEvent{
eventType: eventType,
member: member,
retryCount: 5,
retryWait: 5 * time.Second,
}
return &event, nil
}
// UpdaterEvent create/update/delete of zkmember
type UpdaterEvent struct {
eventType string // create/update/delete
member *zkMember
retryCount int
retryWait time.Duration
}
func newUpdater(zookeeperAddr string) *Updater {
updater := Updater{
events: make(chan UpdaterEvent),
zookeeperAddr: zookeeperAddr,
}
updater.zookeeper.Init()
return &updater
}
// Updater event worker
type Updater struct {
events chan UpdaterEvent
zookeeper Zoo
zookeeperAddr string
}
// Run starts to wait for events and executes them
func (u *Updater) Run(stopCh chan struct{}) {
log.Info("Starting Updater")
err := u.zookeeper.Conn(u.zookeeperAddr)
if err != nil {
log.Errorf("failed to connect to zookeeper: %v", err.Error())
close(stopCh)
}
for {
select {
case event := <-u.events:
log.Debugf("process event: %v service: %v", event.eventType, event.member.name)
switch event.eventType {
case eventCreate:
log.Debugf("create event")
err := u.zookeeper.AddServiceMember(event.member)
if err != nil {
log.Errorf("failed to create member: %v %v", event.member.name, err.Error())
}
case eventUpdate:
log.Debugf("update event not implemented")
err := u.zookeeper.AddServiceMember(event.member)
if err != nil {
log.Errorf("failed to update member: %v %v", event.member.name, err.Error())
}
case eventDelete:
log.Debugf("delete event")
err := u.zookeeper.DeleteServiceMember(event.member)
if err != nil {
log.Errorf("failed to delete member: %v %v", event.member.name, err.Error())
}
}
case _ = <-stopCh:
log.Info("stopping updater runner")
return
}
}
}