This repository has been archived by the owner on Apr 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifests.go
108 lines (80 loc) · 1.97 KB
/
manifests.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
package helmut
import (
"sync"
"k8s.io/apimachinery/pkg/runtime"
)
// Manifests stores the rendered manifests.
type Manifests struct {
objects map[ObjectKey]runtime.Object
scheme *runtime.Scheme
mu sync.RWMutex
once sync.Once
}
// NewManifests creates and returns new Manifests.
// If SchemeOption is omitted, the default scheme will be used.
func NewManifests(options ...SchemeOption) *Manifests {
m := &Manifests{}
m.once.Do(m.init)
opts := &schemeOption{}
for _, o := range options {
o(opts)
}
if opts.Empty() {
return m
}
m.scheme = opts.scheme
return m
}
// init is executed by sync.Once to initialize the struct.
func (m *Manifests) init() {
if m.objects == nil {
m.objects = make(map[ObjectKey]runtime.Object)
}
if m.scheme == nil {
m.scheme = defaultScheme
}
}
// Load returns the object stored in the manifests for a key, or nil if no value is present.
// The ok result indicates whether value was found in the manifests.
func (m *Manifests) Load(key ObjectKey) (runtime.Object, bool) {
m.once.Do(m.init)
m.mu.RLock()
defer m.mu.RUnlock()
obj, ok := m.objects[key]
return obj, ok
}
// Delete deletes the object for a key.
func (m *Manifests) Delete(key ObjectKey) {
m.once.Do(m.init)
m.mu.Lock()
delete(m.objects, key)
m.mu.Unlock()
}
// Store sets the object for a key.
func (m *Manifests) Store(key ObjectKey, value runtime.Object) {
m.once.Do(m.init)
m.mu.Lock()
m.objects[key] = value
m.mu.Unlock()
}
// GetScheme returns the scheme.
func (m *Manifests) GetScheme() *runtime.Scheme {
m.once.Do(m.init)
return m.scheme
}
// Length returns the number of elements in the stored manifest.
func (m *Manifests) Length() int {
m.mu.RLock()
defer m.mu.RUnlock()
return len(m.objects)
}
// GetKeys returns a list of keys for an object.
func (m *Manifests) GetKeys() []ObjectKey {
m.mu.RLock()
defer m.mu.RUnlock()
keys := make([]ObjectKey, 0, m.Length())
for key := range m.objects {
keys = append(keys, key)
}
return keys
}