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: implement and use vertical pod autoscaling #99

Merged
merged 13 commits into from
Oct 4, 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
51 changes: 51 additions & 0 deletions packages/charts/crisiscleanup/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Size } from 'cdk8s'
import * as kplus from 'cdk8s-plus-27'
import { z } from 'zod'

const ImagePullPolicyEnum = z.enum(['Always', 'Never', 'IfNotPresent'])
Expand All @@ -12,9 +14,22 @@ const containerImageSchema = z

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

/**
* @see https://github.com/colinhacks/zod/issues/384
*/
const CpuInstanceSchema = z.custom<kplus.Cpu>(
(data) => data instanceof kplus.Cpu,
)
const SizeInstanceSchema = z.custom<Size>((data) => data instanceof Size)

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

const K8sMillicoresToCpu = K8sMillicores.transform((v) => kplus.Cpu.millis(v))
const K8sMemoryMebibytesToSize = K8sMemoryMebibytes.transform((v) =>
Size.mebibytes(v),
)

const resourcesSchema = z
.object({
cpu: z.object({
Expand Down Expand Up @@ -46,10 +61,46 @@ const scalingSchema = z
.describe('Horizontal Autoscaling Parameters')
.passthrough()

const resourcesRangeSchema = z
.object({
cpu: z.union([CpuInstanceSchema, K8sMillicoresToCpu]).optional(),
memory: z.union([SizeInstanceSchema, K8sMemoryMebibytesToSize]).optional(),
})
.describe('Resource limits for vertical scaling.')
.partial()
.pipe(
z.object({
cpu: CpuInstanceSchema.optional(),
memory: SizeInstanceSchema.optional(),
}),
)
.describe('Resource limits for vertical scaling.')

const verticalScalingContainerPolicySchema = z
.object({
containerName: z
.string()
.describe('Name or pattern of containers to target.'),
minAllowed: resourcesRangeSchema.optional(),
maxAllowed: resourcesRangeSchema.optional(),
})
.describe('Container policy for resource adjustments.')

const verticalScalingSchema = z
.object({
enabled: z.boolean().default(true),
policies: z
.array(verticalScalingContainerPolicySchema)
.optional()
.describe('Container policies for resource adjustments.'),
})
.describe('Vertical Pod Autoscaler Configuration')

const deploymentSchema = z.object({
image: containerImageSchema.optional(),
spread: z.boolean().default(false),
resources: resourcesSchema.optional(),
verticalScaling: verticalScalingSchema.optional().default({ enabled: true }),
})

const withScaling = <T extends typeof deploymentSchema>(inSchema: T) =>
Expand Down
Loading