Skip to content

Commit

Permalink
Merge pull request #46 from tklauser/uname-syscall
Browse files Browse the repository at this point in the history
Use `unix.Uname` instead of exec'ing `uname`
  • Loading branch information
k8s-ci-robot authored Nov 26, 2024
2 parents 85af5a8 + 98a7eee commit aafceee
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.16
require (
github.com/blang/semver/v4 v4.0.0
github.com/stretchr/testify v1.3.0
golang.org/x/sys v0.27.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
9 changes: 6 additions & 3 deletions validators/os_validator_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ package system

import (
"fmt"
"os/exec"
"strings"

"golang.org/x/sys/unix"
)

var _ Validator = &OSValidator{}
Expand All @@ -39,11 +40,13 @@ func (o *OSValidator) Name() string {

// Validate is part of the system.Validator interface.
func (o *OSValidator) Validate(spec SysSpec) ([]error, []error) {
os, err := exec.Command("uname").CombinedOutput()
var utsname unix.Utsname
err := unix.Uname(&utsname)
if err != nil {
return nil, []error{fmt.Errorf("failed to get OS name: %w", err)}
}
if err = o.validateOS(strings.TrimSpace(string(os)), spec.OS); err != nil {
os := strings.TrimSpace(unix.ByteSliceToString(utsname.Sysname[:]))
if err = o.validateOS(os, spec.OS); err != nil {
return nil, []error{err}
}
return nil, nil
Expand Down
9 changes: 0 additions & 9 deletions validators/package_validator_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,6 @@ func (validator *packageValidator) validate(packageSpecs []PackageSpec, manager
return nil, errs
}

// getKernelRelease returns the kernel release of the local machine.
func getKernelRelease() (string, error) {
output, err := exec.Command("uname", "-r").Output()
if err != nil {
return "", fmt.Errorf("failed to get kernel release: %w", err)
}
return strings.TrimSpace(string(output)), nil
}

// getOSDistro returns the OS distro of the local machine.
func getOSDistro() (string, error) {
f := "/etc/lsb-release"
Expand Down
16 changes: 12 additions & 4 deletions validators/types_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ limitations under the License.
package system

import (
"os/exec"
"fmt"
"strings"

"golang.org/x/sys/unix"
)

// DefaultSysSpec is the default SysSpec for Linux
Expand Down Expand Up @@ -96,9 +98,15 @@ var _ KernelValidatorHelper = &KernelValidatorHelperImpl{}

// GetKernelReleaseVersion returns the kernel release version (ex. 4.4.0-96-generic) as a string
func (o *KernelValidatorHelperImpl) GetKernelReleaseVersion() (string, error) {
releaseVersion, err := exec.Command("uname", "-r").CombinedOutput()
return getKernelRelease()
}

// getKernelRelease returns the kernel release of the local machine.
func getKernelRelease() (string, error) {
var utsname unix.Utsname
err := unix.Uname(&utsname)
if err != nil {
return "", err
return "", fmt.Errorf("failed to get kernel release: %w", err)
}
return strings.TrimSpace(string(releaseVersion)), nil
return strings.TrimSpace(unix.ByteSliceToString(utsname.Release[:])), nil
}

0 comments on commit aafceee

Please sign in to comment.