Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common"
"github.com/openshift/machine-config-operator/pkg/daemon/constants"
"github.com/openshift/machine-config-operator/pkg/daemon/osrelease"
mcfginformersv1 "github.com/openshift/machine-config-operator/pkg/generated/informers/externalversions/machineconfiguration.openshift.io/v1"
mcfglistersv1 "github.com/openshift/machine-config-operator/pkg/generated/listers/machineconfiguration.openshift.io/v1"
)
Expand All @@ -49,7 +50,7 @@ type Daemon struct {
name string

// os the operating system the MCD is running on
os OperatingSystem
os osrelease.OperatingSystem

// mock is set if we're running as non-root, probably under unit tests
mock bool
Expand Down Expand Up @@ -219,9 +220,9 @@ func New(
err error
)

hostos := OperatingSystem{}
hostos := osrelease.OperatingSystem{}
if !mock {
hostos, err = GetHostRunningOS()
hostos, err = osrelease.GetHostRunningOS()
if err != nil {
hostOS.WithLabelValues("unsupported", "").Set(1)
return nil, fmt.Errorf("checking operating system: %w", err)
Expand Down
3 changes: 2 additions & 1 deletion pkg/daemon/kernelargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
_ "crypto/sha256"

"github.com/golang/glog"
"github.com/openshift/machine-config-operator/pkg/daemon/osrelease"
"github.com/openshift/machine-config-operator/pkg/daemon/pivot/types"
)

Expand All @@ -29,7 +30,7 @@ var tuneableRHCOSArgsAllowlist = map[string]bool{

// isArgTuneable returns if the argument provided is allowed to be modified
func isArgTunable(arg string) (bool, error) {
os, err := GetHostRunningOS()
os, err := osrelease.GetHostRunningOS()
if err != nil {
return false, fmt.Errorf("failed to get OS for determining whether kernel arg is tuneable: %w", err)
}
Expand Down
81 changes: 0 additions & 81 deletions pkg/daemon/osrelease.go

This file was deleted.

151 changes: 151 additions & 0 deletions pkg/daemon/osrelease/osrelease.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package osrelease

import (
"os"
"path/filepath"
"strings"

"github.com/ashcrow/osrelease"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine as is, but perhaps it'd make sense at some point to have this functionality move into that upstream library. We could also move it to e.g. github.com/coreos/osrelease too for more shared maintenance.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that would be nice. I have a few ideas around some additional things we could port there such as handling paths, etc.

)

// OS Release Paths
const (
EtcOSReleasePath string = "/etc/os-release"
LibOSReleasePath string = "/usr/lib/os-release"
)

// OS IDs
const (
coreos string = "coreos"
fedora string = "fedora"
rhcos string = "rhcos"
scos string = "scos"
)

// OperatingSystem is a wrapper around a subset of the os-release fields
// and also tracks whether ostree is in use.
type OperatingSystem struct {
// id is the ID field from the os-release
id string
// variantID is the VARIANT_ID field from the os-release
variantID string
// version is the VERSION, RHEL_VERSION, or VERSION_ID field from the os-release
version string
// osrelease is the underlying struct from github.com/ashcrow/osrelease
osrelease osrelease.OSRelease
}

func newOperatingSystem(etcPath, libPath string) (OperatingSystem, error) {
ret := OperatingSystem{}

or, err := osrelease.NewWithOverrides(etcPath, libPath)
if err != nil {
return ret, err
}

ret.id = or.ID
ret.variantID = or.VARIANT_ID
ret.version = getOSVersion(or)
ret.osrelease = or

return ret, nil
}

// Returns the underlying OSRelease struct if additional parameters are needed.
func (os OperatingSystem) OSRelease() osrelease.OSRelease {
return os.osrelease
}

// IsEL is true if the OS is an Enterprise Linux variant,
// i.e. RHEL CoreOS (RHCOS) or CentOS Stream CoreOS (SCOS)
func (os OperatingSystem) IsEL() bool {
return os.id == rhcos || os.id == scos
}

// IsEL9 is true if the OS is RHCOS 9 or SCOS 9
func (os OperatingSystem) IsEL9() bool {
return os.IsEL() && strings.HasPrefix(os.version, "9.") || os.version == "9"
}

// IsFCOS is true if the OS is Fedora CoreOS
func (os OperatingSystem) IsFCOS() bool {
return os.id == fedora && os.variantID == coreos
}

// IsSCOS is true if the OS is SCOS
func (os OperatingSystem) IsSCOS() bool {
return os.id == scos
}

// IsCoreOSVariant is true if the OS is FCOS or a derivative (ostree+Ignition)
// which includes SCOS and RHCOS.
func (os OperatingSystem) IsCoreOSVariant() bool {
// In RHCOS8 the variant id is not specified. SCOS (future RHCOS9) and FCOS have VARIANT_ID=coreos.
return os.variantID == coreos || os.IsEL()
}

// IsLikeTraditionalRHEL7 is true if the OS is traditional RHEL7 or CentOS7:
// yum based + kickstart/cloud-init (not Ignition).
func (os OperatingSystem) IsLikeTraditionalRHEL7() bool {
// Today nothing else is going to show up with a version ID of 7
if len(os.version) > 2 {
return strings.HasPrefix(os.version, "7.")
}
return os.version == "7"
}

// ToPrometheusLabel returns a value we historically fed to Prometheus
func (os OperatingSystem) ToPrometheusLabel() string {
// We historically upper cased this
return strings.ToUpper(os.id)
}

// GetHostRunningOS reads os-release to generate the OperatingSystem data.
func GetHostRunningOS() (OperatingSystem, error) {
return newOperatingSystem(EtcOSReleasePath, LibOSReleasePath)
}

// Generates the OperatingSystem data from strings which contain the desired
// content. Mostly useful for testing purposes.
func LoadOSRelease(etcOSReleaseContent, libOSReleaseContent string) (OperatingSystem, error) {
tempDir, err := os.MkdirTemp("", "")
if err != nil {
return OperatingSystem{}, err
}

defer os.RemoveAll(tempDir)

etcOSReleasePath := filepath.Join(tempDir, "etc-os-release")
libOSReleasePath := filepath.Join(tempDir, "lib-os-release")

if err := os.WriteFile(etcOSReleasePath, []byte(etcOSReleaseContent), 0o644); err != nil {
return OperatingSystem{}, err
}

if err := os.WriteFile(libOSReleasePath, []byte(libOSReleaseContent), 0o644); err != nil {
return OperatingSystem{}, err
}

return newOperatingSystem(etcOSReleasePath, libOSReleasePath)
}

// Determines the OS version based upon the contents of the RHEL_VERSION, VERSION or VERSION_ID fields.
func getOSVersion(or osrelease.OSRelease) string {
// If we have the RHEL_VERSION field, we should use that value instead.
if rhelVersion, ok := or.ADDITIONAL_FIELDS["RHEL_VERSION"]; ok {
return rhelVersion
}

// If we have the OPENSHIFT_VERSION field, we can compute the OS version.
if openshiftVersion, ok := or.ADDITIONAL_FIELDS["OPENSHIFT_VERSION"]; ok {
// Move the "." from the middle of the OpenShift version to the end; e.g., 4.12 becomes 412.
openshiftVersion := strings.ReplaceAll(openshiftVersion, ".", "") + "."
if strings.HasPrefix(or.VERSION, openshiftVersion) {
// Strip the OpenShift Version prefix from the VERSION field, if it is found.
return strings.ReplaceAll(or.VERSION, openshiftVersion, "")
}
}

// Fallback to the VERSION_ID field
return or.VERSION_ID
}
Loading