-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Perform ETCD snapshot restore out of webhook ownership references cre…
…ation Signed-off-by: Danil-Grigorev <danil.grigorev@suse.com>
- Loading branch information
1 parent
2561b08
commit 9a2f740
Showing
4 changed files
with
129 additions
and
5 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
exp/etcdrestore/controllers/etcdsnapshot_secret_controller.go
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,110 @@ | ||
/* | ||
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" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
|
||
bootstrapv1 "github.com/rancher/cluster-api-provider-rke2/bootstrap/api/v1beta1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
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/log" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
"github.com/rancher/turtles/exp/etcdrestore/webhooks" | ||
) | ||
|
||
// ETCDSnapshotSecretReconciler reconciles an EtcdMachineSnapshot object. | ||
type ETCDSnapshotSecretReconciler struct { | ||
client.Client | ||
} | ||
|
||
func secretFilter(obj client.Object) bool { | ||
rke2ConfigName, found := obj.GetLabels()[webhooks.RKE2ConfigNameLabel] | ||
|
||
owned := false | ||
for _, owner := range obj.GetOwnerReferences() { | ||
if owner.Name == rke2ConfigName { | ||
owned = true | ||
break | ||
} | ||
} | ||
|
||
return found && !owned | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *ETCDSnapshotSecretReconciler) SetupWithManager(_ context.Context, mgr ctrl.Manager, _ controller.Options) error { | ||
err := ctrl.NewControllerManagedBy(mgr). | ||
For(&corev1.Secret{}). | ||
WithEventFilter(predicate.NewPredicateFuncs(secretFilter)). | ||
Complete(reconcile.AsReconciler(mgr.GetClient(), r)) | ||
if err != nil { | ||
return fmt.Errorf("creating ETCDSnapshotSecretReconciler controller: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Reconcile reconciles the EtcdMachineSnapshot object. | ||
func (r *ETCDSnapshotSecretReconciler) Reconcile(ctx context.Context, secret *corev1.Secret) (ctrl.Result, error) { | ||
log := log.FromContext(ctx) | ||
|
||
rke2ConfigName, found := secret.GetLabels()[webhooks.RKE2ConfigNameLabel] | ||
if !found { | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
rke2Config := &bootstrapv1.RKE2Config{} | ||
|
||
if err := r.Client.Get(ctx, client.ObjectKey{ | ||
Name: rke2ConfigName, | ||
Namespace: secret.Namespace, | ||
}, rke2Config); apierrors.IsNotFound(err) { | ||
return ctrl.Result{}, nil | ||
} else if err != nil { | ||
log.Error(err, "Unable to fetch RKE2Config instance") | ||
|
||
return ctrl.Result{}, err | ||
} | ||
|
||
if len(secret.OwnerReferences) == 0 { | ||
secret.OwnerReferences = []metav1.OwnerReference{} | ||
} | ||
|
||
secret.OwnerReferences = append(secret.OwnerReferences, metav1.OwnerReference{ | ||
APIVersion: rke2Config.APIVersion, | ||
Kind: rke2Config.Kind, | ||
Name: rke2Config.Name, | ||
UID: rke2Config.UID, | ||
}) | ||
|
||
if err := r.Client.Patch(ctx, secret, client.Apply); err != nil { | ||
log.Error(err, "Unable to patch Secret with RKE2Config ownership") | ||
|
||
return ctrl.Result{}, err | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
} |
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