-
Notifications
You must be signed in to change notification settings - Fork 17
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
Add managedosversion finalizer #775
Merged
anmazzotti
merged 7 commits into
rancher:main
from
anmazzotti:add_managedosversion_finalizer
Jun 25, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e57c80a
Implement ManagedOSVersion controller and finalizer
anmazzotti 3482584
Remove unused ManagedOSVersion Status
anmazzotti e024650
Update manifests
anmazzotti d856ef2
Start the ManagedOSVersion controller
anmazzotti 4bc6ff5
Update api/v1beta1/common_consts.go
anmazzotti b7418bf
Update controllers/managedosversion_controller.go
anmazzotti b80eea1
Update cmd/operator/operator/root.go
anmazzotti 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -215,6 +215,7 @@ rules: | |
- managedosversions/status | ||
verbs: | ||
- get | ||
- list | ||
- patch | ||
- update | ||
- apiGroups: | ||
|
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
426 changes: 0 additions & 426 deletions
426
config/crd/bases/elemental.cattle.io_managedosversions.yaml
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -216,6 +216,7 @@ rules: | |
- managedosversions/status | ||
verbs: | ||
- get | ||
- list | ||
- patch | ||
- update | ||
- apiGroups: | ||
|
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
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 |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
Copyright © 2022 - 2024 SUSE LLC | ||
|
||
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" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
elementalv1 "github.com/rancher/elemental-operator/api/v1beta1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
) | ||
|
||
// ManagedOSVersionReconciler reconciles a ManagedOSVersion object. | ||
type ManagedOSVersionReconciler struct { | ||
client.Client | ||
DefaultRegistry string | ||
Scheme *runtime.Scheme | ||
} | ||
|
||
// +kubebuilder:rbac:groups=elemental.cattle.io,resources=managedosversions,verbs=get;list;watch;create;update;patch;delete | ||
// +kubebuilder:rbac:groups=elemental.cattle.io,resources=managedosversions/status,verbs=get;update;patch;list | ||
// +kubebuilder:rbac:groups=elemental.cattle.io,resources=managedosimages,verbs=get;list;watch | ||
|
||
func (r *ManagedOSVersionReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&elementalv1.ManagedOSVersion{}). | ||
Watches( | ||
&elementalv1.ManagedOSImage{}, | ||
handler.EnqueueRequestsFromMapFunc(r.ManagedOSImageToManagedOSVersion), | ||
). | ||
Complete(r) | ||
} | ||
|
||
func (r *ManagedOSVersionReconciler) ManagedOSImageToManagedOSVersion(ctx context.Context, obj client.Object) []ctrl.Request { | ||
logger := ctrl.LoggerFrom(ctx) | ||
logger.Info("Enqueueing ManagedOSVersion reconciliation from ManagedOSImage") | ||
|
||
requests := []ctrl.Request{} | ||
|
||
// Verify we are actually handling a ManagedOSImage object | ||
managedOSImage, ok := obj.(*elementalv1.ManagedOSImage) | ||
if !ok { | ||
logger.Error(errors.New("Enqueueing error"), fmt.Sprintf("Expected a ManagedOSImage object, but got %T", obj)) | ||
return []ctrl.Request{} | ||
} | ||
|
||
// Check the ManagedOSImage was referencing any ManagedOSVersion | ||
if len(managedOSImage.Spec.ManagedOSVersionName) > 0 { | ||
fgiudici marked this conversation as resolved.
Show resolved
Hide resolved
|
||
logger.Info("Adding ManagedOSVersion to reconciliation request", "ManagedOSVersion", managedOSImage.Spec.ManagedOSVersionName) | ||
name := client.ObjectKey{Namespace: managedOSImage.Namespace, Name: managedOSImage.Spec.ManagedOSVersionName} | ||
requests = append(requests, ctrl.Request{NamespacedName: name}) | ||
} | ||
|
||
return requests | ||
} | ||
|
||
func (r *ManagedOSVersionReconciler) Reconcile(ctx context.Context, req reconcile.Request) (_ ctrl.Result, rerr error) { | ||
logger := ctrl.LoggerFrom(ctx).WithValues("Namespace", req.Namespace).WithValues("ManagedOSVersion", req.Name) | ||
logger.Info("Reconciling ManagedOSVersion") | ||
|
||
// Fetch the ManagedOSVersion | ||
managedOSVersion := &elementalv1.ManagedOSVersion{} | ||
if err := r.Client.Get(ctx, req.NamespacedName, managedOSVersion); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
return ctrl.Result{}, nil | ||
} | ||
return ctrl.Result{}, fmt.Errorf("fetching ManagedOSVersion: %w", err) | ||
} | ||
|
||
// Ensure we patch the latest version otherwise we could erratically overlap with other controllers (e.g. backup and restore) | ||
patchBase := client.MergeFromWithOptions(managedOSVersion.DeepCopy(), client.MergeFromWithOptimisticLock{}) | ||
defer func() { | ||
managedOSVersionStatusCopy := managedOSVersion.Status.DeepCopy() // Patch call will erase the status | ||
|
||
if err := r.Patch(ctx, managedOSVersion, patchBase); err != nil { | ||
rerr = errors.Join(rerr, fmt.Errorf("patching ManagedOSVersion: %w", err)) | ||
} | ||
|
||
managedOSVersion.Status = *managedOSVersionStatusCopy | ||
|
||
// If the object was waiting for deletion and we just removed the finalizer, we will get a not found error | ||
if err := r.Status().Patch(ctx, managedOSVersion, patchBase); err != nil && !apierrors.IsNotFound(err) { | ||
rerr = errors.Join(rerr, fmt.Errorf("patching ManagedOSVersion status: %w", err)) | ||
} | ||
}() | ||
|
||
// The object is not up for deletion | ||
if managedOSVersion.GetDeletionTimestamp().IsZero() { | ||
// The object is not being deleted, so register the finalizer | ||
if !controllerutil.ContainsFinalizer(managedOSVersion, elementalv1.ManagedOSVersionFinalizer) { | ||
controllerutil.AddFinalizer(managedOSVersion, elementalv1.ManagedOSVersionFinalizer) | ||
} | ||
|
||
// Reconcile ManagedOSVersion | ||
result, err := r.reconcileNormal(ctx, managedOSVersion) | ||
if err != nil { | ||
return ctrl.Result{}, fmt.Errorf("reconciling ManagedOSVersion: %w", err) | ||
} | ||
return result, nil | ||
} | ||
|
||
// Object is up for deletion | ||
if controllerutil.ContainsFinalizer(managedOSVersion, elementalv1.ManagedOSVersionFinalizer) { | ||
result, err := r.reconcileDelete(ctx, managedOSVersion) | ||
if err != nil { | ||
return ctrl.Result{}, fmt.Errorf("reconciling ManagedOSVersion deletion: %w", err) | ||
} | ||
return result, err | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
} | ||
|
||
func (r *ManagedOSVersionReconciler) reconcileNormal(ctx context.Context, managedOSVersion *elementalv1.ManagedOSVersion) (ctrl.Result, error) { | ||
logger := ctrl.LoggerFrom(ctx).WithValues("Namespace", managedOSVersion.Namespace).WithValues("ManagedOSVersion", managedOSVersion.Name) | ||
logger.Info("Normal ManagedOSVersion reconcile") | ||
// Nothing to do here | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
func (r *ManagedOSVersionReconciler) reconcileDelete(ctx context.Context, managedOSVersion *elementalv1.ManagedOSVersion) (ctrl.Result, error) { | ||
logger := ctrl.LoggerFrom(ctx).WithValues("Namespace", managedOSVersion.Namespace).WithValues("ManagedOSVersion", managedOSVersion.Name) | ||
logger.Info("Deletion ManagedOSVersion reconcile") | ||
managedOSImages, err := r.getManagedOSImages(ctx, *managedOSVersion) | ||
if err != nil { | ||
return ctrl.Result{}, fmt.Errorf("getting linked ManagedOSImages: %w", err) | ||
} | ||
// If there are ManagedOSImages referencing this ManagedOSVersion, hold on deletion. | ||
if len(managedOSImages.Items) > 0 { | ||
imageNames := []string{} | ||
for _, image := range managedOSImages.Items { | ||
imageNames = append(imageNames, image.Name) | ||
} | ||
logger.Info("ManagedOSVersion still in use by ManagedOSImages", "ManagedOSImages", strings.Join(imageNames, ",")) | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
controllerutil.RemoveFinalizer(managedOSVersion, elementalv1.ManagedOSVersionFinalizer) | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
// getManagedOSImages returns a list of ManagedOSImages referencing the ManagedOSVersion. | ||
func (r *ManagedOSVersionReconciler) getManagedOSImages(ctx context.Context, managedOSVersion elementalv1.ManagedOSVersion) (*elementalv1.ManagedOSImageList, error) { | ||
managedOSImages := &elementalv1.ManagedOSImageList{} | ||
|
||
if err := r.List(ctx, managedOSImages, client.InNamespace(managedOSVersion.Namespace), client.MatchingLabels(map[string]string{ | ||
elementalv1.ElementalManagedOSImageVersionNameLabel: managedOSVersion.Name, | ||
})); err != nil { | ||
return nil, fmt.Errorf("listing ManagedOSImages: %w", err) | ||
} | ||
|
||
return managedOSImages, nil | ||
} |
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.
Very random change. Not related to this PR.