From f58f6cd208b2d468298338ecdb8a23a8326a61b7 Mon Sep 17 00:00:00 2001 From: Ciprian Hacman Date: Fri, 8 Sep 2023 15:39:24 +0300 Subject: [PATCH] Update tests to run also on macOS --- pkg/healthchecker/health_checker_darwin.go | 50 +++++++++++++++++++ .../types/{types_linux.go => types_unix.go} | 2 + .../logwatchers/filelog/log_watcher_darwin.go | 29 +++++++++++ .../logwatchers/kmsg/log_watcher_darwin.go | 31 ++++++++++++ ...llector_linux.go => cpu_collector_unix.go} | 2 + ...ctor_linux.go => memory_collector_unix.go} | 2 + pkg/systemstatsmonitor/types/config_darwin.go | 24 +++++++++ pkg/util/{exec_linux.go => exec_unix.go} | 2 + pkg/util/helpers_darwin.go | 43 ++++++++++++++++ .../cadvisor/tail/{tail.go => tail_linux.go} | 0 10 files changed, 185 insertions(+) create mode 100644 pkg/healthchecker/health_checker_darwin.go rename pkg/healthchecker/types/{types_linux.go => types_unix.go} (97%) create mode 100644 pkg/systemlogmonitor/logwatchers/filelog/log_watcher_darwin.go create mode 100644 pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_darwin.go rename pkg/systemstatsmonitor/{cpu_collector_linux.go => cpu_collector_unix.go} (99%) rename pkg/systemstatsmonitor/{memory_collector_linux.go => memory_collector_unix.go} (99%) create mode 100644 pkg/systemstatsmonitor/types/config_darwin.go rename pkg/util/{exec_linux.go => exec_unix.go} (98%) create mode 100644 pkg/util/helpers_darwin.go rename third_party/forked/cadvisor/tail/{tail.go => tail_linux.go} (100%) diff --git a/pkg/healthchecker/health_checker_darwin.go b/pkg/healthchecker/health_checker_darwin.go new file mode 100644 index 000000000..a20f72500 --- /dev/null +++ b/pkg/healthchecker/health_checker_darwin.go @@ -0,0 +1,50 @@ +/* +Copyright 2023 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 healthchecker + +import ( + "runtime" + "time" + + "github.com/golang/glog" + + "k8s.io/node-problem-detector/cmd/healthchecker/options" +) + +// getUptimeFunc returns the time for which the given service has been running. +func getUptimeFunc(service string) func() (time.Duration, error) { + glog.Fatalf("getUptimeFunc is not supported in %s", runtime.GOOS) + return func() (time.Duration, error) { return time.Second, nil } +} + +// getRepairFunc returns the repair function based on the component. +func getRepairFunc(hco *options.HealthCheckerOptions) func() { + glog.Fatalf("getRepairFunc is not supported in %s", runtime.GOOS) + return func() {} +} + +// checkForPattern returns (true, nil) if logPattern occurs less than logCountThreshold number of times since last +// service restart. (false, nil) otherwise. +func checkForPattern(service, logStartTime, logPattern string, logCountThreshold int) (bool, error) { + glog.Fatalf("checkForPattern is not supported in %s", runtime.GOOS) + return false, nil +} + +func getDockerPath() string { + glog.Fatalf("getDockerPath is not supported in %s", runtime.GOOS) + return "" +} diff --git a/pkg/healthchecker/types/types_linux.go b/pkg/healthchecker/types/types_unix.go similarity index 97% rename from pkg/healthchecker/types/types_linux.go rename to pkg/healthchecker/types/types_unix.go index de0abaec4..90f82d7b6 100644 --- a/pkg/healthchecker/types/types_linux.go +++ b/pkg/healthchecker/types/types_unix.go @@ -1,3 +1,5 @@ +//go:build unix + /* Copyright 2021 The Kubernetes Authors All rights reserved. diff --git a/pkg/systemlogmonitor/logwatchers/filelog/log_watcher_darwin.go b/pkg/systemlogmonitor/logwatchers/filelog/log_watcher_darwin.go new file mode 100644 index 000000000..eb3cac60d --- /dev/null +++ b/pkg/systemlogmonitor/logwatchers/filelog/log_watcher_darwin.go @@ -0,0 +1,29 @@ +/* +Copyright 2023 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 ( + "io" + + "github.com/hpcloud/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) { + return tail.OpenFile(path) +} diff --git a/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_darwin.go b/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_darwin.go new file mode 100644 index 000000000..841f3dc31 --- /dev/null +++ b/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_darwin.go @@ -0,0 +1,31 @@ +/* +Copyright 2023 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/systemstatsmonitor/cpu_collector_linux.go b/pkg/systemstatsmonitor/cpu_collector_unix.go similarity index 99% rename from pkg/systemstatsmonitor/cpu_collector_linux.go rename to pkg/systemstatsmonitor/cpu_collector_unix.go index f761fcf41..6896b8655 100644 --- a/pkg/systemstatsmonitor/cpu_collector_linux.go +++ b/pkg/systemstatsmonitor/cpu_collector_unix.go @@ -1,3 +1,5 @@ +//go:build unix + /* Copyright 2020 The Kubernetes Authors All rights reserved. diff --git a/pkg/systemstatsmonitor/memory_collector_linux.go b/pkg/systemstatsmonitor/memory_collector_unix.go similarity index 99% rename from pkg/systemstatsmonitor/memory_collector_linux.go rename to pkg/systemstatsmonitor/memory_collector_unix.go index 83ea955df..58140808b 100644 --- a/pkg/systemstatsmonitor/memory_collector_linux.go +++ b/pkg/systemstatsmonitor/memory_collector_unix.go @@ -1,3 +1,5 @@ +//go:build unix + /* Copyright 2020 The Kubernetes Authors All rights reserved. diff --git a/pkg/systemstatsmonitor/types/config_darwin.go b/pkg/systemstatsmonitor/types/config_darwin.go new file mode 100644 index 000000000..cf6f45a57 --- /dev/null +++ b/pkg/systemstatsmonitor/types/config_darwin.go @@ -0,0 +1,24 @@ +/* +Copyright 2023 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 types + +const defaultProcPath = "" + +func (ssc *SystemStatsConfig) validateProcPath() error { + // not supported + return nil +} diff --git a/pkg/util/exec_linux.go b/pkg/util/exec_unix.go similarity index 98% rename from pkg/util/exec_linux.go rename to pkg/util/exec_unix.go index 29a9afef5..d7311ba3f 100644 --- a/pkg/util/exec_linux.go +++ b/pkg/util/exec_unix.go @@ -1,3 +1,5 @@ +//go:build unix + /* Copyright 2021 The Kubernetes Authors All rights reserved. diff --git a/pkg/util/helpers_darwin.go b/pkg/util/helpers_darwin.go new file mode 100644 index 000000000..98a8d9b5a --- /dev/null +++ b/pkg/util/helpers_darwin.go @@ -0,0 +1,43 @@ +/* +Copyright 2023 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/v3/host" +) + +// GetUptimeDuration returns the time elapsed since last boot. +// For example: "cos 77-12293.0.0", "ubuntu 16.04.6 LTS (Xenial Xerus)". +func GetUptimeDuration() (time.Duration, error) { + ut, err := host.Uptime() + if err != nil { + return 0, err + } + return time.Duration(ut), nil +} + +// GetOSVersion retrieves the version of the current operating system. +// For example: "darwin 13.5"". +func GetOSVersion() (string, error) { + platform, _, version, err := host.PlatformInformation() + if err != nil { + return "", err + } + + return platform + " " + version, nil +} diff --git a/third_party/forked/cadvisor/tail/tail.go b/third_party/forked/cadvisor/tail/tail_linux.go similarity index 100% rename from third_party/forked/cadvisor/tail/tail.go rename to third_party/forked/cadvisor/tail/tail_linux.go