Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions pkg/cmp/cmp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cmp

import (
"fmt"
"strings"

"github.com/google/go-cmp/cmp"
)

// DiffReporter is a simple custom reporter that only records differences
// detected during comparison.
type DiffReporter struct {
path cmp.Path
diffs []string
}

// PushStep ...
func (r *DiffReporter) PushStep(ps cmp.PathStep) {
r.path = append(r.path, ps)
}

// Report ...
func (r *DiffReporter) Report(rs cmp.Result) {
if !rs.Equal() {
vx, vy := r.path.Last().Values()
r.diffs = append(r.diffs, fmt.Sprintf("%#v:\n\t-: %+v\n\t+: %+v\n", r.path, vx, vy))
}
}

// PopStep ...
func (r *DiffReporter) PopStep() {
r.path = r.path[:len(r.path)-1]
}

// String ...
func (r *DiffReporter) String() string {
return strings.Join(r.diffs, "\n")
}

// Diff ...
func Diff(x, y interface{}) string {
var r DiffReporter
cmp.Equal(x, y, cmp.Reporter(&r))
return r.String()
}
7 changes: 6 additions & 1 deletion pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/Sirupsen/logrus"
"github.com/bitnami-labs/kubewatch/config"
"github.com/bitnami-labs/kubewatch/pkg/cmp"
"github.com/bitnami-labs/kubewatch/pkg/event"
"github.com/bitnami-labs/kubewatch/pkg/handlers"
"github.com/bitnami-labs/kubewatch/pkg/utils"
Expand Down Expand Up @@ -62,6 +63,7 @@ type Event struct {
eventType string
namespace string
resourceType string
extras string
}

// Controller object
Expand Down Expand Up @@ -539,7 +541,9 @@ func newResourceController(client kubernetes.Interface, eventHandler handlers.Ha
newEvent.eventType = "update"
newEvent.resourceType = resourceType
newEvent.namespace = utils.GetObjectMetaData(new).Namespace
logrus.WithField("pkg", "kubewatch-"+resourceType).Infof("Processing update to %v: %s", resourceType, newEvent.key)
// using extras filed to send the diff occuring in update events
newEvent.extras = cmp.Diff(old,new)
logrus.WithField("pkg", "kubewatch-"+resourceType).Infof("Processing update to %v: %s", resourceType, newEvent.key)
if err == nil {
queue.Add(newEvent)
}
Expand Down Expand Up @@ -704,6 +708,7 @@ func (c *Controller) processItem(newEvent Event) error {
Kind: newEvent.resourceType,
Status: status,
Reason: "Updated",
Extras: newEvent.extras,
}
if _, ok := global[newEvent.resourceType]; ok {
c.eventHandler.ObjectUpdated(obj, kbEvent)
Expand Down
6 changes: 6 additions & 0 deletions pkg/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Event struct {
Reason string
Status string
Name string
Extras string
}

// Message returns event message in standard format.
Expand Down Expand Up @@ -84,5 +85,10 @@ func (e *Event) Message() (msg string) {
e.Name,
)
}

if e.Reason == "Updated" {
msg = fmt.Sprintf("%s\n```%s```", msg, e.Extras)
}

return msg
}