Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascal Davoust committed Sep 14, 2023
1 parent e8311c1 commit a56708a
Show file tree
Hide file tree
Showing 29 changed files with 187 additions and 37 deletions.
9 changes: 9 additions & 0 deletions api/v1alpha1/utils/vaultobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"strings"

"github.com/google/go-cmp/cmp"
vault "github.com/hashicorp/vault/api"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -71,6 +72,7 @@ func (ve *VaultEndpoint) DeleteKVv2IfExists(context context.Context) error {

func (ve *VaultEndpoint) DeleteIfExists(context context.Context) error {
log := log.FromContext(context)
log.V(1).Info("deleting vault resource from Vault", "op", "DeleteIfExists")
vaultClient := context.Value("vaultClient").(*vault.Client)
_, err := vaultClient.Logical().Delete(ve.vaultObject.GetPath())
if err != nil {
Expand All @@ -86,11 +88,14 @@ func (ve *VaultEndpoint) DeleteIfExists(context context.Context) error {
}

func (ve *VaultEndpoint) Create(context context.Context) error {
log := log.FromContext(context)
log.V(1).Info("creating vault resource, writing to Vault", "op", "Create")
return write(context, ve.vaultObject.GetPath(), ve.vaultObject.GetPayload())
}

func (ve *VaultEndpoint) CreateOrUpdate(context context.Context) error {
log := log.FromContext(context)
log.V(1).Info("reading resource from Vault", "op", "CreateOrUpdate")
currentPayload, found, err := read(context, ve.vaultObject.GetPath())
if err != nil {
log.Error(err, "unable to read object at", "path", ve.vaultObject.GetPath())
Expand All @@ -100,7 +105,11 @@ func (ve *VaultEndpoint) CreateOrUpdate(context context.Context) error {
return write(context, ve.vaultObject.GetPath(), ve.vaultObject.GetPayload())
} else {
if !ve.vaultObject.IsEquivalentToDesiredState(currentPayload) {
log.V(1).Info("vault resource is not in sync, writing to Vault", "op", "CreateOrUpdate",
"diff", cmp.Diff(currentPayload, ve.vaultObject.GetPayload()))
return write(context, ve.vaultObject.GetPath(), ve.vaultObject.GetPayload())
} else {
log.V(1).Info("vault resource is already in sync", "op", "CreateOrUpdate")
}
}
return nil
Expand Down
10 changes: 7 additions & 3 deletions controllers/databasesecretengineconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

redhatcopv1alpha1 "github.com/redhat-cop/vault-config-operator/api/v1alpha1"
"github.com/redhat-cop/vault-config-operator/controllers/k8sevt"
"github.com/redhat-cop/vault-config-operator/controllers/vaultresourcecontroller"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -210,14 +211,16 @@ func (r *DatabaseSecretEngineConfigReconciler) SetupWithManager(mgr ctrl.Manager
}

return ctrl.NewControllerManagedBy(mgr).
For(&redhatcopv1alpha1.DatabaseSecretEngineConfig{}, builder.WithPredicates(vaultresourcecontroller.ResourceGenerationChangedPredicate{})).
For(&redhatcopv1alpha1.DatabaseSecretEngineConfig{},
builder.WithPredicates(vaultresourcecontroller.ResourceGenerationChangedPredicate{}, k8sevt.Log{})).
Watches(&corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
},
}, handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, a client.Object) []reconcile.Request {
res := []reconcile.Request{}
s := a.(*corev1.Secret)
r.Log.V(1).Info("fanning event on Secret out to applicable DatabaseSecretEngineConfigs", "namespace", s.Namespace, "name", s.Name)
dbsecs, err := r.findApplicableBDSCForSecret(ctx, s)
if err != nil {
r.Log.Error(err, "unable to find applicable databaseSecretEngines for namespace", "namespace", s.Name)
Expand All @@ -232,14 +235,15 @@ func (r *DatabaseSecretEngineConfigReconciler) SetupWithManager(mgr ctrl.Manager
})
}
return res
}), builder.WithPredicates(isBasicAuthSecret)).
}), builder.WithPredicates(isBasicAuthSecret, k8sevt.Log{})).
Watches(&redhatcopv1alpha1.RandomSecret{
TypeMeta: metav1.TypeMeta{
Kind: "RandomSecret",
},
}, handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, a client.Object) []reconcile.Request {
res := []reconcile.Request{}
rs := a.(*redhatcopv1alpha1.RandomSecret)
r.Log.V(1).Info("fanning event on RandomSecret out to applicable RandomSecrets", "namespace", rs.Namespace, "name", rs.Name)
dbsecs, err := r.findApplicableDBSCForRandomSecret(ctx, rs)
if err != nil {
r.Log.Error(err, "unable to find applicable databaseSecretEngines for namespace", "namespace", rs.Name)
Expand All @@ -254,7 +258,7 @@ func (r *DatabaseSecretEngineConfigReconciler) SetupWithManager(mgr ctrl.Manager
})
}
return res
}), builder.WithPredicates(isUpdatedRandomSecret)).
}), builder.WithPredicates(isUpdatedRandomSecret, k8sevt.Log{})).
Complete(r)
}

