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

feat: support configuring component resources via configs, update resource defaults #97

Merged
merged 7 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ const Cdk8sConstructBuilder = new ProjectBuilder(cdk8s.ConstructLibraryCdk8s)
// K8s Constructs
const k8sComponentConstruct = Cdk8sConstructBuilder.build({
name: 'k8s.construct.component',
deps: ['debug', 'defu', 'js-yaml'],
deps: ['debug', 'defu', 'js-yaml', 'zod'],
devDeps: ['@types/debug'],
jest: false,
})
Expand Down
18 changes: 18 additions & 0 deletions packages/charts/crisiscleanup/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ const containerImageSchema = z

const metricPercent = z.number().min(0).max(100)

const K8sMillicores = z.number().describe('Kubernetes CPU Millicores')
const K8sMemoryMebibytes = z.number().describe('Kubernetes Memory Mebibytes')

const resourcesSchema = z
.object({
cpu: z.object({
limit: K8sMillicores.optional(),
request: K8sMillicores.optional(),
}),
memory: z.object({
limit: K8sMemoryMebibytes.optional(),
request: K8sMemoryMebibytes.optional(),
}),
})
.describe('Kubernetes resource limits/requests')
.deepPartial()

const scalingSchema = z
.object({
minReplicas: z.number().optional(),
Expand All @@ -32,6 +49,7 @@ const scalingSchema = z
const deploymentSchema = z.object({
image: containerImageSchema.optional(),
spread: z.boolean().default(false),
resources: resourcesSchema.optional(),
})

const withScaling = <T extends typeof deploymentSchema>(inSchema: T) =>
Expand Down
21 changes: 12 additions & 9 deletions packages/charts/crisiscleanup/test/__snapshots__/main.spec.ts.snap

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

82 changes: 49 additions & 33 deletions packages/k8s/construct/api/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,17 @@ export class ApiWSGI
}) as ApiWSGIProps
super(scope, id, propsWithDefaults)

const resources = this.getResources({
cpu: {
request: 1000,
limit: 1800,
},
memory: {
request: 1200,
limit: 1200,
},
})

const backend = this.addContainer({
name: 'gunicorn',
portNumber: 5000,
Expand All @@ -220,13 +231,7 @@ export class ApiWSGI
'--timeout=90',
],
...(props.probes ?? this.createHttpProbes(this.httpProbePath)),
resources: {
cpu: this.props.containerDefaults!.resources!.cpu!,
memory: {
request: Size.mebibytes(1200),
limit: Size.mebibytes(1200),
},
},
resources,
})

const staticVolume = kplus.Volume.fromEmptyDir(
Expand Down Expand Up @@ -342,6 +347,14 @@ export class ApiASGI
user: 1000,
group: 1000,
},
resources: {
// todo: properly expose via configs
memory: this.props.containerDefaults!.resources!.memory!,
cpu: {
request: kplus.Cpu.millis(500),
limit: kplus.Cpu.millis(1500),
},
},
})

// rag channels stateful set
Expand Down Expand Up @@ -392,7 +405,7 @@ export class ApiASGI
},
resources: {
cpu: {
limit: kplus.Cpu.units(3),
//limit: kplus.Cpu.units(3),
request: kplus.Cpu.millis(200),
},
memory: {
Expand Down Expand Up @@ -514,6 +527,15 @@ export class CeleryBeat extends ApiComponent {

constructor(scope: Construct, id: string, props: ApiProps) {
super(scope, id, props)
const resources = this.getResources({
cpu: {
request: 20,
},
memory: {
request: 400,
limit: 400,
},
})
this.addContainer({
name: 'celerybeat',
command: ['/serve.sh', 'celerybeat'],
Expand All @@ -524,15 +546,7 @@ export class CeleryBeat extends ApiComponent {
user: 1000,
group: 1000,
},
resources: {
cpu: {
request: kplus.Cpu.millis(20),
},
memory: {
request: Size.mebibytes(400),
limit: Size.mebibytes(400),
},
},
resources,
})
}

Expand All @@ -552,6 +566,13 @@ export class CeleryWorker extends ApiComponent<CeleryProps> {

const queues = [...new Set(props.queues)]

const resources = this.getResources({
memory: {
limit: 900,
request: 900,
},
})

this.addContainer({
name,
command: [
Expand All @@ -571,13 +592,7 @@ export class CeleryWorker extends ApiComponent<CeleryProps> {
user: 1000,
group: 1000,
},
resources: {
cpu: this.props.containerDefaults!.resources!.cpu!,
memory: {
limit: Size.mebibytes(900),
request: Size.mebibytes(900),
},
},
resources,
})
}
}
Expand All @@ -587,6 +602,15 @@ export class AdminWebSocket extends ApiComponent {

constructor(scope: Construct, id: string, props: ApiProps) {
super(scope, id, props)
const resources = this.getResources({
cpu: {
request: 3,
},
memory: {
request: 250,
limit: 250,
},
})
this.addContainer({
name: 'adminwebsocket',
command: ['/serve.sh', 'adminwebsocket'],
Expand All @@ -597,15 +621,7 @@ export class AdminWebSocket extends ApiComponent {
user: 1000,
group: 1000,
},
resources: {
cpu: {
request: kplus.Cpu.millis(3),
},
memory: {
request: Size.mebibytes(250),
limit: Size.mebibytes(250),
},
},
resources,
})
}
}
4 changes: 4 additions & 0 deletions packages/k8s/construct/component/.projen/deps.json

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

4 changes: 2 additions & 2 deletions packages/k8s/construct/component/.projen/tasks.json

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

3 changes: 2 additions & 1 deletion packages/k8s/construct/component/package.json

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

Loading