Skip to content

Commit

Permalink
Fix: nondeterministic fetching of env/envFrom (fixes #116) (#120)
Browse files Browse the repository at this point in the history
* Fix: nondeterministic fetching of env/envFrom

Also moved the code for env into its own function to be more consistent.

* Update: simplify double loop and regex
  • Loading branch information
alewitt2 authored Jun 30, 2020
1 parent 329a4b2 commit 9040e40
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions lib/FetchEnvs.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ module.exports = class FetchEnvs {
result = Number(result);
break;
case 'boolean':
result = (result === 'true');
result = (result.toLowerCase() === 'true');
break;
case 'json':
result = JSON.parse(result);
Expand All @@ -108,10 +108,8 @@ module.exports = class FetchEnvs {
return (result);
}


async _getMaps(envFrom) {
const result = [];
await Promise.all(envFrom.map(async (element) => {
async _processEnvFrom(envFrom) {
return await Promise.all(envFrom.map(async (element) => {
const mapData = clone(element);
let optional = objectPath.get(element, 'optional', false);
if (objectPath.has(element, 'configMapRef')) {
Expand Down Expand Up @@ -180,29 +178,16 @@ module.exports = class FetchEnvs {
this.updateRazeeLogs('warn', { controller: 'FetchEnvs', warn: msg });
}
}
result.push(mapData);
return mapData;
}));
return result;
}

async get(path = 'spec') {
let result = {};
let envFrom = objectPath.get(this.data, `object.${path.replace(/^\.*|\.*$|(\.envFrom\.*$)|(\.env\.*$)/g, '')}.envFrom`, []);
envFrom = await this._getMaps(envFrom);
let data = envFrom.map((e) => { return e.data ? e.data : []; }); // data = [data{}, ...]
data.forEach((e) => {
if (e) {
Object.assign(result, e);
}
});

let env = objectPath.get(this.data, `object.${path.replace(/^\.*|\.*$|(\.envFrom\.*$)|(\.env\.*$)/g, '')}.env`, []);
await Promise.all(env.map(async (e) => {
let name = e.name;
async _processEnv(env) {
return await Promise.all(env.map(async (e) => {
let optional = objectPath.get(e, 'optional', false);
let defaultValue = objectPath.get(e, 'default');
if (e.value) {
result[name] = e.value;
return e;
} else if (e.valueFrom) {
let value = await this._lookupDataReference(e.valueFrom);
if (value === undefined && defaultValue === undefined) {
Expand All @@ -215,17 +200,39 @@ module.exports = class FetchEnvs {
}
} else {
if (value !== undefined) {
result[name] = value;
e.value = value;
} else if (defaultValue !== undefined) {
result[name] = defaultValue;
e.value = defaultValue;

let msg = `${JSON.stringify(e.valueFrom)} not found. Using default value.`;
log.info(msg);
this.updateRazeeLogs('info', { controller: 'FetchEnvs', warn: msg });
}
}
return e;
}
}));
}

async get(path = 'spec') {
let result = {};
// removes any number of '.' at the start and end of the path, and
// removes the '.env' or '.envFrom' if the paths ends in either
path = path.replace(/^\.*|\.*$|(\.envFrom\.*$)|(\.env\.*$)/g, '');
let envFrom = objectPath.get(this.data, `object.${path}.envFrom`, []);
envFrom = await this._processEnvFrom(envFrom);
envFrom.forEach((e) => {
let data = objectPath.get(e, 'data', {});
Object.assign(result, data);
});

let env = objectPath.get(this.data, `object.${path}.env`, []);
env = await this._processEnv(env);
env.forEach((e) => {
if (e.value !== undefined) {
result[e.name] = e.value;
}
});
return result;
}
};

0 comments on commit 9040e40

Please sign in to comment.