github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/operator/cilium_node.go (about) 1 // Copyright 2019 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "reflect" 19 20 "github.com/cilium/cilium/pkg/k8s" 21 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2" 22 "github.com/cilium/cilium/pkg/k8s/informer" 23 24 "k8s.io/api/core/v1" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/apimachinery/pkg/fields" 27 "k8s.io/apimachinery/pkg/util/wait" 28 "k8s.io/client-go/tools/cache" 29 ) 30 31 func startSynchronizingCiliumNodes() { 32 log.Info("Starting to synchronize CiliumNode custom resources...") 33 34 // TODO: The operator is currently storing a full copy of the 35 // CiliumNode resource, as the resource grows, we may want to consider 36 // introducing a slim version of it. 37 _, ciliumNodeInformer := informer.NewInformer( 38 cache.NewListWatchFromClient(ciliumK8sClient.CiliumV2().RESTClient(), 39 "ciliumnodes", v1.NamespaceAll, fields.Everything()), 40 &v2.CiliumNode{}, 41 0, 42 cache.ResourceEventHandlerFuncs{ 43 AddFunc: func(obj interface{}) { 44 if node, ok := obj.(*v2.CiliumNode); ok { 45 ciliumNodeUpdated(node.DeepCopy()) 46 } else { 47 log.Warningf("Unknown CiliumNode object type %s received: %+v", reflect.TypeOf(obj), obj) 48 } 49 }, 50 UpdateFunc: func(oldObj, newObj interface{}) { 51 if node, ok := newObj.(*v2.CiliumNode); ok { 52 ciliumNodeUpdated(node.DeepCopy()) 53 } else { 54 log.Warningf("Unknown CiliumNode object type %s received: %+v", reflect.TypeOf(newObj), newObj) 55 } 56 }, 57 DeleteFunc: func(obj interface{}) { 58 deletedObj, ok := obj.(cache.DeletedFinalStateUnknown) 59 if ok { 60 // Delete was not observed by the 61 // watcher but is removed from 62 // kube-apiserver. This is the last 63 // known state and the object no longer 64 // exists. 65 if node, ok := deletedObj.Obj.(*v2.CiliumNode); ok { 66 ciliumNodeDeleted(node.Name) 67 return 68 } 69 } else if node, ok := obj.(*v2.CiliumNode); ok { 70 ciliumNodeDeleted(node.Name) 71 return 72 } 73 log.Warningf("Unknown CiliumNode object type %s received: %+v", reflect.TypeOf(obj), obj) 74 }, 75 }, 76 k8s.ConvertToCiliumNode, 77 ) 78 79 go ciliumNodeInformer.Run(wait.NeverStop) 80 } 81 82 func deleteCiliumNode(name string) { 83 if err := ciliumK8sClient.CiliumV2().CiliumNodes().Delete(name, &metav1.DeleteOptions{}); err == nil { 84 log.WithField("name", name).Info("Removed CiliumNode after receiving node deletion event") 85 } 86 ciliumNodeDeleted(name) 87 }