-
Notifications
You must be signed in to change notification settings - Fork 13
/
syncronizedmap.go
67 lines (50 loc) · 1.59 KB
/
syncronizedmap.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
package syncronizedmap
import (
"fmt"
"sync"
)
//go:generate mockgen -source=syncronizedmap.go -package=syncronizedmap -destination=mock_syncronizedmap.go
type KernelOsDtkMapping interface {
SetNodeInfo(kernelVersion, osImage string)
SetImageStreamInfo(osImage, dtkImage string)
GetImage(kernelVersion string) (string, error)
}
type kernelOsDtkMapping struct {
kernelToOsMutex *sync.RWMutex
osToDtkMutext *sync.RWMutex
kernelToOs map[string]string
osToDtk map[string]string
}
func NewKernelOsDtkMapping() KernelOsDtkMapping {
return &kernelOsDtkMapping{
kernelToOsMutex: &sync.RWMutex{},
osToDtkMutext: &sync.RWMutex{},
kernelToOs: map[string]string{},
osToDtk: map[string]string{},
}
}
func (skom *kernelOsDtkMapping) SetNodeInfo(kernelVersion, osImage string) {
skom.kernelToOsMutex.Lock()
defer skom.kernelToOsMutex.Unlock()
skom.kernelToOs[kernelVersion] = osImage
}
func (skom *kernelOsDtkMapping) SetImageStreamInfo(osImage, dtkImage string) {
skom.osToDtkMutext.Lock()
defer skom.osToDtkMutext.Unlock()
skom.osToDtk[osImage] = dtkImage
}
func (skom *kernelOsDtkMapping) GetImage(kernelVersion string) (string, error) {
skom.kernelToOsMutex.RLock()
defer skom.kernelToOsMutex.RUnlock()
osImage, ok := skom.kernelToOs[kernelVersion]
if !ok {
return "", fmt.Errorf("could not find kernel %s in kernel --> OS mapping", kernelVersion)
}
skom.osToDtkMutext.RLock()
defer skom.osToDtkMutext.RUnlock()
dtk, ok := skom.osToDtk[osImage]
if !ok {
return "", fmt.Errorf("could not find osImage %s in OS --> DTK mapping", osImage)
}
return dtk, nil
}