Skip to content

Commit a262565

Browse files
authored
Merge branch 'main' into dependabot/npm_and_yarn/npm-dependencies-4e2696c65d
2 parents 3a13f31 + defdc2f commit a262565

File tree

6 files changed

+50
-26
lines changed

6 files changed

+50
-26
lines changed

src/cmd/apply-as-apps.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import {
55
setHeaderOptions,
66
V1ResourceRequirements,
77
} from '@kubernetes/client-node'
8-
import { existsSync, statSync, mkdirSync, rmSync } from 'fs'
9-
import { glob } from 'glob'
8+
import { existsSync, mkdirSync, rmSync, statSync } from 'fs'
109
import { readFile } from 'fs/promises'
10+
import { glob } from 'glob'
1111
import { appPatches, genericPatch } from 'src/applicationPatches.json'
1212
import { cleanupHandler, prepareEnvironment } from 'src/common/cli'
1313
import { logLevelString, terminal } from 'src/common/debug'
@@ -16,12 +16,11 @@ import { appRevisionMatches, k8s, patchArgoCdApp, patchContainerResourcesOfSts }
1616
import { getFilename, loadYaml } from 'src/common/utils'
1717
import { getImageTagFromValues, objectToYaml } from 'src/common/values'
1818
import { getParsedArgs, HelmArguments, helmOptions, setParsedArgs } from 'src/common/yargs'
19+
import { operatorEnv } from 'src/operator/validators'
1920
import { Argv, CommandModule } from 'yargs'
2021
import { ARGOCD_APP_DEFAULT_SYNC_POLICY, ARGOCD_APP_PARAMS } from '../common/constants'
2122
import { env } from '../common/envalid'
2223

