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

fix: avoids memory leak in istio sidecar termination #972

Merged
merged 17 commits into from
Nov 6, 2024
Merged
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
37 changes: 26 additions & 11 deletions src/pepr/istio/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import { Exec, KubeConfig } from "@kubernetes/client-node";
import { Capability, a } from "pepr";
import { Readable } from "stream";
import { Component, setupLogger } from "../logger";

// configure subproject logger
Expand Down Expand Up @@ -49,16 +50,22 @@ When(a.Pod)
log.error(pod, `Invalid container status in Pod`);
return;
}
const shouldTerminate = pod.status.containerStatuses
// Ignore the istio-proxy container
.filter(c => c.name != "istio-proxy")
// and if ALL are terminated AND restartPolicy is Never or is OnFailure with a 0 exit code then shouldTerminate is true
.every(
c =>
c.state?.terminated &&
(pod.spec?.restartPolicy == "Never" ||
(pod.spec?.restartPolicy == "OnFailure" && c.state.terminated.exitCode == 0)),

// if ALL (non istio-proxy) are terminated AND restartPolicy is Never
// or is OnFailure with a 0 exit code
// and istio-proxy is not already terminated then shouldTerminate is true
const shouldTerminate = pod.status.containerStatuses.every(c => {
// handle scenario where proxy was already terminated
if (c.name == "istio-proxy") {
return c.state?.terminated == undefined;
}

return (
c.state?.terminated &&
(pod.spec?.restartPolicy == "Never" ||
(pod.spec?.restartPolicy == "OnFailure" && c.state.terminated.exitCode == 0))
);
});

if (shouldTerminate) {
// Mark the pod as seen
Expand All @@ -70,15 +77,23 @@ When(a.Pod)
kc.loadFromDefault();
const exec = new Exec(kc);

// Trying to avoid passing in process.stdin (this stream read is a no-op)
// The exec call fails with null stdin stream
const dummyStream = new Readable({
read() {
this.push(null);
},
});

await exec.exec(
namespace,
name,
"istio-proxy",
["pilot-agent", "request", "POST", "/quitquitquit"],
null, // Could capture exec stdout here
null, // Could capture exec stderr here
process.stdin,
true,
dummyStream,
false,
);

log.info(`Terminated sidecar for ${key}`);
Expand Down