Expand Down
4 changes: 3 additions & 1 deletion controllers/databasesecretenginerole_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

redhatcopv1alpha1 "github.com/redhat-cop/vault-config-operator/api/v1alpha1"
"github.com/redhat-cop/vault-config-operator/controllers/k8sevt"
"github.com/redhat-cop/vault-config-operator/controllers/vaultresourcecontroller"
)

Expand Down Expand Up @@ -80,6 +81,7 @@ func (r *DatabaseSecretEngineRoleReconciler) Reconcile(ctx context.Context, req
// SetupWithManager sets up the controller with the Manager.
func (r *DatabaseSecretEngineRoleReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&redhatcopv1alpha1.DatabaseSecretEngineRole{}, builder.WithPredicates(vaultresourcecontroller.ResourceGenerationChangedPredicate{})).
For(&redhatcopv1alpha1.DatabaseSecretEngineRole{},
builder.WithPredicates(vaultresourcecontroller.ResourceGenerationChangedPredicate{}, k8sevt.Log{})).
Complete(r)
}
4 changes: 3 additions & 1 deletion controllers/databasesecretenginestaticrole_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

redhatcopv1alpha1 "github.com/redhat-cop/vault-config-operator/api/v1alpha1"
"github.com/redhat-cop/vault-config-operator/controllers/k8sevt"
"github.com/redhat-cop/vault-config-operator/controllers/vaultresourcecontroller"
)

Expand Down Expand Up @@ -71,6 +72,7 @@ func (r *DatabaseSecretEngineStaticRoleReconciler) Reconcile(ctx context.Context
// SetupWithManager sets up the controller with the Manager.
func (r *DatabaseSecretEngineStaticRoleReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&redhatcopv1alpha1.DatabaseSecretEngineStaticRole{}, builder.WithPredicates(vaultresourcecontroller.ResourceGenerationChangedPredicate{})).
For(&redhatcopv1alpha1.DatabaseSecretEngineStaticRole{},
builder.WithPredicates(vaultresourcecontroller.ResourceGenerationChangedPredicate{}, k8sevt.Log{})).
Complete(r)
}
7 changes: 5 additions & 2 deletions controllers/githubsecretengineconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

redhatcopv1alpha1 "github.com/redhat-cop/vault-config-operator/api/v1alpha1"
"github.com/redhat-cop/vault-config-operator/controllers/k8sevt"
"github.com/redhat-cop/vault-config-operator/controllers/vaultresourcecontroller"
)

Expand Down Expand Up @@ -118,14 +119,16 @@ func (r *GitHubSecretEngineConfigReconciler) SetupWithManager(mgr ctrl.Manager)
}

