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

Move to multi-cluster controller with added resiliency of managed resources #218

Closed
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
8 changes: 7 additions & 1 deletion addons/agent_mirrorpeer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/cluster"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"
)

// MirrorPeerReconciler reconciles a MirrorPeer object
type MirrorPeerReconciler struct {
HubCluster cluster.Cluster
HubClient client.Client
Scheme *runtime.Scheme
SpokeClient client.Client
Expand Down Expand Up @@ -452,7 +456,9 @@ func (r *MirrorPeerReconciler) SetupWithManager(mgr ctrl.Manager) error {
r.Logger.Info("Setting up controller with manager")
mpPredicate := utils.ComposePredicates(predicate.GenerationChangedPredicate{}, mirrorPeerSpokeClusterPredicate)
return ctrl.NewControllerManagedBy(mgr).
For(&multiclusterv1alpha1.MirrorPeer{}, builder.WithPredicates(mpPredicate)).
Named("agent_mirrorpeer_controller").
WatchesRawSource(source.Kind(r.HubCluster.GetCache(), &multiclusterv1alpha1.MirrorPeer{}), &handler.EnqueueRequestForObject{},
builder.WithPredicates(mpPredicate)).
Complete(r)
}

Expand Down
30 changes: 30 additions & 0 deletions addons/blue_secret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"context"
"log/slog"

"github.com/red-hat-storage/odf-multicluster-orchestrator/controllers/utils"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/cluster"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -19,6 +22,7 @@ import (
// BlueSecretReconciler reconciles a MirrorPeer object
type BlueSecretReconciler struct {
Scheme *runtime.Scheme
HubCluster cluster.Cluster
HubClient client.Client
SpokeClient client.Client
SpokeClusterName string
Expand Down Expand Up @@ -46,12 +50,38 @@ func (r *BlueSecretReconciler) SetupWithManager(mgr ctrl.Manager) error {
},
}

isBlueSecretOnHub := func(obj client.Object) bool {
if s, ok := obj.(*corev1.Secret); ok {
if s.Labels[utils.SecretLabelTypeKey] == string(utils.SourceLabel) {
return true
}
}
return false
}

blueSecretHubPredicate := predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return false
},
DeleteFunc: func(e event.DeleteEvent) bool {
return isBlueSecretOnHub(e.Object)
},
UpdateFunc: func(e event.UpdateEvent) bool {
return isBlueSecretOnHub(e.ObjectNew) || isBlueSecretOnHub(e.ObjectOld)
},
GenericFunc: func(_ event.GenericEvent) bool {
return false
},
}

r.Logger.Info("Setting up controller with manager")

return ctrl.NewControllerManagedBy(mgr).
Named("bluesecret_controller").
Watches(&corev1.Secret{}, &handler.EnqueueRequestForObject{},
builder.WithPredicates(predicate.GenerationChangedPredicate{}, predicate.LabelChangedPredicate{}, blueSecretPredicate)).
WatchesRawSource(source.Kind(r.HubCluster.GetCache(), &corev1.Secret{}), &handler.EnqueueRequestForObject{},
builder.WithPredicates(predicate.GenerationChangedPredicate{}, predicate.LabelChangedPredicate{}, blueSecretHubPredicate)).
Complete(r)
}

Expand Down
43 changes: 41 additions & 2 deletions addons/green_secret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@ import (
"fmt"
"log/slog"

"github.com/red-hat-storage/odf-multicluster-orchestrator/addons/setup"
"github.com/red-hat-storage/odf-multicluster-orchestrator/controllers/utils"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/cluster"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

// GreenSecretReconciler reconciles a MirrorPeer object
type GreenSecretReconciler struct {
Scheme *runtime.Scheme
HubCluster cluster.Cluster
HubClient client.Client
SpokeClient client.Client
SpokeClusterName string
Expand Down Expand Up @@ -52,12 +58,45 @@ func (r *GreenSecretReconciler) SetupWithManager(mgr ctrl.Manager) error {
},
}

greebSecretSpokePredicate := predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return false
},
DeleteFunc: func(e event.DeleteEvent) bool {
return true
},
UpdateFunc: func(e event.UpdateEvent) bool {
return true
},
GenericFunc: func(_ event.GenericEvent) bool {
return false
},
}

mapSecretToGreenSecret := func(ctx context.Context, obj client.Object) []reconcile.Request {
if s, ok := obj.(*corev1.Secret); ok {
if s.Labels[utils.CreatedByLabelKey] == setup.TokenExchangeName {
return []reconcile.Request{
{
NamespacedName: types.NamespacedName{
Namespace: r.SpokeClusterName,
Name: s.Name,
},
},
}
}
}
return []reconcile.Request{}
}

r.Logger.Info("Setting up controller with manager")

return ctrl.NewControllerManagedBy(mgr).
Named("greensecret_controller").
Watches(&corev1.Secret{}, &handler.EnqueueRequestForObject{},
builder.WithPredicates(predicate.GenerationChangedPredicate{}, greenSecretPredicate)).
Watches(&corev1.Secret{}, handler.EnqueueRequestsFromMapFunc(mapSecretToGreenSecret),
builder.WithPredicates(predicate.GenerationChangedPredicate{}, predicate.LabelChangedPredicate{}, greebSecretSpokePredicate)).
WatchesRawSource(source.Kind(r.HubCluster.GetCache(), &corev1.Secret{}), &handler.EnqueueRequestForObject{},
builder.WithPredicates(predicate.GenerationChangedPredicate{}, predicate.LabelChangedPredicate{}, greenSecretPredicate)).
Complete(r)
}

Expand Down
Loading
Loading