From c0ea0d8c4e4c3375ff0765c980952c3cbb064df9 Mon Sep 17 00:00:00 2001 From: Aditya Choudhari <48932219+adityachoudhari26@users.noreply.github.com> Date: Tue, 3 Sep 2024 12:57:56 -0700 Subject: [PATCH] fix: Log the diff of specs (#23) --- 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 b496e4c..5a15a06 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 ca66ca6..7117493 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 +}