/
controller.go
293 lines (231 loc) · 7.98 KB
/
controller.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gateway
import (
"context"
"fmt"
"os"
"sync/atomic"
"github.com/pkg/errors"
"github.com/submariner-io/admiral/pkg/log"
"github.com/submariner-io/admiral/pkg/workqueue"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/cache"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)
var logger = log.Logger{Logger: logf.Log.WithName("Gateway")}
type Controller struct {
informer cache.Controller
store cache.Store
queue workqueue.Interface
stopCh chan struct{}
clusterStatusMap atomic.Value
localClusterID atomic.Value
gatewayAvailable bool
}
func NewController() *Controller {
controller := &Controller{
queue: workqueue.New("Gateway Controller"),
stopCh: make(chan struct{}),
gatewayAvailable: true,
}
controller.clusterStatusMap.Store(make(map[string]bool))
localClusterID := os.Getenv("SUBMARINER_CLUSTERID")
logger.Infof("Setting localClusterID from env: %q", localClusterID)
controller.localClusterID.Store(localClusterID)
return controller
}
func (c *Controller) Start(client dynamic.Interface) error {
gwClientset, err := c.getCheckedClientset(client)
if apierrors.IsNotFound(err) {
logger.Infof("Connectivity component is not installed, disabling Gateway status controller")
c.gatewayAvailable = false
return nil
}
if err != nil {
return err
}
logger.Infof("Starting Gateway status Controller")
c.store, c.informer = cache.NewInformer(&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
return gwClientset.List(context.TODO(), options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return gwClientset.Watch(context.TODO(), options)
},
}, &unstructured.Unstructured{}, 0, cache.ResourceEventHandlerFuncs{
AddFunc: c.queue.Enqueue,
UpdateFunc: func(_ interface{}, newObj interface{}) {
c.queue.Enqueue(newObj)
},
DeleteFunc: func(obj interface{}) {
key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
logger.V(log.DEBUG).Infof("GatewayStatus %q deleted", key)
},
})
go c.informer.Run(c.stopCh)
if ok := cache.WaitForCacheSync(c.stopCh, c.informer.HasSynced); !ok {
return fmt.Errorf("failed to wait for informer cache to sync")
}
go c.queue.Run(c.stopCh, c.processNextGateway)
return nil
}
func (c *Controller) Stop() {
close(c.stopCh)
c.queue.ShutDown()
logger.Infof("Gateway status Controller stopped")
}
func (c *Controller) processNextGateway(key, _, _ string) (bool, error) {
obj, exists, err := c.store.GetByKey(key)
if err != nil {
// requeue the item to work on later.
return true, errors.Wrapf(err, "error retrieving Gateway with key %q from the cache", key)
}
if exists {
c.gatewayCreatedOrUpdated(obj.(*unstructured.Unstructured))
}
return false, nil
}
func (c *Controller) gatewayCreatedOrUpdated(obj *unstructured.Unstructured) {
connections, localClusterID, ok := getGatewayStatus(obj)
if !ok {
return
}
// Updating
c.updateLocalClusterIDIfNeeded(localClusterID)
c.updateClusterStatusMap(connections)
}
func (c *Controller) updateClusterStatusMap(connections []interface{}) {
var newMap map[string]bool
currentMap := c.getClusterStatusMap()
for _, connection := range connections {
connectionMap := connection.(map[string]interface{})
status, found, err := unstructured.NestedString(connectionMap, "status")
if err != nil || !found {
logger.Errorf(nil, "status field not found in %#v", connectionMap)
}
clusterID, found, err := unstructured.NestedString(connectionMap, "endpoint", "cluster_id")
if !found || err != nil {
logger.Errorf(nil, "cluster_id field not found in %#v", connectionMap)
continue
}
if status == "connected" {
_, found := currentMap[clusterID]
if !found {
if newMap == nil {
newMap = copyMap(currentMap)
}
newMap[clusterID] = true
}
} else {
_, found = currentMap[clusterID]
if found {
if newMap == nil {
newMap = copyMap(currentMap)
}
delete(newMap, clusterID)
}
}
}
if newMap != nil {
logger.Infof("Updating the gateway status %#v ", newMap)
c.clusterStatusMap.Store(newMap)
}
}
func (c *Controller) updateLocalClusterIDIfNeeded(clusterID string) {
updateNeeded := clusterID != "" && clusterID != c.GetLocalClusterID()
if updateNeeded {
logger.Infof("Updating the gateway localClusterID %q ", clusterID)
c.localClusterID.Store(clusterID)
}
}
func getGatewayStatus(obj *unstructured.Unstructured) (connections []interface{}, clusterID string, gwStatus bool) {
status, found, err := unstructured.NestedMap(obj.Object, "status")
if !found || err != nil {
logger.Errorf(err, "status field not found in %#v, err was", obj)
return nil, "", false
}
localClusterID, found, err := unstructured.NestedString(status, "localEndpoint", "cluster_id")
if !found || err != nil {
logger.Errorf(err, "localEndpoint->cluster_id not found in %#v, err was", status)
localClusterID = ""
} else {
connections = append(connections, map[string]interface{}{
"status": "connected",
"endpoint": map[string]interface{}{
"cluster_id": localClusterID,
},
})
}
haStatus, found, err := unstructured.NestedString(status, "haStatus")
if !found || err != nil {
logger.Errorf(err, "haStatus field not found in %#v, err was", status)
return connections, localClusterID, true
}
if haStatus == "active" {
rconns, _, err := unstructured.NestedSlice(status, "connections")
if err != nil {
logger.Errorf(err, "connections field not found in %#v, err was", status)
return connections, localClusterID, false
}
connections = append(connections, rconns...)
}
return connections, localClusterID, true
}
func (c *Controller) getClusterStatusMap() map[string]bool {
return c.clusterStatusMap.Load().(map[string]bool)
}
func (c *Controller) getCheckedClientset(client dynamic.Interface) (dynamic.ResourceInterface, error) {
// First check if the Submariner resource is present.
gvr, _ := schema.ParseResourceArg("submariners.v1alpha1.submariner.io")
list, err := client.Resource(*gvr).Namespace(v1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if apierrors.IsNotFound(err) || meta.IsNoMatchError(err) || (err == nil && len(list.Items) == 0) {
return nil, apierrors.NewNotFound(gvr.GroupResource(), "")
}
if err != nil {
return nil, errors.Wrap(err, "error listing Submariner resources")
}
gvr, _ = schema.ParseResourceArg("gateways.v1.submariner.io")
gwClient := client.Resource(*gvr).Namespace(v1.NamespaceAll)
_, err = gwClient.List(context.TODO(), metav1.ListOptions{})
if apierrors.IsNotFound(err) || meta.IsNoMatchError(err) {
return nil, apierrors.NewNotFound(gvr.GroupResource(), "")
}
if err != nil {
return nil, errors.Wrap(err, "error listing Gateway resources")
}
return gwClient, nil
}
func copyMap(src map[string]bool) map[string]bool {
m := make(map[string]bool)
for k, v := range src {
m[k] = v
}
return m
}
// Public API.
func (c *Controller) IsConnected(clusterID string) bool {
return !c.gatewayAvailable || c.getClusterStatusMap()[clusterID]
}
func (c *Controller) GetLocalClusterID() string {
return c.localClusterID.Load().(string)
}