1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2024-12-14 11:57:51 +00:00

source: parametrise host directory paths

Specify and handle system paths we use for discovery in a unified way.
This commit is contained in:
Markus Lehtonen 2020-05-20 11:31:09 +03:00
parent f65def9460
commit 248859c64d
5 changed files with 45 additions and 4 deletions

View file

@ -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

39
source/config.go Normal file
View file

@ -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...)...)
}

View file

@ -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
}

View file

@ -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())
}

View file

@ -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
}