From 8512b18b2acdef87185a0150d33088488a568e44 Mon Sep 17 00:00:00 2001 From: Aditya Choudhari Date: Tue, 3 Sep 2024 12:54:55 -0700 Subject: [PATCH] fix: Log the diff of specs --- controllers/weightsandbiases_controller.go | 3 ++ pkg/wandb/spec/spec.go | 35 ++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/controllers/weightsandbiases_controller.go b/controllers/weightsandbiases_controller.go index b496e4c1..5a15a066 100644 --- a/controllers/weightsandbiases_controller.go +++ b/controllers/weightsandbiases_controller.go @@ -190,6 +190,9 @@ func (r *WeightsAndBiasesReconciler) Reconcile(ctx context.Context, req ctrl.Req log.Info("No changes found") statusManager.Set(status.Completed) return ctrlqueue.Requeue(desiredSpec) + } else { + diff := currentActiveSpec.DiffValues(desiredSpec) + log.Info("Changes found", "diff", diff) } } diff --git a/pkg/wandb/spec/spec.go b/pkg/wandb/spec/spec.go index ca66ca6d..7117493c 100644 --- a/pkg/wandb/spec/spec.go +++ b/pkg/wandb/spec/spec.go @@ -155,3 +155,38 @@ func maskValues(values map[string]interface{}) map[string]interface{} { } return newValues } + +func (s *Spec) DiffValues(other *Spec) map[string]interface{} { + return diffMaps(s.Values, other.Values) +} + +func diffMaps(a, b map[string]interface{}) map[string]interface{} { + diff := make(map[string]interface{}) + for key, aValue := range a { + if bValue, ok := b[key]; ok { + if !reflect.DeepEqual(aValue, bValue) { + switch aValue.(type) { + case map[string]interface{}: + if bMap, ok := bValue.(map[string]interface{}); ok { + nestedDiff := diffMaps(aValue.(map[string]interface{}), bMap) + if len(nestedDiff) > 0 { + diff[key] = nestedDiff + } + } else { + diff[key] = aValue + } + default: + diff[key] = aValue + } + } + } else { + diff[key] = aValue + } + } + for key, bValue := range b { + if _, ok := a[key]; !ok { + diff[key] = bValue + } + } + return diff +}