forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcilium_node.go
89 lines (81 loc) · 2.95 KB
/
cilium_node.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
// Copyright 2019 Authors of Cilium
//
// 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 main
import (
"reflect"
"github.com/cilium/cilium/pkg/k8s"
"github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
"github.com/cilium/cilium/pkg/k8s/informer"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
)
func startSynchronizingCiliumNodes() {
log.Info("Starting to synchronize CiliumNode custom resources...")
// TODO: The operator is currently storing a full copy of the
// CiliumNode resource, as the resource grows, we may want to consider
// introducing a slim version of it.
_, ciliumNodeInformer := informer.NewInformer(
cache.NewListWatchFromClient(ciliumK8sClient.CiliumV2().RESTClient(),
"ciliumnodes", v1.NamespaceAll, fields.Everything()),
&v2.CiliumNode{},
0,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if node, ok := obj.(*v2.CiliumNode); ok {
// node is deep copied before it is stored in pkg/aws/eni
ciliumNodeUpdated(node)
} else {
log.Warningf("Unknown CiliumNode object type %s received: %+v", reflect.TypeOf(obj), obj)
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
if node, ok := newObj.(*v2.CiliumNode); ok {
// node is deep copied before it is stored in pkg/aws/eni
ciliumNodeUpdated(node)
} else {
log.Warningf("Unknown CiliumNode object type %s received: %+v", reflect.TypeOf(newObj), newObj)
}
},
DeleteFunc: func(obj interface{}) {
deletedObj, ok := obj.(cache.DeletedFinalStateUnknown)
if ok {
// Delete was not observed by the
// watcher but is removed from
// kube-apiserver. This is the last
// known state and the object no longer
// exists.
if node, ok := deletedObj.Obj.(*v2.CiliumNode); ok {
ciliumNodeDeleted(node.Name)
return
}
} else if node, ok := obj.(*v2.CiliumNode); ok {
ciliumNodeDeleted(node.Name)
return
}
log.Warningf("Unknown CiliumNode object type %s received: %+v", reflect.TypeOf(obj), obj)
},
},
k8s.ConvertToCiliumNode,
)
go ciliumNodeInformer.Run(wait.NeverStop)
}
func deleteCiliumNode(name string) {
if err := ciliumK8sClient.CiliumV2().CiliumNodes().Delete(name, &metav1.DeleteOptions{}); err == nil {
log.WithField("name", name).Info("Removed CiliumNode after receiving node deletion event")
}
ciliumNodeDeleted(name)
}