Skip to content

Commit 7d88f7a

Browse files
authored
fix: update merging of services (#30)
* fix: update merge * lint
1 parent 33fb167 commit 7d88f7a

File tree

2 files changed

+27
-48
lines changed

2 files changed

+27
-48
lines changed

src/service.ts

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { run_v1 } from 'googleapis';
18-
import { get } from 'lodash';
18+
import { get, merge } from 'lodash';
1919
import fs from 'fs';
2020
import YAML from 'yaml';
2121

@@ -61,7 +61,7 @@ export function parseEnvVars(envVarInput: string): run_v1.Schema$EnvVar[] {
6161
* @returns Service.
6262
*/
6363
export class Service {
64-
readonly request: run_v1.Schema$Service;
64+
request: run_v1.Schema$Service;
6565
readonly name: string;
6666

6767
constructor(opts: ServiceOptions) {
@@ -140,58 +140,35 @@ export class Service {
140140
const name = get(this.request, 'spec.template.metadata.name');
141141
const previousName = get(prevService, 'spec.template.metadata.name');
142142

143-
// Merge Revision metadata
144-
const labels = {
145-
...prevService.spec?.template?.metadata?.labels,
146-
...this.request.spec?.template?.metadata?.labels,
147-
};
148-
const annotations = {
149-
...prevService.spec?.template?.metadata?.annotations,
150-
...this.request.spec?.template?.metadata?.annotations,
151-
};
152-
this.request.spec!.template!.metadata = {
153-
annotations,
154-
labels,
155-
};
143+
// Deep Merge Service
144+
const mergedServices = merge(prevService, this.request);
156145

157146
// Force update with Revision name change
158-
this.request.spec!.template!.metadata!.name = this.generateRevisionName(
147+
mergedServices.spec!.template!.metadata!.name = this.generateRevisionName(
159148
name,
160149
previousName,
161150
);
162151

163152
// Merge Container spec
164-
const prevContainer = prevService.spec!.template!.spec!.containers![0];
165-
const currentContainer = this.request.spec!.template!.spec!.containers![0];
166-
const container = { ...prevContainer, ...currentContainer };
167-
168-
// Merge Revision spec
169-
const spec = {
170-
...prevService.spec?.template?.spec,
171-
...this.request.spec!.template!.spec,
172-
};
173-
if (!currentContainer.command) {
174-
// Remove entrypoint cmd and arguments if not specified
175-
delete container.command;
176-
delete container.args;
177-
}
153+
const prevEnvVars = prevService.spec!.template!.spec!.containers![0].env;
154+
const currentEnvVars = this.request.spec!.template!.spec!.containers![0]
155+
.env;
178156

179157
// Merge Env vars
180158
let env: run_v1.Schema$EnvVar[] = [];
181-
if (currentContainer.env) {
182-
env = currentContainer.env.map(
183-
(envVar) => envVar as run_v1.Schema$EnvVar,
184-
);
159+
if (currentEnvVars) {
160+
env = currentEnvVars.map((envVar) => envVar as run_v1.Schema$EnvVar);
185161
}
186162
const keys = env?.map((envVar) => envVar.name);
187-
prevContainer.env?.forEach((envVar) => {
163+
prevEnvVars?.forEach((envVar) => {
188164
if (!keys.includes(envVar.name)) {
165+
// Add old env vars without duplicating
189166
return env.push(envVar);
190167
}
191168
});
192-
container.env = env;
193-
spec.containers = [container];
194-
this.request.spec!.template!.spec = spec;
169+
// Set Env vars
170+
mergedServices.spec!.template!.spec!.containers![0].env = env;
171+
this.request = mergedServices;
195172
}
196173

197174
private generateRevisionName(name?: string, prevName?: string): string {

tests/e2e.test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import * as _ from 'lodash';
2222
import 'mocha';
2323
import { run_v1 } from 'googleapis';
2424

25-
describe('E2E tests', function() {
25+
describe('E2E tests', function () {
2626
const {
2727
PARAMS,
2828
ANNOTATIONS,
@@ -36,7 +36,7 @@ describe('E2E tests', function() {
3636
let URL: string;
3737
let client: CloudRun;
3838
let service: run_v1.Schema$Service;
39-
before(async function() {
39+
before(async function () {
4040
if (process.env.URL) {
4141
URL = process.env.URL;
4242
} else {
@@ -50,7 +50,7 @@ describe('E2E tests', function() {
5050
}
5151
});
5252

53-
it('can make a request', async function() {
53+
it('can make a request', async function () {
5454
// Requires ADC to be set
5555
const auth = new GoogleAuth();
5656
const client = await auth.getIdTokenClient(URL);
@@ -59,7 +59,7 @@ describe('E2E tests', function() {
5959
expect(response.data).to.include('Congrat');
6060
});
6161

62-
it('has the correct env vars', function() {
62+
it('has the correct env vars', function () {
6363
if (ENV && service) {
6464
const expected = parseEnvVars(ENV);
6565
const containers = _.get(service, 'spec.template.spec.containers');
@@ -74,7 +74,7 @@ describe('E2E tests', function() {
7474
}
7575
});
7676

77-
it('has the correct params', function() {
77+
it('has the correct params', function () {
7878
if (PARAMS && service) {
7979
const expected = JSON.parse(PARAMS);
8080
const actual = _.get(service, 'spec.template.spec');
@@ -97,7 +97,7 @@ describe('E2E tests', function() {
9797
}
9898
});
9999

100-
it('has the correct annotations', function() {
100+
it('has the correct annotations', function () {
101101
if (ANNOTATIONS && service) {
102102
const expected = JSON.parse(ANNOTATIONS);
103103
const actual = _.get(service, 'spec.template.metadata.annotations');
@@ -111,7 +111,7 @@ describe('E2E tests', function() {
111111
}
112112
});
113113

114-
it('has the correct labels', function() {
114+
it('has the correct labels', function () {
115115
if (LABELS && service) {
116116
const expected = JSON.parse(LABELS);
117117
const actual = _.get(service, 'spec.template.metadata.labels');
@@ -125,15 +125,17 @@ describe('E2E tests', function() {
125125
}
126126
});
127127

128-
it('has the correct revision count', async function() {
128+
it('has the correct revision count', async function () {
129129
if (COUNT && SERVICE) {
130130
const revisions = await client.listRevisions();
131-
const filtered = revisions.filter((name) => name.includes(SERVICE.substring(0, 52)));
131+
const filtered = revisions.filter((name) =>
132+
name.includes(SERVICE.substring(0, 52)),
133+
);
132134
expect(filtered.length).to.equal(parseInt(COUNT));
133135
}
134136
});
135137

136-
it('has the correct revision name', function() {
138+
it('has the correct revision name', function () {
137139
if (REVISION && service) {
138140
const actual = _.get(service, 'spec.template.metadata.name');
139141
expect(REVISION).to.equal(actual);

0 commit comments

Comments
 (0)