Skip to content

Commit 99553f3

Browse files
feat: patch argocd resources when oomkilled (#1814)
Co-authored-by: jeho <[email protected]>
1 parent 69d7364 commit 99553f3

File tree

6 files changed

+516
-31
lines changed

6 files changed

+516
-31
lines changed

charts/otomi-pipelines/templates/tekton-otomi-task-teams.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ spec:
7070
- 'binzx/otomi validate-values'
7171
- name: apply
7272
computeResources: {}
73+
{{/* Be aware that during the upgrade this task is not immediately upgraded */}}
7374
script: |
7475
#!/bin/bash
7576
set -e

charts/otomi-pipelines/templates/tekton-otomi-task.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ spec:
7777
- 'binzx/otomi validate-values'
7878
- name: apply
7979
computeResources: {}
80+
{{/* Be aware that during the upgrade this task is not immediately upgraded */}}
8081
script: |
8182
#!/bin/bash
8283
set -e

src/cmd/apply-as-apps.ts

+35-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import { writeFile } from 'fs/promises'
44
import { cleanupHandler, prepareEnvironment } from 'src/common/cli'
55
import { logLevelString, terminal } from 'src/common/debug'
66
import { hf } from 'src/common/hf'
7-
import { isResourcePresent } from 'src/common/k8s'
7+
import { patchContainerResourcesOfSts, isResourcePresent, k8s } from 'src/common/k8s'
88
import { getFilename, loadYaml } from 'src/common/utils'
99
import { getImageTag, objectToYaml } from 'src/common/values'
1010
import { HelmArguments, getParsedArgs, helmOptions, setParsedArgs } from 'src/common/yargs'
1111
import { Argv, CommandModule } from 'yargs'
1212
import { $ } from 'zx'
13+
import { V1ResourceRequirements } from '@kubernetes/client-node/dist/gen/model/v1ResourceRequirements'
1314

1415
const cmdName = getFilename(__filename)
1516
const dir = '/tmp/otomi'
@@ -113,17 +114,47 @@ const removeApplication = async (release: HelmRelease): Promise<void> => {
113114
}
114115
}
115116

