forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
extended_validator.go
73 lines (60 loc) · 2.5 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
package controller
import (
"fmt"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/watch"
kapi "k8s.io/kubernetes/pkg/apis/core"
routeapi "github.com/openshift/origin/pkg/route/apis/route"
"github.com/openshift/origin/pkg/route/apis/route/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
}
// 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,
}
}
// 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)
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)
p.plugin.HandleRoute(watch.Deleted, route)
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()
}