forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
extended_validator.go
84 lines (69 loc) · 2.86 KB
/
extended_validator.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
package controller
import (
"fmt"
"reflect"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/watch"
kapi "k8s.io/kubernetes/pkg/api"
routeapi "github.com/openshift/origin/pkg/route/api"
"github.com/openshift/origin/pkg/route/api/validation"
"github.com/openshift/origin/pkg/router"
)
// ExtendedValidator implements the router.Plugin interface to provide
// extended config validation for template based, backend-agnostic routers.
type ExtendedValidator struct {
// plugin is the next plugin in the chain.
plugin router.Plugin
// recorder is an interface for indicating route rejections.
recorder RejectionRecorder
// invalidRoutes is a map of invalid routes previously encountered.
invalidRoutes map[string]routeapi.Route
}
// NewExtendedValidator creates a plugin wrapper that ensures only routes that
// pass extended validation are relayed to the next plugin in the chain.
// Recorder is an interface for indicating why a route was rejected.
func NewExtendedValidator(plugin router.Plugin, recorder RejectionRecorder) *ExtendedValidator {
return &ExtendedValidator{
plugin: plugin,
recorder: recorder,
invalidRoutes: make(map[string]routeapi.Route),
}
}
// HandleNode processes watch events on the node resource
func (p *ExtendedValidator) HandleNode(eventType watch.EventType, node *kapi.Node) error {
return p.plugin.HandleNode(eventType, node)
}
// HandleEndpoints processes watch events on the Endpoints resource.
func (p *ExtendedValidator) HandleEndpoints(eventType watch.EventType, endpoints *kapi.Endpoints) error {
return p.plugin.HandleEndpoints(eventType, endpoints)
}
// HandleRoute processes watch events on the Route resource.
func (p *ExtendedValidator) HandleRoute(eventType watch.EventType, route *routeapi.Route) error {
// Check if previously seen route and its Spec is unchanged.
routeName := routeNameKey(route)
old, ok := p.invalidRoutes[routeName]
if ok && reflect.DeepEqual(old.Spec, route.Spec) {
// Route spec was unchanged and it is already marked in
// error, we don't need to do anything more.
return fmt.Errorf("invalid route configuration")
}
if errs := validation.ExtendedValidateRoute(route); len(errs) > 0 {
errmsg := ""
for i := 0; i < len(errs); i++ {
errmsg = errmsg + "\n - " + errs[i].Error()
}
glog.Errorf("Skipping route %s due to invalid configuration: %s", routeName, errmsg)
p.recorder.RecordRouteRejection(route, "ExtendedValidationFailed", errmsg)
return fmt.Errorf("invalid route configuration")
}
return p.plugin.HandleRoute(eventType, route)
}
// HandleNamespaces limits the scope of valid routes to only those that match
// the provided namespace list.
func (p *ExtendedValidator) HandleNamespaces(namespaces sets.String) error {
return p.plugin.HandleNamespaces(namespaces)
}
func (p *ExtendedValidator) Commit() error {
return p.plugin.Commit()
}