117+
function getResources(values: Record<string, any>) {
118+
const config = values
119+
const resources: V1ResourceRequirements = {
120+
limits: {
121+
cpu: config.controller?.resources?.limits?.cpu,
122+
memory: config.controller?.resources?.limits?.memory,
123+
},
124+
requests: {
125+
cpu: config.controller?.resources?.requests?.cpu,
126+
memory: config.controller?.resources?.requests?.memory,
127+
},
128+
}
129+
return resources
130+
}
131+
132+
async function patchArgocdResources(release: HelmRelease, values: Record<string, any>) {
133+
if (release.name === 'argocd') {
134+
const resources = getResources(values)
135+
await patchContainerResourcesOfSts(
136+
'argocd-application-controller',
137+
'argocd',
138+
'application-controller',
139+
resources,
140+
k8s.app(),
141+
k8s.core(),
142+
d,
143+
)
144+
}
145+
}
146+
116147
const writeApplicationManifest = async (release: HelmRelease, otomiVersion: string): Promise<void> => {
117148
const appName = `${release.namespace}-${release.name}`
118-
// d.info(`Generating Argocd Application at ${appName}`)
119149
const applicationPath = `${appsDir}/${appName}.yaml`
120150
const valuesPath = `${valuesDir}/${appName}.yaml`
121-
// d.info(`Loading values file from ${valuesPath}`)
122151
let values = {}
152+
123153
if (await pathExists(valuesPath)) values = (await loadYaml(valuesPath)) || {}
124154
const manifest = getArgocdAppManifest(release, values, otomiVersion)
125-
// d.info(`Saving Argocd Application at ${applicationPath}`)
126155
await writeFile(applicationPath, objectToYaml(manifest))
156+
157+
await patchArgocdResources(release, values)
127158
}
128159
export const applyAsApps = async (argv: HelmArguments): Promise<void> => {
129160
const helmfileSource = argv.file?.toString() || 'helmfile.d/'

src/cmd/commit.ts

+9-21
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { CoreV1Api, CustomObjectsApi, KubeConfig } from '@kubernetes/client-node'
1+
import { CoreV1Api } from '@kubernetes/client-node'
22
import retry from 'async-retry'
33
import { bootstrapGit, setIdentity } from 'src/common/bootstrap'
44
import { prepareEnvironment } from 'src/common/cli'
55
import { encrypt } from 'src/common/crypt'
66
import { terminal } from 'src/common/debug'
77
import { env, isCi } from 'src/common/envalid'
88
import { hfValues } from 'src/common/hf'
9-
import { createGenericSecret, waitTillGitRepoAvailable } from 'src/common/k8s'
9+
import { createGenericSecret, k8s, waitTillGitRepoAvailable } from 'src/common/k8s'
1010
import { getFilename } from 'src/common/utils'
1111
import { getRepo } from 'src/common/values'
1212
import { HelmArguments, getParsedArgs, setParsedArgs } from 'src/common/yargs'
@@ -128,12 +128,9 @@ export async function retryCheckingForPipelineRun() {
128128

129129
export async function retryIsOAuth2ProxyRunning() {
130130
const d = terminal(`cmd:${cmdName}:isOAuth2ProxyRunning`)
131-
const kc = new KubeConfig()
132-
kc.loadFromDefault()
133-
const coreV1Api = kc.makeApiClient(CoreV1Api)
134131
await retry(
135132
async () => {
136-
await isOAuth2ProxyAvailable(coreV1Api)
133+
await isOAuth2ProxyAvailable(k8s.core())
137134
},
138135
{ retries: env.RETRIES, randomize: env.RANDOM, minTimeout: env.MIN_TIMEOUT, factor: env.FACTOR },
139136
).catch((e) => {
@@ -142,10 +139,10 @@ export async function retryIsOAuth2ProxyRunning() {
142139
})
143140
}
144141

145-
export async function isOAuth2ProxyAvailable(k8s: CoreV1Api): Promise<void> {
142+
export async function isOAuth2ProxyAvailable(coreV1Api: CoreV1Api): Promise<void> {
146143
const d = terminal(`cmd:${cmdName}:isOAuth2ProxyRunning`)
147144
d.info('Checking if OAuth2Proxy is available, waiting...')
148-
const { body: oauth2ProxyEndpoint } = await k8s.readNamespacedEndpoints('oauth2-proxy', 'istio-system')
145+
const { body: oauth2ProxyEndpoint } = await coreV1Api.readNamespacedEndpoints('oauth2-proxy', 'istio-system')
149146
if (!oauth2ProxyEndpoint) {
150147
throw new Error('OAuth2Proxy endpoint not found, waiting...')
151148
}
@@ -163,16 +160,10 @@ export async function isOAuth2ProxyAvailable(k8s: CoreV1Api): Promise<void> {
163160

164161
export async function checkIfPipelineRunExists(): Promise<void> {
165162
const d = terminal(`cmd:${cmdName}:pipelineRun`)
166-
const kc = new KubeConfig()
167-
kc.loadFromDefault()
168-
const customObjectsApi = kc.makeApiClient(CustomObjectsApi)
169163

170-
const response = await customObjectsApi.listNamespacedCustomObject(
171-
'tekton.dev',
172-
'v1beta1',
173-
'otomi-pipelines',
174-
'pipelineruns',
175-
)
164+
const response = await k8s
165+
.custom()
166+
.listNamespacedCustomObject('tekton.dev', 'v1beta1', 'otomi-pipelines', 'pipelineruns')
176167

177168
const pipelineRuns = (response.body as { items: any[] }).items
178169
if (pipelineRuns.length === 0) {
@@ -186,10 +177,7 @@ export async function checkIfPipelineRunExists(): Promise<void> {
186177

187178
async function createCredentialsSecret(secretName: string, username: string, password: string): Promise<void> {
188179
const secretData = { username, password }
189-
const kc = new KubeConfig()
190-
kc.loadFromDefault()
191-
const coreV1Api = kc.makeApiClient(CoreV1Api)
192-
await createGenericSecret(coreV1Api, secretName, 'keycloak', secretData)
180+
await createGenericSecret(k8s.core(), secretName, 'keycloak', secretData)
193181
}
194182

195183
export const printWelcomeMessage = async (): Promise<void> => {

0 commit comments

Comments
 (0)