/
applicationworker.go
236 lines (204 loc) · 5.86 KB
/
applicationworker.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright 2020 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package caasfirewallersidecar
import (
"strings"
"github.com/juju/errors"
"github.com/juju/worker/v3"
"github.com/juju/worker/v3/catacomb"
"github.com/juju/juju/caas"
"github.com/juju/juju/core/network"
"github.com/juju/juju/core/watcher"
)
type applicationWorker struct {
catacomb catacomb.Catacomb
controllerUUID string
modelUUID string
appName string
firewallerAPI CAASFirewallerAPI
broker CAASBroker
portMutator PortMutator
serviceUpdater ServiceUpdater
appWatcher watcher.NotifyWatcher
portsWatcher watcher.StringsWatcher
lifeGetter LifeGetter
initial bool
previouslyExposed bool
currentPorts network.GroupedPortRanges
logger Logger
}
func newApplicationWorker(
controllerUUID string,
modelUUID string,
appName string,
firewallerAPI CAASFirewallerAPI,
broker CAASBroker,
lifeGetter LifeGetter,
logger Logger,
) (worker.Worker, error) {
w := &applicationWorker{
controllerUUID: controllerUUID,
modelUUID: modelUUID,
appName: appName,
firewallerAPI: firewallerAPI,
broker: broker,
lifeGetter: lifeGetter,
initial: true,
logger: logger,
}
if err := catacomb.Invoke(catacomb.Plan{
Site: &w.catacomb,
Work: w.loop,
}); err != nil {
return nil, errors.Trace(err)
}
return w, nil
}
// Kill is part of the worker.Worker interface.
func (w *applicationWorker) Kill() {
w.catacomb.Kill(nil)
}
// Wait is part of the worker.Worker interface.
func (w *applicationWorker) Wait() error {
return w.catacomb.Wait()
}
func (w *applicationWorker) setUp() (err error) {
w.appWatcher, err = w.firewallerAPI.WatchApplication(w.appName)
if err != nil {
return errors.Trace(err)
}
if err := w.catacomb.Add(w.appWatcher); err != nil {
return errors.Trace(err)
}
w.portsWatcher, err = w.firewallerAPI.WatchOpenedPorts()
if err != nil {
return errors.Trace(err)
}
if err := w.catacomb.Add(w.portsWatcher); err != nil {
return errors.Trace(err)
}
// TODO(sidecar): support deployment other than statefulset
app := w.broker.Application(w.appName, caas.DeploymentStateful)
w.portMutator = app
w.serviceUpdater = app
if w.currentPorts, err = w.firewallerAPI.GetOpenedPorts(w.appName); err != nil {
return errors.Annotatef(err, "failed to get initial openned ports for application")
}
return nil
}
func (w *applicationWorker) loop() (err error) {
defer func() {
// If the application has been deleted, we can return nil.
if errors.IsNotFound(err) {
w.logger.Debugf("sidecar caas firewaller application %v has been removed", w.appName)
err = nil
}
}()
if err = w.setUp(); err != nil {
return errors.Trace(err)
}
for {
select {
case <-w.catacomb.Dying():
return w.catacomb.ErrDying()
case _, ok := <-w.appWatcher.Changes():
if !ok {
return errors.New("application watcher closed")
}
// We know this is a v2 charm at this point, because this child
// worker is only ever started for v2 charms.
if err := w.onApplicationChanged(); err != nil {
if strings.Contains(err.Error(), "unexpected EOF") {
return nil
}
return errors.Trace(err)
}
case _, ok := <-w.portsWatcher.Changes():
if !ok {
return errors.New("application watcher closed")
}
// TODO(sidecar): implement portWatcher to return application names having port changes,
/*
if !sets.NewString(changes...).Contains(w.appName){continue}
*/
if err := w.onPortChanged(); err != nil {
return errors.Trace(err)
}
}
}
}
func toServicePorts(in network.GroupedPortRanges) []caas.ServicePort {
ports := in.UniquePortRanges()
network.SortPortRanges(ports)
out := make([]caas.ServicePort, len(ports))
for i, p := range ports {
out[i] = caas.ServicePort{
// k8s complains about `/`.
Name: strings.Replace(p.String(), "/", "-", -1),
Port: p.FromPort,
TargetPort: p.ToPort,
Protocol: p.Protocol,
}
}
return out
}
func (w *applicationWorker) onPortChanged() error {
changedPortRanges, err := w.firewallerAPI.GetOpenedPorts(w.appName)
if err != nil {
return err
}
w.logger.Tracef("current port for app %q, %v", w.appName, w.currentPorts)
w.logger.Tracef("port changed for app %q, %v", w.appName, changedPortRanges)
if w.currentPorts.EqualTo(changedPortRanges) {
w.logger.Debugf("no port changes for app %q", w.appName)
return nil
}
err = w.portMutator.UpdatePorts(toServicePorts(changedPortRanges), false)
if errors.Is(err, errors.NotFound) {
return nil
}
if err != nil {
return errors.Annotatef(err, "cannot update service port for application %q", w.appName)
}
w.currentPorts = changedPortRanges
return nil
}
func (w *applicationWorker) onApplicationChanged() (err error) {
defer func() {
// Not found could be because the app got removed or there's
// no container service created yet as the app is still being set up.
if errors.IsNotFound(err) {
// Perhaps the app got removed while we were processing.
if _, err2 := w.lifeGetter.Life(w.appName); err2 != nil {
err = err2
return
}
// Ignore not found error because the ip could be not ready yet at this stage.
w.logger.Warningf("processing change for application %q, %v", w.appName, err)
err = nil
}
}()
exposed, err := w.firewallerAPI.IsExposed(w.appName)
if err != nil {
return errors.Trace(err)
}
if !w.initial && exposed == w.previouslyExposed {
return nil
}
w.initial = false
w.previouslyExposed = exposed
if exposed {
return errors.Trace(exposeService(w.serviceUpdater))
}
return errors.Trace(unExposeService(w.serviceUpdater))
}
func exposeService(app ServiceUpdater) error {
// TODO(sidecar): implement expose once it's modelled.
// app.UpdateService()
return nil
}
func unExposeService(app ServiceUpdater) error {
// TODO(sidecar): implement un-expose once it's modelled.
// app.UpdateService()
return nil
}