Skip to content

Commit 4984ef5

Browse files
committed
frontend/plugins/examples: Add project list override example
Signed-off-by: Joaquim Rocha <[email protected]>
1 parent d10e86b commit 4984ef5

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed
Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,54 @@
11
# Projects customization example
22

3-
This plugin demonstrates how to customize projects feature
3+
This plugin demonstrates how to customize projects feature including:
4+
5+
- Custom project creation workflows
6+
- Custom project details tabs
7+
- Custom project overview sections
8+
- Custom project list processors (extend or modify project discovery)
49

510
```bash
611
cd plugins/examples/projects
712
npm start
813
```
914

1015
The main code for the example plugin is in [src/index.tsx](src/index.tsx).
16+
17+
## Project List Processors
18+
19+
The `registerProjectListProcessor` function allows plugins to extend or modify how projects are discovered and listed. Processors receive the current list of projects (from namespaces or previous processors) and can:
20+
21+
- Add new projects from Custom Resources, external APIs, or other sources
22+
- Filter existing projects based on conditions
23+
- Modify project properties like namespaces or clusters
24+
- Completely replace the project list if needed
25+
26+
### Key Features
27+
28+
- **Additive by default**: Processors receive existing projects and can extend the list
29+
- **Chainable**: Multiple processors can be registered and will run in sequence
30+
- **Error handling**: Failed processors don't break the application
31+
- **Duplicate prevention**: Easy to check for existing projects by ID
32+
33+
### Example Usage
34+
35+
```typescript
36+
// Add new projects while keeping existing ones
37+
registerProjectListProcessor((currentProjects) => {
38+
const newProjects = [
39+
{
40+
id: 'my-custom-project',
41+
namespaces: ['default', 'kube-system'],
42+
clusters: ['cluster1']
43+
}
44+
];
45+
46+
// Only add projects that don't already exist
47+
const existingIds = currentProjects.map(p => p.id);
48+
const projectsToAdd = newProjects.filter(p => !existingIds.includes(p.id));
49+
50+
return [...currentProjects, ...projectsToAdd];
51+
});
52+
```
53+
54+
This approach ensures backward compatibility while providing maximum flexibility for project customization.

plugins/examples/projects/src/index.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
ApiProxy,
1919
registerCustomCreateProject,
2020
registerProjectDetailsTab,
21+
registerProjectListProcessor,
2122
registerProjectOverviewSection,
2223
} from '@kinvolk/headlamp-plugin/lib';
2324

@@ -55,10 +56,34 @@ registerCustomCreateProject({
5556
registerProjectDetailsTab({
5657
id: 'my-tab',
5758
label: 'Metrics',
58-
component: ({ project }) => <div>Metrics for project {project.name}</div>,
59+
icon: 'mdi:chart-line',
60+
component: ({ project }) => <div>Metrics for project {project.id}</div>,
5961
});
6062

6163
registerProjectOverviewSection({
6264
id: 'resource-usage',
63-
component: ({ project }) => <div>Custom resource usage for project {project.name}</div>,
65+
component: ({ project }) => <div>Custom resource usage for project {project.id}</div>,
66+
});
67+
68+
// Example 1: Extend the project list with additional projects
69+
// This keeps the existing namespace-based projects and adds new ones
70+
registerProjectListProcessor((currentProjects) => {
71+
const newProjects = [
72+
{
73+
id: 'example-project-1',
74+
namespaces: ['default', 'kube-system'],
75+
clusters: ['cluster1']
76+
},
77+
{
78+
id: 'example-project-2',
79+
namespaces: ['example-ns'],
80+
clusters: ['cluster1', 'cluster2']
81+
}
82+
];
83+
84+
// Only add projects that don't already exist
85+
const existingIds = currentProjects.map(p => p.id);
86+
const projectsToAdd = newProjects.filter(p => !existingIds.includes(p.id));
87+
88+
return [...currentProjects, ...projectsToAdd];
6489
});

0 commit comments

Comments
 (0)