1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-05 08:17:04 +00:00

Implement kernel version detection

Add a new 'kernel' feature source, detecting the kernel version. The
kernel version is split into multiple labels in order to make this more
usable in label selectors. Kernel version in the format X.Y.Z-patch will
be presented as
  node.alpha.kubernetes-incubator.io/nfd-kernel-version.full=X.Y.Z-patch
  node.alpha.kubernetes-incubator.io/nfd-kernel-version.major=X
  node.alpha.kubernetes-incubator.io/nfd-kernel-version.minor=Y
  node.alpha.kubernetes-incubator.io/nfd-kernel-version.revision=Z

The '.full' label will always be avaiable. The other labels if these
components can be parsed from the kernel version number.
This commit is contained in:
Markus Lehtonen 2018-07-09 16:27:24 +03:00
parent 56c2ab3d58
commit 86947fc16b
4 changed files with 89 additions and 4 deletions

View file

@ -54,7 +54,7 @@ node-feature-discovery.
will override settings read from the config file.
[Default: ]
--sources=<sources> Comma separated list of feature sources.
[Default: cpuid,iommu,memory,network,pci,pstate,rdt,selinux,storage]
[Default: cpuid,iommu,kernel,memory,network,pci,pstate,rdt,selinux,storage]
--no-publish Do not publish discovered features to the
cluster-local Kubernetes API server.
--label-whitelist=<pattern> Regular expression to filter label names to
@ -79,6 +79,7 @@ The current set of feature sources are the following:
- [CPUID][cpuid] for x86/Arm64 CPU details
- IOMMU
- Kernel
- Memory
- Network
- Pstate ([Intel P-State driver][intel-pstate])
@ -114,6 +115,7 @@ the only label value published for features is the string `"true"`._
"node.alpha.kubernetes-incubator.io/node-feature-discovery.version": "v0.3.0",
"node.alpha.kubernetes-incubator.io/nfd-cpuid-<feature-name>": "true",
"node.alpha.kubernetes-incubator.io/nfd-iommu-<feature-name>": "true",
"node.alpha.kubernetes-incubator.io/nfd-kernel-version.<version component>": "<version number>",
"node.alpha.kubernetes-incubator.io/nfd-memory-<feature-name>": "true",
"node.alpha.kubernetes-incubator.io/nfd-network-<feature-name>": "true",
"node.alpha.kubernetes-incubator.io/nfd-pci-<device label>.present": "true",
@ -165,6 +167,15 @@ such as restricting discovered features with the --label-whitelist option._
| :------------: | :---------------------------------------------------------------------------------: |
| enabled | IOMMU is present and enabled in the kernel
### Kernel Features
| Feature | Attribute | Description |
| ------- | --------- | ------------------------------------------------------ |
| version | full | Full kernel version as reported by `/proc/sys/kernel/osrelease` (e.g. '4.5.6-7-g123abcde')
| <br> | major | First component of the kernel version (e.g. '4')
| <br> | minor | Second component of the kernel version (e.g. '5')
| <br> | revision | Third component of the kernel version (e.g. '6')
### Memory Features
| Feature name | Description |

View file

@ -15,6 +15,7 @@ import (
"github.com/kubernetes-incubator/node-feature-discovery/source/cpuid"
"github.com/kubernetes-incubator/node-feature-discovery/source/fake"
"github.com/kubernetes-incubator/node-feature-discovery/source/iommu"
"github.com/kubernetes-incubator/node-feature-discovery/source/kernel"
"github.com/kubernetes-incubator/node-feature-discovery/source/memory"
"github.com/kubernetes-incubator/node-feature-discovery/source/network"
"github.com/kubernetes-incubator/node-feature-discovery/source/panic_fake"
@ -165,7 +166,7 @@ func argsParse(argv []string) (args Args) {
will override settings read from the config file.
[Default: ]
--sources=<sources> Comma separated list of feature sources.
[Default: cpuid,iommu,memory,network,pci,pstate,rdt,selinux,storage]
[Default: cpuid,iommu,kernel,memory,network,pci,pstate,rdt,selinux,storage]
--no-publish Do not publish discovered features to the
cluster-local Kubernetes API server.
--label-whitelist=<pattern> Regular expression to filter label names to
@ -243,6 +244,7 @@ func configureParameters(sourcesWhiteList []string, labelWhiteListStr string) (e
cpuid.Source{},
fake.Source{},
iommu.Source{},
kernel.Source{},
memory.Source{},
network.Source{},
panic_fake.Source{},

View file

@ -139,7 +139,7 @@ func TestArgsParse(t *testing.T) {
So(args.sleepInterval, ShouldEqual, 60*time.Second)
So(args.noPublish, ShouldBeTrue)
So(args.oneshot, ShouldBeTrue)
So(args.sources, ShouldResemble, []string{"cpuid", "iommu", "memory", "network", "pci", "pstate", "rdt", "selinux", "storage"})
So(args.sources, ShouldResemble, []string{"cpuid", "iommu", "kernel", "memory", "network", "pci", "pstate", "rdt", "selinux", "storage"})
So(len(args.labelWhiteList), ShouldEqual, 0)
})
})
@ -161,7 +161,7 @@ func TestArgsParse(t *testing.T) {
Convey("args.labelWhiteList is set to appropriate value and args.sources is set to default value", func() {
So(args.noPublish, ShouldBeFalse)
So(args.sources, ShouldResemble, []string{"cpuid", "iommu", "memory", "network", "pci", "pstate", "rdt", "selinux", "storage"})
So(args.sources, ShouldResemble, []string{"cpuid", "iommu", "kernel", "memory", "network", "pci", "pstate", "rdt", "selinux", "storage"})
So(args.labelWhiteList, ShouldResemble, ".*rdt.*")
})
})

72
source/kernel/kernel.go Normal file
View file

@ -0,0 +1,72 @@
/*
Copyright 2018 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 kernel
import (
"io/ioutil"
"regexp"
"strings"
"github.com/golang/glog"
"github.com/kubernetes-incubator/node-feature-discovery/source"
)
// Implement FeatureSource interface
type Source struct{}
func (s Source) Name() string { return "kernel" }
func (s Source) Discover() (source.Features, error) {
features := source.Features{}
// Read kernel version
version, err := parseVersion()
if err != nil {
glog.Errorf("Failed to get kernel version: %v", err)
} else {
for key := range version {
features["version."+key] = version[key]
}
}
return features, nil
}
// Read and parse kernel version
func parseVersion() (map[string]string, error) {
version := map[string]string{}
// Open file for reading
raw, err := ioutil.ReadFile("/proc/sys/kernel/osrelease")
if err != nil {
return nil, err
}
full := strings.TrimSpace(string(raw))
version["full"] = full
// Regexp for parsing version components
re := regexp.MustCompile(`^(?P<major>\d+)(\.(?P<minor>\d+))?(\.(?P<revision>\d+))?(-.*)?$`)
if m := re.FindStringSubmatch(full); m != nil {
for i, name := range re.SubexpNames() {
if i != 0 && name != "" {
version[name] = m[i]
}
}
}
return version, nil
}