Skip to content
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
16 changes: 16 additions & 0 deletions docs/schema/yaml/1.0.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ services:
builder:
# @param services.helm.docker.builder.engine
engine: ''
# @param services.helm.docker.builder.resources
resources:
# @param services.helm.docker.builder.resources.requests
requests:

# @param services.helm.docker.builder.resources.limits
limits:

# @param services.helm.docker.app (required)
app:
# @param services.helm.docker.app.afterBuildPipelineConfig
Expand Down Expand Up @@ -372,6 +380,14 @@ services:
builder:
# @param services.github.docker.builder.engine
engine: ''
# @param services.github.docker.builder.resources
resources:
# @param services.github.docker.builder.resources.requests
requests:

# @param services.github.docker.builder.resources.limits
limits:

# @param services.github.docker.app (required)
app:
# @param services.github.docker.app.afterBuildPipelineConfig
Expand Down
36 changes: 36 additions & 0 deletions src/server/lib/jsonschema/schemas/1.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,24 @@
"properties": {
"engine": {
"type": "string"
},
"resources": {
"type": "object",
"additionalProperties": false,
"properties": {
"requests": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"limits": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
},
Expand Down Expand Up @@ -708,6 +726,24 @@
"properties": {
"engine": {
"type": "string"
},
"resources": {
"type": "object",
"additionalProperties": false,
"properties": {
"requests": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"limits": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
},
Expand Down
175 changes: 175 additions & 0 deletions src/server/lib/nativeBuild/__tests__/buildkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,181 @@ describe('buildkitBuild', () => {
});
});

describe('build resource precedence', () => {
const mockDeploy = {
deployable: { name: 'test-service' },
$fetchGraph: jest.fn(),
build: { isStatic: false },
} as any;

const baseOptions: BuildkitBuildOptions = {
ecrRepo: 'test-repo',
ecrDomain: '123456789.dkr.ecr.us-east-1.amazonaws.com',
envVars: { NODE_ENV: 'production' },
dockerfilePath: 'Dockerfile',
tag: 'v1.0.0',
revision: 'abc123def456789',
repo: 'owner/repo',
branch: 'main',
namespace: 'env-test-123',
buildId: '456',
deployUuid: 'test-service-abc123',
jobTimeout: 1800,
};

beforeEach(() => {
jest.clearAllMocks();
(getGitHubToken as jest.Mock).mockResolvedValue('github-token-123');
(shellPromise as jest.Mock).mockResolvedValue('');
(waitForJobAndGetLogs as jest.Mock).mockResolvedValue({
logs: 'Build completed successfully',
success: true,
});
});

it('uses yaml resources (options.resources) over global config', async () => {
const globalConfig = {
buildDefaults: {
resources: {
buildkit: {
requests: { cpu: '1', memory: '2Gi' },
limits: { cpu: '2', memory: '4Gi' },
},
},
},
};
(GlobalConfigService.getInstance as jest.Mock).mockReturnValue({
getAllConfigs: jest.fn().mockResolvedValue(globalConfig),
});

const yamlResources = {
requests: { cpu: '4', memory: '8Gi' },
limits: { cpu: '8', memory: '16Gi' },
};

await buildkitBuild(mockDeploy, { ...baseOptions, resources: yamlResources });

const kubectlCalls = (shellPromise as jest.Mock).mock.calls;
const applyCall = kubectlCalls.find((call) => call[0].includes('kubectl apply'));
const fullCommand = applyCall[0];

expect(fullCommand).toContain('cpu: "4"');
expect(fullCommand).toContain('memory: "8Gi"');
expect(fullCommand).toContain('cpu: "8"');
expect(fullCommand).toContain('memory: "16Gi"');
expect(fullCommand).not.toContain('cpu: "1"');
expect(fullCommand).not.toContain('cpu: "2"');
});

it('falls back to global config resources when yaml resources not set', async () => {
const globalConfig = {
buildDefaults: {
resources: {
buildkit: {
requests: { cpu: '1', memory: '2Gi' },
limits: { cpu: '3', memory: '6Gi' },
},
},
},
};
(GlobalConfigService.getInstance as jest.Mock).mockReturnValue({
getAllConfigs: jest.fn().mockResolvedValue(globalConfig),
});

await buildkitBuild(mockDeploy, baseOptions);

const kubectlCalls = (shellPromise as jest.Mock).mock.calls;
const applyCall = kubectlCalls.find((call) => call[0].includes('kubectl apply'));
const fullCommand = applyCall[0];

expect(fullCommand).toContain('cpu: "1"');
expect(fullCommand).toContain('memory: "2Gi"');
expect(fullCommand).toContain('cpu: "3"');
expect(fullCommand).toContain('memory: "6Gi"');
});

it('falls back to default resources when neither yaml nor global config set', async () => {
(GlobalConfigService.getInstance as jest.Mock).mockReturnValue({
getAllConfigs: jest.fn().mockResolvedValue({}),
});

await buildkitBuild(mockDeploy, baseOptions);

const kubectlCalls = (shellPromise as jest.Mock).mock.calls;
const applyCall = kubectlCalls.find((call) => call[0].includes('kubectl apply'));
const fullCommand = applyCall[0];

expect(fullCommand).toContain('cpu: "500m"');
expect(fullCommand).toContain('memory: "1Gi"');
expect(fullCommand).toContain('cpu: "2"');
expect(fullCommand).toContain('memory: "4Gi"');
});

it('uses yaml resources for kaniko over global config', async () => {
const { kanikoBuild } = require('../engines');
const globalConfig = {
buildDefaults: {
resources: {
kaniko: {
requests: { cpu: '300m', memory: '750Mi' },
limits: { cpu: '1', memory: '2Gi' },
},
},
},
};
(GlobalConfigService.getInstance as jest.Mock).mockReturnValue({
getAllConfigs: jest.fn().mockResolvedValue(globalConfig),
});

const yamlResources = {
requests: { cpu: '2', memory: '4Gi' },
limits: { cpu: '4', memory: '8Gi' },
};

await kanikoBuild(mockDeploy, { ...baseOptions, resources: yamlResources });

const kubectlCalls = (shellPromise as jest.Mock).mock.calls;
const applyCall = kubectlCalls.find((call) => call[0].includes('kubectl apply'));
const fullCommand = applyCall[0];

expect(fullCommand).toContain('cpu: "2"');
expect(fullCommand).toContain('memory: "4Gi"');
expect(fullCommand).toContain('cpu: "4"');
expect(fullCommand).toContain('memory: "8Gi"');
expect(fullCommand).not.toContain('cpu: "300m"');
expect(fullCommand).not.toContain('memory: "750Mi"');
});

it('uses partial yaml resources without merging with global config', async () => {
const globalConfig = {
buildDefaults: {
resources: {
buildkit: {
requests: { cpu: '1', memory: '2Gi' },
limits: { cpu: '2', memory: '4Gi' },
},
},
},
};
(GlobalConfigService.getInstance as jest.Mock).mockReturnValue({
getAllConfigs: jest.fn().mockResolvedValue(globalConfig),
});

const yamlResources = {
requests: { cpu: '4', memory: '8Gi' },
};

await buildkitBuild(mockDeploy, { ...baseOptions, resources: yamlResources });

const kubectlCalls = (shellPromise as jest.Mock).mock.calls;
const applyCall = kubectlCalls.find((call) => call[0].includes('kubectl apply'));
const fullCommand = applyCall[0];

expect(fullCommand).toContain('cpu: "4"');
expect(fullCommand).toContain('memory: "8Gi"');
});
});

describe('generateSecretArgsScript', () => {
it('returns comment when no secret keys provided', () => {
expect(generateSecretArgsScript(undefined)).toBe('# No secret env keys');
Expand Down
8 changes: 8 additions & 0 deletions src/server/lib/yamlSchemas/schema_1_0_0/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ export const docker = {
additionalProperties: true,
properties: {
engine: { type: 'string' },
resources: {
type: 'object',
additionalProperties: false,
properties: {
requests: { type: 'object', additionalProperties: { type: 'string' } },
limits: { type: 'object', additionalProperties: { type: 'string' } },
},
},
},
},
app: {
Expand Down
4 changes: 4 additions & 0 deletions src/server/models/yaml/YamlService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ export interface ServiceDiskConfig {

export interface Builder {
readonly engine?: string;
readonly resources?: {
readonly requests?: Record<string, string>;
readonly limits?: Record<string, string>;
};
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/server/services/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,7 @@ export default class DeployService extends BaseService {
buildId: String(deploy.build.id),
deployUuid: deploy.uuid,
cacheRegistry: buildDefaults?.cacheRegistry,
resources: deployable.builder?.resources,
secretRefs: buildSecretNames,
secretEnvKeys: Array.from(secretEnvKeys),
};
Expand Down