Skip to content

Commit

Permalink
feat: removed pod watcher added interval
Browse files Browse the repository at this point in the history
  • Loading branch information
ElderMatt committed Dec 13, 2023
1 parent 3a3c321 commit 2a3abfd
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions src/operator/gitea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import * as k8s from '@kubernetes/client-node'
import { KubeConfig } from '@kubernetes/client-node'
import stream from 'stream'

interface ConditionCheckResult {
ready: boolean
pod: k8s.V1Pod // Replace 'Pod' with the type you have for your pod
}

const kc = new KubeConfig()
kc.loadFromCluster()
kc.loadFromDefault()
const k8sApi = kc.makeApiClient(k8s.CoreV1Api)

function buildTeamString(teamNames: any[]): string {
Expand All @@ -28,18 +33,6 @@ function buildTeamString(teamNames: any[]): string {
}

async function execGiteaCLICommand(object: k8s.V1Pod) {
// Check if 'gitea-0' pod has a container named 'gitea'
const containerStatuses = object.status?.containerStatuses || []
const giteaContainer = containerStatuses.find((container) => container.name === 'gitea')
// Check if the gitea container is 'READY'
if (giteaContainer === undefined) {
console.debug('Gitea container is not found')
return
}
if (!giteaContainer?.ready) {
console.debug('Gitea container is not ready: ', giteaContainer.state!)
return
}
try {
console.debug('Finding namespaces')
let namespaces: any
Expand Down Expand Up @@ -96,30 +89,43 @@ async function execGiteaCLICommand(object: k8s.V1Pod) {
}
}

async function checkGiteaContainer(object: k8s.V1Pod): Promise<ConditionCheckResult> {
const giteaPod = (await k8sApi.readNamespacedPod('gitea-0', 'gitea')).body
// Check if 'gitea-0' pod has a container named 'gitea'
const containerStatuses = object.status?.containerStatuses || []
const giteaContainer = containerStatuses.find((container) => container.name === 'gitea')
// Check if the gitea container is 'READY'
if (giteaContainer === undefined) {
console.debug('Gitea container is not found')
return { ready: false, pod: giteaPod }
}
if (!giteaContainer?.ready) {
console.debug('Gitea container is not ready: ', giteaContainer.state!)
return { ready: false, pod: giteaPod }
}
return { ready: true, pod: giteaPod }
}

export default class MyOperator extends Operator {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
protected async init() {
console.debug('Starting initializing')
// Watch all namespaces
try {
await this.watchResource('', 'v1', 'namespaces', async (e) => {
const { object } = e
const { object }: { object: k8s.V1Pod } = e
const { metadata } = object
// Check if namespace starts with prefix 'team-'
if (metadata && !metadata.name?.startsWith('team-')) return
const giteaPod = (await k8sApi.readNamespacedPod('gitea-0', 'gitea')).body
await execGiteaCLICommand(giteaPod)
})
} catch (error) {
console.debug(error)
}
try {
await this.watchResource('', 'v1', 'pods', async (e) => {
const { object }: { object: k8s.V1Pod } = e
const { metadata } = object
// Check if pod is named 'gitea-0'
if (metadata && metadata.name !== 'gitea-0') return
await execGiteaCLICommand(object)
let giteaPod = await checkGiteaContainer(object)
while (!giteaPod.ready) {
// eslint-disable-next-line no-await-in-loop
await new Promise((resolve) => setTimeout(resolve, 30000))
// eslint-disable-next-line no-await-in-loop
giteaPod = await checkGiteaContainer(object)
}

await execGiteaCLICommand(giteaPod.pod)
})
} catch (error) {
console.debug(error)
Expand Down

0 comments on commit 2a3abfd

Please sign in to comment.