Skip to content

Commit 4e6fd6b

Browse files
Discover the Blacksmith agent via BLACKSMITH_AGENT_ADDR and fall back to the local builder when unavailable
Co-authored-by: Codesmith Staging <codesmith-bot@users.noreply.github.com>
1 parent 1e75355 commit 4e6fd6b

5 files changed

Lines changed: 69 additions & 22 deletions

File tree

dist/index.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/reporter.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { UserInputError } from "./user-input-error";
33
import axios from "axios";
44
import axiosRetry from "axios-retry";
55
import { create } from "@bufbuild/protobuf";
6-
import { Client, createClient } from "@connectrpc/connect";
6+
import { Client, Code, ConnectError, createClient } from "@connectrpc/connect";
77
import {
88
createGrpcTransport,
99
Http2SessionManager,
@@ -55,10 +55,33 @@ let cachedAgentSessionManager: Http2SessionManager | undefined;
5555
let cachedAgentClient: Client<typeof StickyDiskService> | undefined;
5656
let cachedAgentBaseUrl: string | undefined;
5757

58+
export function getAgentAddr(): string | undefined {
59+
return process.env.BLACKSMITH_AGENT_ADDR || undefined;
60+
}
61+
62+
// True for environments where the agent is not expected to serve sticky
63+
// disks (no BLACKSMITH_AGENT_ADDR, or the agent rejected the RPC as
64+
// unimplemented); these are not infra failures and are not reported.
65+
export function isAgentUnsupportedError(error: unknown): boolean {
66+
if (
67+
error instanceof Error &&
68+
error.message.includes("BLACKSMITH_AGENT_ADDR is not set")
69+
) {
70+
return true;
71+
}
72+
return error instanceof ConnectError && error.code === Code.Unimplemented;
73+
}
74+
5875
export function createBlacksmithAgentClient(): Client<
5976
typeof StickyDiskService
6077
> {
61-
const baseUrl = `http://192.168.127.1:${process.env.BLACKSMITH_STICKY_DISK_GRPC_PORT || "5557"}`;
78+
const addr = getAgentAddr();
79+
if (!addr) {
80+
throw new Error(
81+
"BLACKSMITH_AGENT_ADDR is not set; cannot dial the Blacksmith agent",
82+
);
83+
}
84+
const baseUrl = `http://${addr}:${process.env.BLACKSMITH_STICKY_DISK_GRPC_PORT || "5557"}`;
6285

6386
if (cachedAgentClient && cachedAgentBaseUrl === baseUrl) {
6487
return cachedAgentClient;
@@ -72,9 +95,7 @@ export function createBlacksmithAgentClient(): Client<
7295
}
7396
}
7497

75-
core.info(
76-
`Creating Blacksmith agent client with port: ${process.env.BLACKSMITH_STICKY_DISK_GRPC_PORT || "5557"}`,
77-
);
98+
core.info(`Creating Blacksmith agent client for ${baseUrl}`);
7899

79100
cachedAgentSessionManager = new Http2SessionManager(baseUrl);
80101
const transport = createGrpcTransport({

src/setup-builder.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ vi.mock("./reporter", () => ({
2121
reportMetric: vi.fn(),
2222
commitStickyDisk: vi.fn(),
2323
reportBuild: vi.fn(),
24+
getAgentAddr: vi.fn(() => process.env.BLACKSMITH_AGENT_ADDR || undefined),
25+
isAgentUnsupportedError: vi.fn(() => false),
2426
}));
2527

2628
vi.mock("child_process", () => ({
@@ -48,6 +50,7 @@ describe("setup_builder", () => {
4850
process.env.GITHUB_REPO_NAME = "test-repo";
4951
process.env.BLACKSMITH_REGION = "eu-central";
5052
process.env.BLACKSMITH_VM_ID = "test-vm-id";
53+
process.env.BLACKSMITH_AGENT_ADDR = "192.168.127.1";
5154
});
5255

5356
describe("getStickyDisk", () => {
@@ -101,6 +104,7 @@ describe("setup_builder", () => {
101104

102105
describe("writeDockerContainerBuildkitdTomlFile", () => {
103106
it("writes a docker-container BuildKit config with the Docker mirror", async () => {
107+
process.env.BLACKSMITH_AGENT_ADDR = "192.168.127.1";
104108
const writeFile = vi.mocked(fs.promises.writeFile);
105109

106110
await setupBuilder.writeDockerContainerBuildkitdTomlFile(
@@ -117,6 +121,19 @@ describe("setup_builder", () => {
117121
"Wrote Docker container BuildKit config to local-buildkitd.toml",
118122
);
119123
});
124+
125+
it("omits the Docker mirror when BLACKSMITH_AGENT_ADDR is not set", async () => {
126+
delete process.env.BLACKSMITH_AGENT_ADDR;
127+
const writeFile = vi.mocked(fs.promises.writeFile);
128+
129+
await setupBuilder.writeDockerContainerBuildkitdTomlFile(
130+
"local-buildkitd.toml",
131+
);
132+
133+
const config = writeFile.mock.calls[0][1] as string;
134+
expect(config).not.toContain("mirrors");
135+
expect(config).not.toContain("192.168.127.1:5000");
136+
});
120137
});
121138

122139
describe("logBuildCacheContents", () => {

src/setup_builder.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,18 @@ async function getRoutableHostDns(): Promise<string[]> {
165165
}
166166

167167
function getDockerMirrorRegistryConfig(): TOML.JsonMap {
168+
const agentAddr = reporter.getAgentAddr();
169+
if (!agentAddr) {
170+
return {};
171+
}
172+
const mirror = `${agentAddr}:5000`;
168173
return {
169174
"docker.io": {
170-
mirrors: ["http://192.168.127.1:5000"],
175+
mirrors: [`http://${mirror}`],
171176
http: true,
172177
insecure: true,
173178
},
174-
"192.168.127.1:5000": {
179+
[mirror]: {
175180
http: true,
176181
insecure: true,
177182
},
@@ -421,12 +426,13 @@ export async function getStickyDisk(options?: {
421426
const client = await reporter.createBlacksmithAgentClient();
422427
core.info(`Created Blacksmith agent client`);
423428

424-
// Test connection using up endpoint
429+
// Rethrow the original error so callers can classify it from the gRPC code.
425430
try {
426431
await client.up({}, { signal: options?.signal });
427432
core.info("Successfully connected to Blacksmith agent");
428433
} catch (error) {
429-
throw new Error(`grpc connection test failed: ${(error as Error).message}`);
434+
core.warning(`grpc connection test failed: ${(error as Error).message}`);
435+
throw error;
430436
}
431437

432438
const stickyDiskKey = cacheKey;
@@ -746,11 +752,14 @@ export async function setupStickyDisk(): Promise<{
746752
return { device, exposeId };
747753
} catch (error) {
748754
core.warning(`Error in setupStickyDisk: ${(error as Error).message}`);
749-
await reporter.reportBuildPushActionFailure(
750-
"STICKYDISK_SETUP",
751-
error as Error,
752-
"sticky disk setup",
753-
);
755+
// Unsupported environments are expected; don't report them as failures.
756+
if (!reporter.isAgentUnsupportedError(error)) {
757+
await reporter.reportBuildPushActionFailure(
758+
"STICKYDISK_SETUP",
759+
error as Error,
760+
"sticky disk setup",
761+
);
762+
}
754763
throw error;
755764
}
756765
}

0 commit comments

Comments
 (0)