return ctrl.NewControllerManagedBy(mgr).
For(&redhatcopv1alpha1.GitHubSecretEngineConfig{}, builder.WithPredicates(vaultresourcecontroller.ResourceGenerationChangedPredicate{})).
For(&redhatcopv1alpha1.GitHubSecretEngineConfig{},
builder.WithPredicates(vaultresourcecontroller.ResourceGenerationChangedPredicate{}, k8sevt.Log{})).
Watches(&corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
},
}, handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, a client.Object) []reconcile.Request {
res := []reconcile.Request{}
s := a.(*corev1.Secret)
r.Log.V(1).Info("fanning event on Secret out to applicable GitHubSecretEngineConfigs", "namespace", s.Namespace, "name", s.Name)
dbsecs, err := r.findApplicableGHSCForSecret(ctx, s)
if err != nil {
r.Log.Error(err, "unable to find applicable github SecretEngines for namespace", "namespace", s.Name)
Expand All @@ -140,7 +143,7 @@ func (r *GitHubSecretEngineConfigReconciler) SetupWithManager(mgr ctrl.Manager)
})
}
return res
}), builder.WithPredicates(isSSHSecret)).
}), builder.WithPredicates(isSSHSecret, k8sevt.Log{})).
Complete(r)
}

Expand Down
4 changes: 3 additions & 1 deletion controllers/githubsecretenginerole_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

redhatcopv1alpha1 "github.com/redhat-cop/vault-config-operator/api/v1alpha1"
"github.com/redhat-cop/vault-config-operator/controllers/k8sevt"
"github.com/redhat-cop/vault-config-operator/controllers/vaultresourcecontroller"
)

Expand Down Expand Up @@ -80,6 +81,7 @@ func (r *GitHubSecretEngineRoleReconciler) Reconcile(ctx context.Context, req ct
// SetupWithManager sets up the controller with the Manager.
func (r *GitHubSecretEngineRoleReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&redhatcopv1alpha1.GitHubSecretEngineRole{}, builder.WithPredicates(vaultresourcecontroller.ResourceGenerationChangedPredicate{})).
For(&redhatcopv1alpha1.GitHubSecretEngineRole{},
builder.WithPredicates(vaultresourcecontroller.ResourceGenerationChangedPredicate{}, k8sevt.Log{})).
Complete(r)
}
4 changes: 3 additions & 1 deletion controllers/group_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

redhatcopv1alpha1 "github.com/redhat-cop/vault-config-operator/api/v1alpha1"
"github.com/redhat-cop/vault-config-operator/controllers/k8sevt"
"github.com/redhat-cop/vault-config-operator/controllers/vaultresourcecontroller"
)

Expand Down Expand Up @@ -77,6 +78,7 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
// SetupWithManager sets up the controller with the Manager.
func (r *GroupReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&redhatcopv1alpha1.Group{}, builder.WithPredicates(vaultresourcecontroller.ResourceGenerationChangedPredicate{})).
For(&redhatcopv1alpha1.Group{},
builder.WithPredicates(vaultresourcecontroller.ResourceGenerationChangedPredicate{}, k8sevt.Log{})).
Complete(r)
}
4 changes: 3 additions & 1 deletion controllers/groupalias_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

redhatcopv1alpha1 "github.com/redhat-cop/vault-config-operator/api/v1alpha1"
"github.com/redhat-cop/vault-config-operator/controllers/k8sevt"
"github.com/redhat-cop/vault-config-operator/controllers/vaultresourcecontroller"
)

Expand Down Expand Up @@ -77,6 +78,7 @@ func (r *GroupAliasReconciler) Reconcile(ctx context.Context, req ctrl.Request)
// SetupWithManager sets up the controller with the Manager.
func (r *GroupAliasReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&redhatcopv1alpha1.GroupAlias{}, builder.WithPredicates(vaultresourcecontroller.ResourceGenerationChangedPredicate{})).
For(&redhatcopv1alpha1.GroupAlias{},
builder.WithPredicates(vaultresourcecontroller.ResourceGenerationChangedPredicate{}, k8sevt.Log{})).
Complete(r)
}
10 changes: 7 additions & 3 deletions controllers/jwtoidcauthengineconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

