-
Notifications
You must be signed in to change notification settings - Fork 9
/
monitor.go
113 lines (97 loc) · 3.54 KB
/
monitor.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
/*
Copyright (c) 2021 Nordix Foundation
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 monitor
import (
"context"
"fmt"
"github.com/go-logr/logr"
"github.com/nordix/meridio/pkg/configuration/reader"
"github.com/nordix/meridio/pkg/k8s/watcher"
"github.com/nordix/meridio/pkg/log"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
type ConfigMapMonitor struct {
watcher.ObjectMonitorInterface
ConfigurationRegistry ConfigurationRegistry
ConfigMapName string
Namespace string
logger logr.Logger
}
func New(configMapName string, namespace string, ConfigurationRegistry ConfigurationRegistry) (*ConfigMapMonitor, error) {
configMapMonitor := &ConfigMapMonitor{
ConfigMapName: configMapName,
Namespace: namespace,
ConfigurationRegistry: ConfigurationRegistry,
logger: log.Logger.WithValues("class", "ConfigMapMonitor"),
}
monitor, err := watcher.NewObjectMonitor(
context.TODO(),
configMapName,
namespace,
watcher.WatchEventHandler(configMapMonitor),
func(namespace string) (watcher.WatchObject, error) {
clientCfg, err := rest.InClusterConfig()
if err != nil {
return nil, fmt.Errorf("failed to get InClusterConfig for configmap monitor (%s): %w", configMapName, err)
}
clientset, err := kubernetes.NewForConfig(clientCfg)
if err != nil {
return nil, fmt.Errorf("failed to create clientSet for configmap monitor (%s): %w", configMapName, err)
}
return clientset.CoreV1().ConfigMaps(namespace), nil
},
)
if err != nil {
return nil, fmt.Errorf("failed to create object monitor for configmap monitor (%s): %w", configMapName, err)
}
configMapMonitor.ObjectMonitorInterface = monitor
return configMapMonitor, nil
}
func (cmm *ConfigMapMonitor) Handle(ctx context.Context, event *watch.Event) {
if event == nil {
return
}
switch event.Type {
case watch.Added:
cmm.eventHandler(event)
case watch.Modified:
cmm.eventHandler(event)
case watch.Deleted:
default:
}
}
func (cmm *ConfigMapMonitor) End(ctx context.Context, namespace, name string) {}
func (cmm *ConfigMapMonitor) eventHandler(event *watch.Event) {
configmap, ok := event.Object.(*corev1.ConfigMap)
if !ok {
cmm.logger.Info("Failed to cast event.Object")
return
}
trench, conduits, streams, flows, vips, attractors, gateways, err := reader.UnmarshalConfig(configmap.Data)
if err != nil {
cmm.logger.Error(err, "unmarshal")
return
}
trenchConverted, conduitsConverted, streamsConverted, flowsConverted, vipsConverted, attractorsConverted, gatewaysConverted := reader.ConvertAll(
trench, conduits, streams, flows, vips, attractors, gateways)
cmm.ConfigurationRegistry.SetTrench(trenchConverted)
cmm.ConfigurationRegistry.SetConduits(conduitsConverted)
cmm.ConfigurationRegistry.SetStreams(streamsConverted)
cmm.ConfigurationRegistry.SetFlows(flowsConverted)
cmm.ConfigurationRegistry.SetVips(vipsConverted)
cmm.ConfigurationRegistry.SetAttractors(attractorsConverted)
cmm.ConfigurationRegistry.SetGateways(gatewaysConverted)
}