From 248859c64d583b4834c74581cb9f4c457372f0c5 Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Wed, 20 May 2020 11:31:09 +0300 Subject: [PATCH] source: parametrise host directory paths Specify and handle system paths we use for discovery in a unified way. --- Dockerfile | 2 +- source/config.go | 39 +++++++++++++++++++++++++++++++++++++++ source/kernel/kernel.go | 2 +- source/kernel/selinux.go | 4 +++- source/system/system.go | 2 +- 5 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 source/config.go diff --git a/Dockerfile b/Dockerfile index ac5107ba5..e7cd1e639 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,7 +14,7 @@ COPY . /go/node-feature-discovery ARG NFD_VERSION RUN go install \ - -ldflags "-s -w -X sigs.k8s.io/node-feature-discovery/pkg/version.version=$NFD_VERSION" \ + -ldflags "-s -w -X sigs.k8s.io/node-feature-discovery/pkg/version.version=$NFD_VERSION -X sigs.k8s.io/node-feature-discovery/source.pathPrefix=/host-" \ ./cmd/* RUN install -D -m644 nfd-worker.conf.example /etc/kubernetes/node-feature-discovery/nfd-worker.conf diff --git a/source/config.go b/source/config.go new file mode 100644 index 000000000..1047c4314 --- /dev/null +++ b/source/config.go @@ -0,0 +1,39 @@ +/* +Copyright 2020 The Kubernetes Authors. + +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 source + +import ( + "path/filepath" +) + +var ( + pathPrefix = "/" + // BootPath is where the /boot directory of the system to be inspected is located + BootDir = HostDir(pathPrefix + "boot") + // EtcPath is where the /etc directory of the system to be inspected is located + EtcDir = HostDir(pathPrefix + "etc") + // SysfsPath is where the /sys directory of the system to be inspected is located + SysfsDir = HostDir(pathPrefix + "sys") +) + +// HostDir is a helper for handling host system directories +type HostDir string + +// Path returns a full path to a file under HostDir +func (d HostDir) Path(elem ...string) string { + return filepath.Join(append([]string{string(d)}, elem...)...) +} diff --git a/source/kernel/kernel.go b/source/kernel/kernel.go index e2b9a34e0..37c301c77 100644 --- a/source/kernel/kernel.go +++ b/source/kernel/kernel.go @@ -167,7 +167,7 @@ func parseKconfig() (map[string]string, error) { return nil, err } // Read kconfig - raw, err = ioutil.ReadFile("/host-boot/config-" + uname) + raw, err = ioutil.ReadFile(source.BootDir.Path("config-" + uname)) if err != nil { return nil, err } diff --git a/source/kernel/selinux.go b/source/kernel/selinux.go index bf34ebe7d..9a67d3b45 100644 --- a/source/kernel/selinux.go +++ b/source/kernel/selinux.go @@ -19,11 +19,13 @@ package kernel import ( "fmt" "io/ioutil" + + "sigs.k8s.io/node-feature-discovery/source" ) // Detect if selinux has been enabled in the kernel func SelinuxEnabled() (bool, error) { - status, err := ioutil.ReadFile("/host-sys/fs/selinux/enforce") + status, err := ioutil.ReadFile(source.SysfsDir.Path("fs/selinux/enforce")) if err != nil { return false, fmt.Errorf("Failed to detect the status of selinux, please check if the system supports selinux and make sure /sys on the host is mounted into the container: %s", err.Error()) } diff --git a/source/system/system.go b/source/system/system.go index 750a5d824..dab3ce89e 100644 --- a/source/system/system.go +++ b/source/system/system.go @@ -66,7 +66,7 @@ func (s Source) Discover() (source.Features, error) { func parseOSRelease() (map[string]string, error) { release := map[string]string{} - f, err := os.Open("/host-etc/os-release") + f, err := os.Open(source.EtcDir.Path("os-release")) if err != nil { return nil, err }