Skip to content

Commit

Permalink
Setup basic watch of ResourceSets from Tenant controller and better s…
Browse files Browse the repository at this point in the history
…ha256 comparison.
  • Loading branch information
kristofferahl committed Jan 27, 2022
1 parent 33356de commit edd3fdd
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
2 changes: 1 addition & 1 deletion controllers/core/resourceset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (r *ResourceSetReconciler) updateStatus(ctx reconcile.Context, resourceSet
rs.Status.ObservedGeneration = rs.GetGeneration()
rs.Status.ResourceVersion = rs.GetResourceVersion()

if util.AsSha256(resourceSet.Status) != util.AsSha256(rs.Status) {
if eq, err := util.Sha256Equal(resourceSet.Status, rs.Status); err != nil || !eq {
ctx.Log.V(1).Info("updating ResourceSet status")
if err := r.Status().Update(ctx.Context, &rs); err != nil {
ctx.Log.Error(err, "failed to update ResourceSet status")
Expand Down
33 changes: 33 additions & 0 deletions controllers/core/tenant_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
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/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
kreconcile "sigs.k8s.io/controller-runtime/pkg/reconcile" // Required for Watching
"sigs.k8s.io/controller-runtime/pkg/source"

corev1alpha1 "github.com/kristofferahl/aeto/apis/core/v1alpha1"
"github.com/kristofferahl/aeto/internal/pkg/config"
Expand Down Expand Up @@ -446,5 +451,33 @@ func (r *TenantReconciler) updateStatus(ctx reconcile.Context, tenant corev1alph
func (r *TenantReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&corev1alpha1.Tenant{}).
Watches(
&source.Kind{Type: &corev1alpha1.ResourceSet{}},
handler.EnqueueRequestsFromMapFunc(r.findTenantForResourceSet),
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
).
Complete(r)
}

func (r *TenantReconciler) findTenantForResourceSet(resourceSet client.Object) []kreconcile.Request {
tenantList := &corev1alpha1.TenantList{}
// TODO: Implement paging
// TODO: Allow operator to define namespaces to watch for tenants
options := &client.ListOptions{} // Fetch from all namespaces
err := r.List(context.TODO(), tenantList, options)
if err != nil {
return []kreconcile.Request{}
}

requests := make([]kreconcile.Request, 0)
for _, item := range tenantList.Items {
requests = append(requests, kreconcile.Request{
NamespacedName: types.NamespacedName{
Name: item.GetName(),
Namespace: item.GetNamespace(),
},
})

}
return requests
}
24 changes: 20 additions & 4 deletions internal/pkg/util/sha256.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,27 @@ package util

import (
"crypto/sha256"
"encoding/json"
"fmt"
)

func AsSha256(o interface{}) string {
h := sha256.New()
h.Write([]byte(fmt.Sprintf("%v", o)))
return fmt.Sprintf("%x", h.Sum(nil))
func AsSha256(o interface{}) (string, error) {
b, err := json.Marshal(o)
if err != nil {
return "", err
}
sum := sha256.Sum256(b)
return fmt.Sprintf("%x", sum), nil
}

func Sha256Equal(a, b interface{}) (bool, error) {
aSum, err := AsSha256(a)
if err != nil {
return false, err
}
bSum, err := AsSha256(b)
if err != nil {
return false, err
}
return aSum == bSum, nil
}

0 comments on commit edd3fdd

Please sign in to comment.