-
Notifications
You must be signed in to change notification settings - Fork 36
OCPBUGS-90560: resolve boot disk image at reconcile time #181
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
Open
nader-ziada
wants to merge
4
commits into
openshift:main
Choose a base branch
from
nader-ziada:resolve-boot-image-in-reconciler
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1d3edb6
feat(machine): resolve boot disk image at reconcile time
nader-ziada 7d53743
use streams key from coreos-bootimages ConfigMap
nader-ziada efc3d2f
address review findings for boot image resolver
nader-ziada ede75e9
update fallback boot images from 4.18 to 5.0
nader-ziada 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 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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| package machine | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| "github.com/coreos/stream-metadata-go/stream" | ||
| "github.com/openshift/machine-api-provider-gcp/pkg/cloud/gcp/actuators/util" | ||
| corev1 "k8s.io/api/core/v1" | ||
| apimachineryerrors "k8s.io/apimachinery/pkg/api/errors" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/klog/v2" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
|
|
||
| var osImageStreamGVK = schema.GroupVersionKind{ | ||
| Group: "machineconfiguration.openshift.io", | ||
| Version: "v1", | ||
| Kind: "OSImageStream", | ||
| } | ||
|
|
||
| func (r *Reconciler) resolveBootImage() (string, error) { | ||
| arch := r.resolveArchitecture() | ||
|
|
||
| image, err := r.resolveImageFromConfigMap(arch) | ||
| if err != nil { | ||
| klog.Warningf("Failed to resolve boot image from coreos-bootimages ConfigMap: %v, using fallback", err) | ||
| return fallbackImage(arch), nil | ||
| } | ||
| if image == "" { | ||
| klog.Warningf("No GCP image found in coreos-bootimages for arch %s, using fallback", arch) | ||
| return fallbackImage(arch), nil | ||
| } | ||
|
|
||
| klog.V(3).Infof("Resolved boot image from coreos-bootimages ConfigMap: %s (arch: %s)", image, arch) | ||
| return image, nil | ||
| } | ||
|
|
||
| func (r *Reconciler) resolveArchitecture() util.NormalizedArch { | ||
| mt, err := r.computeService.MachineTypesGet(r.projectID, r.providerSpec.Zone, r.providerSpec.MachineType) | ||
| if err != nil || mt == nil { | ||
| klog.V(3).Infof("Failed to get machine type %s from GCP API, falling back to prefix-based detection", r.providerSpec.MachineType) | ||
| return util.CPUArchitecture(r.providerSpec.MachineType) | ||
| } | ||
|
|
||
| switch mt.Architecture { | ||
| case "ARM64": | ||
| return util.ArchitectureArm64 | ||
| case "X86_64": | ||
| return util.ArchitectureAmd64 | ||
| case "", "ARCHITECTURE_UNSPECIFIED": | ||
| klog.V(3).Infof("GCP API returned no architecture for machine type %s, falling back to prefix-based detection", r.providerSpec.MachineType) | ||
| return util.CPUArchitecture(r.providerSpec.MachineType) | ||
| default: | ||
| klog.Warningf("Unknown GCP architecture %q for machine type %s, falling back to prefix-based detection", mt.Architecture, r.providerSpec.MachineType) | ||
| return util.CPUArchitecture(r.providerSpec.MachineType) | ||
| } | ||
| } | ||
|
|
||
| func (r *Reconciler) resolveImageFromConfigMap(arch util.NormalizedArch) (string, error) { | ||
| var cm corev1.ConfigMap | ||
| if err := r.apiReader.Get(r.Context, client.ObjectKey{ | ||
| Namespace: coreOSBootImagesNamespace, | ||
| Name: coreOSBootImagesName, | ||
| }, &cm); err != nil { | ||
| return "", fmt.Errorf("failed to get coreos-bootimages ConfigMap: %w", err) | ||
| } | ||
|
|
||
| streamData, err := r.resolveStreamData(cm.Data) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| var st stream.Stream | ||
| if err := json.Unmarshal([]byte(streamData), &st); err != nil { | ||
| return "", fmt.Errorf("failed to parse stream metadata: %w", err) | ||
| } | ||
|
|
||
| streamArch := archToStreamArch(arch) | ||
| archData, ok := st.Architectures[streamArch] | ||
| if !ok { | ||
| return "", fmt.Errorf("no architecture %q in stream metadata", streamArch) | ||
| } | ||
|
|
||
| if archData.Images.Gcp == nil { | ||
| return "", fmt.Errorf("no GCP image entry for architecture %q in stream metadata", streamArch) | ||
| } | ||
|
|
||
| return gcpImageReference(archData.Images.Gcp.Project, archData.Images.Gcp.Name), nil | ||
| } | ||
|
|
||
| func (r *Reconciler) resolveStreamData(cmData map[string]string) (string, error) { | ||
| streamsRaw, hasStreams := cmData["streams"] | ||
| if hasStreams { | ||
| streamName := r.resolveActiveStreamName() | ||
| var streams map[string]json.RawMessage | ||
| if err := json.Unmarshal([]byte(streamsRaw), &streams); err != nil { | ||
| return "", fmt.Errorf("failed to parse streams data from ConfigMap: %w", err) | ||
| } | ||
|
|
||
| data, ok := streams[streamName] | ||
| if !ok { | ||
| return "", fmt.Errorf("stream %q not found in coreos-bootimages ConfigMap streams key", streamName) | ||
| } | ||
| return string(data), nil | ||
| } | ||
|
|
||
| streamData, hasStream := cmData["stream"] | ||
| if hasStream { | ||
| klog.V(3).Info("coreos-bootimages ConfigMap missing 'streams' key, falling back to deprecated 'stream' key") | ||
| return streamData, nil | ||
| } | ||
|
|
||
| return "", fmt.Errorf("coreos-bootimages ConfigMap missing both 'streams' and 'stream' keys") | ||
| } | ||
|
|
||
| func (r *Reconciler) resolveActiveStreamName() string { | ||
| obj := &unstructured.Unstructured{} | ||
| obj.SetGroupVersionKind(osImageStreamGVK) | ||
|
|
||
| err := r.apiReader.Get(r.Context, client.ObjectKey{Name: osImageStreamName}, obj) | ||
| if err != nil { | ||
| if apimachineryerrors.IsNotFound(err) { | ||
| klog.V(3).Infof("OSImageStream CR not found, defaulting to stream %q", defaultOSStreamName) | ||
| } else { | ||
| klog.Warningf("Failed to get OSImageStream CR: %v, defaulting to stream %q", err, defaultOSStreamName) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| return defaultOSStreamName | ||
| } | ||
|
|
||
| defaultStream, found, err := unstructured.NestedString(obj.Object, "spec", "defaultStream") | ||
| if err != nil || !found || defaultStream == "" { | ||
| klog.V(3).Infof("OSImageStream CR has no spec.defaultStream set, defaulting to stream %q", defaultOSStreamName) | ||
| return defaultOSStreamName | ||
| } | ||
|
|
||
| klog.V(3).Infof("Resolved active OS stream from OSImageStream CR: %s", defaultStream) | ||
| return defaultStream | ||
| } | ||
|
|
||
| func archToStreamArch(arch util.NormalizedArch) string { | ||
| switch arch { | ||
| case util.ArchitectureArm64: | ||
| return "aarch64" | ||
| case util.ArchitectureAmd64: | ||
| return "x86_64" | ||
| default: | ||
| return "x86_64" | ||
| } | ||
| } | ||
|
|
||
| func fallbackImage(arch util.NormalizedArch) string { | ||
| if arch == util.ArchitectureArm64 { | ||
| return defaultGCPBootImageARM | ||
| } | ||
| return defaultGCPBootImageX86 | ||
| } | ||
|
|
||
| func gcpImageReference(project, name string) string { | ||
| return fmt.Sprintf("projects/%s/global/images/%s", project, name) | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.