-
Notifications
You must be signed in to change notification settings - Fork 25
MGMT-24419: Delete DataImage after cluster is installed and remove detached annotation #844
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
Draft
giladravid16
wants to merge
1
commit into
openshift:main
Choose a base branch
from
giladravid16:MGMT-24419
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.
Draft
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /* | ||
| Copyright 2023. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package controllers | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| k8sapierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| bmh_v1alpha1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1" | ||
| "github.com/sirupsen/logrus" | ||
|
|
||
| "github.com/openshift/image-based-install-operator/api/v1alpha1" | ||
| ) | ||
|
|
||
| func getDataImage(ctx context.Context, c client.Client, namespace, name string) (*bmh_v1alpha1.DataImage, error) { | ||
| dataImage := &bmh_v1alpha1.DataImage{} | ||
| key := types.NamespacedName{Name: name, Namespace: namespace} | ||
| if err := c.Get(ctx, key, dataImage); err != nil { | ||
| return nil, err | ||
| } | ||
| return dataImage, nil | ||
| } | ||
|
|
||
| func deleteDataImage(ctx context.Context, c client.Client, log logrus.FieldLogger, dataImageRef types.NamespacedName) (*bmh_v1alpha1.DataImage, error) { | ||
| dataImage := &bmh_v1alpha1.DataImage{} | ||
| if err := c.Get(ctx, dataImageRef, dataImage); err != nil { | ||
| if k8sapierrors.IsNotFound(err) { | ||
| log.Infof("Can't find DataImage from BareMetalHost %s/%s, Nothing to remove", dataImageRef.Namespace, dataImageRef.Name) | ||
| return nil, nil | ||
| } | ||
| return nil, fmt.Errorf("failed to get DataImage %s/%s: %w", dataImageRef.Namespace, dataImageRef.Name, err) | ||
| } | ||
|
|
||
| log.Infof("Deleting DataImage %s/%s, deletion may take some time since a BMH restart is required", dataImageRef.Namespace, dataImageRef.Name) | ||
| if err := c.Delete(ctx, dataImage); err != nil { | ||
| return dataImage, err | ||
| } | ||
| return dataImage, nil | ||
| } | ||
|
|
||
| func removeBMHDataImage(ctx context.Context, c client.Client, log logrus.FieldLogger, bmhRef types.NamespacedName) (*bmh_v1alpha1.DataImage, error) { | ||
| dataImage, err := deleteDataImage(ctx, c, log, bmhRef) | ||
| if err != nil || dataImage == nil { | ||
| return dataImage, err | ||
| } | ||
|
|
||
| bmh := &bmh_v1alpha1.BareMetalHost{} | ||
| if err := c.Get(ctx, bmhRef, bmh); err != nil { | ||
| if k8sapierrors.IsNotFound(err) { | ||
| log.Warnf("Referenced BareMetalHost %s/%s does not exist, not waiting for dataImage deletion", bmhRef.Namespace, bmhRef.Name) | ||
| return nil, nil | ||
| } | ||
| return dataImage, fmt.Errorf("failed to get BareMetalHost %s/%s: %w", bmhRef.Namespace, bmhRef.Name, err) | ||
| } | ||
| return dataImage, attachAndRebootBMH(ctx, c, log, bmh) | ||
| } | ||
|
|
||
| func attachAndRebootBMH(ctx context.Context, c client.Client, log logrus.FieldLogger, bmh *bmh_v1alpha1.BareMetalHost) error { | ||
| patch := client.MergeFrom(bmh.DeepCopy()) | ||
| dirty := false | ||
| if annotationExists(&bmh.ObjectMeta, detachedAnnotation) { | ||
| log.Infof("Removing Detached annotation if exists on BareMetalHost %s/%s", bmh.Namespace, bmh.Name) | ||
| delete(bmh.ObjectMeta.Annotations, detachedAnnotation) | ||
| dirty = true | ||
| } | ||
|
|
||
| if setAnnotationIfNotExists(&bmh.ObjectMeta, rebootAnnotation, rebootAnnotationValue) { | ||
| log.Infof("Adding reboot annotation to BareMetalHost %s/%s", bmh.Namespace, bmh.Name) | ||
| dirty = true | ||
| } | ||
| if dirty { | ||
| return c.Patch(ctx, bmh, patch) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func getBMH(ctx context.Context, c client.Client, bmhRef *v1alpha1.BareMetalHostReference) (*bmh_v1alpha1.BareMetalHost, error) { | ||
| bmh := &bmh_v1alpha1.BareMetalHost{} | ||
| key := types.NamespacedName{ | ||
| Name: bmhRef.Name, | ||
| Namespace: bmhRef.Namespace, | ||
| } | ||
| if err := c.Get(ctx, key, bmh); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return bmh, nil | ||
| } | ||
|
|
||
| func setAnnotationIfNotExists(meta *metav1.ObjectMeta, key string, value string) bool { | ||
| if meta.Annotations == nil { | ||
| meta.Annotations = make(map[string]string) | ||
| } | ||
| if _, ok := meta.Annotations[key]; !ok { | ||
| meta.Annotations[key] = value | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func annotationExists(meta *metav1.ObjectMeta, key string) bool { | ||
| if meta.Annotations == nil { | ||
| return false | ||
| } | ||
| _, ok := meta.Annotations[key] | ||
| return ok | ||
| } | ||
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
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.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Decouple detached-annotation cleanup from DataImage existence.
When
deleteDataImagereturnsnilfor an already-missing DataImage, Line 62 returns before fetching the BMH, sodetachedAnnotationis never removed. That leaves the lifecycle-blocking annotation in exactly the already-cleaned-up/DataImage-missing case this PR should handle.Possible fix direction
Then patch the BMH so detached removal always runs, while adding the reboot annotation only when a DataImage deletion was actually requested.
📝 Committable suggestion
🤖 Prompt for AI Agents