1
16
17
21 package cache
22
23 import (
24 "fmt"
25 "sync"
26 "time"
27
28 "k8s.io/klog/v2"
29 )
30
31
32
33
34
35 type DesiredStateOfWorld interface {
36
37
38
39 AddOrUpdatePlugin(socketPath string) error
40
41
42
43
44 RemovePlugin(socketPath string)
45
46
47
48 GetPluginsToRegister() []PluginInfo
49
50
51
52 PluginExists(socketPath string) bool
53 }
54
55
56 func NewDesiredStateOfWorld() DesiredStateOfWorld {
57 return &desiredStateOfWorld{
58 socketFileToInfo: make(map[string]PluginInfo),
59 }
60 }
61
62 type desiredStateOfWorld struct {
63
64
65
66 socketFileToInfo map[string]PluginInfo
67 sync.RWMutex
68 }
69
70 var _ DesiredStateOfWorld = &desiredStateOfWorld{}
71
72
73 func generatePluginMsgDetailed(prefixMsg, suffixMsg, socketPath, details string) (detailedMsg string) {
74 return fmt.Sprintf("%v for plugin at %q %v %v", prefixMsg, socketPath, details, suffixMsg)
75 }
76
77
78 func generatePluginMsg(prefixMsg, suffixMsg, socketPath, details string) (simpleMsg, detailedMsg string) {
79 simpleMsg = fmt.Sprintf("%v for plugin at %q %v", prefixMsg, socketPath, suffixMsg)
80 return simpleMsg, generatePluginMsgDetailed(prefixMsg, suffixMsg, socketPath, details)
81 }
82
83
84
85
86 func (plugin *PluginInfo) GenerateMsgDetailed(prefixMsg, suffixMsg string) (detailedMsg string) {
87 detailedStr := fmt.Sprintf("(plugin details: %v)", plugin)
88 return generatePluginMsgDetailed(prefixMsg, suffixMsg, plugin.SocketPath, detailedStr)
89 }
90
91
92
93
94 func (plugin *PluginInfo) GenerateMsg(prefixMsg, suffixMsg string) (simpleMsg, detailedMsg string) {
95 detailedStr := fmt.Sprintf("(plugin details: %v)", plugin)
96 return generatePluginMsg(prefixMsg, suffixMsg, plugin.SocketPath, detailedStr)
97 }
98
99
100
101
102 func (plugin *PluginInfo) GenerateErrorDetailed(prefixMsg string, err error) (detailedErr error) {
103 return fmt.Errorf(plugin.GenerateMsgDetailed(prefixMsg, errSuffix(err)))
104 }
105
106
107
108
109 func (plugin *PluginInfo) GenerateError(prefixMsg string, err error) (simpleErr, detailedErr error) {
110 simpleMsg, detailedMsg := plugin.GenerateMsg(prefixMsg, errSuffix(err))
111 return fmt.Errorf(simpleMsg), fmt.Errorf(detailedMsg)
112 }
113
114
115 func errSuffix(err error) string {
116 errStr := ""
117 if err != nil {
118 errStr = fmt.Sprintf(": %v", err)
119 }
120 return errStr
121 }
122
123 func (dsw *desiredStateOfWorld) AddOrUpdatePlugin(socketPath string) error {
124 dsw.Lock()
125 defer dsw.Unlock()
126
127 if socketPath == "" {
128 return fmt.Errorf("socket path is empty")
129 }
130 if _, ok := dsw.socketFileToInfo[socketPath]; ok {
131 klog.V(2).InfoS("Plugin exists in desired state cache, timestamp will be updated", "path", socketPath)
132 }
133
134
135
136
137
138 dsw.socketFileToInfo[socketPath] = PluginInfo{
139 SocketPath: socketPath,
140 Timestamp: time.Now(),
141 }
142 return nil
143 }
144
145 func (dsw *desiredStateOfWorld) RemovePlugin(socketPath string) {
146 dsw.Lock()
147 defer dsw.Unlock()
148
149 delete(dsw.socketFileToInfo, socketPath)
150 }
151
152 func (dsw *desiredStateOfWorld) GetPluginsToRegister() []PluginInfo {
153 dsw.RLock()
154 defer dsw.RUnlock()
155
156 pluginsToRegister := []PluginInfo{}
157 for _, pluginInfo := range dsw.socketFileToInfo {
158 pluginsToRegister = append(pluginsToRegister, pluginInfo)
159 }
160 return pluginsToRegister
161 }
162
163 func (dsw *desiredStateOfWorld) PluginExists(socketPath string) bool {
164 dsw.RLock()
165 defer dsw.RUnlock()
166
167 _, exists := dsw.socketFileToInfo[socketPath]
168 return exists
169 }
170
View as plain text