redhatcopv1alpha1 "github.com/redhat-cop/vault-config-operator/api/v1alpha1"
"github.com/redhat-cop/vault-config-operator/controllers/k8sevt"
"github.com/redhat-cop/vault-config-operator/controllers/vaultresourcecontroller"
)

Expand Down Expand Up @@ -142,14 +143,16 @@ func (r *JWTOIDCAuthEngineConfigReconciler) SetupWithManager(mgr ctrl.Manager) e
}

return ctrl.NewControllerManagedBy(mgr).
For(&redhatcopv1alpha1.JWTOIDCAuthEngineConfig{}, builder.WithPredicates(vaultresourcecontroller.ResourceGenerationChangedPredicate{})).
For(&redhatcopv1alpha1.JWTOIDCAuthEngineConfig{},
builder.WithPredicates(vaultresourcecontroller.ResourceGenerationChangedPredicate{}, k8sevt.Log{})).
Watches(&corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
},
}, handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, a client.Object) []reconcile.Request {
res := []reconcile.Request{}
s := a.(*corev1.Secret)
r.Log.V(1).Info("fanning event on Secret out to applicable JWTOIDCAuthEngineConfigs", "namespace", s.Namespace, "name", s.Name)
dbsecs, err := r.findApplicableJOAEForSecret(ctx, s)
if err != nil {
r.Log.Error(err, "unable to find applicable JWTOIDCAuthEngine for namespace", "namespace", s.Name)
Expand All @@ -164,14 +167,15 @@ func (r *JWTOIDCAuthEngineConfigReconciler) SetupWithManager(mgr ctrl.Manager) e
})
}
return res
}), builder.WithPredicates(isBasicAuthSecret)).
}), builder.WithPredicates(isBasicAuthSecret, k8sevt.Log{})).
Watches(&redhatcopv1alpha1.RandomSecret{
TypeMeta: metav1.TypeMeta{
Kind: "RandomSecret",
},
}, handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, a client.Object) []reconcile.Request {
res := []reconcile.Request{}
rs := a.(*redhatcopv1alpha1.RandomSecret)
r.Log.V(1).Info("fanning event on RandomSecret out to applicable JWTOIDCAuthEngineConfigs", "namespace", rs.Namespace, "name", rs.Name)
dbsecs, err := r.findApplicableJOAEForRandomSecret(ctx, rs)
if err != nil {
r.Log.Error(err, "unable to find applicable JWTOIDCAuthEngine for namespace", "namespace", rs.Name)
Expand All @@ -186,7 +190,7 @@ func (r *JWTOIDCAuthEngineConfigReconciler) SetupWithManager(mgr ctrl.Manager) e
})
}
return res
}), builder.WithPredicates(isUpdatedRandomSecret)).
}), builder.WithPredicates(isUpdatedRandomSecret, k8sevt.Log{})).
Complete(r)

}
Expand Down
4 changes: 3 additions & 1 deletion controllers/jwtoidcauthenginerole_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

redhatcopv1alpha1 "github.com/redhat-cop/vault-config-operator/api/v1alpha1"
"github.com/redhat-cop/vault-config-operator/controllers/k8sevt"
"github.com/redhat-cop/vault-config-operator/controllers/vaultresourcecontroller"
)

Expand Down Expand Up @@ -75,6 +76,7 @@ func (r *JWTOIDCAuthEngineRoleReconciler) Reconcile(ctx context.Context, req ctr
// SetupWithManager sets up the controller with the Manager.
func (r *JWTOIDCAuthEngineRoleReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&redhatcopv1alpha1.JWTOIDCAuthEngineRole{}, builder.WithPredicates(vaultresourcecontroller.ResourceGenerationChangedPredicate{})).
For(&redhatcopv1alpha1.JWTOIDCAuthEngineRole{},
builder.WithPredicates(vaultresourcecontroller.ResourceGenerationChangedPredicate{}, k8sevt.Log{})).
Complete(r)
}
74 changes: 74 additions & 0 deletions controllers/k8sevt/k8sevtlogging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Logging capable event handler that mimics handler.EnqueueRequestForObject
See "sigs.k8s.io/controller-runtime/pkg/handler"
*/

