diff --git a/source/internal/kernelutils/kernel_kconfig.go b/source/internal/kernelutils/kernel_kconfig.go index 1aa292e7b..9d8187d28 100644 --- a/source/internal/kernelutils/kernel_kconfig.go +++ b/source/internal/kernelutils/kernel_kconfig.go @@ -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", diff --git a/source/internal/kernelutils/kernel_version.go b/source/internal/kernelutils/kernel_version.go new file mode 100644 index 000000000..2da110e0d --- /dev/null +++ b/source/internal/kernelutils/kernel_version.go @@ -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 +} diff --git a/source/kernel/kernel.go b/source/kernel/kernel.go index 3325f331d..c1334256f 100644 --- a/source/kernel/kernel.go +++ b/source/kernel/kernel.go @@ -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, "_")