Skip to content

Commit 3158436

Browse files
feat: mount application.env as a configMap (#310)
Signed-off-by: Jeromy Cannon <[email protected]>
1 parent 4f603e7 commit 3158436

File tree

7 files changed

+71
-29
lines changed

7 files changed

+71
-29
lines changed

src/commands/init.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ export class InitCommand extends BaseCommand {
161161
flags.keyFormat,
162162
flags.fstChartVersion,
163163
flags.profileName,
164-
flags.profileFile
164+
flags.profileFile,
165+
flags.applicationEnv
165166
)
166167
},
167168
handler: (argv) => {

src/commands/network.mjs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ export class NetworkCommand extends BaseCommand {
7373
}
7474

7575
const profileName = this.configManager.getFlag(flags.profileName)
76-
const profileValuesFile = await this.profileManager.prepareValuesForFstChart(profileName)
77-
if (profileValuesFile) {
78-
valuesArg += this.prepareValuesFiles(profileValuesFile)
76+
this.profileValuesFile = await this.profileManager.prepareValuesForFstChart(profileName, config.applicationEnv)
77+
if (this.profileValuesFile) {
78+
valuesArg += this.prepareValuesFiles(this.profileValuesFile)
7979
}
8080

8181
// do not deploy mirror node until after we have the updated address book
@@ -126,7 +126,8 @@ export class NetworkCommand extends BaseCommand {
126126
tlsClusterIssuerType: this.configManager.getFlag(flags.tlsClusterIssuerType),
127127
enableHederaExplorerTls: this.configManager.getFlag(flags.enableHederaExplorerTls),
128128
hederaExplorerTlsHostName: this.configManager.getFlag(flags.hederaExplorerTlsHostName),
129-
enablePrometheusSvcMonitor: this.configManager.getFlag(flags.enablePrometheusSvcMonitor)
129+
enablePrometheusSvcMonitor: this.configManager.getFlag(flags.enablePrometheusSvcMonitor),
130+
applicationEnv: this.configManager.getFlag(flags.applicationEnv)
130131
}
131132

132133
// compute values
@@ -418,7 +419,8 @@ export class NetworkCommand extends BaseCommand {
418419
flags.enablePrometheusSvcMonitor,
419420
flags.fstChartVersion,
420421
flags.profileFile,
421-
flags.profileName
422+
flags.profileName,
423+
flags.applicationEnv
422424
),
423425
handler: argv => {
424426
networkCmd.logger.debug('==== Running \'network deploy\' ===')
@@ -469,7 +471,8 @@ export class NetworkCommand extends BaseCommand {
469471
flags.enableHederaExplorerTls,
470472
flags.hederaExplorerTlsLoadBalancerIp,
471473
flags.hederaExplorerTlsHostName,
472-
flags.enablePrometheusSvcMonitor
474+
flags.enablePrometheusSvcMonitor,
475+
flags.applicationEnv
473476
),
474477
handler: argv => {
475478
networkCmd.logger.debug('==== Running \'chart upgrade\' ===')

src/commands/node.mjs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,6 @@ export class NodeCommand extends BaseCommand {
640640
ctx.config = {
641641
namespace: self.configManager.getFlag(flags.namespace),
642642
nodeIds: helpers.parseNodeIds(self.configManager.getFlag(flags.nodeIDs)),
643-
applicationEnv: self.configManager.getFlag(flags.applicationEnv),
644643
cacheDir: self.configManager.getFlag(flags.cacheDir)
645644
}
646645

@@ -907,7 +906,6 @@ export class NodeCommand extends BaseCommand {
907906
releaseTag: self.configManager.getFlag(flags.releaseTag),
908907
cacheDir: self.configManager.getFlag(flags.cacheDir),
909908
force: self.configManager.getFlag(flags.force),
910-
applicationEnv: self.configManager.getFlag(flags.applicationEnv),
911909
keyFormat: self.configManager.getFlag(flags.keyFormat),
912910
devMode: self.configManager.getFlag(flags.devMode),
913911
curDate: new Date()
@@ -1481,15 +1479,6 @@ export class NodeCommand extends BaseCommand {
14811479
title: `Start node: ${chalk.yellow(nodeId)}`,
14821480
task: async () => {
14831481
await this.k8.execContainer(podName, constants.ROOT_CONTAINER, ['bash', '-c', `rm -rf ${constants.HEDERA_HAPI_PATH}/output/*`])
1484-
1485-
// copy application.env file if required
1486-
if (config.applicationEnv) {
1487-
const stagingDir = Templates.renderStagingDir(this.configManager, flags)
1488-
const applicationEnvFile = path.join(stagingDir, 'application.env')
1489-
fs.cpSync(config.applicationEnv, applicationEnvFile)
1490-
await this.k8.copyTo(podName, constants.ROOT_CONTAINER, applicationEnvFile, `${constants.HEDERA_HAPI_PATH}`)
1491-
}
1492-
14931482
await this.k8.execContainer(podName, constants.ROOT_CONTAINER, ['systemctl', 'restart', 'network-node'])
14941483
}
14951484
})
@@ -1576,8 +1565,7 @@ export class NodeCommand extends BaseCommand {
15761565
desc: 'Start a node',
15771566
builder: y => flags.setCommandFlags(y,
15781567
flags.namespace,
1579-
flags.nodeIDs,
1580-
flags.applicationEnv
1568+
flags.nodeIDs
15811569
),
15821570
handler: argv => {
15831571
nodeCmd.logger.debug('==== Running \'node start\' ===')
@@ -1643,7 +1631,6 @@ export class NodeCommand extends BaseCommand {
16431631
flags.nodeIDs,
16441632
flags.releaseTag,
16451633
flags.cacheDir,
1646-
flags.applicationEnv,
16471634
flags.keyFormat
16481635
),
16491636
handler: argv => {

src/core/platform_installer.mjs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,19 @@ export class PlatformInstaller {
234234
if (!podName) throw new MissingArgumentError('podName is required')
235235
if (!destPath) throw new MissingArgumentError('destPath is required')
236236

237+
const recursiveFlag = recursive ? '-R' : ''
237238
try {
238-
const recursiveFlag = recursive ? '-R' : ''
239239
await this.k8.execContainer(podName, container, `chown ${recursiveFlag} hedera:hedera ${destPath}`)
240+
} catch (e) {
241+
// ignore error, can't change settings on files that come from configMaps or secrets
242+
}
243+
try {
240244
await this.k8.execContainer(podName, container, `chmod ${recursiveFlag} ${mode} ${destPath}`)
241-
return true
242245
} catch (e) {
243-
throw new FullstackTestingError(`failed to set permission in '${podName}': ${destPath}`, e)
246+
// ignore error, can't change settings on files that come from configMaps or secrets
244247
}
248+
249+
return true
245250
}
246251

247252
async setPlatformDirPermissions (podName) {

src/core/profile_manager.mjs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,11 @@ export class ProfileManager {
212212

213213
/**
214214
* Prepare a values file for FST Helm chart
215-
* @param profileName resource profile name
215+
* @param {string} profileName resource profile name
216+
* @param {string} applicationEnvFilePath path to the application.env file
216217
* @return {Promise<string>} return the full path to the values file
217218
*/
218-
prepareValuesForFstChart (profileName) {
219+
prepareValuesForFstChart (profileName, applicationEnvFilePath = '') {
219220
if (!profileName) throw new MissingArgumentError('profileName is required')
220221
const profile = this.getProfile(profileName)
221222

@@ -229,6 +230,10 @@ export class ProfileManager {
229230
this.resourcesForEnvoyProxyPod(profile, yamlRoot)
230231
this.resourcesForMinioTenantPod(profile, yamlRoot)
231232

233+
if (applicationEnvFilePath) {
234+
this._setFileContentsAsValue('hedera.configMaps.applicationEnv', applicationEnvFilePath, yamlRoot)
235+
}
236+
232237
// write the yaml
233238
const cachedValuesFile = path.join(this.cacheDir, `fst-${profileName}.yaml`)
234239
return new Promise((resolve, reject) => {
@@ -308,4 +313,16 @@ export class ProfileManager {
308313
})
309314
})
310315
}
316+
317+
/**
318+
* Writes the contents of a file as a value for the given nested item path in the yaml object
319+
* @param {string} itemPath nested item path in the yaml object to store the file contents
320+
* @param {string} valueFilePath path to the file whose contents will be stored in the yaml object
321+
* @param {Object} yamlRoot root of the yaml object
322+
* @private
323+
*/
324+
_setFileContentsAsValue (itemPath, valueFilePath, yamlRoot) {
325+
const fileContents = fs.readFileSync(valueFilePath, 'utf8')
326+
this._setValue(itemPath, fileContents, yamlRoot)
327+
}
311328
}

test/e2e/commands/network.test.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
import {
2727
bootstrapTestVariables,
2828
getDefaultArgv,
29+
getTmpDir,
2930
HEDERA_PLATFORM_VERSION_TAG
3031
} from '../../test_util.js'
3132
import {
@@ -34,10 +35,15 @@ import {
3435
import { flags } from '../../../src/commands/index.mjs'
3536
import * as version from '../../../version.mjs'
3637
import { sleep } from '../../../src/core/helpers.mjs'
38+
import path from 'path'
39+
import fs from 'fs'
3740

3841
describe('NetworkCommand', () => {
3942
const testName = 'network-cmd-e2e'
4043
const namespace = testName
44+
const applicationEnvFileContents = '# row 1\n# row 2\n# row 3'
45+
const applicationEnvParentDirectory = path.join(getTmpDir(), 'network-command-test')
46+
const applicationEnvFilePath = path.join(applicationEnvParentDirectory, 'application.env')
4147
const argv = getDefaultArgv()
4248
argv[flags.namespace.name] = namespace
4349
argv[flags.releaseTag.name] = HEDERA_PLATFORM_VERSION_TAG
@@ -48,6 +54,7 @@ describe('NetworkCommand', () => {
4854
argv[flags.deployMinio.name] = true
4955
argv[flags.fstChartVersion.name] = version.FST_CHART_VERSION
5056
argv[flags.force.name] = true
57+
argv[flags.applicationEnv.name] = applicationEnvFilePath
5158
// set the env variable SOLO_FST_CHARTS_DIR if developer wants to use local FST charts
5259
argv[flags.chartDirectory.name] = process.env.SOLO_FST_CHARTS_DIR ? process.env.SOLO_FST_CHARTS_DIR : undefined
5360

@@ -66,6 +73,8 @@ describe('NetworkCommand', () => {
6673

6774
beforeAll(async () => {
6875
await clusterCmd.setup(argv)
76+
fs.mkdirSync(applicationEnvParentDirectory, { recursive: true })
77+
fs.writeFileSync(applicationEnvFilePath, applicationEnvFileContents)
6978
})
7079

7180
it('network deploy command should succeed', async () => {
@@ -85,6 +94,15 @@ describe('NetworkCommand', () => {
8594
}
8695
}, 240000)
8796

97+
it('application env file contents should be in cached values file', async () => {
98+
expect.assertions(3)
99+
const valuesYaml = fs.readFileSync(networkCmd.profileValuesFile).toString()
100+
const fileRows = applicationEnvFileContents.split('\n')
101+
for (const fileRow of fileRows) {
102+
expect(valuesYaml).toContain(fileRow)
103+
}
104+
})
105+
88106
it('network destroy should success', async () => {
89107
argv[flags.deletePvcs.name] = true
90108
configManager.update(argv, true)

test/unit/core/profile_manager.test.mjs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe('ProfileManager', () => {
6868
expect(fs.existsSync(valuesFile)).toBeTruthy()
6969

7070
// validate the yaml
71-
const valuesYaml = yaml.load(fs.readFileSync(valuesFile))
71+
const valuesYaml = yaml.load(fs.readFileSync(valuesFile).toString())
7272
expect(valuesYaml.hedera.nodes.length).toStrictEqual(3)
7373
expect(valuesYaml.defaults.root.resources.limits.cpu).not.toBeNull()
7474
expect(valuesYaml.defaults.root.resources.limits.memory).not.toBeNull()
@@ -98,7 +98,7 @@ describe('ProfileManager', () => {
9898
expect(fs.existsSync(valuesFile)).toBeTruthy()
9999

100100
// validate yaml
101-
const valuesYaml = yaml.load(fs.readFileSync(valuesFile))
101+
const valuesYaml = yaml.load(fs.readFileSync(valuesFile).toString())
102102
expect(valuesYaml['hedera-mirror-node'].postgresql.persistence.size).not.toBeNull()
103103
expect(valuesYaml['hedera-mirror-node'].postgresql.postgresql.resources.limits.cpu).not.toBeNull()
104104
expect(valuesYaml['hedera-mirror-node'].postgresql.postgresql.resources.limits.memory).not.toBeNull()
@@ -118,9 +118,20 @@ describe('ProfileManager', () => {
118118
const valuesFile = await profileManager.prepareValuesForRpcRelayChart(input.profileName)
119119
expect(fs.existsSync(valuesFile)).toBeTruthy()
120120
// validate yaml
121-
const valuesYaml = yaml.load(fs.readFileSync(valuesFile))
121+
const valuesYaml = yaml.load(fs.readFileSync(valuesFile).toString())
122122
expect(valuesYaml.resources.limits.cpu).not.toBeNull()
123123
expect(valuesYaml.resources.limits.memory).not.toBeNull()
124124
})
125125
})
126+
127+
it('prepareValuesForFstChart should set the value of a key to the contents of a file', async () => {
128+
configManager.setFlag(flags.profileFile, testProfileFile)
129+
// profileManager.loadProfiles(true)
130+
const file = path.join(tmpDir, '_setFileContentsAsValue.txt')
131+
const fileContents = '# row 1\n# row 2\n# row 3'
132+
fs.writeFileSync(file, fileContents)
133+
const cachedValuesFile = await profileManager.prepareValuesForFstChart('test', file)
134+
const valuesYaml = yaml.load(fs.readFileSync(cachedValuesFile).toString())
135+
expect(valuesYaml.hedera.configMaps.applicationEnv).toEqual(fileContents)
136+
})
126137
})

0 commit comments

Comments
 (0)