-
Notifications
You must be signed in to change notification settings - Fork 665
/
status.go
160 lines (133 loc) · 4.58 KB
/
status.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright Project Contour Authors
// 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 k8s
import (
"context"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// StatusUpdate contains an all the information needed to change an object's status to perform a specific update.
// Send down a channel to the goroutine that actually writes the changes back.
type StatusUpdate struct {
NamespacedName types.NamespacedName
Resource client.Object
Mutator StatusMutator
}
func NewStatusUpdate(name, namespace string, resource client.Object, mutator StatusMutator) StatusUpdate {
return StatusUpdate{
NamespacedName: types.NamespacedName{
Name: name,
Namespace: namespace,
},
Resource: resource,
Mutator: mutator,
}
}
// StatusMutator is an interface to hold mutator functions for status updates.
type StatusMutator interface {
Mutate(obj client.Object) client.Object
}
// StatusMutatorFunc is a function adaptor for StatusMutators.
type StatusMutatorFunc func(client.Object) client.Object
// Mutate adapts the StatusMutatorFunc to fit through the StatusMutator interface.
func (m StatusMutatorFunc) Mutate(old client.Object) client.Object {
if m == nil {
return nil
}
return m(old)
}
// StatusUpdateHandler holds the details required to actually write an Update back to the referenced object.
type StatusUpdateHandler struct {
log logrus.FieldLogger
client client.Client
sendUpdates chan struct{}
updateChannel chan StatusUpdate
}
func NewStatusUpdateHandler(log logrus.FieldLogger, client client.Client) *StatusUpdateHandler {
return &StatusUpdateHandler{
log: log,
client: client,
sendUpdates: make(chan struct{}),
updateChannel: make(chan StatusUpdate, 100),
}
}
func (suh *StatusUpdateHandler) apply(upd StatusUpdate) {
if err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
obj := upd.Resource
// Get the resource.
if err := suh.client.Get(context.Background(), upd.NamespacedName, obj); err != nil {
return err
}
newObj := upd.Mutator.Mutate(obj)
if isStatusEqual(obj, newObj) {
suh.log.WithField("name", upd.NamespacedName.Name).
WithField("namespace", upd.NamespacedName.Namespace).
Debug("update was a no-op")
return nil
}
return suh.client.Status().Update(context.Background(), newObj)
}); err != nil {
suh.log.WithError(err).
WithField("name", upd.NamespacedName.Name).
WithField("namespace", upd.NamespacedName.Namespace).
Error("unable to update status")
}
}
func (suh *StatusUpdateHandler) NeedLeaderElection() bool {
return true
}
// Start runs the goroutine to perform status writes.
func (suh *StatusUpdateHandler) Start(ctx context.Context) error {
suh.log.Info("started status update handler")
defer suh.log.Info("stopped status update handler")
// Enable StatusUpdaters to start sending updates to this handler.
close(suh.sendUpdates)
for {
select {
case <-ctx.Done():
return nil
case upd := <-suh.updateChannel:
suh.log.WithField("name", upd.NamespacedName.Name).
WithField("namespace", upd.NamespacedName.Namespace).
Debug("received a status update")
suh.apply(upd)
}
}
}
// Writer retrieves the interface that should be used to write to the StatusUpdateHandler.
func (suh *StatusUpdateHandler) Writer() StatusUpdater {
return &StatusUpdateWriter{
enabled: suh.sendUpdates,
updateChannel: suh.updateChannel,
}
}
// StatusUpdater describes an interface to send status updates somewhere.
type StatusUpdater interface {
Send(su StatusUpdate)
}
// StatusUpdateWriter takes status updates and sends these to the StatusUpdateHandler via a channel.
type StatusUpdateWriter struct {
enabled <-chan struct{}
updateChannel chan<- StatusUpdate
}
// Send sends the given StatusUpdate off to the update channel for writing by the StatusUpdateHandler.
func (suw *StatusUpdateWriter) Send(update StatusUpdate) {
// Non-blocking receive to see if we should pass along update.
select {
case <-suw.enabled:
suw.updateChannel <- update
default:
}
}