Skip to content

Commit b66c4df

Browse files
authored
Merge pull request #39 from Random-Liu/journald-support
Journald support
2 parents ba5f5a1 + 457272c commit b66c4df

26 files changed

+829
-364
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/bin/node-problem-detector
2+
/Dockerfile

Dockerfile renamed to Dockerfile.in

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
FROM alpine:3.4
15+
FROM @BASEIMAGE@
1616
MAINTAINER Random Liu <[email protected]>
17+
18+
# Avoid symlink of /etc/localtime.
19+
RUN test -h /etc/localtime && rm -f /etc/localtime && cp /usr/share/zoneinfo/UTC /etc/localtime || true
20+
1721
ADD ./bin/node-problem-detector /node-problem-detector
1822
ADD config /config
1923
ENTRYPOINT ["/node-problem-detector", "--kernel-monitor=/config/kernel-monitor.json"]

Godeps/Godeps.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,73 @@
1-
.PHONY: all build-container build-tar build push-container push-tar push clean vet fmt version
1+
# Copyright 2017 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Build the node-problem-detector image.
16+
17+
.PHONY: all build-container build-tar build push-container push-tar push clean vet fmt version Dockerfile
218

319
all: build
420

5-
VERSION := $(shell git describe --tags --dirty)
21+
# VERSION is the version of the binary.
22+
VERSION:=$(shell git describe --tags --dirty)
623

7-
TAG ?= $(VERSION)
24+
# TAG is the tag of the container image, default to binary version.
25+
TAG?=$(VERSION)
826

9-
UPLOAD_PATH ?= gs://kubernetes-release
27+
# PROJ is the image project.
28+
PROJ?=gcr.io/google_containers
29+
30+
# UPLOAD_PATH is the cloud storage path to upload release tar.
31+
UPLOAD_PATH?=gs://kubernetes-release
1032
# Trim the trailing '/' in the path
11-
UPLOAD_PATH := $(shell echo $(UPLOAD_PATH) | sed '$$s/\/*$$//')
33+
UPLOAD_PATH:=$(shell echo $(UPLOAD_PATH) | sed '$$s/\/*$$//')
34+
35+
# PKG is the package name of node problem detector repo.
36+
PKG:=k8s.io/node-problem-detector
37+
38+
# PKG_SOURCES are all the go source code.
39+
PKG_SOURCES:=$(shell find pkg cmd -name '*.go')
1240

13-
PROJ ?= google_containers
41+
# TARBALL is the name of release tar. Include binary version by default.
42+
TARBALL:=node-problem-detector-$(VERSION).tar.gz
1443

15-
PKG := k8s.io/node-problem-detector
44+
# IMAGE is the image name of the node problem detector container image.
45+
IMAGE:=$(PROJ)/node-problem-detector:$(TAG)
1646

17-
PKG_SOURCES := $(shell find pkg cmd -name '*.go')
47+
# ENABLE_JOURNALD enables build journald support or not. Building journald support needs libsystemd-dev
48+
# or libsystemd-journal-dev.
49+
# TODO(random-liu): Build NPD inside container.
50+
ENABLE_JOURNALD?=1
1851

19-
TARBALL := node-problem-detector-$(VERSION).tar.gz
52+
# TODO(random-liu): Support different architectures.
53+
BASEIMAGE:=alpine:3.4
2054

21-
IMAGE := gcr.io/$(PROJ)/node-problem-detector:$(TAG)
55+
# Disable cgo by default to make the binary statically linked.
56+
CGO_ENABLED:=0
57+
58+
# NOTE that enable journald will increase the image size.
59+
ifeq ($(ENABLE_JOURNALD), 1)
60+
# Enable journald build tag.
61+
BUILD_TAGS:=-tags journald
62+
# Use fedora because it has newer systemd version (229) and support +LZ4. +LZ4 is needed
63+
# on some os distros such as GCI.
64+
BASEIMAGE:=fedora
65+
# Enable cgo because sdjournal needs cgo to compile. The binary will be dynamically
66+
# linked if CGO_ENABLED is enabled. This is fine because fedora already has necessary
67+
# dynamic library. We can not use `-extldflags "-static"` here, because go-systemd uses
68+
# dlopen, and dlopen will not work properly in a statically linked application.
69+
CGO_ENABLED:=1
70+
endif
2271

