Skip to content

Commit

Permalink
fix: Add informative logging to targets
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 committed Sep 1, 2024
1 parent cbdac26 commit 21e8383
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
11 changes: 9 additions & 2 deletions apps/event-worker/src/target-scan/gke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,18 @@ export const getGkeTargets = async (
googleServiceAccountEmail,
);

if (!googleClusterClient) return [];

const clusters = (
await Promise.allSettled(
config.projectIds.map(async (project) => {
const clusters = await getClusters(googleClusterClient, project);
return { project, clusters };
try {
const clusters = await getClusters(googleClusterClient, project);
return { project, clusters };
} catch (e) {
log.error("error getting clusters");
return { project, clusters: [] };
}
}),
)
)
Expand Down
42 changes: 28 additions & 14 deletions apps/event-worker/src/target-scan/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,40 @@ import { KubeConfig } from "@kubernetes/client-node";
import { GoogleAuth, Impersonated } from "google-auth-library";
import { SemVer } from "semver";

import { logger } from "@ctrlplane/logger";

import { omitNullUndefined } from "../utils.js";

const sourceCredentials = new GoogleAuth({
scopes: ["https://www.googleapis.com/auth/cloud-platform"],
});

export const getGoogleClusterClient = async (targetPrincipal?: string | null) =>
new Container.v1.ClusterManagerClient({
authClient: new Impersonated(
targetPrincipal != null
? {
sourceClient: await sourceCredentials.getClient(),
targetPrincipal,
lifetime: 3600, // Token lifetime in seconds
delegates: [],
targetScopes: ["https://www.googleapis.com/auth/cloud-platform"],
}
: {},
),
});
const log = logger.child({ label: "target-scan/gke" });

export const getGoogleClusterClient = async (
targetPrincipal?: string | null,
) => {
try {
const sourceClient = await sourceCredentials.getClient();
log.info("got source client");
const impersonated = new Impersonated({
sourceClient,
targetPrincipal: targetPrincipal ?? undefined,
lifetime: 3600,
delegates: [],
targetScopes: ["https://www.googleapis.com/auth/cloud-platform"],
});
log.info("got impersonated");

const clusterClient = new Container.v1.ClusterManagerClient({
authClient: impersonated,
});
log.info("got cluster client");
return clusterClient;
} catch (e) {
log.error(e);
}
};

export const getClusters = async (
clusterClient: ClusterManagerClient,
Expand Down

0 comments on commit 21e8383

Please sign in to comment.