From f7e32567bbef8e843e190b3c594b4c0727d1eb1e Mon Sep 17 00:00:00 2001 From: Angel Misevski Date: Tue, 19 Oct 2021 15:45:28 -0400 Subject: [PATCH] Improve diff logging when updating items Use diffOpts when printing spec vs cluster object diffs when updates are required. Signed-off-by: Angel Misevski --- pkg/provision/sync/cluster_api.go | 1 + pkg/provision/sync/sync.go | 43 +++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/pkg/provision/sync/cluster_api.go b/pkg/provision/sync/cluster_api.go index 88f82f5f9..a8a7bbdf9 100644 --- a/pkg/provision/sync/cluster_api.go +++ b/pkg/provision/sync/cluster_api.go @@ -41,6 +41,7 @@ type NotInSyncReason string const ( UpdatedObjectReason NotInSyncReason = "Updated object" CreatedObjectReason NotInSyncReason = "Created object" + DeletedObjectReason NotInSyncReason = "Deleted object" NeedRetryReason NotInSyncReason = "Need to retry" ) diff --git a/pkg/provision/sync/sync.go b/pkg/provision/sync/sync.go index a09b49958..8fdb4b1a9 100644 --- a/pkg/provision/sync/sync.go +++ b/pkg/provision/sync/sync.go @@ -17,9 +17,15 @@ import ( "fmt" "reflect" + "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1" "github.com/devfile/devworkspace-operator/pkg/config" + "github.com/go-logr/logr" "github.com/google/go-cmp/cmp" + routev1 "github.com/openshift/api/route/v1" + appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" + networkingv1 "k8s.io/api/networking/v1" + rbacv1 "k8s.io/api/rbac/v1" k8sErrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" crclient "sigs.k8s.io/controller-runtime/pkg/client" @@ -51,20 +57,16 @@ func SyncObjectWithCluster(specObj crclient.Object, api ClusterAPI) (crclient.Ob } shouldDelete, shouldUpdate := diffFunc(specObj, clusterObj) if shouldDelete { - if config.ExperimentalFeaturesEnabled() { - api.Logger.Info(fmt.Sprintf("Diff: %s", cmp.Diff(specObj, clusterObj))) - } + printDiff(specObj, clusterObj, api.Logger) err := api.Client.Delete(api.Ctx, specObj) if err != nil { return nil, err } api.Logger.Info("Deleted object", "kind", objType.String(), "name", specObj.GetName()) - return nil, NewNotInSync(specObj, UpdatedObjectReason) + return nil, NewNotInSync(specObj, DeletedObjectReason) } if shouldUpdate { - if config.ExperimentalFeaturesEnabled() { - api.Logger.Info(fmt.Sprintf("Diff: %s", cmp.Diff(specObj, clusterObj))) - } + printDiff(specObj, clusterObj, api.Logger) return nil, updateObjectGeneric(specObj, clusterObj, api) } return clusterObj, nil @@ -95,7 +97,7 @@ func updateObjectGeneric(specObj, clusterObj crclient.Object, api ClusterAPI) er return err } api.Logger.Info("Deleted object", "kind", reflect.TypeOf(specObj).Elem().String(), "name", specObj.GetName()) - return NewNotInSync(specObj, UpdatedObjectReason) + return NewNotInSync(specObj, DeletedObjectReason) } err = api.Client.Update(api.Ctx, updatedObj) @@ -121,3 +123,28 @@ func isMutableObject(obj crclient.Object) bool { return true } } + +func printDiff(specObj, clusterObj crclient.Object, log logr.Logger) { + if config.ExperimentalFeaturesEnabled() { + var diffOpts cmp.Options + switch specObj.(type) { + case *rbacv1.Role: + diffOpts = roleDiffOpts + case *rbacv1.RoleBinding: + diffOpts = rolebindingDiffOpts + case *appsv1.Deployment: + diffOpts = deploymentDiffOpts + case *corev1.ConfigMap: + diffOpts = configmapDiffOpts + case *v1alpha1.DevWorkspaceRouting: + diffOpts = routingDiffOpts + case *networkingv1.Ingress: + diffOpts = ingressDiffOpts + case *routev1.Route: + diffOpts = routeDiffOpts + default: + diffOpts = nil + } + log.Info(fmt.Sprintf("Diff: %s", cmp.Diff(specObj, clusterObj, diffOpts))) + } +}