Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add more visibility into watch #791

Merged
merged 11 commits into from
May 13, 2024
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);
}
}
Loading