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 checks before killing pods when updating istio annotations #457

Merged
merged 3 commits into from
Jun 5, 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
20 changes: 14 additions & 6 deletions src/pepr/operator/controllers/istio/injection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ export async function enableInjection(pkg: UDSPackage) {

const sourceNS = await K8s(kind.Namespace).Get(pkg.metadata.namespace);
const labels = sourceNS.metadata?.labels || {};
const originalInjectionLabel = labels[injectionLabel];
const annotations = sourceNS.metadata?.annotations || {};
const pkgKey = `uds.dev/pkg-${pkg.metadata.name}`;

// Mark the original namespace injection setting for if all packages are removed
if (!annotations[injectionAnnotation]) {
annotations[injectionAnnotation] = labels[injectionLabel] || "non-existent";
annotations[injectionAnnotation] = originalInjectionLabel || "non-existent";
}

// Ensure the namespace is configured
if (!annotations[pkgKey] || labels[injectionLabel] !== "enabled") {
if (!annotations[pkgKey] || originalInjectionLabel !== "enabled") {
// Ensure Istio injection is enabled
labels[injectionLabel] = "enabled";

Expand All @@ -45,7 +46,10 @@ export async function enableInjection(pkg: UDSPackage) {
{ force: true },
);

await killPods(pkg.metadata.namespace, true);
// Kill the pods if we changed the value of the istio-injection label
if (originalInjectionLabel !== labels[injectionLabel]) {
await killPods(pkg.metadata.namespace, true);
}
}
}

Expand All @@ -61,6 +65,7 @@ export async function cleanupNamespace(pkg: UDSPackage) {

const sourceNS = await K8s(kind.Namespace).Get(pkg.metadata.namespace);
const labels = sourceNS.metadata?.labels || {};
const originalInjectionLabel = labels[injectionLabel];
const annotations = sourceNS.metadata?.annotations || {};

// Remove the package annotation
Expand Down Expand Up @@ -88,7 +93,10 @@ export async function cleanupNamespace(pkg: UDSPackage) {
{ force: true },
);

await killPods(pkg.metadata.namespace, false);
// Kill the pods if we changed the value of the istio-injection label
if (originalInjectionLabel !== labels[injectionLabel]) {
await killPods(pkg.metadata.namespace, false);
}
}

/**
Expand Down Expand Up @@ -129,8 +137,8 @@ async function killPods(ns: string, enableInjection: boolean) {

// Delete each group of pods
for (const group of Object.values(groups)) {
// If this is a daemonset, delete the pods in reverse name order
if (group[0].metadata?.ownerReferences?.find(ref => ref.kind === "DaemonSet")) {
// If this is a statefulset, delete the pods in reverse name order
if (group[0].metadata?.ownerReferences?.find(ref => ref.kind === "StatefulSet")) {
group.sort((a, b) => (b.metadata?.name || "").localeCompare(a.metadata?.name || ""));
}

Expand Down