Skip to content

Commit

Permalink
feat(volumes): Add support for pre-seeding volumes
Browse files Browse the repository at this point in the history
* Volumes work end to end

* Fix

* Removed console.log

* Fixed fmt

* Fixed tag command.

* Fix based on comments

* Add gen import

* Add volume config

---------

Co-authored-by: Michael Muesch <[email protected]>
  • Loading branch information
mueschm and Michael Muesch authored Jun 21, 2023
1 parent 222311b commit 92cbab4
Show file tree
Hide file tree
Showing 53 changed files with 1,005 additions and 875 deletions.
22 changes: 21 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,29 @@
],
"args": [
"deploy",
"-v",
"-e",
"do-env",
"architect/auth:latest"
"muesch.io/muesch/architect/auth:latest"
],
"attachSimplePort": 9229,
"console": "integratedTerminal"
},
{
"name": "Push",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "deno",
"runtimeArgs": [
"run",
"--inspect-brk",
"-A",
"main.ts"
],
"args": [
"push",
"muesch.io/muesch/architect/auth:latest"
],
"attachSimplePort": 9229,
"console": "integratedTerminal"
Expand Down
817 changes: 175 additions & 642 deletions deno.lock

Large diffs are not rendered by default.

90 changes: 82 additions & 8 deletions src/@providers/kubernetes/modules/deployment.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
import k8s from '@kubernetes/client-node';
import { Construct } from 'constructs';
import { ResourceOutputs } from '../../../@resources/index.ts';
import { ResourceModule, ResourceModuleOptions } from '../../module.ts';
import { Deployment } from '../.gen/providers/kubernetes/deployment/index.ts';
import { PersistentVolumeClaim } from '../.gen/providers/kubernetes/persistent-volume-claim/index.ts';
import { KubernetesCredentials } from '../credentials.ts';
import KubernetesUtils from '../utils.ts';

export class KubernetesDeploymentModule extends ResourceModule<'deployment', KubernetesCredentials> {
private deployment: Deployment;
private volumeClaims: Record<string, PersistentVolumeClaim>;
outputs: ResourceOutputs['deployment'];

constructor(scope: Construct, options: ResourceModuleOptions<'deployment', KubernetesCredentials>) {
super(scope, options);

const normalizedName = this.inputs?.name.replace(/\//g, '--') || 'unknown';

this.volumeClaims = {};
for (const volume_mount of this.inputs?.volume_mounts || []) {
const name = `volume-${volume_mount.volume.split('-').pop()}`;
this.volumeClaims[volume_mount.volume] = new PersistentVolumeClaim(this, `${name}-claim`, {
metadata: {
name: volume_mount.volume,
namespace: this.inputs?.namespace,
},
waitUntilBound: false,
spec: {
accessModes: ['ReadWriteOnce'],
resources: {
requests: {
storage: '500Mi',
},
},
},
});
}

this.deployment = new Deployment(this, 'deployment', {
metadata: {
name: normalizedName,
Expand All @@ -36,6 +60,42 @@ export class KubernetesDeploymentModule extends ResourceModule<'deployment', Kub
},
},
spec: {
initContainer: this.inputs?.volume_mounts?.map((volume) => {
const [repo, tag] = (volume.image || '').split(':');
const repoParts = repo.split('/');
repoParts.splice(1, 0, 'v2');
const fullRepo = repoParts.join('/');

const manifest_url = `${fullRepo}/manifests/${tag}`;
return {
name: `${normalizedName}-volume-${volume.volume.split('-').pop()}`,
image: 'cydrive/volume:latest',
env: [
{
name: 'MANIFEST_URL',
value: manifest_url,
},
{
name: 'OUTPUT_DIR',
value: volume.mount_path,
},
],
volumeMount: [
{
name: volume.volume,
mountPath: volume.mount_path,
},
],
};
}),
volume: this.inputs?.volume_mounts?.map((volume) => {
return {
name: volume.volume,
persistentVolumeClaim: {
claimName: this.volumeClaims[volume.volume].metadata.name,
},
};
}),
container: [
{
name: normalizedName,
Expand All @@ -47,10 +107,6 @@ export class KubernetesDeploymentModule extends ResourceModule<'deployment', Kub
name: key,
value: String(value),
})),
volumeMount: this.inputs?.volume_mounts?.map((mount) => ({
name: mount.volume,
mountPath: mount.mount_path,
})),
resources: {
requests: {
...(this.inputs?.cpu ? { cpu: String(this.inputs.cpu) } : {}),
Expand All @@ -61,6 +117,12 @@ export class KubernetesDeploymentModule extends ResourceModule<'deployment', Kub
...(this.inputs?.memory ? { memory: this.inputs.memory } : {}),
},
},
volumeMount: (this.inputs?.volume_mounts || []).map((volume) => {
return {
name: volume.volume,
mountPath: volume.mount_path,
};
}),
},
...(this.inputs?.sidecars?.map((container, index) => ({
name: `${normalizedName}-sidecar-${index}`,
Expand All @@ -70,9 +132,9 @@ export class KubernetesDeploymentModule extends ResourceModule<'deployment', Kub
name: key,
value: String(value),
})),
volumeMount: container.volume_mounts.map((mount) => ({
name: mount.volume,
mountPath: mount.mount_path,
volumeMount: container.volume_mounts.map((volume) => ({
name: volume.volume,
mountPath: volume.mount_path,
})),
resources: {
requests: {
Expand All @@ -96,8 +158,20 @@ export class KubernetesDeploymentModule extends ResourceModule<'deployment', Kub
};
}

genImports(resourceId: string): Promise<Record<string, string>> {
async genImports(resourceId: string): Promise<Record<string, string>> {
const [namespace, name] = resourceId.split('/');
const client = KubernetesUtils.getClient(this.credentials, k8s.CoreV1Api);
const pods = await client.listNamespacedPod(namespace);
const normalizedName = name.replace(/\//g, '--') || 'unknown';
const currentPod = pods.body.items.find((pod) => pod.metadata?.labels?.['architect.io/name'] === normalizedName);
const volumeIds: Record<string, string> = {};
if (currentPod) {
currentPod.spec?.volumes?.forEach((volume) => {
volumeIds[this.getResourceRef(this.volumeClaims[volume.persistentVolumeClaim?.claimName || ''])] = volume.name;
});
}
return Promise.resolve({
...volumeIds,
[this.getResourceRef(this.deployment)]: resourceId,
});
}
Expand Down
14 changes: 2 additions & 12 deletions src/@providers/kubernetes/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { KubernetesHelmChartService } from './services/helm-chart.ts';
import { KubernetesIngressRuleService } from './services/ingress-rule.ts';
import { KubernetesNamespaceService } from './services/namespace.ts';
import { KubernetesServiceService } from './services/service.ts';
import KubernetesUtils from './utils.ts';

export default class KubernetesProvider extends Provider<KubernetesCredentials> {
readonly type = 'kubernetes';
Expand All @@ -21,18 +22,7 @@ export default class KubernetesProvider extends Provider<KubernetesCredentials>
};

public async testCredentials(): Promise<boolean> {
const kubeConfig = new k8s.KubeConfig();
if (this.credentials.configPath) {
kubeConfig.loadFromFile(this.credentials.configPath);
} else {
kubeConfig.loadFromDefault();
}

if (this.credentials.configContext) {
kubeConfig.setCurrentContext(this.credentials.configContext);
}

const client = kubeConfig.makeApiClient(k8s.VersionApi);
const client = KubernetesUtils.getClient(this.credentials, k8s.VersionApi);
try {
const res = await client.getCode();
return Number(res.body.major) >= 1 && Number(res.body.minor) >= 18;
Expand Down
25 changes: 25 additions & 0 deletions src/@providers/kubernetes/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import k8s, { ApiType } from '@kubernetes/client-node';
import { KubernetesCredentials } from './credentials.ts';

type ApiConstructor<T extends ApiType> = new (server: string) => T;
export default class KubernetesUtils {
public static getKubeConfig(credentials: KubernetesCredentials): k8s.KubeConfig {
const kubeConfig = new k8s.KubeConfig();
if (credentials.configPath) {
kubeConfig.loadFromFile(credentials.configPath);
} else {
kubeConfig.loadFromDefault();
}

if (credentials.configContext) {
kubeConfig.setCurrentContext(credentials.configContext);
}

return kubeConfig;
}

public static getClient<T extends ApiType>(credentials: KubernetesCredentials, apiClientType: ApiConstructor<T>): T {
const kubeConfig = KubernetesUtils.getKubeConfig(credentials);
return kubeConfig.makeApiClient(apiClientType);
}
}
2 changes: 1 addition & 1 deletion src/@resources/arcctlAccount/inputs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
"type": "object"
}
}
}
}
2 changes: 1 addition & 1 deletion src/@resources/cronjob/inputs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,4 @@
"type": "object"
}
}
}
}
2 changes: 1 addition & 1 deletion src/@resources/database/inputs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@
"type": "object"
}
}
}
}
2 changes: 1 addition & 1 deletion src/@resources/databaseSchema/inputs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
"type": "object"
}
}
}
}
2 changes: 1 addition & 1 deletion src/@resources/databaseSize/inputs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
"type": "object"
}
}
}
}
2 changes: 1 addition & 1 deletion src/@resources/databaseType/inputs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
"type": "object"
}
}
}
}
2 changes: 1 addition & 1 deletion src/@resources/databaseUser/inputs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
"type": "object"
}
}
}
}
2 changes: 1 addition & 1 deletion src/@resources/databaseVersion/inputs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
"type": "object"
}
}
}
}
8 changes: 7 additions & 1 deletion src/@resources/deployment/inputs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@
"items": {
"additionalProperties": false,
"properties": {
"digest": {
"type": "string"
},
"mount_path": {
"type": "string"
},
Expand Down Expand Up @@ -257,6 +260,9 @@
"items": {
"additionalProperties": false,
"properties": {
"digest": {
"type": "string"
},
"mount_path": {
"type": "string"
},
Expand Down Expand Up @@ -417,4 +423,4 @@
]
}
}
}
}
1 change: 1 addition & 0 deletions src/@resources/deployment/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ type Container = {
volume_mounts: Array<{
volume: string;
mount_path: string;
image?: string;
readonly: boolean;
}>;

Expand Down
2 changes: 1 addition & 1 deletion src/@resources/dnsRecord/inputs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
"type": "object"
}
}
}
}
2 changes: 1 addition & 1 deletion src/@resources/dnsZone/inputs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
"type": "object"
}
}
}
}
2 changes: 1 addition & 1 deletion src/@resources/dockerBuild/inputs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@
"type": "object"
}
}
}
}
2 changes: 1 addition & 1 deletion src/@resources/helmChart/inputs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@
"type": "object"
}
}
}
}
2 changes: 1 addition & 1 deletion src/@resources/ingressRule/inputs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@
"type": "object"
}
}
}
}
8 changes: 7 additions & 1 deletion src/@resources/input.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,9 @@
"items": {
"additionalProperties": false,
"properties": {
"digest": {
"type": "string"
},
"mount_path": {
"type": "string"
},
Expand Down Expand Up @@ -723,6 +726,9 @@
"items": {
"additionalProperties": false,
"properties": {
"digest": {
"type": "string"
},
"mount_path": {
"type": "string"
},
Expand Down Expand Up @@ -1834,4 +1840,4 @@
}
},
"$ref": "#/definitions/InputSchema"
}
}
2 changes: 1 addition & 1 deletion src/@resources/kubernetesCluster/inputs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@
"type": "object"
}
}
}
}
2 changes: 1 addition & 1 deletion src/@resources/kubernetesVersion/inputs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
"type": "object"
}
}
}
}
Loading

0 comments on commit 92cbab4

Please sign in to comment.