Skip to content

Commit

Permalink
feat: added try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
ElderMatt committed Dec 8, 2023
1 parent 6d6812d commit dda91e1
Showing 1 changed file with 63 additions and 59 deletions.
122 changes: 63 additions & 59 deletions src/operator/gitea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,67 +32,71 @@ export default class MyOperator extends Operator {
protected async init() {
console.debug('Starting initializing')
// Watch all namespaces
await this.watchResource('', 'v1', 'namespaces', async (e) => {
console.debug(e)
const { object } = e
console.debug(object)
const { metadata } = object
console.debug(metadata)
// Check if namespace starts with prefix 'team-'
if (metadata && !metadata.name?.startsWith('team-')) return
try {
console.debug('Finding namespaces')
const namespaces = await k8sApi.listNamespace()
console.debug('Filtering namespaces with "team-" prefix')
const teamNamespaces = namespaces.body.items
.map((namespace) => namespace.metadata?.name)
.filter((name) => name && name.startsWith('team-') && name !== 'team-admin')
if (teamNamespaces.length > 0) {
const giteaPodLabel = 'app=gitea'
console.debug('Getting giteapod list')
const giteaPodList = await k8sApi.listNamespacedPod(
'gitea',
undefined,
undefined,
undefined,
undefined,
giteaPodLabel,
try {
await this.watchResource('', 'v1', 'namespaces', async (e) => {
console.debug(e)
const { object } = e
console.debug(object)
const { metadata } = object
console.debug(metadata)
// Check if namespace starts with prefix 'team-'
if (metadata && !metadata.name?.startsWith('team-')) return
try {
console.debug('Finding namespaces')
const namespaces = await k8sApi.listNamespace()
console.debug('Filtering namespaces with "team-" prefix')
const teamNamespaces = namespaces.body.items
.map((namespace) => namespace.metadata?.name)
.filter((name) => name && name.startsWith('team-') && name !== 'team-admin')
if (teamNamespaces.length > 0) {
const giteaPodLabel = 'app=gitea'
console.debug('Getting giteapod list')
const giteaPodList = await k8sApi.listNamespacedPod(
'gitea',
undefined,
undefined,
undefined,
undefined,
giteaPodLabel,
)
console.debug('Selecting giteapod from list')
const giteaPod = giteaPodList.body.items[0]
console.debug('Creating exec command')
const execCommand = [
'sh',
'-c',
`gitea admin auth update-oauth --id 1 --group-team-map '${buildTeamString(teamNamespaces)}'`,
]
const exec = new k8s.Exec(kc)
console.debug('Trying to run the following commands:')
console.debug(execCommand)
// Run gitea CLI command to update the gitea oauth group mapping
exec.exec(
giteaPod.metadata?.namespace || 'gitea',
giteaPod.metadata?.name || 'gitea-0',
'gitea',
execCommand,
process.stdout as stream.Writable,
process.stderr as stream.Writable,
process.stdin as stream.Readable,
true,
(status: k8s.V1Status) => {
console.log('Exited with status:')
console.log(JSON.stringify(status, null, 2))
},
)
} else {
console.debug('No team namespaces found')
}
} catch (error) {
console.debug(
`Error updating IDP group mapping: statuscode: ${error.response.body.code} - message: ${error.response.body.message}`,
)
console.debug('Selecting giteapod from list')
const giteaPod = giteaPodList.body.items[0]
console.debug('Creating exec command')
const execCommand = [
'sh',
'-c',
`gitea admin auth update-oauth --id 1 --group-team-map '${buildTeamString(teamNamespaces)}'`,
]
const exec = new k8s.Exec(kc)
console.debug('Trying to run the following commands:')
console.debug(execCommand)
// Run gitea CLI command to update the gitea oauth group mapping
exec.exec(
giteaPod.metadata?.namespace || 'gitea',
giteaPod.metadata?.name || 'gitea-0',
'gitea',
execCommand,
process.stdout as stream.Writable,
process.stderr as stream.Writable,
process.stdin as stream.Readable,
true,
(status: k8s.V1Status) => {
console.log('Exited with status:')
console.log(JSON.stringify(status, null, 2))
},
)
} else {
console.debug('No team namespaces found')
}
} catch (error) {
console.debug(
`Error updating IDP group mapping: statuscode: ${error.response.body.code} - message: ${error.response.body.message}`,
)
}
})
})
} catch (error) {
console.debug(error)
}
}
}

Expand Down

0 comments on commit dda91e1

Please sign in to comment.