forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
238 lines (214 loc) · 7.95 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package controller
import (
"fmt"
"time"
"github.com/golang/glog"
lru "github.com/hashicorp/golang-lru"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/util"
"k8s.io/kubernetes/pkg/util/sets"
"k8s.io/kubernetes/pkg/watch"
"github.com/openshift/origin/pkg/client"
routeapi "github.com/openshift/origin/pkg/route/api"
"github.com/openshift/origin/pkg/router"
)
// StatusAdmitter ensures routes added to the plugin have status set.
type StatusAdmitter struct {
plugin router.Plugin
client client.RoutesNamespacer
routerName string
contentionInterval time.Duration
expected *lru.Cache
}
// NewStatusAdmitter creates a plugin wrapper that ensures every accepted
// route has a status field set that matches this router. The admitter manages
// an LRU of recently seen conflicting updates to handle when two router processes
// with differing configurations are writing updates at the same time.
func NewStatusAdmitter(plugin router.Plugin, client client.RoutesNamespacer, name string) *StatusAdmitter {
expected, _ := lru.New(1024)
return &StatusAdmitter{
plugin: plugin,
client: client,
routerName: name,
contentionInterval: 1 * time.Minute,
expected: expected,
}
}
// nowFn allows the package to be tested
var nowFn = unversioned.Now
// findOrCreateIngress loops through the router status ingress array looking for an entry
// that matches name. If there is no entry in the array, it creates one and appends it
// to the array. If there are multiple entries with that name, the first one is
// returned and later ones are removed. Changed is returned as true if any part of the
// array is altered.
func findOrCreateIngress(route *routeapi.Route, name string) (_ *routeapi.RouteIngress, changed bool) {
position := -1
updated := make([]routeapi.RouteIngress, 0, len(route.Status.Ingress))
for i := range route.Status.Ingress {
existing := &route.Status.Ingress[i]
if existing.RouterName != name {
updated = append(updated, *existing)
continue
}
if position != -1 {
changed = true
continue
}
updated = append(updated, *existing)
position = i
}
switch {
case position == -1:
position = len(route.Status.Ingress)
route.Status.Ingress = append(route.Status.Ingress, routeapi.RouteIngress{
RouterName: name,
Host: route.Spec.Host,
})
changed = true
case changed:
route.Status.Ingress = updated
}
ingress := &route.Status.Ingress[position]
if ingress.Host != route.Spec.Host {
ingress.Host = route.Spec.Host
changed = true
}
return ingress, changed
}
// setIngressCondition records the condition on the ingress, returning true if the ingress was changed and
// false if no modification was made.
func setIngressCondition(ingress *routeapi.RouteIngress, condition routeapi.RouteIngressCondition) bool {
for _, existing := range ingress.Conditions {
//existing.LastTransitionTime = nil
if existing == condition {
return false
}
}
now := nowFn()
condition.LastTransitionTime = &now
ingress.Conditions = []routeapi.RouteIngressCondition{condition}
return true
}
func ingressConditionTouched(ingress *routeapi.RouteIngress) *unversioned.Time {
var lastTouch *unversioned.Time
for _, condition := range ingress.Conditions {
if t := condition.LastTransitionTime; t != nil {
switch {
case lastTouch == nil, t.After(lastTouch.Time):
lastTouch = t
}
}
}
return lastTouch
}
// recordIngressConditionFailure updates the matching ingress on the route (or adds a new one) with the specified
// condition, returning true if the object was modified.
func recordIngressConditionFailure(route *routeapi.Route, name string, condition routeapi.RouteIngressCondition) (*routeapi.RouteIngress, bool, *unversioned.Time) {
for i := range route.Status.Ingress {
existing := &route.Status.Ingress[i]
if existing.RouterName != name {
continue
}
lastTouch := ingressConditionTouched(existing)
return existing, setIngressCondition(existing, condition), lastTouch
}
route.Status.Ingress = append(route.Status.Ingress, routeapi.RouteIngress{RouterName: name})
ingress := &route.Status.Ingress[len(route.Status.Ingress)-1]
setIngressCondition(ingress, condition)
return ingress, true, nil
}
// hasIngressBeenTouched returns true if the route appears to have been touched since the last time
func (a *StatusAdmitter) hasIngressBeenTouched(route *routeapi.Route, lastTouch *unversioned.Time) bool {
glog.V(4).Infof("has last touch %v for %s/%s", lastTouch, route.Namespace, route.Name)
if lastTouch.IsZero() {
return false
}
old, ok := a.expected.Get(route.UID)
if !ok || old.(time.Time).Equal(lastTouch.Time) {
return false
}
return true
}
// recordIngressTouch
func (a *StatusAdmitter) recordIngressTouch(route *routeapi.Route, touch *unversioned.Time, err error) {
switch {
case err == nil:
if touch != nil {
a.expected.Add(route.UID, touch.Time)
}
case errors.IsConflict(err):
a.expected.Add(route.UID, time.Time{})
}
}
// admitRoute returns true if the route has already been accepted to this router, or
// updates the route to contain an accepted condition. Returns an error if the route could
// not be admitted.
func (a *StatusAdmitter) admitRoute(oc client.RoutesNamespacer, route *routeapi.Route, name string) (bool, error) {
ingress, updated := findOrCreateIngress(route, name)
if !updated {
for i := range ingress.Conditions {
cond := &ingress.Conditions[i]
if cond.Type == routeapi.RouteAdmitted && cond.Status == kapi.ConditionTrue {
glog.V(4).Infof("admit: route already admitted")
return true, nil
}
}
}
if a.hasIngressBeenTouched(route, ingressConditionTouched(ingress)) {
glog.V(4).Infof("admit: observed a route update from someone else: route %s/%s has been updated to an inconsistent value, doing nothing", route.Namespace, route.Name)
return true, nil
}
setIngressCondition(ingress, routeapi.RouteIngressCondition{
Type: routeapi.RouteAdmitted,
Status: kapi.ConditionTrue,
})
glog.V(4).Infof("admit: admitting route by updating status: %s (%t): %s", route.Name, updated, route.Spec.Host)
_, err := oc.Routes(route.Namespace).UpdateStatus(route)
a.recordIngressTouch(route, ingress.Conditions[0].LastTransitionTime, err)
return err == nil, err
}
// RecordRouteRejection attempts to update the route status with a reason for a route being rejected.
func (a *StatusAdmitter) RecordRouteRejection(route *routeapi.Route, reason, message string) {
ingress, changed, lastTouch := recordIngressConditionFailure(route, a.routerName, routeapi.RouteIngressCondition{
Type: routeapi.RouteAdmitted,
Status: kapi.ConditionFalse,
Reason: reason,
Message: message,
})
if !changed {
glog.V(4).Infof("reject: no changes to route needed: %s/%s", route.Namespace, route.Name)
return
}
if a.hasIngressBeenTouched(route, lastTouch) {
glog.V(4).Infof("reject: observed a route update from someone else: route %s/%s has been updated to an inconsistent value, doing nothing", route.Namespace, route.Name)
return
}
_, err := a.client.Routes(route.Namespace).UpdateStatus(route)
a.recordIngressTouch(route, ingress.Conditions[0].LastTransitionTime, err)
if err != nil {
util.HandleError(fmt.Errorf("unable to write route rejection to the status: %v", err))
}
}
// HandleRoute attempts to admit the provided route on watch add / modifications.
func (a *StatusAdmitter) HandleRoute(eventType watch.EventType, route *routeapi.Route) error {
switch eventType {
case watch.Added, watch.Modified:
ok, err := a.admitRoute(a.client, route, a.routerName)
if err != nil {
return err
}
if !ok {
glog.V(4).Infof("skipping route: %s", route.Name)
return nil
}
}
return a.plugin.HandleRoute(eventType, route)
}
func (a *StatusAdmitter) HandleEndpoints(eventType watch.EventType, route *kapi.Endpoints) error {
return a.plugin.HandleEndpoints(eventType, route)
}
func (a *StatusAdmitter) HandleNamespaces(namespaces sets.String) error {
return a.plugin.HandleNamespaces(namespaces)
}