Skip to content

Commit

Permalink
reconcile exchanged secrets
Browse files Browse the repository at this point in the history
Signed-off-by: Umanga Chapagain <[email protected]>
  • Loading branch information
umangachapagain committed Jul 18, 2024
1 parent 8787924 commit 79c59a7
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 2 deletions.
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
38 changes: 37 additions & 1 deletion addons/green_secret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ 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"
)

Expand Down Expand Up @@ -55,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.EnqueueRequestsFromMapFunc(mapSecretToGreenSecret),
builder.WithPredicates(predicate.GenerationChangedPredicate{}, predicate.LabelChangedPredicate{}, greebSecretSpokePredicate)).
WatchesRawSource(source.Kind(r.HubCluster.GetCache(), &corev1.Secret{}), &handler.EnqueueRequestForObject{},
builder.WithPredicates(predicate.GenerationChangedPredicate{}, greenSecretPredicate)).
builder.WithPredicates(predicate.GenerationChangedPredicate{}, predicate.LabelChangedPredicate{}, greenSecretPredicate)).
Complete(r)
}

Expand Down
2 changes: 2 additions & 0 deletions addons/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ func (o *AddonAgentOptions) RunAgent(ctx context.Context) {

if err = (&BlueSecretReconciler{
Scheme: mgr.GetScheme(),
HubCluster: hubCluster,
HubClient: hubCluster.GetClient(),
SpokeClient: mgr.GetClient(),
SpokeClusterName: o.SpokeClusterName,
Expand All @@ -227,6 +228,7 @@ func (o *AddonAgentOptions) RunAgent(ctx context.Context) {

if err = (&S3SecretReconciler{
Scheme: mgr.GetScheme(),
HubCluster: hubCluster,
HubClient: hubCluster.GetClient(),
SpokeClient: mgr.GetClient(),
SpokeClusterName: o.SpokeClusterName,
Expand Down
47 changes: 46 additions & 1 deletion addons/s3_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package addons

import (
"context"
"encoding/json"
"log/slog"
"os"
"strings"
Expand All @@ -10,19 +11,25 @@ import (
obv1alpha1 "github.com/kube-object-storage/lib-bucket-provisioner/pkg/apis/objectbucket.io/v1alpha1"
"github.com/red-hat-storage/odf-multicluster-orchestrator/controllers/utils"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"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"
)

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

s3SecretHubPredicate := 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
},
}

mapSecretToOBC := func(ctx context.Context, obj client.Object) []reconcile.Request {
if s, ok := obj.(*corev1.Secret); ok {
if s.Labels[utils.SecretLabelTypeKey] == string(utils.InternalLabel) {
data := make(map[string][]byte)
if err := json.Unmarshal(s.Data[utils.SecretDataKey], &data); err != nil {
r.Logger.Error("Failed to map S3 secret on hub to OBC in spoke cluster. Not requeueing request", "secret", s.Name, "error", err)
return []reconcile.Request{}
}
return []reconcile.Request{
{
NamespacedName: types.NamespacedName{
Namespace: string(s.Data[utils.NamespaceKey]),
Name: string(data[utils.S3BucketName]),
},
},
}
}
}
return []reconcile.Request{}
}

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

return ctrl.NewControllerManagedBy(mgr).
Named("s3secret_controller").
Watches(&obv1alpha1.ObjectBucketClaim{}, &handler.EnqueueRequestForObject{},
builder.WithPredicates(predicate.GenerationChangedPredicate{}, s3BucketPredicate)).
builder.WithPredicates(predicate.GenerationChangedPredicate{}, predicate.LabelChangedPredicate{}, s3BucketPredicate)).
WatchesRawSource(source.Kind(r.HubCluster.GetCache(), &corev1.Secret{}), handler.EnqueueRequestsFromMapFunc(mapSecretToOBC),
builder.WithPredicates(predicate.GenerationChangedPredicate{}, predicate.LabelChangedPredicate{}, s3SecretHubPredicate)).
Complete(r)
}

Expand Down

0 comments on commit 79c59a7

Please sign in to comment.