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 7fd465e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 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
24 changes: 13 additions & 11 deletions pkg/exporters/k8sexporter/problemclient/problem_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ type Client interface {
}

type nodeProblemClient struct {
nodeName string
client typedcorev1.CoreV1Interface
clock clock.Clock
recorders map[string]record.EventRecorder
nodeRef *v1.ObjectReference
nodeName string
client typedcorev1.CoreV1Interface
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,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func newFakeProblemClient() *nodeProblemClient {
// TODO(random-liu): Add test for SetConditions when we have good fake for *client.Client
clock: &clock.FakeClock{},
recorders: make(map[string]record.EventRecorder),
nodeRef: getNodeRef(testNode),
nodeRef: getNodeRef("", testNode),
}
}

Expand Down

0 comments on commit 7fd465e

Please sign in to comment.