Skip to content

Commit

Permalink
feat: otomi chart (#37)
Browse files Browse the repository at this point in the history
task for otomi-chart to merge values
  • Loading branch information
Maurice Faber authored Jul 8, 2021
1 parent 9566a6b commit 47219a7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ rules:
no-unused-vars: error
prefer-destructuring: error
no-use-before-define: error
no-console: off
no-console: warn
object-shorthand: error
no-debugger: error
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,5 @@
"tag": true
}
},
"version": "0.2.7"
}
"version": "0.2.6"
}
41 changes: 16 additions & 25 deletions src/tasks/otomi/otomi-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { omit, merge, pick } from 'lodash'
import yaml from 'js-yaml'
import fs from 'fs'
import $RefParser from '@apidevtools/json-schema-ref-parser'

import { cleanEnv, OTOMI_VALUES_INPUT, OTOMI_SCHEMA_PATH, OTOMI_ENV_DIR } from '../../validators'
import { cleanValues } from '../../utils'

const env = cleanEnv({
OTOMI_VALUES_INPUT,
Expand All @@ -13,6 +13,9 @@ const env = cleanEnv({

const schemaKeywords = ['properties', 'anyOf', 'allOf', 'oneOf']

let suffix = ''
if (fs.existsSync(`${env.OTOMI_ENV_DIR}/.sops.yaml`)) suffix = '.dec'

export function extractSecrets(schema: any, parentAddress?: string): Array<string> {
return Object.keys(schema)
.flatMap((key) => {
Expand All @@ -28,30 +31,20 @@ export function extractSecrets(schema: any, parentAddress?: string): Array<strin
.filter(Boolean) as Array<string>
}

function mergeValues(targetPath: string, newValues): void {
let values
if (fs.existsSync(targetPath)) {
if (targetPath.includes('/secrets.')) {
values = yaml.load(fs.readFileSync(`${targetPath}.dec`).toString())
} else {
values = yaml.load(fs.readFileSync(targetPath).toString())
}

if (!values) {
values = {}
}

merge(values, newValues)

if (targetPath.includes('/secrets.')) {
fs.writeFileSync(`${targetPath}.dec`, yaml.safeDump(values))
} else {
fs.writeFileSync(targetPath, yaml.safeDump(values))
}
} else {
// if the targetPath doesn't exist, just create it and write the valueObject on it. Doesn't matter if it is secret or not. and always write in its yaml file
function mergeValues(targetPath: string, inValues: object): void {
const newValues = cleanValues(inValues)
// console.debug(`targetPath: ${targetPath}, values: ${JSON.stringify(newValues)}`)
if (!fs.existsSync(targetPath)) {
// If the targetPath doesn't exist, just create it and write the valueObject in it.
// It doesn't matter if it is secret or not. and always write in its yaml file
fs.writeFileSync(targetPath, yaml.safeDump(newValues))
return
}
let useSuffix = suffix
if (!targetPath.includes('/secrets.')) useSuffix = ''
const values = cleanValues(yaml.load(fs.readFileSync(`${targetPath}${useSuffix}`).toString()))
merge(values, newValues)
fs.writeFileSync(`${targetPath}${useSuffix}`, yaml.safeDump(values))
}

export default async function main(): Promise<void> {
Expand All @@ -63,8 +56,6 @@ export default async function main(): Promise<void> {
const cleanSchema = omit(derefSchema, ['definitions', 'properties.teamConfig']) // FIXME: lets fix the team part later
const secretsJsonPath = extractSecrets(cleanSchema)
const secrets = pick(values, secretsJsonPath)
console.log(secretsJsonPath)
console.log(secrets)

// mergeValues(`${env.OTOMI_ENV_DIR}/env/secrets.teams.yaml`, { teamConfig: secrets.teamConfig }) // FIXME: lets fix the team part later
const secretSettings = omit(secrets, ['cluster', 'policies', 'teamConfig', 'charts'])
Expand Down
9 changes: 8 additions & 1 deletion src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import sinon from 'sinon'
import { expect } from 'chai'
import { cloneDeep } from 'lodash'
import http from 'http'
import { createPullSecret, deletePullSecret, getApiClient, objectToArray } from './utils'
import { cleanValues, createPullSecret, deletePullSecret, getApiClient, objectToArray } from './utils'

describe('Utils', () => {
it('should convert an object to array', (done) => {
Expand Down Expand Up @@ -116,4 +116,11 @@ describe('Secret creation', () => {
expect(patchSpy).to.have.been.calledWith('default', namespace, saNewEmpty)
expect(deleteSpy).to.have.been.calledWith(name, namespace)
})

it('should clean an object successfully', async () => {
const inp = { teamConfig: { teams: null } }
const out = { teamConfig: {} }
const res = cleanValues(inp)
expect(res).to.deep.equal(out)
})
})
10 changes: 9 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import http from 'http'
import { findIndex, mapValues } from 'lodash'
import { findIndex, isNil, mapValues, omitBy } from 'lodash'
import { CoreV1Api, KubeConfig, V1Secret, V1ObjectMeta, V1ServiceAccount } from '@kubernetes/client-node'

let apiClient: CoreV1Api
Expand Down Expand Up @@ -185,3 +185,11 @@ export async function deletePullSecret(teamId: string, name: string): Promise<vo
throw new Error(`Secret '${name}' does not exist in namespace '${namespace}'`)
}
}

export function cleanValues(inObj) {
const obj = omitBy(inObj, isNil)
Object.keys(obj).forEach((k) => {
if (typeof obj[k] === 'object') obj[k] = cleanValues(obj[k])
})
return obj
}

0 comments on commit 47219a7

Please sign in to comment.