2372
vet:
2473
go list ./... | grep -v "./vendor/*" | xargs go vet
@@ -30,14 +79,17 @@ version:
3079
@echo $(VERSION)
3180

3281
./bin/node-problem-detector: $(PKG_SOURCES)
33-
GOOS=linux go build -o bin/node-problem-detector \
34-
-ldflags '-w -extldflags "-static" -X $(PKG)/pkg/version.version=$(VERSION)' \
35-
cmd/node_problem_detector.go
82+
CGO_ENABLED=$(CGO_ENABLED) GOOS=linux go build -o bin/node-problem-detector \
83+
-ldflags '-w -X $(PKG)/pkg/version.version=$(VERSION)' \
84+
$(BUILD_TAGS) cmd/node_problem_detector.go
85+
86+
Dockerfile: Dockerfile.in
87+
sed -e 's|@BASEIMAGE@|$(BASEIMAGE)|g' $< >$@
3688

3789
test: vet fmt
38-
go test -timeout=1m -v -race ./pkg/...
90+
go test -timeout=1m -v -race ./pkg/... $(BUILD_TAGS)
3991

40-
build-container: ./bin/node-problem-detector
92+
build-container: ./bin/node-problem-detector Dockerfile
4193
docker build -t $(IMAGE) .
4294

4395
build-tar: ./bin/node-problem-detector

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,17 @@ spec:
100100
- name: log
101101
mountPath: /log
102102
readOnly: true
103+
- name: localtime
104+
mountPath: /etc/localtime
105+
readOnly: true
103106
volumes:
104107
- name: log
105108
# Config `log` to your system log directory
106109
hostPath:
107110
path: /var/log/
111+
- name: localtime
112+
hostPath:
113+
path: /etc/localtime
108114
```
109115
* Edit node-problem-detector.yaml to fit your environment: Set `log` volume to your system log diretory. (Used by KernelMonitor)
110116
* Create the DaemonSet with `kubectl create -f node-problem-detector.yaml`

cmd/node_problem_detector.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ package main
1818

1919
import (
2020
"flag"
21+
"fmt"
2122
"net/url"
2223
"os"
2324

25+
"github.com/golang/glog"
26+
2427
"k8s.io/node-problem-detector/pkg/kernelmonitor"
2528
"k8s.io/node-problem-detector/pkg/problemdetector"
2629
"k8s.io/node-problem-detector/pkg/version"
27-
28-
"github.com/golang/glog"
29-
"fmt"
3030
)
3131

3232
// TODO: Move flags to options directory.
@@ -86,5 +86,7 @@ func main() {
8686

8787
k := kernelmonitor.NewKernelMonitorOrDie(*kernelMonitorConfigPath)
8888
p := problemdetector.NewProblemDetector(k, *apiServerOverride, nodeName)
89-
p.Run()
89+
if err := p.Run(); err != nil {
90+
glog.Fatalf("Problem detector failed with error: %v", err)
91+
}
9092
}

config/kernel-monitor.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"logPath": "/log/kern.log",
2+
"plugin": "journald",
3+
"logPath": "/var/log/journal",
34
"lookback": "10m",
45
"startPattern": "Initializing cgroup subsys cpuset",
56
"bufferSize": 10,

node-problem-detector.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ spec:
1212
- name: node-problem-detector
1313
command:
1414
- /node-problem-detector
15+
- --logtostderr
1516
- --kernel-monitor=/config/kernel-monitor.json
1617
image: gcr.io/google_containers/node-problem-detector:v0.2
1718
imagePullPolicy: Always
@@ -24,7 +25,12 @@ spec:
2425
fieldPath: spec.nodeName
2526
volumeMounts:
2627
- name: log
27-
mountPath: /log
28+
mountPath: /var/log
29+
readOnly: true
30+
# Make sure node problem detector is in the same timezone
31+
# with the host.
32+
- name: localtime
33+
mountPath: /etc/localtime
2834
readOnly: true
2935
- name: config
3036
mountPath: /config
@@ -34,6 +40,9 @@ spec:
3440
# Config `log` to your system log directory
3541
hostPath:
3642
path: /var/log/
43+
- name: localtime
44+
hostPath:
45+
path: /etc/localtime
3746
- name: config
3847
configMap:
3948
name: node-problem-detector-config

pkg/kernelmonitor/README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@ The rule list is extensible.
99

1010
## Limitations
1111

12-
* Kernel Monitor only supports file based kernel log now. It doesn't support log tools
13-
like journald. There is an [open issue](https://github.com/kubernetes/node-problem-detector/issues/14)
14-
to add journald support.
15-
16-
* Kernel Monitor has assumption on kernel log format, now it only works on Ubuntu and
17-
Debian. However, it is easy to extend it to [support other log format](#support-other-log-format).
12+
* Kernel Monitor only supports syslog (rsyslog) and journald now, but it is easy
13+
to extend it with [new log watcher](#new-log-watcher)
1814

1915
## Add New NodeConditions
2016

@@ -43,14 +39,23 @@ with new rule definition:
4339
}
4440
```
4541

