Skip to content

Commit

Permalink
chore: more logging around watch (#748)
Browse files Browse the repository at this point in the history
## Description

Adds debug logging to Pepr around every single watch event that may
occur.

## Related Issue

Fixes #747 
<!-- or -->
Relates to #

## Type of change

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

## Checklist before merging

- [x] Test, docs, adr added or updated as needed
- [x] [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 Apr 18, 2024
1 parent c1d1475 commit dda83ad
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/lib/watch-processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ import { GenericClass, K8s, KubernetesObject, kind } from "kubernetes-fluent-cli
import { K8sInit, WatchPhase } from "kubernetes-fluent-client/dist/fluent/types";
import { WatchCfg, WatchEvent, Watcher } from "kubernetes-fluent-client/dist/fluent/watch";
import { Capability } from "./capability";
import { setupWatch } from "./watch-processor";
import { setupWatch, logEvent } from "./watch-processor";
import Log from "./logger";

type onCallback = (eventName: string | symbol, listener: (msg: string) => void) => void;

// Mock the dependencies
jest.mock("kubernetes-fluent-client");

jest.mock("./logger", () => ({
debug: jest.fn(),
error: jest.fn(),
}));

describe("WatchProcessor", () => {
const mockStart = jest.fn();
const mockK8s = jest.mocked(K8s);
Expand Down Expand Up @@ -206,3 +212,30 @@ describe("WatchProcessor", () => {
expect(watchCallbackUpdate).toHaveBeenCalledTimes(0);
});
});

describe("logEvent function", () => {
it("should handle data events", () => {
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);
});

it("should handle CONNECT events", () => {
logEvent(WatchEvent.CONNECT);
expect(Log.debug).toHaveBeenCalledWith(`Watch event ${WatchEvent.CONNECT} received`, "");
});

it("should handle BOOKMARK events", () => {
const mockObj = { id: "123", type: "Pod" } as KubernetesObject;
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);
});

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);
});
});
28 changes: 28 additions & 0 deletions src/lib/watch-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,26 @@ async function runBinding(binding: Binding, capabilityNamespaces: string[]) {
process.exit(1);
});

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

watcher.events.on(WatchEvent.BOOKMARK, 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}`),
);
watcher.events.on(WatchEvent.RECONNECT, (err, retryCount) =>
logEvent(WatchEvent.RECONNECT, `Reconnecting after ${retryCount} attempts`, err),
);
watcher.events.on(WatchEvent.RECONNECT_PENDING, () => logEvent(WatchEvent.RECONNECT_PENDING));
watcher.events.on(WatchEvent.GIVE_UP, err => logEvent(WatchEvent.GIVE_UP, err.message));
watcher.events.on(WatchEvent.ABORT, err => logEvent(WatchEvent.ABORT, err.message));
watcher.events.on(WatchEvent.OLD_RESOURCE_VERSION, err => logEvent(WatchEvent.OLD_RESOURCE_VERSION, err));
watcher.events.on(WatchEvent.RESYNC, err => logEvent(WatchEvent.RESYNC, err.message));
watcher.events.on(WatchEvent.NETWORK_ERROR, err => logEvent(WatchEvent.NETWORK_ERROR, err.message));

// Start the watch
try {
await watcher.start();
Expand All @@ -95,3 +115,11 @@ async function runBinding(binding: Binding, capabilityNamespaces: string[]) {
process.exit(1);
}
}

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

0 comments on commit dda83ad

Please sign in to comment.