-
Notifications
You must be signed in to change notification settings - Fork 21
/
cache_manager.go
117 lines (98 loc) · 3.62 KB
/
cache_manager.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
114
115
116
117
// MIT License
//
// Copyright (c) 2022 Kubeshop
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package manager
import (
"context"
"fmt"
"strings"
"sync"
cache_v3 "github.com/envoyproxy/go-control-plane/pkg/cache/v3"
"github.com/go-logr/logr"
)
// cacheManager provides cache and methods to update it with new configuration for Envoy fleet
// it is invisible for clients importing the package
type cacheManager struct {
cache_v3.SnapshotCache
fleetSnapshot map[string]*cache_v3.Snapshot // active snapshot per fleet
mu sync.RWMutex
logger logr.Logger
}
func NewCacheManager(snapshotCache cache_v3.SnapshotCache, logger logr.Logger) *cacheManager {
return &cacheManager{
SnapshotCache: snapshotCache,
fleetSnapshot: make(map[string]*cache_v3.Snapshot),
mu: sync.RWMutex{},
logger: logger.WithName("CacheManager"),
}
}
func (cm *cacheManager) IsNodeExist(nodeID string) bool {
if status := cm.GetStatusInfo(nodeID); status != nil {
return true
}
return false
}
func (cm *cacheManager) getNodesWithCluster(cluster string) []string {
var nodesIDs []string
for _, nodeID := range cm.GetStatusKeys() {
if cm.GetStatusInfo(nodeID).GetNode().Cluster == cluster {
nodesIDs = append(nodesIDs, nodeID)
}
}
return nodesIDs
}
// setNodeSnapshot sets new node snapshot from active fleet configuration snapshot
func (cm *cacheManager) setNodeSnapshot(nodeID string, fleet string) error {
cm.mu.RLock()
snapshot, ok := cm.fleetSnapshot[fleet]
cm.mu.RUnlock()
if !ok {
return fmt.Errorf("no such %s Envoy fleet (cluster) configuration exist", fleet)
}
cm.logger.Info("setting new node snapshot", "nodeID", nodeID, "fleet", fleet)
return cm.SetSnapshot(context.Background(), nodeID, snapshot)
}
// applyNewFleetSnapshot assigns active snapshot and updates all nodes with it
func (cm *cacheManager) applyNewFleetSnapshot(fleet string, newSnapshot *cache_v3.Snapshot) error {
if err := newSnapshot.Consistent(); err != nil {
return fmt.Errorf("inconsistent snapshot %v", newSnapshot)
}
cm.mu.Lock()
cm.fleetSnapshot[fleet] = newSnapshot
cm.mu.Unlock()
errs := []error{}
// Update caches for existing nodes with only this fleet
for _, nodeID := range cm.getNodesWithCluster(fleet) {
if err := cm.setNodeSnapshot(nodeID, fleet); err != nil {
errs = append(errs, err)
}
}
// Convert list of errors to strings.
if len(errs) > 0 {
errors := make([]string, len(errs))
for i := 0; i < len(errs); i++ {
errors[i] = errs[i].Error()
}
return fmt.Errorf(strings.Join(errors, "\n"))
}
cm.logger.Info("assigning active snapshot and updating all nodes", "fleet", fleet)
return nil
}