Skip to content

Commit

Permalink
add google cloud gke sync
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbroks committed Aug 26, 2024
1 parent 465e3a2 commit 00b24d1
Show file tree
Hide file tree
Showing 15 changed files with 287 additions and 108 deletions.
3 changes: 2 additions & 1 deletion packages/validators/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import baseConfig from "@ctrlplane/eslint-config/base";
import baseConfig, { requireJsSuffix } from "@ctrlplane/eslint-config/base";

/** @type {import('typescript-eslint').Config} */
export default [
{
ignores: ["dist/**"],
},
...requireJsSuffix,
...baseConfig,
];
8 changes: 6 additions & 2 deletions packages/validators/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./src/index.ts"
"types": "./src/index.ts",
"default": "./dist/index.js"
},
"./targets": {
"types": "./src/targets/index.ts",
"default": "./dist/targets/index.js"
}
},
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/validators/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./config-file";
export * from "./config-file.js";
20 changes: 0 additions & 20 deletions packages/validators/src/targets/application/v1/instance.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/validators/src/targets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./kubernetes-v1.js";
42 changes: 42 additions & 0 deletions packages/validators/src/targets/kubernetes-v1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { z } from "zod";

const clusterConfig = z.object({
name: z.string(),
server: z.object({
certificateAuthorityData: z.string(),
endpoint: z.string().url(),
}),
});
export const kubernetesClusterApiV1 = z.object({
version: z.literal("kubernetes/v1"),
kind: z.literal("ClusterAPI"),
identifier: z.string(),
name: z.string(),
config: clusterConfig,
labels: z.record(z.string()).and(
z
.object({
"kubernetes/version": z.string(),
"kubernetes/distribution": z.string(),
"kubernetes/master-version": z.string(),
"kubernetes/master-version-major": z.string(),
"kubernetes/master-version-minor": z.string(),
"kubernetes/master-version-patch": z.string(),
"kubernetes/autoscaling-enabled": z.string().optional(),
})
.partial(),
),
});

export type KubernetesClusterAPIV1 = z.infer<typeof kubernetesClusterApiV1>;

export const kubernetesNamespaceV1 = z.object({
version: z.literal("kubernetes/v1"),
kind: z.literal("Namespace"),
identifier: z.string(),
name: z.string(),
config: clusterConfig.and(z.object({ namespace: z.string() })),
labels: z.record(z.string()).and(z.object({}).partial()),
});

export type KubernetesNamespaceV1 = z.infer<typeof kubernetesNamespaceV1>;
24 changes: 0 additions & 24 deletions packages/validators/src/targets/kubernetes/v1/cluster.ts

This file was deleted.

24 changes: 0 additions & 24 deletions packages/validators/src/targets/salesforce/v1/account.ts

This file was deleted.

32 changes: 0 additions & 32 deletions packages/validators/src/targets/terraform/v1/workspace.ts

This file was deleted.

26 changes: 22 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions providers/google-cloud/compute-scanner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@
"dependencies": {
"@ctrlplane/logger": "workspace:*",
"@ctrlplane/node-sdk": "workspace:*",
"@ctrlplane/validators": "workspace:*",
"@google-cloud/container": "^5.16.0",
"@kubernetes/client-node": "^0.21.0",
"@t3-oss/env-core": "^0.10.1",
"cron": "^3.1.7",
"dotenv": "^16.4.5",
"google-auth-library": "^9.13.0",
"handlebars": "^4.7.8",
"lodash": "^4.17.21",
"p-retry": "^6.2.0",
"semver": "^7.6.2",
"zod": "catalog:"
Expand All @@ -34,6 +38,7 @@
"@ctrlplane/eslint-config": "workspace:*",
"@ctrlplane/prettier-config": "workspace:*",
"@ctrlplane/tsconfig": "workspace:*",
"@types/lodash": "^4.17.5",
"eslint": "catalog:",
"prettier": "catalog:",
"typescript": "^5.4.5"
Expand Down
43 changes: 43 additions & 0 deletions providers/google-cloud/compute-scanner/src/gke-connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { ClusterManagerClient } from "@google-cloud/container";
import { KubeConfig } from "@kubernetes/client-node";
import { GoogleAuth } from "google-auth-library";

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

export const connectToCluster = async (
clusterClient: ClusterManagerClient,
project: string,
clusterName: string,
clusterLocation: string,
) => {
const [credentials] = await clusterClient.getCluster({
name: `projects/${project}/locations/${clusterLocation}/clusters/${clusterName}`,
});
const kubeConfig = new KubeConfig();
kubeConfig.loadFromOptions({
clusters: [
{
name: clusterName,
server: `https://${credentials.endpoint}`,
caData: credentials.masterAuth!.clusterCaCertificate!,
},
],
users: [
{
name: clusterName,
token: (await sourceCredentials.getAccessToken())!,
},
],
contexts: [
{
name: clusterName,
user: clusterName,
cluster: clusterName,
},
],
currentContext: clusterName,
});
return kubeConfig;
};
Loading

0 comments on commit 00b24d1

Please sign in to comment.