From 4adec4bbc64fa5c059a961209939bb9a1965383f Mon Sep 17 00:00:00 2001 From: Jeremy Edwards <1312331+jeremyje@users.noreply.github.com> Date: Sat, 5 Dec 2020 23:54:52 +0000 Subject: [PATCH] Introduce Windows build of Node Problem Detector --- Makefile | 55 +++++++++++++++++++ .../logwatchers/filelog/log_watcher.go | 26 --------- .../logwatchers/filelog/log_watcher_linux.go | 48 ++++++++++++++++ .../filelog/log_watcher_windows.go | 29 ++++++++++ .../{log_watcher.go => log_watcher_linux.go} | 0 ...cher_test.go => log_watcher_linux_test.go} | 0 .../logwatchers/kmsg/log_watcher_windows.go | 31 +++++++++++ pkg/util/helpers.go | 9 --- pkg/util/helpers_linux.go | 31 +++++++++++ pkg/util/helpers_windows.go | 31 +++++++++++ 10 files changed, 225 insertions(+), 35 deletions(-) create mode 100644 pkg/systemlogmonitor/logwatchers/filelog/log_watcher_linux.go create mode 100644 pkg/systemlogmonitor/logwatchers/filelog/log_watcher_windows.go rename pkg/systemlogmonitor/logwatchers/kmsg/{log_watcher.go => log_watcher_linux.go} (100%) rename pkg/systemlogmonitor/logwatchers/kmsg/{log_watcher_test.go => log_watcher_linux_test.go} (100%) create mode 100644 pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_windows.go create mode 100644 pkg/util/helpers_linux.go create mode 100644 pkg/util/helpers_windows.go diff --git a/Makefile b/Makefile index 785e777db..27afad827 100644 --- a/Makefile +++ b/Makefile @@ -86,6 +86,60 @@ fmt: version: @echo $(VERSION) +WINDOWS_AMD64_BINARIES = bin/windows_amd64/node-problem-detector.exe bin/windows_amd64/health-checker.exe +WINDOWS_AMD64_TEST_BINARIES = test/bin/windows_amd64/problem-maker.exe +LINUX_AMD64_BINARIES = bin/linux_amd64/node-problem-detector bin/linux_amd64/health-checker +LINUX_AMD64_TEST_BINARIES = test/bin/linux_amd64/problem-maker +ifeq ($(ENABLE_JOURNALD), 1) + LINUX_AMD64_BINARIES += bin/linux_amd64/log-counter +endif + +windows-binaries: $(WINDOWS_AMD64_BINARIES) $(WINDOWS_AMD64_TEST_BINARIES) + +bin/windows_amd64/%.exe: $(PKG_SOURCES) +ifeq ($(ENABLE_JOURNALD), 1) + echo "Journald on Windows is not supported, use make ENABLE_JOURNALD=0 [TARGET]" +endif + GOOS=windows GOARCH=amd64 CGO_ENABLED=$(CGO_ENABLED) GO111MODULE=on go build \ + -mod vendor \ + -o $@ \ + -ldflags '-X $(PKG)/pkg/version.version=$(VERSION)' \ + -tags "$(BUILD_TAGS)" \ + ./cmd/$(subst -,,$*)/$(subst -,_,$*).go + touch $@ + +./test/bin/windows_amd64/%.exe: $(PKG_SOURCES) +ifeq ($(ENABLE_JOURNALD), 1) + echo "Journald on Windows is not supported, use make ENABLE_JOURNALD=0 [TARGET]" +endif + GOOS=windows GOARCH=amd64 CGO_ENABLED=$(CGO_ENABLED) GO111MODULE=on go build \ + -mod vendor \ + -o $@ \ + -tags "$(BUILD_TAGS)" \ + ./test/e2e/$(subst -,,$*)/$(subst -,_,$*).go + +bin/linux_amd64/%: $(PKG_SOURCES) + GOOS=linux GOARCH=amd64 CGO_ENABLED=$(CGO_ENABLED) GO111MODULE=on go build \ + -mod vendor \ + -o $@ \ + -ldflags '-X $(PKG)/pkg/version.version=$(VERSION)' \ + -tags "$(BUILD_TAGS)" \ + ./cmd/$(subst -,,$*)/$(subst -,_,$*).go + touch $@ + +./test/bin/linux_amd64/%: $(PKG_SOURCES) + GOOS=linux GOARCH=amd64 CGO_ENABLED=$(CGO_ENABLED) GO111MODULE=on go build \ + -mod vendor \ + -o $@ \ + -tags "$(BUILD_TAGS)" \ + ./test/e2e/$(subst -,,$*)/$(subst -,_,$*).go + +ifneq ($(ENABLE_JOURNALD), 1) +bin/linux_amd64/log-counter: + echo "Warning: log-counter requires journald, skipping." +endif + +# In the future these targets should be deprecated. ./bin/log-counter: $(PKG_SOURCES) ifeq ($(ENABLE_JOURNALD), 1) CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GO111MODULE=on go build \ @@ -175,5 +229,6 @@ clean: rm -f bin/health-checker rm -f bin/log-counter rm -f bin/node-problem-detector + rm -f $(WINDOWS_AMD64_BINARIES) $(LINUX_AMD64_BINARIES) rm -f test/bin/problem-maker rm -f node-problem-detector-*.tar.gz diff --git a/pkg/systemlogmonitor/logwatchers/filelog/log_watcher.go b/pkg/systemlogmonitor/logwatchers/filelog/log_watcher.go index ef6829e8d..3150b3d7a 100644 --- a/pkg/systemlogmonitor/logwatchers/filelog/log_watcher.go +++ b/pkg/systemlogmonitor/logwatchers/filelog/log_watcher.go @@ -19,15 +19,12 @@ package filelog import ( "bufio" "bytes" - "fmt" "io" - "os" "strings" "time" utilclock "code.cloudfoundry.org/clock" "github.com/golang/glog" - "github.com/google/cadvisor/utils/tail" "k8s.io/node-problem-detector/pkg/systemlogmonitor/logwatchers/types" logtypes "k8s.io/node-problem-detector/pkg/systemlogmonitor/types" @@ -135,26 +132,3 @@ func (s *filelogWatcher) watchLoop() { s.logCh <- log } } - -// getLogReader returns log reader for filelog log. Note that getLogReader doesn't look back -// to the rolled out logs. -func getLogReader(path string) (io.ReadCloser, error) { - if path == "" { - return nil, fmt.Errorf("unexpected empty log path") - } - // To handle log rotation, tail will not report error immediately if - // the file doesn't exist. So we check file existence first. - // This could go wrong during mid-rotation. It should recover after - // several restart when the log file is created again. The chance - // is slim but we should still fix this in the future. - // TODO(random-liu): Handle log missing during rotation. - _, err := os.Stat(path) - if err != nil { - return nil, fmt.Errorf("failed to stat the file %q: %v", path, err) - } - tail, err := tail.NewTail(path) - if err != nil { - return nil, fmt.Errorf("failed to tail the file %q: %v", path, err) - } - return tail, nil -} diff --git a/pkg/systemlogmonitor/logwatchers/filelog/log_watcher_linux.go b/pkg/systemlogmonitor/logwatchers/filelog/log_watcher_linux.go new file mode 100644 index 000000000..4b09772f3 --- /dev/null +++ b/pkg/systemlogmonitor/logwatchers/filelog/log_watcher_linux.go @@ -0,0 +1,48 @@ +/* +Copyright 2016 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 filelog + +import ( + "fmt" + "io" + "os" + + "github.com/google/cadvisor/utils/tail" +) + +// getLogReader returns log reader for filelog log. Note that getLogReader doesn't look back +// to the rolled out logs. +func getLogReader(path string) (io.ReadCloser, error) { + if path == "" { + return nil, fmt.Errorf("unexpected empty log path") + } + // To handle log rotation, tail will not report error immediately if + // the file doesn't exist. So we check file existence first. + // This could go wrong during mid-rotation. It should recover after + // several restart when the log file is created again. The chance + // is slim but we should still fix this in the future. + // TODO(random-liu): Handle log missing during rotation. + _, err := os.Stat(path) + if err != nil { + return nil, fmt.Errorf("failed to stat the file %q: %v", path, err) + } + tail, err := tail.NewTail(path) + if err != nil { + return nil, fmt.Errorf("failed to tail the file %q: %v", path, err) + } + return tail, nil +} diff --git a/pkg/systemlogmonitor/logwatchers/filelog/log_watcher_windows.go b/pkg/systemlogmonitor/logwatchers/filelog/log_watcher_windows.go new file mode 100644 index 000000000..c52c6c06e --- /dev/null +++ b/pkg/systemlogmonitor/logwatchers/filelog/log_watcher_windows.go @@ -0,0 +1,29 @@ +/* +Copyright 2016 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 filelog + +import ( + "fmt" + "io" +) + +// getLogReader returns log reader for filelog log. Note that getLogReader doesn't look back +// to the rolled out logs. +func getLogReader(path string) (io.ReadCloser, error) { + // TODO: Support this on windows. + return nil, fmt.Errorf("not supported on windows.") +} diff --git a/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher.go b/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_linux.go similarity index 100% rename from pkg/systemlogmonitor/logwatchers/kmsg/log_watcher.go rename to pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_linux.go diff --git a/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_test.go b/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_linux_test.go similarity index 100% rename from pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_test.go rename to pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_linux_test.go diff --git a/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_windows.go b/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_windows.go new file mode 100644 index 000000000..17ee0ceaf --- /dev/null +++ b/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_windows.go @@ -0,0 +1,31 @@ +/* +Copyright 2017 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 kmsg + +import ( + "runtime" + + "github.com/golang/glog" + + "k8s.io/node-problem-detector/pkg/systemlogmonitor/logwatchers/types" +) + +// NewKmsgWatcher creates a watcher which will read messages from /dev/kmsg +func NewKmsgWatcher(cfg types.WatcherConfig) types.LogWatcher { + glog.Fatalf("kmsg parser is not supported in %s", runtime.GOOS) + return nil +} diff --git a/pkg/util/helpers.go b/pkg/util/helpers.go index 25f9d6b7b..dfadd4327 100644 --- a/pkg/util/helpers.go +++ b/pkg/util/helpers.go @@ -17,7 +17,6 @@ package util import ( "fmt" - "syscall" "time" "github.com/cobaugh/osrelease" @@ -37,14 +36,6 @@ func GenerateConditionChangeEvent(t string, status types.ConditionStatus, reason } } -func GetUptimeDuration() (time.Duration, error) { - var info syscall.Sysinfo_t - if err := syscall.Sysinfo(&info); err != nil { - return 0, fmt.Errorf("failed to get system info: %v", err) - } - return time.Duration(info.Uptime) * time.Second, nil -} - func GetStartTime(now time.Time, uptimeDuration time.Duration, lookbackStr string, delayStr string) (time.Time, error) { startTime := now.Add(-uptimeDuration) diff --git a/pkg/util/helpers_linux.go b/pkg/util/helpers_linux.go new file mode 100644 index 000000000..2c7a93eed --- /dev/null +++ b/pkg/util/helpers_linux.go @@ -0,0 +1,31 @@ +/* +Copyright 2017 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 util + +import ( + "fmt" + "syscall" + "time" +) + +// GetUptimeDuration returns the time elapsed since last boot. +func GetUptimeDuration() (time.Duration, error) { + var info syscall.Sysinfo_t + if err := syscall.Sysinfo(&info); err != nil { + return 0, fmt.Errorf("failed to get system info: %v", err) + } + return time.Duration(info.Uptime) * time.Second, nil +} diff --git a/pkg/util/helpers_windows.go b/pkg/util/helpers_windows.go new file mode 100644 index 000000000..294f10b94 --- /dev/null +++ b/pkg/util/helpers_windows.go @@ -0,0 +1,31 @@ +/* +Copyright 2017 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 util + +import ( + "time" + + "github.com/shirou/gopsutil/host" +) + +// GetUptimeDuration returns the time elapsed since last boot. +func GetUptimeDuration() (time.Duration, error) { + ut, err := host.Uptime() + if err != nil { + return 0, err + } + return time.Duration(ut), nil +}