Skip to content

Commit

Permalink
fix host release name nil pointer (#2250)
Browse files Browse the repository at this point in the history
* fix host release name nil pointer

* lint

* added host node to e2e test

---------

Co-authored-by: Roman Dodin <dodin.roman@gmail.com>
  • Loading branch information
steiler and hellt authored Oct 21, 2024
1 parent 24bbcf3 commit 5beff79
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
29 changes: 3 additions & 26 deletions nodes/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ package host
import (
"bytes"
"context"
"os"
"path/filepath"
"regexp"

osexec "os/exec"

Expand All @@ -19,6 +16,7 @@ import (
"github.com/srl-labs/containerlab/nodes/state"
"github.com/srl-labs/containerlab/runtime"
"github.com/srl-labs/containerlab/types"
"github.com/srl-labs/containerlab/utils"
)

var kindnames = []string{"host"}
Expand Down Expand Up @@ -59,30 +57,9 @@ func (*host) WithMgmtNet(*types.MgmtNet) {}
// UpdateConfigWithRuntimeInfo is a noop for hosts.
func (*host) UpdateConfigWithRuntimeInfo(_ context.Context) error { return nil }

// getOSRelease returns the OS release of the host by inspecting /etc/*-release.
func getOSRelease() string {
image := "N/A"

matches, err := filepath.Glob("/etc/*-release")
if err != nil {
return image
}
dat, err := os.ReadFile(matches[0])
if err != nil {
return image
}
// DISTRIB_DESCRIPTION exists in lsb-release, but not os-release.
// the lsb-release is coming first in the glob, so it works.
re := regexp.MustCompile(`DISTRIB_DESCRIPTION="(.*)"`)

regexres := re.FindSubmatch(dat)

return string(regexres[1])
}

// GetContainers returns a basic skeleton of a container to enable graphing of hosts kinds.
func (*host) GetContainers(_ context.Context) ([]runtime.GenericContainer, error) {
image := getOSRelease()
func (h *host) GetContainers(_ context.Context) ([]runtime.GenericContainer, error) {
image := utils.GetOSRelease()

return []runtime.GenericContainer{
{
Expand Down
2 changes: 2 additions & 0 deletions tests/01-smoke/03-linux-nodes-to-bridge-and-host.clab.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ topology:
cmd: ash -c "sleep 9999"
br-01-03-clab:
kind: bridge
hostnode: # is not used, it is here for coverage
kind: host

links:
- endpoints: ["l1:eth1", "br-01-03-clab:l1-eth1"]
Expand Down
35 changes: 35 additions & 0 deletions utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"os/exec"
"os/user"
"path/filepath"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -471,3 +472,37 @@ func recursiveChown(path string, uid, gid int) error {
return err
})
}

var osRelease string

// GetOSRelease returns the OS release of the host by inspecting /etc/*-release files.
func GetOSRelease() string {
// return cached result
if osRelease != "" {
return osRelease
}
osRelease = "N/A"

matches, err := filepath.Glob("/etc/*-release")
if err != nil {
return osRelease
}

re := regexp.MustCompile(`(DISTRIB_DESCRIPTION|PRETTY_NAME)="(.*)"`)

for _, match := range matches {
data, err := os.ReadFile(match)
if err != nil {
log.Error(err)
}

match := re.FindSubmatch(data)
// [0] = whole line match, [1] = left side of "=", [2] = right side of "="
if len(match) >= 3 {
osRelease = string(match[2])
break
}
}

return osRelease
}

0 comments on commit 5beff79

Please sign in to comment.