Skip to content

Commit

Permalink
Add event generation code for blueprint validation errors.
Browse files Browse the repository at this point in the history
Note that this requires the router to have permissions to generate events.

Example if the blueprints are in namespace called `blues`:
  $ oc create role evite --verb=create --resource=event -n blues
  $ oc adm policy add-role-to-user evite  \
      system:serviceaccount:default:router --role-namespace=blues -n blues
  • Loading branch information
ramr committed Aug 21, 2018
1 parent 6b8adcc commit ce74c06
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
7 changes: 6 additions & 1 deletion pkg/cmd/infra/router/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/apiserver/pkg/server/healthz"
authenticationclient "k8s.io/client-go/kubernetes/typed/authentication/v1beta1"
authorizationclient "k8s.io/client-go/kubernetes/typed/authorization/v1beta1"
coreclient "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"

Expand Down Expand Up @@ -445,6 +446,10 @@ func (o *TemplateRouterOptions) Run() error {
if err != nil {
return err
}
eventclient, err := coreclient.NewForConfig(config)
if err != nil {
return err
}
routeclient, err := routeclientset.NewForConfig(config)
if err != nil {
return err
Expand Down Expand Up @@ -472,7 +477,7 @@ func (o *TemplateRouterOptions) Run() error {
}
cfgManager = haproxyconfigmanager.NewHAProxyConfigManager(cmopts)
if len(o.BlueprintRouteNamespace) > 0 {
blueprintPlugin = haproxyconfigmanager.NewBlueprintPlugin(cfgManager)
blueprintPlugin = haproxyconfigmanager.NewBlueprintPlugin(cfgManager, eventclient)
}
}

Expand Down
41 changes: 38 additions & 3 deletions pkg/router/template/configmanager/haproxy/blueprint_plugin.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package haproxy

import (
"fmt"

"github.com/golang/glog"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/watch"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
kapi "k8s.io/kubernetes/pkg/apis/core"

routev1 "github.com/openshift/api/route/v1"
Expand All @@ -13,18 +19,21 @@ import (
// from the blueprint namespace for the associated config manager.
type BlueprintPlugin struct {
manager templaterouter.ConfigManager
client corev1client.CoreV1Interface
}

// NewBlueprintPlugin returns a new blueprint routes plugin.
func NewBlueprintPlugin(cm templaterouter.ConfigManager) *BlueprintPlugin {
return &BlueprintPlugin{manager: cm}
func NewBlueprintPlugin(cm templaterouter.ConfigManager, coreclient corev1client.CoreV1Interface) *BlueprintPlugin {
return &BlueprintPlugin{manager: cm, client: coreclient}
}

// HandleRoute processes watch events on blueprint routes.
func (p *BlueprintPlugin) HandleRoute(eventType watch.EventType, route *routev1.Route) error {
switch eventType {
case watch.Added, watch.Modified:
p.manager.AddBlueprint(route)
if err := p.manager.AddBlueprint(route); err != nil {
p.generateBlueprintFailureEvent(route, err.Error())
}
case watch.Deleted:
p.manager.RemoveBlueprint(route)
}
Expand Down Expand Up @@ -53,3 +62,29 @@ func (p *BlueprintPlugin) Commit() error {
// any blueprint routes change.
return nil
}

func (p *BlueprintPlugin) generateBlueprintFailureEvent(route *routev1.Route, failure string) {
name := fmt.Sprintf("%s/%s", route.Namespace, route.Name)

e := &v1.Event{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("hapcm-%s", route.Name),
Namespace: route.Namespace,
},

InvolvedObject: v1.ObjectReference{Kind: "Route", Name: route.Name, UID: route.UID, Namespace: route.Namespace},

Message: failure,
Source: v1.EventSource{
Component: fmt.Sprintf("blueprint route %s", name),
},
Type: v1.EventTypeWarning,

ReportingController: "cm.haproxy.router.openshift.io/blueprints",
}

if _, err := p.client.Events(route.Namespace).Create(e); err != nil {
glog.Errorf("adding blueprint route error event: %v", err)
return
}
}

0 comments on commit ce74c06

Please sign in to comment.