Skip to content

Commit

Permalink
chore: add more visibility into watch (#791)
Browse files Browse the repository at this point in the history
## Description

Enhances the log function to capture messages and set the
allowWatchBookmarks to be true by default.

Also updates the resync message, in this case message is redudant
```bash
[10:47:20.788] DEBUG (Resync/97227): Watch event reconnect received. Reconnecting after 1 attempts.
    message: "Resync triggered by resyncIntervalSec"
```

## Related Issue

Fixes #790 
Fixes #789 
<!-- or -->
Depends on
defenseunicorns/kubernetes-fluent-client#244



## Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Other (security config, docs update, etc)

## Checklist before merging

- [ ] Test, docs, adr added or updated as needed
- [ ] [Contributor Guide
Steps](https://docs.pepr.dev/main/contribute/contributor-guide/#submitting-a-pull-request)
followed

---------

Signed-off-by: Case Wylie <cmwylie19@defenseunicorns.com>
  • Loading branch information
cmwylie19 committed May 13, 2024
1 parent d58515a commit e93d211
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
13 changes: 7 additions & 6 deletions src/lib/watch-processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,24 +218,25 @@ describe("logEvent function", () => {
const mockObj = { id: "123", type: "Pod" } as KubernetesObject;
const message = "Test message";
logEvent(WatchEvent.DATA, message, mockObj);
expect(Log.debug).toHaveBeenCalledWith(mockObj, `Watch event ${WatchEvent.DATA} received`, message);
expect(Log.debug).toHaveBeenCalledWith(mockObj, `Watch event ${WatchEvent.DATA} received. ${message}.`);
});

it("should handle CONNECT events", () => {
logEvent(WatchEvent.CONNECT);
expect(Log.debug).toHaveBeenCalledWith(`Watch event ${WatchEvent.CONNECT} received`, "");
const url = "/api/v1/namespaces/default/pods?watch=true&resourceVersion=0";
logEvent(WatchEvent.CONNECT, url);
expect(Log.debug).toHaveBeenCalledWith(`Watch event ${WatchEvent.CONNECT} received. ${url}.`);
});

it("should handle BOOKMARK events", () => {
const mockObj = { id: "123", type: "Pod" } as KubernetesObject;
const message = "Changes up to the given resourceVersion have been sent.";
const message = "Changes up to the given resourceVersion have been sent";
logEvent(WatchEvent.BOOKMARK, message, mockObj);
expect(Log.debug).toHaveBeenCalledWith(mockObj, `Watch event ${WatchEvent.BOOKMARK} received`, message);
expect(Log.debug).toHaveBeenCalledWith(mockObj, `Watch event ${WatchEvent.BOOKMARK} received. ${message}.`);
});

it("should handle DATA_ERROR events", () => {
const message = "Test message";
logEvent(WatchEvent.DATA_ERROR, message);
expect(Log.debug).toHaveBeenCalledWith(`Watch event ${WatchEvent.DATA_ERROR} received`, message);
expect(Log.debug).toHaveBeenCalledWith(`Watch event ${WatchEvent.DATA_ERROR} received. ${message}.`);
});
});
15 changes: 8 additions & 7 deletions src/lib/watch-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const watchCfg: WatchCfg = {
resyncIntervalSec: process.env.PEPR_RESYNCINTERVALSECONDS
? parseInt(process.env.PEPR_RESYNCINTERVALSECONDS, 10)
: 300,
allowWatchBookmarks: process.env.PEPR_ALLOWWATCHBOOKMARKS ? process.env.PEPR_ALLOWWATCHBOOKMARKS === "true" : false,
allowWatchBookmarks: process.env.PEPR_ALLOWWATCHBOOKMARKS === "false" ? false : true,
};

// Map the event to the watch phase
Expand Down Expand Up @@ -93,18 +93,18 @@ async function runBinding(binding: Binding, capabilityNamespaces: string[]) {
process.exit(1);
});

watcher.events.on(WatchEvent.CONNECT, () => logEvent(WatchEvent.CONNECT));
watcher.events.on(WatchEvent.CONNECT, url => logEvent(WatchEvent.CONNECT, url));

watcher.events.on(WatchEvent.BOOKMARK, obj =>
logEvent(WatchEvent.BOOKMARK, "Changes up to the given resourceVersion have been sent.", obj),
logEvent(WatchEvent.BOOKMARK, "Changes up to the given resourceVersion have been sent", obj),
);

watcher.events.on(WatchEvent.DATA_ERROR, err => logEvent(WatchEvent.DATA_ERROR, err.message));
watcher.events.on(WatchEvent.RESOURCE_VERSION, resourceVersion =>
logEvent(WatchEvent.RESOURCE_VERSION, `Resource version: ${resourceVersion}`),
logEvent(WatchEvent.RESOURCE_VERSION, `${resourceVersion}`),
);
watcher.events.on(WatchEvent.RECONNECT, (err, retryCount) =>
logEvent(WatchEvent.RECONNECT, `Reconnecting after ${retryCount} attempts`, err),
logEvent(WatchEvent.RECONNECT, err ? `Reconnecting after ${retryCount} attempts` : ""),
);
watcher.events.on(WatchEvent.RECONNECT_PENDING, () => logEvent(WatchEvent.RECONNECT_PENDING));
watcher.events.on(WatchEvent.GIVE_UP, err => logEvent(WatchEvent.GIVE_UP, err.message));
Expand All @@ -123,9 +123,10 @@ async function runBinding(binding: Binding, capabilityNamespaces: string[]) {
}

export function logEvent(type: WatchEvent, message: string = "", obj?: KubernetesObject) {
const logMessage = `Watch event ${type} received${message ? `. ${message}.` : "."}`;
if (obj) {
Log.debug(obj, `Watch event ${type} received`, message);
Log.debug(obj, logMessage);
} else {
Log.debug(`Watch event ${type} received`, message);
Log.debug(logMessage);
}
}

0 comments on commit e93d211

Please sign in to comment.