Skip to content

Commit 3a344b4

Browse files
authored
fix: process resource quota as list (#2055)
1 parent 0eab891 commit 3a344b4

File tree

7 files changed

+50
-9
lines changed

7 files changed

+50
-9
lines changed

charts/team-ns/values.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ cluster:
88
disableHttp2: false
99
namespace: ""
1010
resourceQuota:
11-
pods: "50"
12-
services.loadbalancers: 0
11+
- name: pods
12+
value: "50"
13+
- name: services.loadbalancers
14+
value: "0"
1315
apps: {}
1416
services: []
1517
selfService:

helmfile.d/snippets/defaults.gotmpl

+4-2
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,10 @@ environments:
193193
egressPublic: true
194194
ingressPrivate: true
195195
resourceQuota:
196-
pods: "50"
197-
services.loadbalancers: "0"
196+
- name: pods
197+
value: "50"
198+
- name: services.loadbalancers
199+
value: "0"
198200
selfService:
199201
apps: []
200202
policies:

src/cmd/migrate.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { randomUUID } from 'crypto'
55
import { diff } from 'deep-diff'
66
import { copy, createFileSync, move, pathExists, renameSync, rm } from 'fs-extra'
77
import { mkdir, readFile, writeFile } from 'fs/promises'
8-
import { cloneDeep, each, get, isObject, mapKeys, mapValues, omit, pick, pull, set, unset } from 'lodash'
8+
import { cloneDeep, each, get, isUndefined, isObject, mapKeys, mapValues, omit, pick, pull, set, unset } from 'lodash'
99
import { basename, dirname, join } from 'path'
1010
import { prepareEnvironment } from 'src/common/cli'
1111
import { decrypt, encrypt } from 'src/common/crypt'
@@ -51,6 +51,7 @@ interface Change {
5151
[mutation: string]: string
5252
}>
5353
networkPoliciesMigration?: boolean
54+
teamResourceQuotaMigration?: boolean
5455
}
5556

5657
export type Changes = Array<Change>
@@ -300,6 +301,23 @@ const networkPoliciesMigration = async (values: Record<string, any>): Promise<vo
300301
)
301302
}
302303

304+
const teamResourceQuotaMigration = (values: Record<string, any>) => {
305+
Object.entries(values?.teamConfig as Record<string, any>).forEach(([teamName, teamValues]) => {
306+
const resourceQuota = teamValues?.settings?.resourceQuota
307+
if (!isUndefined(resourceQuota) && !Array.isArray(resourceQuota)) {
308+
set(
309+
teamValues,
310+
'settings.resourceQuota',
311+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
312+
Object.entries(resourceQuota || {}).map(([name, value]) => ({ name, value })),
313+
)
314+
console.log('Completed migration of resourceQuota for team', teamName)
315+
} else {
316+
console.log('No migration needed of resourceQuota for team', teamName)
317+
}
318+
})
319+
}
320+
303321
const bulkAddition = (path: string, values: any, filePath: string) => {
304322
// eslint-disable-next-line @typescript-eslint/no-var-requires
305323
const val = require(filePath)
@@ -362,6 +380,7 @@ export const applyChanges = async (
362380
}
363381

364382
if (c.networkPoliciesMigration) await networkPoliciesMigration(values)
383+
if (c.teamResourceQuotaMigration) teamResourceQuotaMigration(values)
365384

366385
Object.assign(values, { version: c.version })
367386
}
@@ -530,7 +549,7 @@ export const migrate = async (): Promise<boolean> => {
530549
}
531550
const changes: Changes = (await loadYaml(`${rootDir}/values-changes.yaml`))?.changes
532551
const versions = await loadYaml(`${env.ENV_DIR}/env/settings/versions.yaml`, { noError: true })
533-
const prevVersion: number = versions?.specVersion
552+
const prevVersion: number = versions?.spec?.specVersion
534553
if (!prevVersion) {
535554
d.log('No changes detected, skipping')
536555
return false

tests/fixtures/env/teams/demo/settings.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ spec:
2525
oidc:
2626
groupMapping: somesecretvalue
2727
resourceQuota:
28-
services.loadbalancers: '1'
28+
- name: services.loadbalancers
29+
value: '1'
2930
selfService:
3031
access:
3132
- shell

values-changes.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,5 @@ changes:
341341
- 'databases.keycloak.imported'
342342
- 'databases.gitea.imported'
343343
- 'databases.gitea.useOtomiDB'
344+
- version: 34
345+
teamResourceQuotaMigration: true

values-schema.yaml

+11-1
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,17 @@ definitions:
11311131
x-secret: ''
11321132
resourceQuota:
11331133
description: 'List of kubernetes resource quota. Should adhere to the "spec.hard" format as described here: https://kubernetes.io/docs/concepts/policy/resource-quotas/. Not validated as there is no schema published. Change at your own risk.'
1134-
type: object
1134+
type: array
1135+
items:
1136+
type: object
1137+
properties:
1138+
name:
1139+
type: string
1140+
value:
1141+
type: string
1142+
required:
1143+
- name
1144+
- value
11351145
selfService:
11361146
$ref: '#/definitions/teamSelfService'
11371147
alerts:

values/team-ns/team-ns.gotmpl

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
{{- end }}
1818
{{- end }}
1919

20+
{{- $teamResourceQuotas := dict }}
21+
{{- range $team | get "settings.resourceQuota" list }}
22+
{{- $_ := set $teamResourceQuotas .name .value }}
23+
{{- end }}
24+
2025
apps:
2126
harbor:
2227
enabled: {{ $a.harbor.enabled }}
@@ -48,7 +53,7 @@ teamApps: {{- toYaml $teamApps | nindent 2 }}
4853
teamIds: {{- toYaml (keys $v.teamConfig) | nindent 2 }}
4954
tracingEnabled: {{ $a.istio.tracing.enabled }}
5055
services: {{- concat $coreTeamServices $teamServices | toYaml | nindent 2 }}
51-
resourceQuota: {{- $team | get "settings.resourceQuota" dict | toYaml | nindent 2 }}
56+
resourceQuota: {{- $teamResourceQuotas | toYaml | nindent 2 }}
5257

5358
gitOps:
5459
adminPassword: {{ $a.gitea.adminPassword }}

0 commit comments

Comments
 (0)