github.com/zhyoulun/cilium@v1.6.12/pkg/node/store/store.go (about) 1 // Copyright 2018-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 store 16 17 import ( 18 "path" 19 20 "github.com/cilium/cilium/pkg/kvstore" 21 "github.com/cilium/cilium/pkg/kvstore/store" 22 "github.com/cilium/cilium/pkg/logging" 23 "github.com/cilium/cilium/pkg/logging/logfields" 24 "github.com/cilium/cilium/pkg/node" 25 "github.com/cilium/cilium/pkg/option" 26 "github.com/cilium/cilium/pkg/source" 27 ) 28 29 var ( 30 // NodeStorePrefix is the kvstore prefix of the shared store 31 // 32 // WARNING - STABLE API: Changing the structure or values of this will 33 // break backwards compatibility 34 NodeStorePrefix = path.Join(kvstore.BaseKeyPrefix, "state", "nodes", "v1") 35 36 // KeyCreator creates a node for a shared store 37 KeyCreator = func() store.Key { 38 n := node.Node{} 39 return &n 40 } 41 42 log = logging.DefaultLogger.WithField(logfields.LogSubsys, "node-store") 43 ) 44 45 // NodeObserver implements the store.Observer interface and delegates update 46 // and deletion events to the node object itself. 47 type NodeObserver struct { 48 manager NodeManager 49 } 50 51 // NewNodeObserver returns a new NodeObserver associated with the specified 52 // node manager 53 func NewNodeObserver(manager NodeManager) *NodeObserver { 54 return &NodeObserver{manager: manager} 55 } 56 57 func (o *NodeObserver) OnUpdate(k store.Key) { 58 if n, ok := k.(*node.Node); ok { 59 nodeCopy := n.DeepCopy() 60 nodeCopy.Source = source.KVStore 61 o.manager.NodeUpdated(*nodeCopy) 62 } 63 } 64 65 func (o *NodeObserver) OnDelete(k store.NamedKey) { 66 if n, ok := k.(*node.Node); ok { 67 nodeCopy := n.DeepCopy() 68 nodeCopy.Source = source.KVStore 69 o.manager.NodeDeleted(*nodeCopy) 70 } 71 } 72 73 // NodeRegistrar is a wrapper around store.SharedStore. 74 type NodeRegistrar struct { 75 *store.SharedStore 76 } 77 78 // NodeManager is the interface that the manager of nodes has to implement 79 type NodeManager interface { 80 // NodeUpdated is called when the store detects a change in node 81 // information 82 NodeUpdated(n node.Node) 83 84 // NodeDeleted is called when the store detects a deletion of a node 85 NodeDeleted(n node.Node) 86 87 // Exists is called to verify if a node exists 88 Exists(id node.Identity) bool 89 } 90 91 // RegisterNode registers the local node in the cluster 92 func (nr *NodeRegistrar) RegisterNode(n *node.Node, manager NodeManager) error { 93 if option.Config.KVStore == "" { 94 return nil 95 } 96 97 // Join the shared store holding node information of entire cluster 98 store, err := store.JoinSharedStore(store.Configuration{ 99 Prefix: NodeStorePrefix, 100 KeyCreator: KeyCreator, 101 Observer: NewNodeObserver(manager), 102 }) 103 104 if err != nil { 105 return err 106 } 107 108 if err = store.UpdateLocalKeySync(n); err != nil { 109 store.Release() 110 return err 111 } 112 113 nr.SharedStore = store 114 115 return nil 116 } 117 118 // UpdateLocalKeySync synchronizes the local key for the node using the 119 // SharedStore. 120 func (nr *NodeRegistrar) UpdateLocalKeySync(n *node.Node) error { 121 return nr.SharedStore.UpdateLocalKeySync(n) 122 }