Skip to content

Commit 69e2560

Browse files
authored
fix(native-build): harden registry login retries (#182)
* fix(native-build): harden registry login retries * fix(native-build): remove unsupported apk retry flags
1 parent d910e16 commit 69e2560

2 files changed

Lines changed: 109 additions & 8 deletions

File tree

‎src/server/lib/nativeBuild/__tests__/buildkit.test.ts‎

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { buildkitBuild, BuildkitBuildOptions, generateSecretArgsScript } from '../engines';
17+
import { buildkitBuild, NativeBuildOptions, generateSecretArgsScript } from '../engines';
1818
import { shellPromise } from '../../shell';
1919
import { waitForJobAndGetLogs, getGitHubToken } from '../utils';
2020
import GlobalConfigService from '../../../services/globalConfig';
@@ -68,7 +68,7 @@ describe('buildkitBuild', () => {
6868
build: { isStatic: false },
6969
} as any;
7070

71-
const mockOptions: BuildkitBuildOptions = {
71+
const mockOptions: NativeBuildOptions = {
7272
ecrRepo: 'test-repo',
7373
ecrDomain: '123456789.dkr.ecr.us-east-1.amazonaws.com',
7474
envVars: { NODE_ENV: 'production' },
@@ -247,6 +247,35 @@ describe('buildkitBuild', () => {
247247
expect(fullCommand).toContain('build-arg:NODE_ENV=production');
248248
});
249249

250+
it('keeps registry bootstrap installs default and adds AWS retry env vars', async () => {
251+
await buildkitBuild(mockDeploy, mockOptions);
252+
253+
const kubectlCalls = (shellPromise as jest.Mock).mock.calls;
254+
const applyCall = kubectlCalls.find((call) => call[0].includes('kubectl apply'));
255+
const fullCommand = applyCall[0];
256+
257+
expect(fullCommand).toContain('apk add --no-cache aws-cli docker-cli');
258+
expect(fullCommand).toContain('apk add --no-cache docker-cli');
259+
expect(fullCommand).toContain('export AWS_MAX_ATTEMPTS=5');
260+
expect(fullCommand).toContain('export AWS_RETRY_MODE=adaptive');
261+
});
262+
263+
it('renders registry domain safely for non-ECR buildkit targets', async () => {
264+
const optionsWithCustomRegistry = {
265+
...mockOptions,
266+
ecrDomain: 'registry.internal.svc.cluster.local',
267+
};
268+
269+
await buildkitBuild(mockDeploy, optionsWithCustomRegistry);
270+
271+
const kubectlCalls = (shellPromise as jest.Mock).mock.calls;
272+
const applyCall = kubectlCalls.find((call) => call[0].includes('kubectl apply'));
273+
const fullCommand = applyCall[0];
274+
275+
expect(fullCommand).toContain('REGISTRY_DOMAIN=\\"registry.internal.svc.cluster.local\\"');
276+
expect(fullCommand).toContain('apk add --no-cache docker-cli');
277+
});
278+
250279
it('coerces numeric env var values to strings for Kubernetes compatibility', async () => {
251280
const optionsWithNumericEnv = {
252281
...mockOptions,
@@ -320,7 +349,7 @@ describe('build resource precedence', () => {
320349
build: { isStatic: false },
321350
} as any;
322351

323-
const baseOptions: BuildkitBuildOptions = {
352+
const baseOptions: NativeBuildOptions = {
324353
ecrRepo: 'test-repo',
325354
ecrDomain: '123456789.dkr.ecr.us-east-1.amazonaws.com',
326355
envVars: { NODE_ENV: 'production' },
@@ -495,7 +524,7 @@ describe('build pod annotations', () => {
495524
build: { isStatic: false },
496525
} as any;
497526

498-
const baseOptions: BuildkitBuildOptions = {
527+
const baseOptions: NativeBuildOptions = {
499528
ecrRepo: 'test-repo',
500529
ecrDomain: '123456789.dkr.ecr.us-east-1.amazonaws.com',
501530
envVars: { NODE_ENV: 'production' },
@@ -655,3 +684,69 @@ describe('generateSecretArgsScript', () => {
655684
expect(result).toContain('--opt build-arg:MY_SECRET=');
656685
});
657686
});
687+
688+
describe('kaniko registry login bootstrap', () => {
689+
const mockDeploy = {
690+
deployable: { name: 'test-service' },
691+
$fetchGraph: jest.fn(),
692+
build: { isStatic: false },
693+
} as any;
694+
695+
const baseOptions: NativeBuildOptions = {
696+
ecrRepo: 'test-repo',
697+
ecrDomain: '123456789.dkr.ecr.us-east-1.amazonaws.com',
698+
envVars: { NODE_ENV: 'production' },
699+
dockerfilePath: 'Dockerfile',
700+
tag: 'v1.0.0',
701+
revision: 'abc123def456789',
702+
repo: 'owner/repo',
703+
branch: 'main',
704+
namespace: 'env-test-123',
705+
buildId: '456',
706+
deployUuid: 'test-service-abc123',
707+
jobTimeout: 1800,
708+
};
709+
710+
beforeEach(() => {
711+
jest.clearAllMocks();
712+
(GlobalConfigService.getInstance as jest.Mock).mockReturnValue({
713+
getAllConfigs: jest.fn().mockResolvedValue({}),
714+
});
715+
(getGitHubToken as jest.Mock).mockResolvedValue('github-token-123');
716+
(shellPromise as jest.Mock).mockResolvedValue('');
717+
(waitForJobAndGetLogs as jest.Mock).mockResolvedValue({
718+
logs: 'Build completed successfully',
719+
success: true,
720+
});
721+
});
722+
723+
it('adds AWS retry env vars for ECR login', async () => {
724+
const { kanikoBuild } = require('../engines');
725+
await kanikoBuild(mockDeploy, baseOptions);
726+
727+
const kubectlCalls = (shellPromise as jest.Mock).mock.calls;
728+
const applyCall = kubectlCalls.find((call) => call[0].includes('kubectl apply'));
729+
const fullCommand = applyCall[0];
730+
731+
expect(fullCommand).toContain('image: "amazon/aws-cli:2.13.0"');
732+
expect(fullCommand).toContain('export AWS_MAX_ATTEMPTS=5');
733+
expect(fullCommand).toContain('export AWS_RETRY_MODE=adaptive');
734+
expect(fullCommand).toContain('aws ecr get-login-password --region us-east-1');
735+
});
736+
737+
it('keeps non-ECR login bootstrap generic', async () => {
738+
const { kanikoBuild } = require('../engines');
739+
await kanikoBuild(mockDeploy, {
740+
...baseOptions,
741+
ecrDomain: 'registry.internal.svc.cluster.local',
742+
});
743+
744+
const kubectlCalls = (shellPromise as jest.Mock).mock.calls;
745+
const applyCall = kubectlCalls.find((call) => call[0].includes('kubectl apply'));
746+
const fullCommand = applyCall[0];
747+
748+
expect(fullCommand).toContain('image: "alpine:3.18"');
749+
expect(fullCommand).toContain('Using in-cluster registry: registry.internal.svc.cluster.local');
750+
expect(fullCommand).not.toContain('AWS_MAX_ATTEMPTS=5');
751+
});
752+
});

‎src/server/lib/nativeBuild/engines.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ if echo "\${REGISTRY_DOMAIN}" | grep -qE "^[0-9]+\\.dkr\\.ecr\\.([a-z0-9-]+)\\.a
133133
134134
echo "Installing AWS CLI and Docker CLI..."
135135
apk add --no-cache aws-cli docker-cli
136+
137+
export AWS_MAX_ATTEMPTS=5
138+
export AWS_RETRY_MODE=adaptive
136139
137140
echo "Testing AWS credentials..."
138141
if aws sts get-caller-identity; then
@@ -316,10 +319,13 @@ export async function buildWithEngine(
316319
const ecrMatch = registryDomain.match(ecrRegex);
317320
if (ecrMatch) {
318321
const region = ecrMatch[1] || 'us-west-2';
319-
registryLoginScript =
320-
`aws ecr get-login-password --region ${region} | ` +
321-
`{ read PASSWORD; mkdir -p /workspace/.docker && ` +
322-
`echo '{"auths":{"${registryDomain}":{"auth":"'$(echo -n "AWS:$PASSWORD" | base64)'"}}}' > /workspace/.docker/config.json; }`;
322+
registryLoginScript = [
323+
'set -e',
324+
'export AWS_MAX_ATTEMPTS=5',
325+
'export AWS_RETRY_MODE=adaptive',
326+
`aws ecr get-login-password --region ${region} | { read PASSWORD; mkdir -p /workspace/.docker && ` +
327+
`echo '{"auths":{"${registryDomain}":{"auth":"'$(echo -n "AWS:$PASSWORD" | base64)'"}}}' > /workspace/.docker/config.json; }`,
328+
].join('\n');
323329
} else {
324330
registryLoginScript =
325331
`echo "Using in-cluster registry: ${registryDomain}"; ` +

0 commit comments

Comments
 (0)