Skip to content

feat: Add informer to operator #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,88 @@ You can optionally register a custom resource definition from code, to auto-crea

## Examples

### Operator that informs on pods in a namespace

```typescript
import { CoreV1Api } from '@kubernetes/client-node';

export default class MyOperator extends Operator {
protected async init() {
const coreV1Api = this.kubeConfig.makeApiClient(CoreV1Api)
const listFnBody = coreV1Api.listNamespacedPod('default');
// Call the watchResourceWithInformer with the listFn
await this.informResource(
'v1',
'pods',
async (event) => {
console.log('Received event:', event.type, event.meta.name);
console.log('Object:', event.object);
console.log(new Date().toISOString());
},
listFnBody,
)
}
}

const operator = new MyOperator();

async function main() {
await operator.start()
const exit = (reason: string) => {
console.log(reason)
operator.stop()
process.exit(0)
}
process.on('SIGTERM', () => exit('SIGTERM')).on('SIGINT', () => exit('SIGINT'))
}

main()
```

### Operator that informs on a custom resource

```typescript
export default class MyOperator extends Operator {
protected async init() {
const customObjectsApi = this.kubeConfig.makeApiClient(CustomObjectsApi)
const listFnBody = async () => {
const res = await customObjectsApi.listClusterCustomObject('group.company.com', 'v1alpha1', 'resources');
return {
response: res.response,
body: res.body as KubernetesListObject<KubernetesObject>,
};
};

// Call the watchResourceWithInformer with the listFn
await this.informResource(
'v1alpha1',
'resources',
async (event) => {
console.log('Received event:', event.type, event.meta.name);
console.log('Object:', event.object);
console.log(new Date().toISOString());
},
listFnBody,
'group.company.com',
)
}
}

const operator = new MyOperator();

async function main() {
await operator.start()
const exit = (reason: string) => {
console.log(reason)
operator.stop()
process.exit(0)
}
process.on('SIGTERM', () => exit('SIGTERM')).on('SIGINT', () => exit('SIGINT'))
}

main()
```

### Operator that watches namespaces

```javascript
Expand Down Expand Up @@ -192,7 +274,7 @@ export default class MyOperator extends Operator {

### Operator that watches a custom resource

You will typicall create an interface to define your custom resource:
You will typically create an interface to define your custom resource:

```javascript
export interface MyCustomResource extends KubernetesObject {
Expand Down
10 changes: 10 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @ts-check

import eslint from '@eslint/js'
import tseslint from 'typescript-eslint'

const config = tseslint.config(eslint.configs.recommended, ...tseslint.configs.recommended)

config.push({ rules: { '@typescript-eslint/ban-ts-comment': 'error' } })

export default config
Loading
Loading