Skip to content

Commit f1fc677

Browse files
committed
Merge remote-tracking branch 'origin/main' into APL-1260
2 parents 39f1e7b + d6fae09 commit f1fc677

File tree

7 files changed

+47
-30
lines changed

7 files changed

+47
-30
lines changed

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export default defineConfig([
7575
varsIgnorePattern: '^_',
7676
},
7777
],
78+
7879
'@typescript-eslint/no-use-before-define': ['error'],
7980
'@typescript-eslint/restrict-template-expressions': 'off',
8081
'@typescript-eslint/naming-convention': [

src/git.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@ export class Git {
203203
await writeFile(absolutePath, content, 'utf8')
204204
}
205205

206+
async writeTextFile(file: string, content: string): Promise<void> {
207+
const absolutePath = join(this.path, file)
208+
debug(`Writing to file: ${absolutePath}`)
209+
await ensureDir(dirname(absolutePath))
210+
await writeFile(absolutePath, content, 'utf8')
211+
}
212+
206213
async fileExists(relativePath: string): Promise<boolean> {
207214
const absolutePath = join(this.path, relativePath)
208215
return await pathExists(absolutePath)

src/otomi-models.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ export interface Repo {
268268
users: User[]
269269
versions: Versions
270270
teamConfig: Record<string, TeamConfig>
271+
files: Record<string, string>
271272
}
272273

273274
export interface TeamConfig {

src/otomi-stack.ts

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CoreV1Api, KubeConfig, User as k8sUser, V1ObjectReference } from '@kubernetes/client-node'
1+
import { CoreV1Api, User as k8sUser, KubeConfig, V1ObjectReference } from '@kubernetes/client-node'
22
import Debug from 'debug'
33

44
import { getRegions, ObjectStorageKeyRegions } from '@linode/api-v4'
@@ -94,6 +94,10 @@ import {
9494
} from 'src/validators'
9595
import { v4 as uuidv4 } from 'uuid'
9696
import { parse as parseYaml, stringify as stringifyYaml } from 'yaml'
97+
import { getAIModels } from './ai/aiModelHandler'
98+
import { AkamaiAgentCR } from './ai/AkamaiAgentCR'
99+
import { AkamaiKnowledgeBaseCR } from './ai/AkamaiKnowledgeBaseCR'
100+
import { DatabaseCR } from './ai/DatabaseCR'
97101
import {
98102
apply,
99103
checkPodExists,
@@ -121,10 +125,6 @@ import { getSealedSecretsPEM, sealedSecretManifest, SealedSecretManifestType } f
121125
import { getKeycloakUsers, isValidUsername } from './utils/userUtils'
122126
import { ObjectStorageClient } from './utils/wizardUtils'
123127
import { fetchChartYaml, fetchWorkloadCatalog, NewHelmChartValues, sparseCloneChart } from './utils/workloadUtils'
124-
import { getAIModels } from './ai/aiModelHandler'
125-
import { AkamaiKnowledgeBaseCR } from './ai/AkamaiKnowledgeBaseCR'
126-
import { AkamaiAgentCR } from './ai/AkamaiAgentCR'
127-
import { DatabaseCR } from './ai/DatabaseCR'
128128

129129
interface ExcludedApp extends App {
130130
managed: boolean
@@ -159,6 +159,14 @@ function getTeamSealedSecretsValuesFilePath(teamId: string, sealedSecretsName: s
159159
return `env/teams/${teamId}/sealedsecrets/${sealedSecretsName}`
160160
}
161161

162+
function getTeamWorkloadValuesManagedFilePath(teamId: string, workloadName: string): string {
163+
return `env/teams/${teamId}/workloadValues/${workloadName}.managed.yaml`
164+
}
165+
166+
function getTeamWorkloadValuesFilePath(teamId: string, workloadName: string): string {
167+
return `env/teams/${teamId}/workloadValues/${workloadName}.yaml`
168+
}
169+
162170
function getTeamKnowledgeBaseValuesFilePath(teamId: string, knowledgeBaseName: string): string {
163171
return `env/teams/${teamId}/knowledgebases/${knowledgeBaseName}`
164172
}
@@ -272,16 +280,13 @@ export default class OtomiStack {
272280
})
273281
}
274282

275-
transformWorkloads(workloads: AplWorkloadResponse[], workloadValues: Record<string, any>[]): AplWorkloadResponse[] {
276-
const values = {}
277-
workloadValues.forEach((value) => {
278-
const workloadName = value.name
279-
if (workloadName) {
280-
values[workloadName] = value.values
281-
}
282-
})
283+
transformWorkloads(workloads: AplWorkloadResponse[], files: string[]): AplWorkloadResponse[] {
283284
return workloads.map((workload) => {
284-
return merge(workload, { spec: { values: values[workload.metadata.name] } })
285+
const workloadName = workload.metadata.name
286+
const teamId = workload.metadata.labels?.['apl.io/teamId']
287+
288+
const filePath = getTeamWorkloadValuesFilePath(teamId, workloadName)
289+
return merge(workload, { spec: { values: files[filePath] } })
285290
})
286291
}
287292

@@ -314,10 +319,9 @@ export default class OtomiStack {
314319
apps: this.transformApps(teamConfig.apps),
315320
policies: this.transformPolicies(teamName, teamConfig.policies || {}),
316321
sealedsecrets: this.transformSecrets(teamName, teamConfig.sealedsecrets || []),
317-
workloads: this.transformWorkloads(teamConfig.workloads || [], teamConfig.workloadValues || []),
322+
workloads: this.transformWorkloads(teamConfig.workloads || [], rawRepo.files || {}),
318323
settings: this.transformTeamSettings(teamConfig.settings),
319324
}))
320-
321325
const repo = rawRepo as Repo
322326
this.repoService = new RepoService(repo)
323327
}
@@ -834,18 +838,16 @@ export default class OtomiStack {
834838
await this.git.saveConfig(repo, fileMap)
835839
}
836840

