Skip to content

Commit

Permalink
Save the distro information the first time we read /etc/os-release, s…
Browse files Browse the repository at this point in the history
…o that we end up only reading that file once

Signed-off-by: Natalie Arellano <narellano@vmware.com>
  • Loading branch information
natalieparellano committed Nov 14, 2024
1 parent 939839e commit 512bd2c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
8 changes: 8 additions & 0 deletions internal/fsutil/os_detection.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ type Detector interface {
HasSystemdFile() bool
ReadSystemdFile() (string, error)
GetInfo(osReleaseContents string) OSInfo
StoredInfo() *OSInfo
InfoOnce(logger log.Logger)
}

// DefaultDetector implements Detector
type DefaultDetector struct {
once sync.Once
info *OSInfo
}

// HasSystemdFile returns true if /etc/os-release exists with contents
Expand Down Expand Up @@ -60,9 +62,15 @@ func (d *DefaultDetector) GetInfo(osReleaseContents string) OSInfo {
break
}
}
d.info = &ret // store for future use
return ret
}

// StoredInfo returns any OSInfo found during the last call to GetInfo
func (d *DefaultDetector) StoredInfo() *OSInfo {
return d.info
}

// InfoOnce logs an info message to the provided logger, but only once in the lifetime of the receiving DefaultDetector.
func (d *DefaultDetector) InfoOnce(logger log.Logger) {
d.once.Do(func() {
Expand Down
20 changes: 14 additions & 6 deletions platform/target_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,22 @@ func matches(target1, target2 string) bool {
// GetTargetOSFromFileSystem populates the provided target metadata with information from /etc/os-release
// if it is available.
func GetTargetOSFromFileSystem(d fsutil.Detector, tm *files.TargetMetadata, logger log.Logger) {
if tm.OS == "" {
tm.OS = "linux" // we shouldn't get here, as OS comes from the image config, and OS is always required
}
if tm.Arch == "" {
tm.Arch = runtime.GOARCH // in a future world where we support cross-platform builds, this should be removed
}

if info := d.StoredInfo(); info != nil {
if info.Version != "" || info.Name != "" {
tm.Distro = &files.OSDistro{Name: info.Name, Version: info.Version}
}
return
}

d.InfoOnce(logger)
if d.HasSystemdFile() {
if tm.OS == "" {
tm.OS = "linux"
}
if tm.Arch == "" {
tm.Arch = runtime.GOARCH // in a future world where we support cross platform builds, this should be removed
}
contents, err := d.ReadSystemdFile()
if err != nil {
logger.Warnf("Encountered error trying to read /etc/os-release file: %s", err.Error())
Expand Down
4 changes: 4 additions & 0 deletions platform/target_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,7 @@ func (d *mockDetector) GetInfo(osReleaseContents string) fsutil.OSInfo {
}

func (d *mockDetector) InfoOnce(_ llog.Logger) {}

func (d *mockDetector) StoredInfo() *fsutil.OSInfo {
return nil
}

0 comments on commit 512bd2c

Please sign in to comment.