-
Notifications
You must be signed in to change notification settings - Fork 1
/
peering.go
95 lines (80 loc) · 2.97 KB
/
peering.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
package monitoring
import (
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic/dynamicinformer"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1"
discovery "github.com/liqotech/liqo/pkg/discovery"
)
func prepareForeignClusterInformer(factory dynamicinformer.DynamicSharedInformerFactory) {
informer := factory.ForResource(discoveryv1alpha1.ForeignClusterGroupVersionResource).Informer()
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: func(_, object interface{}) {
if unstruct, ok := object.(*unstructured.Unstructured); ok {
var fc discoveryv1alpha1.ForeignCluster
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstruct.Object, &fc); err == nil {
handleForeignClusterUpdate(&fc)
return
}
}
panic("Failed to convert the ForeignCluster returned by the informer")
},
})
}
func prepareSecretsInformer(factory dynamicinformer.DynamicSharedInformerFactory) {
informer := factory.ForResource(corev1.SchemeGroupVersion.WithResource("secrets")).Informer()
handle := func(object interface{}, handler func(*corev1.Secret)) {
if unstruct, ok := object.(*unstructured.Unstructured); ok {
var se corev1.Secret
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstruct.Object, &se); err == nil {
handler(&se)
return
}
}
panic("Failed to convert the Secret returned by the informer")
}
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(object interface{}) {
handle(object, handleSecretCreation)
},
UpdateFunc: func(_, object interface{}) {
handle(object, handleSecretUpdate)
},
})
}
func handleForeignClusterUpdate(fc *discoveryv1alpha1.ForeignCluster) {
klog.V(5).Infof("Received update for ForeignCluster %q", namespacedName(fc))
id := fc.Spec.ClusterIdentity.ClusterID
if id != "" {
M().ClusterID(id).SetPeeringStartTimestamp(fc.GetCreationTimestamp().Time)
}
}
func handleSecretCreation(se *corev1.Secret) {
if se.Name != "liqo-remote-certificate" {
return
}
klog.V(5).Infof("Received creation for Secret %q", namespacedName(se))
id, ok := se.GetLabels()[discovery.ClusterIDLabel]
if !ok {
klog.Warning("Secret %q misses the remote ID label", namespacedName(se))
return
}
M().ClusterID(id).SetAuthenticationIncomingEndTimestamp(se.GetCreationTimestamp().Time)
}
func handleSecretUpdate(se *corev1.Secret) {
klog.V(5).Infof("Received update for Secret %q", namespacedName(se))
if value, ok := se.GetLabels()[certificateAvailableLabel]; !ok || value != "true" {
klog.V(5).Infof("Secret %q does not correspond to a valid identity", namespacedName(se))
return
}
id, ok := se.GetLabels()[discovery.ClusterIDLabel]
if !ok {
klog.Warning("Secret %q misses the remote ID label", namespacedName(se))
return
}
M().ClusterID(id).SetAuthenticationOutgoingEndTimestamp(time.Now())
}