-
Notifications
You must be signed in to change notification settings - Fork 16
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
Enhance Cluster Environment Detection for OSTree-Based and Classic RHEL Systems #215
Open
SamD2021
wants to merge
4
commits into
openshift:main
Choose a base branch
from
SamD2021:detect-ostree-based
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+119
−21
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3fc816f
internal/utils/cluster_environment.go: Change the flavour focus away …
SamD2021 05d6acb
internal/utils/path_manager.go: Match each path to its respective fla…
SamD2021 ca9e8ef
99.daemonset.yaml: Mount /run so we can interact with ostree-booted
SamD2021 d66af18
daemon.go: Change the Daemon to retrieve a set of Flavours instead of…
SamD2021 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 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 |
---|---|---|
|
@@ -3,6 +3,7 @@ package utils | |
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
|
@@ -22,32 +23,82 @@ func NewClusterEnvironment(client client.Client) *ClusterEnvironment { | |
} | ||
|
||
type Flavour string | ||
type FlavourSet map[Flavour]struct{} | ||
|
||
// Add a flavour to the set | ||
func (f FlavourSet) Add(flavour Flavour) { | ||
f[flavour] = struct{}{} | ||
} | ||
|
||
// Remove a flavour from the set | ||
func (f FlavourSet) Remove(flavour Flavour) { | ||
delete(f, flavour) | ||
} | ||
|
||
// Check if a flavour exists in the set | ||
func (f FlavourSet) Contains(flavour Flavour) bool { | ||
_, exists := f[flavour] | ||
return exists | ||
} | ||
|
||
// Convert the set to a slice | ||
func (f FlavourSet) ToSlice() []Flavour { | ||
keys := make([]Flavour, 0, len(f)) | ||
for key := range f { | ||
keys = append(keys, key) | ||
} | ||
return keys | ||
} | ||
|
||
const ( | ||
OpenShiftFlavour Flavour = "OpenShift" | ||
MicroShiftFlavour Flavour = "MicroShift" | ||
UnknownFlavour Flavour = "Unknown" | ||
OpenShiftFlavour Flavour = "OpenShift" | ||
MicroShiftFlavour Flavour = "MicroShift" | ||
OstreeFlavour Flavour = "Ostree" | ||
ClassicRhelFlavour Flavour = "ClassicRHEL" | ||
UnknownFlavour Flavour = "Unknown" | ||
) | ||
|
||
func (ce *ClusterEnvironment) Flavour(ctx context.Context) (Flavour, error) { | ||
func (ce *ClusterEnvironment) Flavours(ctx context.Context) (FlavourSet, error) { | ||
detectedFlavours := FlavourSet{} | ||
|
||
microShift, err := ce.isMicroShift(ctx) | ||
if err != nil { | ||
return UnknownFlavour, err | ||
return nil, err | ||
} | ||
if microShift { | ||
return MicroShiftFlavour, nil | ||
detectedFlavours.Add(MicroShiftFlavour) | ||
} | ||
|
||
openShift, err := ce.isOpenShift(ctx) | ||
if err != nil { | ||
return UnknownFlavour, err | ||
return nil, err | ||
} | ||
if openShift { | ||
return OpenShiftFlavour, nil | ||
detectedFlavours.Add(OpenShiftFlavour) | ||
} | ||
|
||
ostree, err := ce.isOSTree() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if ostree { | ||
detectedFlavours.Add(OstreeFlavour) | ||
} | ||
|
||
classic, err := ce.isClassicRHEL(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if classic { | ||
detectedFlavours.Add(ClassicRhelFlavour) | ||
} | ||
return UnknownFlavour, nil | ||
} | ||
|
||
if len(detectedFlavours) == 0 { | ||
detectedFlavours.Add(UnknownFlavour) | ||
} | ||
|
||
return detectedFlavours, nil | ||
} | ||
func (ce *ClusterEnvironment) isMicroShift(ctx context.Context) (bool, error) { | ||
cm := v1.ConfigMap{} | ||
cm.SetName("microshift-version") | ||
|
@@ -74,3 +125,44 @@ func (ce *ClusterEnvironment) isOpenShift(ctx context.Context) (bool, error) { | |
} | ||
return true, nil | ||
} | ||
|
||
// isClassicRHEL checks if the OS running on the current node is classic RHEL. | ||
func (ce *ClusterEnvironment) isClassicRHEL(ctx context.Context) (bool, error) { | ||
// Retrieve the node name from the K8S_NODE environment variable | ||
nodeName := os.Getenv("K8S_NODE") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. downward API always available? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes downward API is always available in kubernetes. |
||
if nodeName == "" { | ||
return false, fmt.Errorf("K8S_NODE environment variable is not set") | ||
} | ||
|
||
// Fetch the Node object using the Kubernetes client | ||
node := &v1.Node{} | ||
if err := ce.client.Get(ctx, types.NamespacedName{Name: nodeName}, node); err != nil { | ||
return false, fmt.Errorf("failed to retrieve node information: %v", err) | ||
} | ||
|
||
// Extract OSImage from the node's status | ||
osImage := node.Status.NodeInfo.OSImage | ||
fmt.Printf("Node OSImage: %s\n", osImage) | ||
|
||
isOstree, err := ce.isOSTree() | ||
if err != nil { | ||
return false, err | ||
} | ||
// Determine if the OS is classic RHEL | ||
if osImage == "Red Hat Enterprise Linux" || (len(osImage) >= 3 && osImage[:3] == "RHEL") && !isOstree { | ||
return true, nil // Classic RHEL detected | ||
} | ||
|
||
return false, nil // Not classic RHEL (could be OSTree-based or something else) | ||
} | ||
|
||
// isOSTree checks for the presence of the /run/ostree-booted file to determine if running on OSTree | ||
func (ce *ClusterEnvironment) isOSTree() (bool, error) { | ||
if _, err := os.Stat("/run/ostree-booted"); err != nil { | ||
if os.IsNotExist(err) { | ||
return false, nil // Not an OSTree-based OS | ||
} | ||
return false, fmt.Errorf("Failed to check OSTree status: %v", err) | ||
} | ||
return true, nil // OSTree-based OS detected | ||
} |
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.
struct{}{}
needs more thought