-
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 0220f50
Showing
4 changed files
with
118 additions
and
5 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
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,99 @@ | ||
/* | ||
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) | ||
|
||
if rke2ConfigName, found := secret.GetLabels()[webhooks.RKE2ConfigNameLabel]; !found { | ||
return ctrl.Result{}, nil | ||
} else { | ||
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 | ||
} | ||
|
||
secret.OwnerReferences = []metav1.OwnerReference{{ | ||
APIVersion: rke2Config.APIVersion, | ||
Kind: rke2Config.Kind, | ||
Name: rke2Config.Name, | ||
UID: rke2Config.UID, | ||
}} | ||
|
||
return ctrl.Result{}, r.Client.Patch(ctx, secret, client.Apply) | ||
} | ||
} |
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