Skip to content

Commit

Permalink
Improve diff logging when updating items
Browse files Browse the repository at this point in the history
Use diffOpts when printing spec vs cluster object diffs when updates are
required.

Signed-off-by: Angel Misevski <[email protected]>
  • Loading branch information
amisevsk committed Oct 21, 2021
1 parent 76d43d7 commit 6c83e0a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions pkg/provision/sync/cluster_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
43 changes: 35 additions & 8 deletions pkg/provision/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)))
}
}

0 comments on commit 6c83e0a

Please sign in to comment.