Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit eedb87c

Browse files
committed
Specify Traefik2 IngressRoute Service Port
Added the ability to specify a `--k8s-service-port` to `spk service create`. This port will now be tracked in the users `bedrock.yaml` file and be injected to the Traefik2 IngressRoutes created via `spk hld reconcile`. closes microsoft/bedrock#818
1 parent 86f87e2 commit eedb87c

File tree

13 files changed

+128
-64
lines changed

13 files changed

+128
-64
lines changed

docs/service-management.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Options:
5050
--git-push SPK CLI will try to commit and push these changes to a new origin/branch named after the service. (default: false)
5151
--variable-group-name <variable-group-name> The Azure DevOps Variable Group.
5252
--middlewares <comma-delimitated-list-of-middleware-names> Traefik2 middlewares you wish to to be injected into your Traefik2 IngressRoutes (default: "")
53+
--k8s-service-port <port> Kubernetes service port which this service is exposed with; will be used to configure Traefik2 IngressRoutes (default: "80")
5354
-h, --help output usage information
5455
```
5556

src/commands/hld/reconcile.test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ describe("addChartToRing", () => {
169169
git,
170170
path
171171
}
172-
}
172+
},
173+
k8sServicePort: 1337
173174
};
174175

175176
/* tslint:disable-next-line: no-string-literal */
@@ -198,7 +199,8 @@ describe("addChartToRing", () => {
198199
path,
199200
sha
200201
}
201-
}
202+
},
203+
k8sServicePort: 1337
202204
};
203205

204206
/* tslint:disable-next-line: no-string-literal */
@@ -225,7 +227,8 @@ describe("addChartToRing", () => {
225227
chart,
226228
repository
227229
}
228-
}
230+
},
231+
k8sServicePort: 1337
229232
};
230233

231234
/* tslint:disable-next-line: no-string-literal */
@@ -277,7 +280,8 @@ describe("reconcile tests", () => {
277280
path,
278281
sha
279282
}
280-
}
283+
},
284+
k8sServicePort: 1337
281285
}
282286
}
283287
};
@@ -323,7 +327,8 @@ describe("reconcile tests", () => {
323327
path,
324328
sha
325329
}
326-
}
330+
},
331+
k8sServicePort: 1337
327332
}
328333
}
329334
};

src/commands/hld/reconcile.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,17 @@ const createIngressRouteForRing = (
264264
staticComponentPathInRing,
265265
"ingress-route.yaml"
266266
);
267-
// TODO: figure out a way to grab the port from _somewhere_; store in bedrock.yaml?
268-
const ingressRoute = TraefikIngressRoute(serviceName, ring, 8000, {
269-
middlewares: [
270-
middlewares.metadata.name,
271-
...(serviceConfig.middlewares ?? [])
272-
]
273-
});
267+
const ingressRoute = TraefikIngressRoute(
268+
serviceName,
269+
ring,
270+
serviceConfig.k8sServicePort,
271+
{
272+
middlewares: [
273+
middlewares.metadata.name,
274+
...(serviceConfig.middlewares ?? [])
275+
]
276+
}
277+
);
274278

275279
const routeYaml = yaml.safeDump(ingressRoute, {
276280
lineWidth: Number.MAX_SAFE_INTEGER

src/commands/project/init.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ describe("Initializing a blank/new bedrock repository", () => {
3535
}
3636

3737
// ensure service specific files do not get created
38-
const filepathsShouldNotExist = ["Dockerfile", "azure-pipelines.yaml"].map(
39-
filename => path.join(randomTmpDir, filename)
40-
);
38+
const filepathsShouldNotExist = [
39+
"Dockerfile",
40+
"azure-pipelines.yaml"
41+
].map(filename => path.join(randomTmpDir, filename));
4142
for (const filepath of filepathsShouldNotExist) {
4243
expect(fs.existsSync(filepath)).toBe(false);
4344
}
@@ -65,7 +66,8 @@ describe("initializing an existing file does not modify it", () => {
6566
path: "./",
6667
sha: "bar"
6768
}
68-
}
69+
},
70+
k8sServicePort: 1337
6971
}
7072
}
7173
};

src/commands/project/init.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ const generateBedrockFile = async (
216216
};
217217

218218
file.services["./" + relPathToPackageFromRoot] = {
219-
helm
219+
helm,
220+
k8sServicePort: 80
220221
};
221222
return file;
222223
},

src/commands/service/create.test.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import os from "os";
33
import path from "path";
44
import { promisify } from "util";
55
import uuid from "uuid/v4";
6-
import { Bedrock } from "../../config";
6+
import { Bedrock, BedrockAsync } from "../../config";
77
import { checkoutCommitPushCreatePRLink } from "../../lib/gitutils";
88
import {
99
disableVerboseLogging,
@@ -39,7 +39,8 @@ describe("validate pipeline config", () => {
3939
"my,middleware,string",
4040
true,
4141
"testVariableGroup",
42-
"testDisplayName"
42+
"testDisplayName",
43+
80
4344
];
4445

4546
it("config is valid", () => {
@@ -81,7 +82,14 @@ describe("Adding a service to a repo directory", () => {
8182
);
8283

8384
// addService call
84-
await createService(randomTmpDir, serviceName, packageDir, false);
85+
const k8sServicePort = 1337;
86+
await createService(
87+
randomTmpDir,
88+
serviceName,
89+
packageDir,
90+
false,
91+
k8sServicePort
92+
);
8593

8694
// Check temp test directory exists
8795
expect(fs.existsSync(randomTmpDir)).toBe(true);
@@ -100,6 +108,10 @@ describe("Adding a service to a repo directory", () => {
100108
}
101109

102110
// TODO: Verify root project bedrock.yaml and maintainers.yaml has been changed too.
111+
const bedrock = Bedrock(randomTmpDir);
112+
const newService = bedrock.services["./" + serviceName];
113+
expect(newService).toBeDefined();
114+
expect(newService.k8sServicePort).toBe(k8sServicePort);
103115
});
104116

105117
test("New directory is created under '/packages' directory with required service files.", async () => {
@@ -117,7 +129,7 @@ describe("Adding a service to a repo directory", () => {
117129
);
118130

119131
// addService call
120-
await createService(randomTmpDir, serviceName, "packages", false);
132+
await createService(randomTmpDir, serviceName, "packages", false, 1337);
121133

122134
// Check temp test directory exists
123135
expect(fs.existsSync(randomTmpDir)).toBe(true);
@@ -153,7 +165,7 @@ describe("Adding a service to a repo directory", () => {
153165
);
154166

155167
// addService call
156-
await createService(randomTmpDir, serviceName, "packages", true);
168+
await createService(randomTmpDir, serviceName, "packages", true, 1337);
157169

158170
// Check temp test directory exists
159171
expect(fs.existsSync(randomTmpDir)).toBe(true);
@@ -187,7 +199,7 @@ describe("Adding a service to a repo directory", () => {
187199
);
188200

189201
// create service with no middleware
190-
await createService(randomTmpDir, serviceName, packageDir, false);
202+
await createService(randomTmpDir, serviceName, packageDir, false, 1337);
191203

192204
// Check temp test directory exists
193205
expect(fs.existsSync(randomTmpDir)).toBe(true);
@@ -225,7 +237,7 @@ describe("Adding a service to a repo directory", () => {
225237

226238
// add some middlewares
227239
const middlewares = ["foo", "bar", "baz"];
228-
await createService(randomTmpDir, serviceName, packageDir, false, {
240+
await createService(randomTmpDir, serviceName, packageDir, false, 1337, {
229241
middlewares
230242
});
231243

@@ -267,4 +279,4 @@ const writeSampleBedrockFileToDir = async (bedrockFilePath: string) => {
267279
createTestBedrockYaml(),
268280
"utf8"
269281
);
270-
};
282+
};

src/commands/service/create.ts

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -85,24 +85,30 @@ export const createCommandDecorator = (command: commander.Command): void => {
8585
"Traefik2 middlewares you wish to to be injected into your Traefik2 IngressRoutes",
8686
""
8787
)
88+
.option(
89+
"--k8s-service-port <port>",
90+
"Kubernetes service port which this service is exposed with; will be used to configure Traefik2 IngressRoutes",
91+
"80"
92+
)
8893
.action(async (serviceName, opts) => {
8994
const bedrock = await BedrockAsync().catch(err => {
9095
logger.warn(err);
9196
return undefined;
9297
});
9398
const {
9499
displayName,
100+
gitPush,
95101
helmChartChart,
96102
helmChartRepository,
97103
helmConfigBranch,
98-
helmConfigPath,
99104
helmConfigGit,
100-
packagesDir,
101-
maintainerName,
105+
helmConfigPath,
102106
maintainerEmail,
107+
maintainerName,
103108
middlewares,
104-
gitPush
109+
packagesDir
105110
} = opts;
111+
const k8sPort = Number(opts.k8sServicePort);
106112
const variableGroupName =
107113
opts.variableGroupName ?? (bedrock?.variableGroups ?? [])[0] ?? ""; // fall back to bedrock.yaml when <variable-group-name> argument is not specified; default to empty string
108114

@@ -122,27 +128,35 @@ export const createCommandDecorator = (command: commander.Command): void => {
122128
middlewares,
123129
gitPush,
124130
variableGroupName,
125-
displayName
131+
displayName,
132+
k8sPort
126133
)
127134
) {
128135
throw Error(`Invalid configuration provided`);
129136
}
130137

131-
await createService(projectPath, serviceName, packagesDir, gitPush, {
132-
displayName,
133-
helmChartChart,
134-
helmChartRepository,
135-
helmConfigBranch,
136-
helmConfigGit,
137-
helmConfigPath,
138-
maintainerEmail,
139-
maintainerName,
140-
middlewares: (middlewares as string)
141-
.split(",")
142-
.map(str => str.trim()),
143-
variableGroups:
144-
variableGroupName.length > 0 ? [variableGroupName] : []
145-
});
138+
await createService(
139+
projectPath,
140+
serviceName,
141+
packagesDir,
142+
gitPush,
143+
k8sPort,
144+
{
145+
displayName,
146+
helmChartChart,
147+
helmChartRepository,
148+
helmConfigBranch,
149+
helmConfigGit,
150+
helmConfigPath,
151+
maintainerEmail,
152+
maintainerName,
153+
middlewares: (middlewares as string)
154+
.split(",")
155+
.map(str => str.trim()),
156+
variableGroups:
157+
variableGroupName.length > 0 ? [variableGroupName] : []
158+
}
159+
);
146160
} catch (err) {
147161
logger.error(
148162
`Error occurred adding service ${serviceName} to project ${projectPath}`
@@ -181,7 +195,8 @@ export const isValidConfig = (
181195
middlewares: any,
182196
gitPush: any,
183197
variableGroupName: any,
184-
displayName: any
198+
displayName: any,
199+
k8sPort: any
185200
): boolean => {
186201
const missingConfig = [];
187202

@@ -251,6 +266,16 @@ export const isValidConfig = (
251266
`variableGroupName must be of type 'string', ${typeof variableGroupName} given.`
252267
);
253268
}
269+
// k8sPort has to be a positive integer
270+
if (
271+
typeof k8sPort !== "number" ||
272+
!Number.isInteger(k8sPort) ||
273+
k8sPort < 0
274+
) {
275+
missingConfig.push(
276+
`k8s-port must be a positive integer, parsed ${k8sPort} from input.`
277+
);
278+
}
254279

255280
if (missingConfig.length > 0) {
256281
logger.error("Error in configuration: " + missingConfig.join(" "));
@@ -272,6 +297,7 @@ export const createService = async (
272297
serviceName: string,
273298
packagesDir: string,
274299
gitPush: boolean,
300+
k8sServicePort: number,
275301
opts?: {
276302
displayName?: string;
277303
helmChartChart?: string;
@@ -362,7 +388,8 @@ export const createService = async (
362388
newServiceRelativeDir,
363389
displayName,
364390
helmConfig,
365-
middlewares
391+
middlewares,
392+
k8sServicePort
366393
);
367394

368395
// If requested, create new git branch, commit, and push

src/config.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ describe("Bedrock", () => {
1818
chart: "elastic",
1919
repository: "some-repo"
2020
}
21-
}
21+
},
22+
k8sServicePort: 1337
2223
},
2324
"foo/b": {
2425
helm: {
@@ -27,7 +28,8 @@ describe("Bedrock", () => {
2728
path: "some/path",
2829
sha: "cef8361c62e7a91887625336eb13a8f90dbcf8df"
2930
}
30-
}
31+
},
32+
k8sServicePort: 1337
3133
}
3234
}
3335
};

src/lib/fileutils.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,17 @@ describe("Adding a new service to a Bedrock file", () => {
261261
repository: "somehelmrepository"
262262
}
263263
};
264+
const traefikMiddlewares = ["foo", "bar"];
265+
const k8sServicePort = 8080;
264266

265267
const writeSpy = jest.spyOn(fs, "writeFileSync");
266268
addNewServiceToBedrockFile(
267269
bedrockFilePath,
268270
servicePath,
269271
svcDisplayName,
270-
helmConfig
272+
helmConfig,
273+
traefikMiddlewares,
274+
k8sServicePort
271275
);
272276

273277
const defaultBedrockFileObject = createTestBedrockYaml(false);
@@ -279,7 +283,8 @@ describe("Adding a new service to a Bedrock file", () => {
279283
["./" + servicePath]: {
280284
displayName: svcDisplayName,
281285
helm: helmConfig,
282-
middlewares: []
286+
k8sServicePort,
287+
middlewares: traefikMiddlewares
283288
}
284289
},
285290
variableGroups: []

0 commit comments

Comments
 (0)