Skip to content

Commit

Permalink
Adding stackdriver exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
Xuewei Zhang committed Sep 11, 2019
1 parent 9355471 commit 0609bed
Show file tree
Hide file tree
Showing 15 changed files with 682 additions and 37 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()
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
55 changes: 33 additions & 22 deletions 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 @@ -85,7 +86,9 @@ type NodeProblemDetectorOptions struct {
}

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

for _, problemDaemonName := range problemdaemon.GetProblemDaemonNames() {
npdo.MonitorConfigPaths[problemDaemonName] = &[]string{}
}
Expand All @@ -94,30 +97,38 @@ func NewNodeProblemDetectorOptions() *NodeProblemDetectorOptions {

// AddFlags adds node problem detector command line options to pflag.
func (npdo *NodeProblemDetectorOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringSliceVar(&npdo.SystemLogMonitorConfigPaths, "system-log-monitors",
[]string{}, "List of paths to system log monitor config files, comma separated.")
fs.StringVar(&npdo.HostnameOverride, "hostname-override", "",
"Custom node name used to override hostname")
fs.BoolVar(&npdo.PrintVersion, "version", false, "Print version information and quit")

fs.StringSliceVar(&npdo.SystemLogMonitorConfigPaths, "system-log-monitors", []string{},
"List of paths to system log monitor config files, comma separated.")
fs.MarkDeprecated("system-log-monitors", "replaced by --config.system-log-monitor. NPD will panic if both --system-log-monitors and --config.system-log-monitor are set.")
fs.StringSliceVar(&npdo.CustomPluginMonitorConfigPaths, "custom-plugin-monitors",
[]string{}, "List of paths to custom plugin monitor config files, comma separated.")
fs.StringSliceVar(&npdo.CustomPluginMonitorConfigPaths, "custom-plugin-monitors", []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.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.")
fs.DurationVar(&npdo.APIServerWaitInterval, "apiserver-wait-interval", time.Duration(5)*time.Second, "The interval between the checks on the readiness of kube-apiserver. This is ignored if --enable-k8s-exporter is false.")
fs.BoolVar(&npdo.PrintVersion, "version", false, "Print version information and quit")
fs.StringVar(&npdo.HostnameOverride, "hostname-override",
"", "Custom node name used to override hostname")
fs.IntVar(&npdo.ServerPort, "port",
20256, "The port to bind the node problem detector server. Use 0 to disable.")
fs.StringVar(&npdo.ServerAddress, "address",
"127.0.0.1", "The address to bind the node problem detector server.")

fs.IntVar(&npdo.PrometheusServerPort, "prometheus-port",
20257, "The port to bind the Prometheus scrape endpoint. Prometheus exporter is enabled by default at port 20257. Use 0 to disable.")
fs.StringVar(&npdo.PrometheusServerAddress, "prometheus-address",
"127.0.0.1", "The address to bind the Prometheus scrape endpoint.")

fs.BoolVar(&npdo.EnableK8sExporter, "enable-k8s-exporter", true, "Enables reporting to Kubernetes API server.")
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.")
fs.DurationVar(&npdo.APIServerWaitInterval, "apiserver-wait-interval", time.Duration(5)*time.Second,
"The interval between the checks on the readiness of kube-apiserver. This is ignored if --enable-k8s-exporter is false.")
fs.IntVar(&npdo.ServerPort, "port", 20256,
"The port to bind the node problem detector server. Use 0 to disable.")
fs.StringVar(&npdo.ServerAddress, "address", "127.0.0.1",
"The address to bind the node problem detector server.")

fs.IntVar(&npdo.PrometheusServerPort, "prometheus-port", 20257,
"The port to bind the Prometheus scrape endpoint. Prometheus exporter is enabled by default at port 20257. Use 0 to disable.")
fs.StringVar(&npdo.PrometheusServerAddress, "prometheus-address", "127.0.0.1",
"The address to bind the Prometheus scrape endpoint.")

for _, exporterName := range exporters.GetExporterNames() {
exporterHandler := exporters.GetExporterHandlerOrDie(exporterName)
exporterHandler.Options.SetFlags(fs)
}
for _, problemDaemonName := range problemdaemon.GetProblemDaemonNames() {
fs.StringSliceVar(
npdo.MonitorConfigPaths[problemDaemonName],
Expand Down
10 changes: 10 additions & 0 deletions config/exporter/stackdriver-exporter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"apiEndpoint": "staging-monitoring.sandbox.googleapis.com:443",
"exportPeriod": "60s",
"gceMetadata": {
"projectID": "xueweiz-experimental",
"zone": "us-central1-a",
"instanceID": "3133007593278616111",
"instanceName": "test-vm-2"
}
}
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/exporter/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
63 changes: 63 additions & 0 deletions pkg/exporters/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
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 exporters

import (
"fmt"

"k8s.io/node-problem-detector/pkg/types"
)

var (
handlers = make(map[types.ExporterType]types.ExporterHandler)
)

// Register registers a exporter factory method, which will be used to create the exporter.
func Register(exporterType types.ExporterType, handler types.ExporterHandler) {
handlers[exporterType] = handler
}

// GetExporterNames retrieves all available exporter types.
func GetExporterNames() []types.ExporterType {
exporterTypes := []types.ExporterType{}
for exporterType := range handlers {
exporterTypes = append(exporterTypes, exporterType)
}
return exporterTypes
}

// GetExporterHandlerOrDie retrieves the ExporterHandler for a specific type of exporter, panic if error occurs..
func GetExporterHandlerOrDie(exporterType types.ExporterType) types.ExporterHandler {
handler, ok := handlers[exporterType]
if !ok {
panic(fmt.Sprintf("Exporter handler for %v does not exist", exporterType))
}
return handler
}

// NewExporters creates all exporters based on the configurations initialized.
func NewExporters() []types.Exporter {
exporters := []types.Exporter{}
for _, handler := range handlers {
exporter := handler.CreateExporterOrDie(handler.Options)
if exporter == nil {
continue
}
exporters = append(exporters, exporter)
}
return exporters
}
69 changes: 69 additions & 0 deletions pkg/exporters/register_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
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 exporters

import (
"testing"

"github.com/stretchr/testify/assert"

"k8s.io/node-problem-detector/pkg/types"
)

func TestRegistration(t *testing.T) {
fooExporterFactory := func(types.CommandLineOptions) types.Exporter {
return nil
}
fooExporterHandler := types.ExporterHandler{
CreateExporterOrDie: fooExporterFactory,
Options: nil,
}

barExporterFactory := func(types.CommandLineOptions) types.Exporter {
return nil
}
barExporterHandler := types.ExporterHandler{
CreateExporterOrDie: barExporterFactory,
Options: nil,
}

Register("foo", fooExporterHandler)
Register("bar", barExporterHandler)

expectedExporterNames := []types.ExporterType{"foo", "bar"}
exporterNames := GetExporterNames()
assert.ElementsMatch(t, expectedExporterNames, exporterNames)

handlers = make(map[types.ExporterType]types.ExporterHandler)
}

func TestGetExporterHandlerOrDie(t *testing.T) {
fooExporterFactory := func(types.CommandLineOptions) types.Exporter {
return nil
}
fooExporterHandler := types.ExporterHandler{
CreateExporterOrDie: fooExporterFactory,
Options: nil,
}

Register("foo", fooExporterHandler)

assert.NotPanics(t, func() { GetExporterHandlerOrDie("foo") })
assert.Panics(t, func() { GetExporterHandlerOrDie("bar") })

handlers = make(map[types.ExporterType]types.ExporterHandler)
}
55 changes: 55 additions & 0 deletions pkg/exporters/stackdriver/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
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 config

import (
"time"

"k8s.io/node-problem-detector/pkg/exporters/stackdriver/gce"
)

var (
defaultExportPeriod = (60 * time.Second).String()
defaultEndpoint = "monitoring.googleapis.com:443"
defaultMetadataFetchTimeout = (600 * time.Second).String()
defaultMetadataFetchInterval = (10 * time.Second).String()
)

type StackdriverExporterConfig struct {
ExportPeriod string `json:"exportPeriod"`
APIEndpoint string `json:"apiEndpoint"`
Metadata gce.Metadata `json:"gceMetadata"`
MetadataFetchTimeout string `json:"metadataFetchTimeout"`
MetadataFetchInterval string `json:"metadataFetchInterval"`
PanicOnMetadataFetchFailure bool `json:"panicOnMetadataFetchFailure"`
}

// ApplyConfiguration applies default configurations.
func (sec *StackdriverExporterConfig) ApplyConfiguration() {
if sec.ExportPeriod == "" {
sec.ExportPeriod = defaultExportPeriod
}
if sec.MetadataFetchTimeout == "" {
sec.MetadataFetchTimeout = defaultMetadataFetchTimeout
}
if sec.MetadataFetchInterval == "" {
sec.MetadataFetchInterval = defaultMetadataFetchInterval
}
if sec.APIEndpoint == "" {
sec.APIEndpoint = defaultEndpoint
}
}
Loading

0 comments on commit 0609bed

Please sign in to comment.