Skip to content

Commit

Permalink
Merge pull request #21 from mittwald/fix/kubernetesClientLogging
Browse files Browse the repository at this point in the history
implement default debug function with debug package
  • Loading branch information
martin-helmich authored May 28, 2019
2 parents ba29ed0 + 863e1c5 commit 069723c
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {LabelSelector, labelSelectorToQueryString} from "./label";
import {isStatus, MetadataObject} from "./types/meta";
import {WatchEvent} from "./types/meta/v1";

const debug = require("debug")("k8s:client");
const debug = require("debug")("kubernetes:client");

export type RequestMethod = "GET"|"POST"|"PUT"|"PATCH"|"DELETE";

Expand All @@ -13,7 +13,7 @@ export interface IKubernetesRESTClientOptions {
}

const defaultRESTClientOptions: IKubernetesRESTClientOptions = {
debugFn: () => { return; },
debugFn: (msg: string) => debug(msg),
};

export interface WatchOptions {
Expand Down Expand Up @@ -139,42 +139,39 @@ export class KubernetesRESTClient implements IKubernetesRESTClient {
if (bodyString.length === 0) {
this.opts.debugFn(`WATCH request on ${url} returned empty response`);
res({resourceVersion: lastVersion});
return;
}

let body: any;

try {
body = JSON.parse(bodyString);
} catch (err) {
if (bodyString.length > 0) {
const bodyLines = bodyString.split("\n");
for (const line of bodyLines) {
try {
const parsedLine: WatchEvent<R> = JSON.parse(line);
if (parsedLine.type === "ADDED" || parsedLine.type === "MODIFIED" || parsedLine.type === "DELETED") {
const resourceVersion = parseInt(parsedLine.object.metadata.resourceVersion || "0", 10);
if (resourceVersion > lastVersion) {
this.opts.debugFn(`watch: emitting missed ${parsedLine.type} event for ${parsedLine.object.metadata.name}`);

lastVersion = resourceVersion;
onUpdate(parsedLine);
}
const bodyLines = bodyString.split("\n");
for (const line of bodyLines) {
try {
const parsedLine: WatchEvent<R> = JSON.parse(line);
if (parsedLine.type === "ADDED" || parsedLine.type === "MODIFIED" || parsedLine.type === "DELETED") {
const resourceVersion = parseInt(parsedLine.object.metadata.resourceVersion || "0", 10);
if (resourceVersion > lastVersion) {
this.opts.debugFn(`watch: emitting missed ${parsedLine.type} event for ${parsedLine.object.metadata.name}`);

lastVersion = resourceVersion;
onUpdate(parsedLine);
}
} catch (err) {
this.opts.debugFn(`watch: could not parse JSON line '${line}'`);
rej(err);
}
} catch (err) {
this.opts.debugFn(`watch: could not parse JSON line '${line}'`);
rej(err);
return;
}

res({resourceVersion: lastVersion});
return;
}

rej(err);
res({resourceVersion: lastVersion});
return;
}

if (isStatus(body) && body.status === "Failure") {
this.opts.debugFn(`watch: failed with status ${body}`)
rej(body.message);
return;
}
Expand Down

0 comments on commit 069723c

Please sign in to comment.