Skip to content

Commit

Permalink
Merge pull request #24 from patoarvizu/watch_changes_in_cr
Browse files Browse the repository at this point in the history
Watch for changes in Vault CR objects
  • Loading branch information
patoarvizu authored Mar 15, 2020
2 parents 95af3b6 + 8e9e6fa commit 5986aa2
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions pkg/controller/vdc/serviceaccount_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,23 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
return err
}

err = c.Watch(&source.Kind{
Type: &bankvaultsv1alpha1.Vault{}},
&handler.EnqueueRequestsFromMapFunc{
ToRequests: handler.ToRequestsFunc(func(h handler.MapObject) []reconcile.Request {
return getRequestsForAllAnnotatedServiceAccounts(mgr)
}),
},
)
if err != nil {
return err
}

err = c.Watch(&source.Kind{
Type: &corev1.ConfigMap{}},
&handler.EnqueueRequestsFromMapFunc{
ToRequests: handler.ToRequestsFunc(func(h handler.MapObject) []reconcile.Request {
namespaces := &corev1.NamespaceList{}
mgr.GetClient().List(context.TODO(), namespaces)
requests := []reconcile.Request{}
for _, ns := range namespaces.Items {
serviceAccounts := &corev1.ServiceAccountList{}
mgr.GetClient().List(context.TODO(), serviceAccounts, client.InNamespace(ns.ObjectMeta.Name))
for _, sa := range serviceAccounts.Items {
if val, ok := sa.ObjectMeta.Annotations[AutoConfigureAnnotation]; ok {
if val == "true" {
requests = append(requests, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: sa.ObjectMeta.Name,
Namespace: sa.ObjectMeta.Namespace,
},
})
}
}
}
}
return requests
return getRequestsForAllAnnotatedServiceAccounts(mgr)
}),
},
)
Expand All @@ -93,6 +86,29 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
return nil
}

func getRequestsForAllAnnotatedServiceAccounts(mgr manager.Manager) []reconcile.Request {
namespaces := &corev1.NamespaceList{}
mgr.GetClient().List(context.TODO(), namespaces)
requests := []reconcile.Request{}
for _, ns := range namespaces.Items {
serviceAccounts := &corev1.ServiceAccountList{}
mgr.GetClient().List(context.TODO(), serviceAccounts, client.InNamespace(ns.ObjectMeta.Name))
for _, sa := range serviceAccounts.Items {
if val, ok := sa.ObjectMeta.Annotations[AnnotationPrefix+"/"+AutoConfigureAnnotation]; ok {
if val == "true" {
requests = append(requests, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: sa.ObjectMeta.Name,
Namespace: sa.ObjectMeta.Namespace,
},
})
}
}
}
}
return requests
}

var _ reconcile.Reconciler = &ReconcileServiceAccount{}

type ReconcileServiceAccount struct {
Expand Down Expand Up @@ -292,13 +308,9 @@ func addOrUpdatePolicy(bvConfig *BankVaultsConfig, metadata metav1.ObjectMeta, c
Name: metadata.Name,
Namespace: metadata.Namespace,
})
for _, r := range bvConfig.Policies {
for i, r := range bvConfig.Policies {
if r.Name == metadata.Name {
existingPolicy, err := bvConfig.GetPolicy(metadata.Name)
if err != nil {
return err
}
existingPolicy.Rules = parsedBuffer.String()
bvConfig.Policies[i].Rules = parsedBuffer.String()
return nil
}
}
Expand Down

0 comments on commit 5986aa2

Please sign in to comment.