-
Notifications
You must be signed in to change notification settings - Fork 67
/
state_owner.go
141 lines (130 loc) · 4.78 KB
/
state_owner.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright 2020 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package provider
import (
"fmt"
"github.com/gardener/controller-manager-library/pkg/controllermanager/controller/reconcile"
"github.com/gardener/controller-manager-library/pkg/logger"
"github.com/gardener/controller-manager-library/pkg/resources"
"github.com/gardener/external-dns-management/pkg/apis/dns/v1alpha1"
"github.com/gardener/external-dns-management/pkg/dns/provider/statistic"
dnsutils "github.com/gardener/external-dns-management/pkg/dns/utils"
"github.com/gardener/external-dns-management/pkg/server/metrics"
)
////////////////////////////////////////////////////////////////////////////////
// state handling for OwnerIds
////////////////////////////////////////////////////////////////////////////////
func (this *state) UpdateOwner(logger logger.LogContext, owner *dnsutils.DNSOwnerObject, setup bool) reconcile.Status {
if !setup && !this.ownerCache.IsResponsibleFor(owner.GetOwnerId()) && owner.IsActive() {
logger.Infof("would activate new owner -> ensure all entries are synchronized")
this.ownerCache.SetPending(owner.GetOwnerId())
done, err := this.context.Synchronize(logger, SYNC_ENTRIES, owner.Object)
if !done || err != nil {
return reconcile.DelayOnError(logger, err)
}
logger.Infof("entries synchronized")
}
this.lock.Lock()
changed, active := this.ownerCache.UpdateOwner(owner)
this.lock.Unlock()
logger.Infof("update: changed owner ids %s", changed)
logger.Debugf(" active owner ids %s", active)
if len(changed) > 0 {
this.TriggerEntriesByOwner(logger, changed)
this.TriggerHostedZones()
}
return reconcile.Succeeded(logger)
}
func (this *state) OwnerDeleted(logger logger.LogContext, key resources.ObjectKey) reconcile.Status {
this.lock.Lock()
changed, active := this.ownerCache.DeleteOwner(key)
this.lock.Unlock()
logger.Infof("delete: changed owner ids %s", changed)
logger.Debugf(" active owner ids %s", active)
if len(changed) > 0 {
this.TriggerEntriesByOwner(logger, changed)
this.TriggerHostedZones()
}
return reconcile.Succeeded(logger)
}
func (this *state) UpdateOwnerCounts(log logger.LogContext) {
if !this.initialized {
return
}
log.Infof("update owner statistic")
statistic := statistic.NewEntryStatistic()
this.UpdateStatistic(statistic)
types := this.GetHandlerFactory().TypeCodes()
metrics.UpdateOwnerStatistic(statistic, types)
changes := this.ownerCache.UpdateCountsWith(statistic.Owners, types)
if len(changes) > 0 {
log.Infof("found %d changes for owner usages", len(changes))
this.ownerupd <- changes
}
}
////////////////////////////////////////////////////////////////////////////////
func startOwnerUpdater(ctx Context, ownerresc resources.Interface) chan OwnerCounts {
log := ctx.AddIndent("updater: ")
requests := make(chan OwnerCounts, 2)
go func() {
log.Infof("starting owner count updater")
for {
select {
case <-ctx.GetContext().Done():
log.Infof("stopping owner updater")
return
case changes := <-requests:
log.Infof("starting owner update for %d changes", len(changes))
for n, counts := range changes {
log.Infof(" updating owner counts %v for %s", counts, n)
_, _, err := ownerresc.ModifyStatusByName(resources.NewObjectName(string(n)), func(data resources.ObjectData) (bool, error) {
owner, ok := data.(*v1alpha1.DNSOwner)
if !ok {
return false, fmt.Errorf("invalid owner object type %T", data)
}
mod := false
if owner.Status.Entries.ByType == nil {
owner.Status.Entries.ByType = ProviderTypeCounts{}
}
for t, v := range counts {
if owner.Status.Entries.ByType[t] != v {
mod = true
owner.Status.Entries.ByType[t] = v
}
if v == 0 {
delete(owner.Status.Entries.ByType, t)
}
}
sum := 0
for _, v := range owner.Status.Entries.ByType {
sum += v
}
if owner.Status.Entries.Amount != sum {
owner.Status.Entries.Amount = sum
mod = true
}
return mod, nil
})
if err != nil {
log.Errorf("update failed: %s", err)
}
}
}
}
}()
return requests
}