46-
## Change Log Path
42+
## Log Watchers
43+
44+
Kernel monitor supports different log management tools with different log
45+
watchers:
46+
* [syslog](https://github.com/kubernetes/node-problem-detector/blob/master/pkg/kernelmonitor/logwatchers/syslog)
47+
* [journald](https://github.com/kubernetes/node-problem-detector/blob/master/pkg/kernelmonitor/logwatchers/journald)
48+
49+
### Change Log Path
4750

48-
Kernel log in different OS distros may locate in different path. The `log`
51+
Kernel log on different OS distros may locate in different path. The `logPath`
4952
field in `config/kernel-monitor.json` is the log path inside the container.
50-
You can always configure it to match your OS distro.
53+
You can always configure `logPath` and volume mount to match your OS distro.
54+
* syslog: `logPath` is the kernel log path, usually `/var/log/kern.log`.
55+
* journald: `logPath` is the journal log directory, usually `/var/log/journal`.
5156

52-
## Support Other Log Format
57+
### New Log Watcher
5358

54-
Kernel monitor uses [`Translator`](https://github.com/kubernetes/node-problem-detector/blob/master/pkg/kernelmonitor/translator/translator.go)
55-
plugin to translate kernel log the internal data structure. It is easy to
56-
implement a new translator for a new log format.
59+
Kernel monitor uses [Log
60+
Watcher](https://github.com/kubernetes/node-problem-detector/blob/master/pkg/kernelmonitor/logwatchers/types/log_watcher.go) to support different log management tools.
61+
It is easy to implement a new log watcher.

pkg/kernelmonitor/config.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright 2016 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package kernelmonitor
18+
19+
import (
20+
watchertypes "k8s.io/node-problem-detector/pkg/kernelmonitor/logwatchers/types"
21+
kerntypes "k8s.io/node-problem-detector/pkg/kernelmonitor/types"
22+
"k8s.io/node-problem-detector/pkg/types"
23+
)
24+
25+
// MonitorConfig is the configuration of kernel monitor.
26+
type MonitorConfig struct {
27+
// WatcherConfig is the configuration of kernel log watcher.
28+
watchertypes.WatcherConfig
29+
// BufferSize is the size (in lines) of the log buffer.
30+
BufferSize int `json:"bufferSize"`
31+
// Source is the source name of the kernel monitor
32+
Source string `json:"source"`
33+
// DefaultConditions are the default states of all the conditions kernel monitor should handle.
34+
DefaultConditions []types.Condition `json:"conditions"`
35+
// Rules are the rules kernel monitor will follow to parse the log file.
36+
Rules []kerntypes.Rule `json:"rules"`
37+
// StartPattern is the pattern of the start line
38+
StartPattern string `json:"startPattern, omitempty"`
39+
}
40+
41+
// applyDefaultConfiguration applies default configurations.
42+
func applyDefaultConfiguration(cfg *MonitorConfig) {
43+
if cfg.BufferSize == 0 {
44+
cfg.BufferSize = 10
45+
}
46+
if cfg.WatcherConfig.Lookback == "" {
47+
cfg.WatcherConfig.Lookback = "0"
48+
}
49+
}

0 commit comments

Comments
 (0)