Skip to content

Commit

Permalink
Add namespace option for events
Browse files Browse the repository at this point in the history
  • Loading branch information
muff1nman committed Mar 6, 2020
1 parent 4ad6227 commit 0a87b5e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
3 changes: 3 additions & 0 deletions cmd/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type NodeProblemDetectorOptions struct {
// k8sExporter options
// EnableK8sExporter is the flag determining whether to report to Kubernetes.
EnableK8sExporter bool
// EventNamespace is the namespace events are written to
EventNamespace string
// ApiServerOverride is the custom URI used to connect to Kubernetes ApiServer.
ApiServerOverride string
// APIServerWaitTimeout is the timeout on waiting for kube-apiserver to be
Expand Down Expand Up @@ -105,6 +107,7 @@ func (npdo *NodeProblemDetectorOptions) AddFlags(fs *pflag.FlagSet) {
[]string{}, "List of paths to custom plugin monitor config files, comma separated.")
fs.MarkDeprecated("custom-plugin-monitors", "replaced by --config.custom-plugin-monitor. NPD will panic if both --custom-plugin-monitors and --config.custom-plugin-monitor are set.")
fs.BoolVar(&npdo.EnableK8sExporter, "enable-k8s-exporter", true, "Enables reporting to Kubernetes API server.")
fs.StringVar(&npdo.EventNamespace, "event-namespace", "", "Namespace for recorded Kubernetes events.")
fs.StringVar(&npdo.ApiServerOverride, "apiserver-override",
"", "Custom URI used to connect to Kubernetes ApiServer. This is ignored if --enable-k8s-exporter is false.")
fs.DurationVar(&npdo.APIServerWaitTimeout, "apiserver-wait-timeout", time.Duration(5)*time.Minute, "The timeout on waiting for kube-apiserver to be ready. This is ignored if --enable-k8s-exporter is false.")
Expand Down
14 changes: 8 additions & 6 deletions pkg/exporters/k8sexporter/problemclient/problem_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type nodeProblemClient struct {
clock clock.Clock
recorders map[string]record.EventRecorder
nodeRef *v1.ObjectReference
eventNamespace string
}

// NewClientOrDie creates a new problem client, panics if error occurs.
Expand All @@ -76,7 +77,8 @@ func NewClientOrDie(npdo *options.NodeProblemDetectorOptions) Client {
// TODO(random-liu): Set QPS Limit
c.client = clientset.NewForConfigOrDie(cfg).CoreV1()
c.nodeName = npdo.NodeName
c.nodeRef = getNodeRef(c.nodeName)
c.eventNamespace = npdo.EventNamespace
c.nodeRef = getNodeRef(c.eventNamespace, c.nodeName)
c.recorders = make(map[string]record.EventRecorder)
return c
}
Expand Down Expand Up @@ -113,7 +115,7 @@ func (c *nodeProblemClient) Eventf(eventType, source, reason, messageFmt string,
recorder, found := c.recorders[source]
if !found {
// TODO(random-liu): If needed use separate client and QPS limit for event.
recorder = getEventRecorder(c.client, c.nodeName, source)
recorder = getEventRecorder(c.client, c.eventNamespace, c.nodeName, source)
c.recorders[source] = recorder
}
recorder.Eventf(c.nodeRef, eventType, reason, messageFmt, args...)
Expand All @@ -133,20 +135,20 @@ func generatePatch(conditions []v1.NodeCondition) ([]byte, error) {
}

// getEventRecorder generates a recorder for specific node name and source.
func getEventRecorder(c typedcorev1.CoreV1Interface, nodeName, source string) record.EventRecorder {
func getEventRecorder(c typedcorev1.CoreV1Interface, namespace, nodeName, source string) record.EventRecorder {
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(glog.V(4).Infof)
recorder := eventBroadcaster.NewRecorder(legacyscheme.Scheme, v1.EventSource{Component: source, Host: nodeName})
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: c.Events("")})
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: c.Events(namespace)})
return recorder
}

func getNodeRef(nodeName string) *v1.ObjectReference {
func getNodeRef(namespace, nodeName string) *v1.ObjectReference {
// TODO(random-liu): Get node to initialize the node reference
return &v1.ObjectReference{
Kind: "Node",
Name: nodeName,
UID: types.UID(nodeName),
Namespace: "",
Namespace: namespace,
}
}

0 comments on commit 0a87b5e

Please sign in to comment.