Skip to content

Commit

Permalink
Add Stackdriver exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
Xuewei Zhang committed Aug 30, 2019
1 parent 35d361b commit b740f18
Show file tree
Hide file tree
Showing 25 changed files with 1,101 additions and 187 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ PKG:=k8s.io/node-problem-detector
PKG_SOURCES:=$(shell find pkg cmd -name '*.go')

# TARBALL is the name of release tar. Include binary version by default.
TARBALL:=node-problem-detector-$(VERSION).tar.gz
TARBALL?=node-problem-detector-$(VERSION).tar.gz

# IMAGE is the image name of the node problem detector container image.
IMAGE:=$(REGISTRY)/node-problem-detector:$(TAG)
Expand Down
20 changes: 20 additions & 0 deletions cmd/nodeproblemdetector/exporterplugins/default_plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Copyright 2019 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package exporterplugins

// This file is necessary to make sure the exporterplugins package non-empty
// under any build tags.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// +build !disable_stackdriver_exporter

/*
Copyright 2019 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package exporterplugins

import (
_ "k8s.io/node-problem-detector/pkg/exporters/stackdriver"
)

// The stackdriver plugin takes about 6MB in the NPD binary.
16 changes: 11 additions & 5 deletions cmd/nodeproblemdetector/node_problem_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import (
"github.com/golang/glog"
"github.com/spf13/pflag"

_ "k8s.io/node-problem-detector/cmd/nodeproblemdetector/exporterplugins"
_ "k8s.io/node-problem-detector/cmd/nodeproblemdetector/problemdaemonplugins"
"k8s.io/node-problem-detector/cmd/options"
"k8s.io/node-problem-detector/pkg/exporters"
"k8s.io/node-problem-detector/pkg/exporters/k8sexporter"
"k8s.io/node-problem-detector/pkg/exporters/prometheusexporter"
"k8s.io/node-problem-detector/pkg/problemdaemon"
Expand Down Expand Up @@ -54,21 +56,25 @@ func main() {
}

// Initialize exporters.
exporters := []types.Exporter{}
initializedExporters := []types.Exporter{}
if ke := k8sexporter.NewExporterOrDie(npdo); ke != nil {
exporters = append(exporters, ke)
initializedExporters = append(initializedExporters, ke)
glog.Info("K8s exporter started.")
}
if pe := prometheusexporter.NewExporterOrDie(npdo); pe != nil {
exporters = append(exporters, pe)
initializedExporters = append(initializedExporters, pe)
glog.Info("Prometheus exporter started.")
}
if len(exporters) == 0 {
plugableExporters := exporters.NewExporters(npdo.ExporterConfigPaths)
if len(plugableExporters) != 0 {
initializedExporters = append(initializedExporters, plugableExporters...)
}
if len(initializedExporters) == 0 {
glog.Fatalf("No exporter is successfully setup")
}

// Initialize NPD core.
p := problemdetector.NewProblemDetector(problemDaemons, exporters)
p := problemdetector.NewProblemDetector(problemDaemons, initializedExporters)
if err := p.Run(); err != nil {
glog.Fatalf("Problem detector failed with error: %v", err)
}
Expand Down
21 changes: 20 additions & 1 deletion cmd/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/spf13/pflag"

"k8s.io/node-problem-detector/pkg/exporters"
"k8s.io/node-problem-detector/pkg/problemdaemon"
"k8s.io/node-problem-detector/pkg/types"
)
Expand Down Expand Up @@ -63,6 +64,9 @@ type NodeProblemDetectorOptions struct {
// PrometheusServerAddress is the address to bind the Prometheus scrape endpoint.
PrometheusServerAddress string

// ExporterConfigPaths specifies the list of paths to configuration files for each exporter.
ExporterConfigPaths types.ExporterConfigPathMap

// problem daemon options

// SystemLogMonitorConfigPaths specifies the list of paths to system log monitor configuration
Expand All @@ -85,7 +89,13 @@ type NodeProblemDetectorOptions struct {
}

func NewNodeProblemDetectorOptions() *NodeProblemDetectorOptions {
npdo := &NodeProblemDetectorOptions{MonitorConfigPaths: types.ProblemDaemonConfigPathMap{}}
npdo := &NodeProblemDetectorOptions{
ExporterConfigPaths: types.ExporterConfigPathMap{},
MonitorConfigPaths: types.ProblemDaemonConfigPathMap{}}

for _, exporterName := range exporters.GetExporterNames() {
npdo.ExporterConfigPaths[exporterName] = &[]string{}
}
for _, problemDaemonName := range problemdaemon.GetProblemDaemonNames() {
npdo.MonitorConfigPaths[problemDaemonName] = &[]string{}
}
Expand Down Expand Up @@ -118,6 +128,15 @@ func (npdo *NodeProblemDetectorOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&npdo.PrometheusServerAddress, "prometheus-address",
"127.0.0.1", "The address to bind the Prometheus scrape endpoint.")

for _, exporterName := range exporters.GetExporterNames() {
fs.StringSliceVar(
npdo.ExporterConfigPaths[exporterName],
"exporter."+string(exporterName),
[]string{},
fmt.Sprintf("Comma separated configurations for %v exporter. %v",
exporterName,
exporters.GetExporterHandlerOrDie(exporterName).CmdOptionDescription))
}
for _, problemDaemonName := range problemdaemon.GetProblemDaemonNames() {
fs.StringSliceVar(
npdo.MonitorConfigPaths[problemDaemonName],
Expand Down
54 changes: 41 additions & 13 deletions cmd/options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,13 @@ func TestSetNodeNameOrDie(t *testing.T) {
}

func TestValidOrDie(t *testing.T) {
emptyMonitorConfigMap := types.ProblemDaemonConfigPathMap{}
fooMonitorConfigMap := types.ProblemDaemonConfigPathMap{}
fooMonitorConfigMap["foo-monitor"] = &[]string{"config-a", "config-b"}

emptyMonitorConfigMap := types.ProblemDaemonConfigPathMap{}
emptyExporterConfigMap := types.ExporterConfigPathMap{}
barExporterConfigMap := types.ExporterConfigPathMap{}
barExporterConfigMap["bar-exporter"] = &[]string{"config-c", "config-d"}

testCases := []struct {
name string
Expand All @@ -133,48 +136,54 @@ func TestValidOrDie(t *testing.T) {
{
name: "default k8s exporter config",
npdo: NodeProblemDetectorOptions{
MonitorConfigPaths: fooMonitorConfigMap,
MonitorConfigPaths: fooMonitorConfigMap,
ExporterConfigPaths: barExporterConfigMap,
},
expectPanic: false,
},
{
name: "enables k8s exporter config",
npdo: NodeProblemDetectorOptions{
ApiServerOverride: "",
EnableK8sExporter: true,
MonitorConfigPaths: fooMonitorConfigMap,
ApiServerOverride: "",
EnableK8sExporter: true,
MonitorConfigPaths: fooMonitorConfigMap,
ExporterConfigPaths: barExporterConfigMap,
},
expectPanic: false,
},
{
name: "k8s exporter config with valid ApiServerOverride",
npdo: NodeProblemDetectorOptions{
ApiServerOverride: "127.0.0.1",
EnableK8sExporter: true,
MonitorConfigPaths: fooMonitorConfigMap,
ApiServerOverride: "127.0.0.1",
EnableK8sExporter: true,
MonitorConfigPaths: fooMonitorConfigMap,
ExporterConfigPaths: barExporterConfigMap,
},
expectPanic: false,
},
{
name: "k8s exporter config with invalid ApiServerOverride",
npdo: NodeProblemDetectorOptions{
ApiServerOverride: ":foo",
EnableK8sExporter: true,
MonitorConfigPaths: fooMonitorConfigMap,
ApiServerOverride: ":foo",
EnableK8sExporter: true,
MonitorConfigPaths: fooMonitorConfigMap,
ExporterConfigPaths: barExporterConfigMap,
},
expectPanic: true,
},
{
name: "non-empty MonitorConfigPaths",
npdo: NodeProblemDetectorOptions{
MonitorConfigPaths: fooMonitorConfigMap,
MonitorConfigPaths: fooMonitorConfigMap,
ExporterConfigPaths: barExporterConfigMap,
},
expectPanic: false,
},
{
name: "empty MonitorConfigPaths",
npdo: NodeProblemDetectorOptions{
MonitorConfigPaths: emptyMonitorConfigMap,
MonitorConfigPaths: emptyMonitorConfigMap,
ExporterConfigPaths: barExporterConfigMap,
},
expectPanic: true,
},
Expand All @@ -188,6 +197,7 @@ func TestValidOrDie(t *testing.T) {
npdo: NodeProblemDetectorOptions{
SystemLogMonitorConfigPaths: []string{"config-a"},
MonitorConfigPaths: fooMonitorConfigMap,
ExporterConfigPaths: barExporterConfigMap,
},
expectPanic: true,
},
Expand All @@ -196,6 +206,7 @@ func TestValidOrDie(t *testing.T) {
npdo: NodeProblemDetectorOptions{
CustomPluginMonitorConfigPaths: []string{"config-a"},
MonitorConfigPaths: fooMonitorConfigMap,
ExporterConfigPaths: barExporterConfigMap,
},
expectPanic: true,
},
Expand All @@ -204,13 +215,15 @@ func TestValidOrDie(t *testing.T) {
npdo: NodeProblemDetectorOptions{
SystemLogMonitorConfigPaths: []string{"config-a"},
MonitorConfigPaths: emptyMonitorConfigMap,
ExporterConfigPaths: barExporterConfigMap,
},
expectPanic: true,
},
{
name: "deprecated SystemLogMonitor option with un-initialized MonitorConfigPaths",
npdo: NodeProblemDetectorOptions{
SystemLogMonitorConfigPaths: []string{"config-a"},
ExporterConfigPaths: barExporterConfigMap,
},
expectPanic: true,
},
Expand All @@ -219,16 +232,31 @@ func TestValidOrDie(t *testing.T) {
npdo: NodeProblemDetectorOptions{
CustomPluginMonitorConfigPaths: []string{"config-b"},
MonitorConfigPaths: emptyMonitorConfigMap,
ExporterConfigPaths: barExporterConfigMap,
},
expectPanic: true,
},
{
name: "deprecated CustomPluginMonitor option with un-initialized MonitorConfigPaths",
npdo: NodeProblemDetectorOptions{
CustomPluginMonitorConfigPaths: []string{"config-b"},
ExporterConfigPaths: barExporterConfigMap,
},
expectPanic: true,
},
{
name: "empty ExporterConfigPaths",
npdo: NodeProblemDetectorOptions{
MonitorConfigPaths: fooMonitorConfigMap,
ExporterConfigPaths: emptyExporterConfigMap,
},
expectPanic: false,
},
{
name: "un-initialized ExporterConfigPaths",
npdo: NodeProblemDetectorOptions{},
expectPanic: true,
},
}

for _, test := range testCases {
Expand Down
3 changes: 3 additions & 0 deletions config/stackdriver-exporter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"exportPeriod": "60s"
}
5 changes: 3 additions & 2 deletions config/systemd/node-problem-detector-metric-only.service
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[Unit]
Description=Node problem detector
Wants=local-fs.target
After=local-fs.target
Wants=network-online.target
After=network-online.target

[Service]
Restart=always
RestartSec=10
ExecStart=/home/kubernetes/bin/node-problem-detector --v=2 --logtostderr --enable-k8s-exporter=false \
--exporter.stackdriver=/home/kubernetes/node-problem-detector/config/stackdriver-exporter.json \
--config.system-log-monitor=/home/kubernetes/node-problem-detector/config/kernel-monitor.json,/home/kubernetes/node-problem-detector/config/docker-monitor.json,/home/kubernetes/node-problem-detector/config/systemd-monitor.json \
--config.custom-plugin-monitor=/home/kubernetes/node-problem-detector/config/kernel-monitor-counter.json,/home/kubernetes/node-problem-detector/config/systemd-monitor-counter.json \
--config.system-stats-monitor=/home/kubernetes/node-problem-detector/config/system-stats-monitor.json
Expand Down
16 changes: 16 additions & 0 deletions config/systemd/node-problem-detector-stackdriver.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Unit]
Description=Node problem detector
Wants=network-online.target
After=network-online.target

[Service]
Restart=always
RestartSec=10
ExecStart=/home/kubernetes/bin/node-problem-detector --v=2 --logtostderr --enable-k8s-exporter=false \
--exporter.stackdriver=/home/kubernetes/node-problem-detector/config/stackdriver-exporter.json \
--config.system-log-monitor=/home/kubernetes/node-problem-detector/config/kernel-monitor.json,/home/kubernetes/node-problem-detector/config/docker-monitor.json,/home/kubernetes/node-problem-detector/config/systemd-monitor.json \
--config.custom-plugin-monitor=/home/kubernetes/node-problem-detector/config/kernel-monitor-counter.json,/home/kubernetes/node-problem-detector/config/systemd-monitor-counter.json \
--config.system-stats-monitor=/home/kubernetes/node-problem-detector/config/system-stats-monitor.json

[Install]
WantedBy=multi-user.target
Loading

0 comments on commit b740f18

Please sign in to comment.