-
Notifications
You must be signed in to change notification settings - Fork 494
OCPBUGS-6945: Fixes node OS detection #3529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
openshift-merge-robot
merged 2 commits into
openshift:master
from
cheesesashimi:zzlotnik/OCPBUGS-6945
Feb 3, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| ) | ||
|
|
||
| // 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 | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.