package k8sevt

import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"

redhatcopv1alpha1 "github.com/redhat-cop/vault-config-operator/api/v1alpha1"
)

// List all types which include unexported fields so that cmp.Diff won't choke on them
var ignoredUnexportedDuringDiff = cmpopts.IgnoreUnexported(
redhatcopv1alpha1.VRole{},
redhatcopv1alpha1.DBSEConfig{},
redhatcopv1alpha1.GHConfig{},
redhatcopv1alpha1.JWTOIDCConfig{},
redhatcopv1alpha1.KAECConfig{},
redhatcopv1alpha1.KubeSEConfig{},
redhatcopv1alpha1.LDAPConfig{},
redhatcopv1alpha1.PKIIntermediate{},
redhatcopv1alpha1.QuayConfig{},
redhatcopv1alpha1.RMQSEConfig{},
redhatcopv1alpha1.RandomSecretSpec{},
redhatcopv1alpha1.GroupAliasSpec{},
)

var handlerLog = ctrl.Log.WithName("eventhandler")

type Log struct {
predicate.Funcs
}

func (Log) Update(evt event.UpdateEvent) bool {
return LogEventWithDiff("UpdateEvent", evt.ObjectOld, evt.ObjectNew)
}

func (Log) Create(evt event.CreateEvent) bool {
return LogEvent("CreateEvent", evt.Object, evt)
}

func (Log) Delete(evt event.DeleteEvent) bool {
return LogEvent("DeleteEvent", evt.Object, evt)
}

func (Log) Generic(evt event.GenericEvent) bool {
return LogEvent("GenericEvent", evt.Object, evt)
}

func LogEvent(eventName string, object client.Object, evt interface{}) bool {
handlerLog.V(1).Info(eventName+" received", "namespace", object.GetNamespace(), "name", object.GetName(), "event", evt)
return true
}

func LogEventWithDiff(eventName string, objectOld client.Object, objectNew client.Object) bool {
if handlerLog.V(1).Enabled() {
switch {
case objectNew != nil:
handlerLog.V(1).Info(eventName+" received", "namespace", objectNew.GetNamespace(), "name", objectNew.GetName(),
"diff", cmp.Diff(objectOld, objectNew, ignoredUnexportedDuringDiff))
case objectOld != nil:
handlerLog.V(1).Info(eventName+" received", "namespace", objectNew.GetNamespace(), "name", objectNew.GetName(),
"diff", cmp.Diff(objectOld, objectNew, ignoredUnexportedDuringDiff))
}
}
return true
}
4 changes: 3 additions & 1 deletion controllers/kubernetesauthengineconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

redhatcopv1alpha1 "github.com/redhat-cop/vault-config-operator/api/v1alpha1"
"github.com/redhat-cop/vault-config-operator/controllers/k8sevt"
"github.com/redhat-cop/vault-config-operator/controllers/vaultresourcecontroller"
)

Expand Down Expand Up @@ -84,6 +85,7 @@ func (r *KubernetesAuthEngineConfigReconciler) Reconcile(ctx context.Context, re
func (r *KubernetesAuthEngineConfigReconciler) SetupWithManager(mgr ctrl.Manager) error {

return ctrl.NewControllerManagedBy(mgr).
For(&redhatcopv1alpha1.KubernetesAuthEngineConfig{}, builder.WithPredicates(vaultresourcecontroller.ResourceGenerationChangedPredicate{})).
For(&redhatcopv1alpha1.KubernetesAuthEngineConfig{},
builder.WithPredicates(vaultresourcecontroller.ResourceGenerationChangedPredicate{}, k8sevt.Log{})).
Complete(r)
}
Loading

0 comments on commit a56708a

Please sign in to comment.