mirror of
https://github.com/kubernetes-sigs/node-feature-discovery.git
synced 2025-03-28 02:37:11 +00:00
source/kernel: add internal helper to get kernel version
Signed-off-by: Mikko Ylinen <mikko.ylinen@intel.com>
This commit is contained in:
parent
aeb7583315
commit
8b156d49fc
3 changed files with 32 additions and 8 deletions
|
@ -57,8 +57,7 @@ func ParseKconfig(configPath string) (map[string]string, error) {
|
|||
var err error
|
||||
var searchPaths []string
|
||||
|
||||
unameRaw, err := ioutil.ReadFile("/proc/sys/kernel/osrelease")
|
||||
kVer := strings.TrimSpace(string(unameRaw))
|
||||
kVer, err := GetKernelVersion()
|
||||
if err != nil {
|
||||
searchPaths = []string{
|
||||
"/proc/config.gz",
|
||||
|
|
30
source/internal/kernelutils/kernel_version.go
Normal file
30
source/internal/kernelutils/kernel_version.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
Copyright 2018-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 kernelutils
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetKernelVersion() (string, error) {
|
||||
unameRaw, err := ioutil.ReadFile("/proc/sys/kernel/osrelease")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return strings.TrimSpace(string(unameRaw)), nil
|
||||
}
|
|
@ -17,10 +17,8 @@ limitations under the License.
|
|||
package kernel
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/node-feature-discovery/source"
|
||||
"sigs.k8s.io/node-feature-discovery/source/internal/kernelutils"
|
||||
|
@ -108,14 +106,11 @@ func (s *Source) Discover() (source.Features, error) {
|
|||
func parseVersion() (map[string]string, error) {
|
||||
version := map[string]string{}
|
||||
|
||||
// Open file for reading
|
||||
raw, err := ioutil.ReadFile("/proc/sys/kernel/osrelease")
|
||||
full, err := kernelutils.GetKernelVersion()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
full := strings.TrimSpace(string(raw))
|
||||
|
||||
// Replace forbidden symbols
|
||||
fullRegex := regexp.MustCompile("[^-A-Za-z0-9_.]")
|
||||
full = fullRegex.ReplaceAllString(full, "_")
|
||||
|
|
Loading…
Add table
Reference in a new issue