-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathstate.go
107 lines (86 loc) · 2.36 KB
/
state.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package source
import (
"sync"
"github.com/gardener/controller-manager-library/pkg/resources"
)
type state struct {
lock sync.Mutex
source DNSSource
feedback map[resources.ClusterObjectKey]DNSFeedback
ownerState *ownerState
used map[resources.ClusterObjectKey]resources.ClusterObjectKeySet
deps map[resources.ClusterObjectKey]resources.ClusterObjectKey
}
// NewState creates a new owner state.
func NewState(ownerState *ownerState) interface{} {
return &state{
source: nil,
feedback: map[resources.ClusterObjectKey]DNSFeedback{},
ownerState: ownerState,
used: map[resources.ClusterObjectKey]resources.ClusterObjectKeySet{},
deps: map[resources.ClusterObjectKey]resources.ClusterObjectKey{},
}
}
func (this *state) CreateFeedbackForObject(obj resources.Object) DNSFeedback {
if !this.ownerState.GetActive() {
return nil
}
fb := this.source.CreateDNSFeedback(obj)
this.SetFeedback(obj.ClusterKey(), fb)
return fb
}
func (this *state) GetFeedback(key resources.ClusterObjectKey) DNSFeedback {
if !this.ownerState.GetActive() {
return nil
}
this.lock.Lock()
defer this.lock.Unlock()
return this.feedback[key]
}
func (this *state) SetFeedback(key resources.ClusterObjectKey, f DNSFeedback) {
this.lock.Lock()
defer this.lock.Unlock()
this.feedback[key] = f
}
func (this *state) DeleteFeedback(key resources.ClusterObjectKey) {
this.lock.Lock()
defer this.lock.Unlock()
delete(this.feedback, key)
}
func (this *state) SetDep(obj resources.ClusterObjectKey, dep *resources.ClusterObjectKey) {
this.lock.Lock()
defer this.lock.Unlock()
old, ok := this.deps[obj]
if ok && (dep == nil || *dep != old) {
delete(this.deps, obj)
set := this.used[old]
if set != nil {
set.Remove(obj)
if len(set) == 0 {
delete(this.used, old)
}
}
}
if dep != nil && (!ok || *dep != old) {
this.deps[obj] = *dep
set := this.used[*dep]
if set == nil {
set = resources.NewClusterObjectKeySet(obj)
this.used[*dep] = set
} else {
set.Add(obj)
}
}
}
func (this *state) GetUsed(obj resources.ClusterObjectKey) resources.ClusterObjectKeySet {
this.lock.Lock()
defer this.lock.Unlock()
set := this.used[obj]
if set != nil {
return resources.NewClusterObjectKeSetBySets(set)
}
return nil
}