-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
145 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,23 @@ | ||
import * as k8s from '@kubernetes/client-node'; | ||
|
||
// export let informer: k8s.Informer<GitRepository> & k8s.ObjectCache<GitRepository> | undefined; | ||
import { createInformers, destroyInformers } from './informers'; | ||
|
||
export let k8sCoreApi: k8s.CoreV1Api | undefined; | ||
export let k8sCustomApi: k8s.CustomObjectsApi | undefined; | ||
export let k8sWatch: k8s.Watch | undefined; | ||
|
||
export function createK8sClients(kc: k8s.KubeConfig) { | ||
destroyK8sClients(); | ||
k8sCoreApi = kc.makeApiClient(k8s.CoreV1Api); | ||
k8sWatch = new k8s.Watch(kc); | ||
k8sCustomApi = kc.makeApiClient(k8s.CustomObjectsApi); | ||
|
||
createInformers(kc); | ||
|
||
} | ||
|
||
export function destroyK8sClients() { | ||
destroyInformers(); | ||
|
||
k8sCoreApi = undefined; | ||
k8sWatch = undefined; | ||
k8sCustomApi = undefined; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import * as k8s from '@kubernetes/client-node'; | ||
import { getAPIParams } from 'cli/kubernetes/apiResources'; | ||
import { GitRepository } from 'types/flux/gitRepository'; | ||
import { Kind, KubernetesListObject, KubernetesObject } from 'types/kubernetes/kubernetesTypes'; | ||
import { KubernetesObjectDataProvider } from 'ui/treeviews/dataProviders/kubernetesObjectDataProvider'; | ||
import { sourceDataProvider, workloadDataProvider } from 'ui/treeviews/treeViews'; | ||
import { k8sCustomApi } from './client'; | ||
import { FluxSourceKinds, FluxWorkloadKinds } from 'types/flux/object'; | ||
|
||
|
||
let informers: k8s.Informer<KubernetesObject>[] = []; | ||
|
||
export function createInformers(kc: k8s.KubeConfig) { | ||
FluxSourceKinds.forEach(kind => { | ||
createInformer(kc, sourceDataProvider, kind); | ||
}); | ||
|
||
FluxWorkloadKinds.forEach(kind => { | ||
createInformer(kc, workloadDataProvider, kind); | ||
}); | ||
} | ||
|
||
export function destroyInformers() { | ||
informers.forEach(informer => { | ||
informer.stop(); | ||
}); | ||
|
||
informers = []; | ||
} | ||
|
||
|
||
async function createInformer(kc: k8s.KubeConfig, receiver: KubernetesObjectDataProvider, kind: Kind) { | ||
const api = getAPIParams(kind); | ||
if (!api) { | ||
return; | ||
} | ||
|
||
const listFn = async () => { | ||
const result = await k8sCustomApi!.listClusterCustomObject(api.group, api.version, api.plural); | ||
const kbody = result.body as KubernetesListObject<GitRepository>; | ||
return Promise.resolve({ response: result.response, body: kbody }); | ||
}; | ||
|
||
const informer = k8s.makeInformer( | ||
kc, | ||
`/apis/${api.group}/${api.version}/${api.plural}`, | ||
listFn, | ||
); | ||
|
||
try { | ||
await informer.start(); | ||
registerInformerEvents(informer, receiver); | ||
informers.push(informer); | ||
} catch (error) { | ||
destroyInformers(); | ||
} | ||
} | ||
|
||
function registerInformerEvents(informer: k8s.Informer<KubernetesObject>, receiver: KubernetesObjectDataProvider) { | ||
informer?.on('add', (obj: KubernetesObject) => { | ||
// console.log('*- informer Add', obj); | ||
receiver.add(obj); | ||
}); | ||
|
||
informer?.on('update', (obj: KubernetesObject) => { | ||
// console.log('*- informer Update', obj); | ||
receiver.update(obj); | ||
}); | ||
|
||
informer?.on('delete', (obj: KubernetesObject) => { | ||
// console.log('*- informer Delete ', obj); | ||
receiver.delete(obj); | ||
}); | ||
|
||
informer?.on('error', (err: Error) => { | ||
console.log('*- informer Error', err); | ||
destroyInformers(); | ||
}); | ||
|
||
// console.log('*- informer listening for events...'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters