Skip to content
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

Remove all cluster v1 code #1024

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3438,12 +3438,8 @@ rules:
- clusters
- clusters/status
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- turtles-capi.cattle.io
Expand Down
4 changes: 0 additions & 4 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,8 @@ rules:
- clusters
- clusters/status
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- turtles-capi.cattle.io
Expand Down
74 changes: 74 additions & 0 deletions internal/controllers/cleanup_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright © 2023 - 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"
"fmt"

"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"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

managementv3 "github.com/rancher/turtles/api/rancher/management/v3"
)

// CAPICleanupReconciler is a reconciler for cleanup of managementv3 clusters.
type CAPICleanupReconciler struct {
RancherClient client.Client
Scheme *runtime.Scheme
}

// SetupWithManager sets up reconciler with manager.
func (r *CAPICleanupReconciler) SetupWithManager(_ context.Context, mgr ctrl.Manager, options controller.Options) error {
if err := ctrl.NewControllerManagedBy(mgr).
Named("cleanup").
For(&managementv3.Cluster{}).
WithOptions(options).
WithEventFilter(predicate.NewPredicateFuncs(func(object client.Object) bool {
_, exist := object.GetLabels()[ownedLabelName]
return exist
})).
Complete(reconcile.AsReconciler(r.RancherClient, r)); err != nil {
return fmt.Errorf("creating new downgrade controller: %w", err)
}

return nil
}

// Reconcile performs check for clusters and removes finalizer on the clusters in deleteion
// still containing the turtles finalizer.
func (r *CAPICleanupReconciler) Reconcile(ctx context.Context, cluster *managementv3.Cluster) (res ctrl.Result, err error) {
log := log.FromContext(ctx)

patchBase := client.MergeFromWithOptions(cluster.DeepCopy(), client.MergeFromWithOptimisticLock{})

if cluster.DeletionTimestamp.IsZero() || !controllerutil.RemoveFinalizer(cluster, managementv3.CapiClusterFinalizer) {
return
}

if err = r.RancherClient.Patch(ctx, cluster, patchBase); err != nil {
log.Error(err, "Unable to remove turtles finalizer from cluster"+cluster.GetName())
}

return
}
Loading
Loading