23-
export const GITOPS_MANIFESTS_NS_PATH = 'env/manifests/namespaces'
24-
export const GITOPS_MANIFESTS_GLOBAL_PATH = 'env/manifests/global'
2524
export const ARGOCD_APP_DEFAULT_LABEL = 'managed'
2625
export const ARGOCD_APP_GITOPS_LABEL = 'generic-gitops'
2726
export const ARGOCD_APP_GITOPS_NS_PREFIX = 'gitops-ns'
@@ -153,7 +152,9 @@ export const getArgocdGitopsManifest = (name: string, targetNamespace?: string)
153152
syncPolicy.syncOptions.push('CreateNamespace=true')
154153
}
155154
const repoURL = `${env.GIT_PROTOCOL}://${env.GIT_URL}:${env.GIT_PORT}/otomi/values.git`
156-
const path = targetNamespace ? `${GITOPS_MANIFESTS_NS_PATH}/${targetNamespace}` : GITOPS_MANIFESTS_GLOBAL_PATH
155+
const path = targetNamespace
156+
? `${operatorEnv.GITOPS_NS_MANIFESTS_RELATIVE_PATH}/${targetNamespace}`
157+
: operatorEnv.GITOPS_GLOBAL_MANIFESTS_RELATIVE_PATH
157158
return getArgoCdAppManifest(name, ARGOCD_APP_GITOPS_LABEL, {
158159
project: 'default',
159160
syncPolicy,
@@ -460,13 +461,17 @@ export const calculateGitOpsAppsDiff = async (
460461
deps = { getApplications },
461462
): Promise<{ toAdd: Set<string>; toRemove: Set<string>; namespaceDirs: string[] }> => {
462463
const envDir = env.ENV_DIR
463-
const namespaceListing = await glob(`${envDir}/${GITOPS_MANIFESTS_NS_PATH}/*`, { withFileTypes: true })
464+
const namespaceListing = await glob(`${envDir}/${operatorEnv.GITOPS_NS_MANIFESTS_RELATIVE_PATH}/*`, {
465+
withFileTypes: true,
466+
})
464467
const namespaceDirs = namespaceListing.filter((path) => path.isDirectory()).map((path) => path.name)
465468
const existingGitOpsApps = new Set(await deps.getApplications(`otomi.io/app=${ARGOCD_APP_GITOPS_LABEL}`))
466469

467470
// First create sets of Applications to be updated
468471
const requiredGitOpsApps = new Set(namespaceDirs.map((dirName) => `${ARGOCD_APP_GITOPS_NS_PREFIX}-${dirName}`))
469-
const globalPath = statSync(`${envDir}/${GITOPS_MANIFESTS_GLOBAL_PATH}`, { throwIfNoEntry: false })
472+
const globalPath = statSync(`${envDir}/${operatorEnv.GITOPS_GLOBAL_MANIFESTS_RELATIVE_PATH}`, {
473+
throwIfNoEntry: false,
474+
})
470475
if (globalPath && globalPath.isDirectory()) {
471476
requiredGitOpsApps.add(ARGOCD_APP_GITOPS_GLOBAL_NAME)
472477
}

src/cmd/bootstrap.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ describe('Bootstrapping values', () => {
4040
copyBasicFiles: jest.fn(),
4141
copyFile: jest.fn(),
4242
createCustomCA: jest.fn(),
43+
ensureManifestDirectories: jest.fn(),
4344
handleFileEntry: jest.fn(),
4445
decrypt: jest.fn(),
4546
encrypt: jest.fn(),

src/cmd/bootstrap.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ import {
2121
secretId,
2222
} from 'src/common/k8s'
2323
import { getKmsSettings } from 'src/common/repo'
24-
import { ensureTeamGitOpsDirectories, getFilename, gucci, isCore, loadYaml, rootDir } from 'src/common/utils'
24+
import {
25+
ensureManifestDirectories,
26+
ensureTeamGitOpsDirectories,
27+
getFilename,
28+
gucci,
29+
isCore,
30+
loadYaml,
31+
rootDir,
32+
} from 'src/common/utils'
2533
import { generateSecrets, writeValues } from 'src/common/values'
2634
import { BasicArguments, setParsedArgs } from 'src/common/yargs'
2735
import { Argv } from 'yargs'
@@ -438,6 +446,7 @@ export const bootstrap = async (
438446
encrypt,
439447
decrypt,
440448
handleFileEntry,
449+
ensureManifestDirectories,
441450
},
442451
): Promise<void> => {
443452
const d = deps.terminal(`cmd:${cmdName}:bootstrap`)
@@ -452,6 +461,7 @@ export const bootstrap = async (
452461
const originalValues = await deps.processValues()
453462
await deps.handleFileEntry()
454463
await deps.bootstrapSops()
464+
await deps.ensureManifestDirectories()
455465
await ensureTeamGitOpsDirectories(ENV_DIR, originalValues)
456466
d.log(`Done bootstrapping values`)
457467
}

src/common/utils.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import $RefParser, { JSONSchema } from '@apidevtools/json-schema-ref-parser'
22
import cleanDeep, { CleanOptions } from 'clean-deep'
33
import { createHash } from 'crypto'
44
import { existsSync, readFileSync } from 'fs'
5-
import { readdir, readFile, writeFile } from 'fs/promises'
5+
import { access, mkdir, readdir, readFile, writeFile } from 'fs/promises'
66
import { glob } from 'glob'
77
import walk from 'ignore-walk'
88
import { dump, load } from 'js-yaml'
99
import { omit } from 'lodash'
1010
import { dirname, join, resolve } from 'path'
1111
import { $, ProcessOutput, within } from 'zx'
12+
import { operatorEnv } from '../operator/validators'
1213
import { terminal } from './debug'
1314
import { env } from './envalid'
1415

@@ -205,21 +206,23 @@ export const getSchemaSecretsPaths = async (teams: string[]): Promise<string[]>
205206
return cleanSecretPaths
206207
}
207208

208-
async function ensureKeepFile(keepFilePath: string, deps = { writeFile }): Promise<void> {
209-
const dirPath = dirname(keepFilePath)
210-
if (!existsSync(dirPath)) {
211-
await $`mkdir -p ${dirname(keepFilePath)}`
209+
export async function ensureManifestDirectories(): Promise<void> {
210+
await ensureDirectoryWithGitkeepAsync(join(env.ENV_DIR, operatorEnv.GITOPS_NS_MANIFESTS_RELATIVE_PATH))
211+
await ensureDirectoryWithGitkeepAsync(join(env.ENV_DIR, operatorEnv.GITOPS_GLOBAL_MANIFESTS_RELATIVE_PATH))
212+
}
213+
214+
async function ensureDirectoryWithGitkeepAsync(dirPath: string, deps = { access, writeFile, mkdir }): Promise<void> {
215+
await deps.mkdir(dirPath, { recursive: true })
216+
const gitkeepPath = join(dirPath, '.gitkeep')
217+
218+
try {
219+
await deps.access(gitkeepPath)
220+
} catch {
221+
await deps.writeFile(gitkeepPath, '')
212222
}
213-
if (existsSync(keepFilePath)) return
214-
// create the .keep file
215-
await deps.writeFile(keepFilePath, '')
216223
}
217224

218-
export async function ensureTeamGitOpsDirectories(
219-
envDir: string,
220-
values: Record<string, any>,
221-
deps = { writeFile, glob },
222-
) {
225+
export async function ensureTeamGitOpsDirectories(envDir: string, values: Record<string, any>, deps = { glob }) {
223226
const dirs = await deps.glob(`${envDir}/env/teams/*`)
224227
const baseGitOpsDirs = ['sealedsecrets', 'workloadValues']
225228
const aiGitOpsDirs = ['databases', 'knowledgebases', 'agents']
@@ -236,10 +239,7 @@ export async function ensureTeamGitOpsDirectories(
236239

237240
await Promise.allSettled(
238241
keepFilePaths.map(async (keepFilePath) => {
239-
await ensureKeepFile(keepFilePath, deps)
240-
if (!existsSync(dirname(keepFilePath))) {
241-
await $`mkdir -p ${dirname(keepFilePath)}`
242-
}
242+
await ensureDirectoryWithGitkeepAsync(dirname(keepFilePath))
243243
}),
244244
)
245245
return keepFilePaths

src/operator/apl-operator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { commit } from '../cmd/commit'
33
import { terminal } from '../common/debug'
44
import { env } from '../common/envalid'
55
import { GitRepoConfig } from '../common/git-config'
6-
import { hfValues } from '../common/hf'
76
import { waitTillGitRepoAvailable } from '../common/gitea'
7+
import { hfValues } from '../common/hf'
88
import { ensureTeamGitOpsDirectories } from '../common/utils'
99
import { writeValues } from '../common/values'
1010
import { HelmArguments } from '../common/yargs'

src/operator/validators.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ dotenv.config()
66
export const operatorEnv = cleanEnv(process.env, {
77
GIT_ORG: str({ desc: 'Git organisation', default: 'otomi' }),
88
GIT_REPO: str({ desc: 'Git repository', default: 'values' }),
9+
GITOPS_NS_MANIFESTS_RELATIVE_PATH: str({
10+
desc: 'Path to the gitops manifests namespace',
11+
default: 'env/manifests/namespaces',
12+
}),
13+
GITOPS_GLOBAL_MANIFESTS_RELATIVE_PATH: str({
14+
desc: 'Path to the gitops manifests global',
15+
default: 'env/manifests/global',
16+
}),
917
POLL_INTERVAL_MS: num({ desc: 'Interval in which the operator polls Git', default: 1000 }),
1018
RECONCILE_INTERVAL_MS: num({ desc: 'Interval in which the operator reconciles the cluster in', default: 300_000 }),
1119
INSTALL_RETRIES: num({ desc: 'Number of installation retry attempts', default: 1000 }),

0 commit comments

Comments
 (0)