Skip to content

Commit

Permalink
feat: switch for namespace watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
ElderMatt committed Jun 21, 2024
1 parent 8435f9d commit 2650ba0
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/operator/keycloak.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Operator, { ResourceEventType } from '@dot-i/k8s-operator'
import { KubernetesObject } from '@dot-i/k8s-operator/node_modules/@kubernetes/client-node/dist'
import * as k8s from '@kubernetes/client-node'
import { KubeConfig } from '@kubernetes/client-node'
import {
Expand Down Expand Up @@ -40,6 +41,11 @@ import {
import { doApiCall, waitTillAvailable } from '../utils'
import { cleanEnv, KEYCLOAK_TOKEN_TTL } from '../validators'

// added the type property which was missing in the original KubernetesObject
interface CustomKubernetesObject extends KubernetesObject {
type: string
}

const errors: string[] = []

interface KeycloakConnection {
Expand Down Expand Up @@ -274,15 +280,24 @@ export default class MyOperator extends Operator {
// Watch team namespaces to see if teams get added or removed
try {
await this.watchResource('', 'v1', 'namespaces', async (e) => {
const { object }: { object: k8s.V1Pod } = e
const { metadata } = object
const { object }: { object: k8s.V1Namespace } = e
const { metadata, type } = object as CustomKubernetesObject
// Check if namespace starts with prefix 'team-'
if (metadata && !metadata.name?.startsWith('team-')) return
if (metadata && metadata.name === 'team-admin') return
console.log('namespace object: ', object)
console.log('namespace metadata: ', object.metadata)
if (object.kind === 'add') await runKeycloakUpdater('addTeam')
if (object.kind === 'remove') await runKeycloakUpdater('removeTeam')
console.log('Type namespace: ', type)
switch (e.type) {
case ResourceEventType.Deleted:
await runKeycloakUpdater('removeTeam')
break
case ResourceEventType.Added:
await runKeycloakUpdater('addTeam')
break
default:
break
}
})
console.log('Watching team namespaces done!')
} catch (error) {
Expand Down

0 comments on commit 2650ba0

Please sign in to comment.