diff --git a/src/commands/hld/reconcile.test.ts b/src/commands/hld/reconcile.test.ts index 503a77d13..9beff98b0 100644 --- a/src/commands/hld/reconcile.test.ts +++ b/src/commands/hld/reconcile.test.ts @@ -356,4 +356,78 @@ describe("reconcile tests", () => { expect(dependencies.createMiddlewareForRing).not.toHaveBeenCalled(); expect(dependencies.createIngressRouteForRing).not.toHaveBeenCalled(); }); + + it("does not create service components if the service path is `.`, and a display name does not exist", async () => { + bedrockYaml.services = { + ".": { + disableRouteScaffold: false, + helm: { + chart: { + git, + path, + sha + } + }, + k8sServicePort: 80 + } + }; + + await reconcileHld(dependencies, bedrockYaml, "service", "./path/to/hld"); + + expect(dependencies.createServiceComponent).not.toHaveBeenCalled(); + }); + + it("does create service components if the service path is `.` and a display name does exist", async () => { + const displayName = "fabrikam"; + + bedrockYaml.services = { + ".": { + disableRouteScaffold: false, + displayName, + helm: { + chart: { + git, + path, + sha + } + }, + k8sServicePort: 80 + } + }; + + await reconcileHld(dependencies, bedrockYaml, "service", "./path/to/hld"); + expect(dependencies.createServiceComponent).toHaveBeenCalled(); + + // Second argument of first invocation of createServiceComponent is the service name + expect( + (dependencies.createServiceComponent as jest.Mock).mock.calls[0][2] + ).toBe(displayName); + }); + + it("uses display name over the service path for creating service components", async () => { + const displayName = "fabrikam"; + + bedrockYaml.services = { + "/my/service/path": { + disableRouteScaffold: false, + displayName, + helm: { + chart: { + git, + path, + sha + } + }, + k8sServicePort: 80 + } + }; + + await reconcileHld(dependencies, bedrockYaml, "service", "./path/to/hld"); + expect(dependencies.createServiceComponent).toHaveBeenCalled(); + + // Second argument of first invocation of createServiceComponent is the service name + expect( + (dependencies.createServiceComponent as jest.Mock).mock.calls[0][2] + ).toBe(displayName); + }); }); diff --git a/src/commands/hld/reconcile.ts b/src/commands/hld/reconcile.ts index 169738d83..2e4b9c698 100644 --- a/src/commands/hld/reconcile.ts +++ b/src/commands/hld/reconcile.ts @@ -151,15 +151,24 @@ export const reconcileHld = async ( for (const [serviceRelPath, serviceConfig] of Object.entries( managedServices )) { - const pathBase = path.basename(serviceRelPath); - const serviceName = pathBase; - logger.info(`Reconciling service: ${pathBase}`); + const serviceName = + serviceConfig.displayName || path.basename(serviceRelPath); + + // No name, cannot generate proper routes and middlewares + if (serviceName === "." || !serviceName) { + logger.warn( + "Service displayName not provided or service path is `.` - not reconciling service" + ); + continue; + } + + logger.info(`Reconciling service: ${serviceName}`); // Utilizes fab add, which is idempotent. await dependencies.createServiceComponent( dependencies.exec, absRepositoryInHldPath, - pathBase + serviceName ); // No rings @@ -168,7 +177,7 @@ export const reconcileHld = async ( } // Create ring components. - const svcPathInHld = path.join(absRepositoryInHldPath, pathBase); + const svcPathInHld = path.join(absRepositoryInHldPath, serviceName); // Will only loop over _existing_ rings in bedrock.yaml - does not cover the deletion case, ie: i remove a ring from bedrock.yaml for (const ring of Object.keys(managedRings)) { @@ -326,12 +335,12 @@ export const createRepositoryComponent = async ( export const createServiceComponent = async ( execCmd: (commandToRun: string) => Promise, absRepositoryInHldPath: string, - pathBase: string + serviceName: string ) => { // Fab add is idempotent. // mkdir -p does not fail if ${pathBase} does not exist. await execCmd( - `cd ${absRepositoryInHldPath} && mkdir -p ${pathBase} config && fab add ${pathBase} --path ./${pathBase} --method local --type component && touch ./config/common.yaml` + `cd ${absRepositoryInHldPath} && mkdir -p ${serviceName} config && fab add ${serviceName} --path ./${serviceName} --method local --type component && touch ./config/common.yaml` ); };