Skip to content

Commit

Permalink
Add eventrecorder
Browse files Browse the repository at this point in the history
  • Loading branch information
pwittrock committed Mar 16, 2018
1 parent 88331d0 commit fd2f696
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cmd/kubebuilder-gen/internal/controllergen/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ func (d *injectGenerator) Imports(c *generator.Context) []string {
repo + "/pkg/client/informers_generated/externalversions",
repo + "/pkg/inject/args",
"rbacv1 \"k8s.io/api/rbac/v1\"",
"k8s.io/apimachinery/pkg/runtime/schema",
"k8s.io/client-go/kubernetes/scheme",
"rscheme " + "\"" + repo + "/pkg/client/clientset_generated/clientset/scheme\"",
}

// Import package for each controller
Expand Down Expand Up @@ -99,6 +100,8 @@ func (d *injectGenerator) Finalize(context *generator.Context, w io.Writer) erro

var injectAPITemplate = `
func init() {
rscheme.AddToScheme(scheme.Scheme)
// Inject Informers
Inject = append(Inject, func(arguments args.InjectArgs) error {
Injector.ControllerManager = arguments.ControllerManager
Expand Down
17 changes: 17 additions & 0 deletions pkg/inject/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ package args
import (
"time"

"github.com/golang/glog"
"github.com/kubernetes-sigs/kubebuilder/pkg/admission"
"github.com/kubernetes-sigs/kubebuilder/pkg/controller"
"github.com/kubernetes-sigs/kubebuilder/pkg/inject/run"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
)

// InjectArgs are the common arguments for initializing controllers and admission hooks
Expand All @@ -46,17 +51,29 @@ type InjectArgs struct {

// AdmissionManager is the admission webhook manager
AdmissionHandler *admission.AdmissionManager

// EventBroadcaster
EventBroadcaster record.EventBroadcaster
}

// CreateRecorder returns a new recorder
func (iargs InjectArgs) CreateRecorder(name string) record.EventRecorder {
return iargs.EventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: name})
}

// CreateInjectArgs returns new arguments for initializing objects
func CreateInjectArgs(config *rest.Config) InjectArgs {
cs := kubernetes.NewForConfigOrDie(config)
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(glog.Infof)
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: cs.CoreV1().Events("")})
return InjectArgs{
Config: config,
KubernetesClientSet: cs,
KubernetesInformers: informers.NewSharedInformerFactory(cs, 2*time.Minute),
ControllerManager: &controller.ControllerManager{},
AdmissionHandler: &admission.AdmissionManager{},
EventBroadcaster: eventBroadcaster,
}
}

Expand Down

0 comments on commit fd2f696

Please sign in to comment.