Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: otomi chart #37

Merged
merged 23 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f58da34
feat: divide and merge chart values to different value files.
mojtabaimani Jun 14, 2021
4f06245
feat: merging all chart values in charts folder
mojtabaimani Jun 15, 2021
b555c7f
feat: adding otomi-chart.ts to map values.yaml to different value files
mojtabaimani Jun 16, 2021
91d07e3
fix: removing schema index from the json path
mojtabaimani Jun 17, 2021
dc2069f
fix: adding env variables for source and target and schema
mojtabaimani Jun 17, 2021
2963fbc
fix: removing console.logs
mojtabaimani Jun 17, 2021
2467d68
fix: unit test added
mojtabaimani Jun 17, 2021
e0aebce
fix: extractSecrets output type fixed.
mojtabaimani Jun 17, 2021
7401ca1
fix: signature of mergeValues function changed
mojtabaimani Jun 17, 2021
48577e0
fix: removing space from the env.sample
mojtabaimani Jun 17, 2021
26ab0aa
fix: fixing the test functions
mojtabaimani Jun 17, 2021
51ac0ec
fix: adding optional parameter to extractSecrets function
mojtabaimani Jun 18, 2021
e716b58
fix: fixing the json sample in test function and export main()
mojtabaimani Jun 18, 2021
4cb5773
fix: fixing function signatures
mojtabaimani Jun 18, 2021
fe253db
fix: isNaN changed to Number.isNaN
mojtabaimani Jun 21, 2021
625210a
fix: import statement in test file fixed.
mojtabaimani Jun 21, 2021
26c09a5
Merge branch 'master' into moj/otomi-chart
Jun 22, 2021
d1a14fa
fix env var names and check input undefined for merge function
mojtabaimani Jun 23, 2021
0be826b
fix: fixing merge function with secret files
mojtabaimani Jun 25, 2021
24d806a
Merge branch 'master' into moj/otomi-chart
Jun 29, 2021
7938d4a
fix: merge logic [ci skip]
mojtabaimani Jul 8, 2021
23ae0c8
Merge branch 'master' into moj/otomi-chart [ci skip]
mojtabaimani Jul 8, 2021
75ec9a3
fix: console logging [ci skip]
mojtabaimani Jul 8, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}