837-
async saveTeamWorkloadValues(data: AplWorkloadResponse) {
841+
async saveTeamWorkloadValues(data: AplWorkloadResponse, createManagedFile = false) {
838842
const { metadata } = data
839843
const teamId = metadata.labels['apl.io/teamId']!
840844
debug(`Saving AplTeamWorkloadValues ${metadata.name} for team ${teamId}`)
841-
const values = {
842-
name: metadata.name,
843-
values: data.spec.values,
845+
const filePath = getTeamWorkloadValuesFilePath(teamId, metadata.name)
846+
await this.git.writeTextFile(filePath, data.spec.values || '{}')
847+
if (createManagedFile) {
848+
const filePathValuesManaged = getTeamWorkloadValuesManagedFilePath(teamId, metadata.name)
849+
await this.git.writeTextFile(filePathValuesManaged, '')
844850
}
845-
const configKey = this.getConfigKey('AplTeamWorkloadValues')
846-
const repo = this.createTeamConfigInRepo(teamId, configKey, [values])
847-
const fileMap = getFileMaps('').find((fm) => fm.kind === 'AplTeamWorkloadValues')!
848-
await this.git.saveConfig(repo, fileMap, false)
849851
}
850852

851853
async saveTeamPolicy(teamId: string, data: AplPolicyResponse): Promise<void> {
@@ -884,8 +886,10 @@ export default class OtomiStack {
884886
const fileMapWorkload = getFileMaps('').find((fm) => fm.kind === 'AplTeamWorkload')!
885887
const repoValues = this.createTeamConfigInRepo(teamId, 'workloads', [data])
886888
const fileMapValues = getFileMaps('').find((fm) => fm.kind === 'AplTeamWorkloadValues')!
889+
const filePathValuesManaged = getTeamWorkloadValuesManagedFilePath(teamId, metadata.name)
887890
await this.git.deleteConfig(repoWorkload, fileMapWorkload)
888891
await this.git.deleteConfig(repoValues, fileMapValues)
892+
await this.git.removeFile(filePathValuesManaged)
889893
}
890894

891895
getTeamBackups(teamId: string): Backup[] {
@@ -1786,7 +1790,7 @@ export default class OtomiStack {
17861790
try {
17871791
const workload = this.repoService.getTeamConfigService(teamId).createWorkload(data)
17881792
await this.saveTeamWorkload(workload)
1789-
await this.saveTeamWorkloadValues(workload)
1793+
await this.saveTeamWorkloadValues(workload, true)
17901794
await this.doTeamDeployment(
17911795
teamId,
17921796
(teamService) => {

src/repo.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,6 @@ export function getFileMap(kind: AplKind, envDir: string): FileMap {
419419
}
420420

421421
export function renderManifest(fileMap: FileMap, jsonPath: jsonpath.PathComponent[], data: Record<string, any>) {
422-
//TODO remove this custom workaround for workloadValues
423-
if (fileMap.kind === 'AplTeamWorkloadValues') {
424-
return { values: data.values }
425-
}
426422
let spec = data
427423
const labels = {}
428424
if (fileMap.resourceGroup === 'team') {
@@ -489,6 +485,10 @@ export async function loadFileToSpec(
489485
const jsonPath = getJsonPath(fileMap, filePath)
490486
try {
491487
const data = (await deps.loadYaml(filePath)) || {}
488+
// ensure that local path does not include envDir and the leading slash
489+
const localFilePath = filePath.replace(fileMap.envDir, '').replace(/^\/+/, '')
490+
// eslint-disable-next-line no-param-reassign
491+
spec.files[localFilePath] = data
492492
if (fileMap.processAs === 'arrayItem') {
493493
const ref: Record<string, any>[] = get(spec, jsonPath)
494494
const name = filePath.match(/\/([^/]+)\.yaml$/)?.[1]
@@ -577,7 +577,9 @@ export async function loadToSpec(
577577
export async function loadValues(envDir: string, deps = { loadToSpec }): Promise<Record<string, any>> {
578578
//We need everything to load to spec for the API
579579
const fileMaps = getFileMaps(envDir)
580-
const spec = {}
580+
const spec = {
581+
files: {},
582+
}
581583

582584
await Promise.all(
583585
fileMaps.map(async (fileMap) => {

src/services/RepoService.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ describe('RepoService', () => {
3939
obj: {},
4040
oidc: { issuer: 'https://issuer.com', clientID: 'client-id', clientSecret: 'client-secret' },
4141
versions: { version: '1.0.0' },
42+
files: {},
4243
} as Repo
4344
service = new RepoService(repo)
4445
service.createTeamConfig(teamSettings)

src/services/RepoService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export class RepoService {
5252
this.repo.users ??= []
5353
this.repo.versions ??= {} as Versions
5454
this.repo.teamConfig ??= {}
55+
this.repo.files ??= {}
5556
}
5657

5758
public getTeamConfigService(teamName: string): TeamConfigService {

0 commit comments

Comments
 (0)