|
1 | 1 | export type Resource = { |
| 2 | + apiVersion: string; |
2 | 3 | kind: string; |
3 | | - items?: { |
4 | | - metadata: { |
5 | | - name: string; |
6 | | - managedFields?: unknown; |
7 | | - }; |
8 | | - }[]; |
| 4 | + items?: Omit<Resource, 'items'>[]; |
9 | 5 | metadata: { |
10 | 6 | name: string; |
| 7 | + namespace?: string; |
| 8 | + labels?: Record<string, string>; |
| 9 | + annotations?: { |
| 10 | + 'kubectl.kubernetes.io/last-applied-configuration'?: string; |
| 11 | + [key: string]: string | undefined; |
| 12 | + }; |
11 | 13 | managedFields?: unknown; |
| 14 | + creationTimestamp?: string; |
| 15 | + finalizers?: string[]; |
| 16 | + generation?: number; |
| 17 | + resourceVersion?: string; |
| 18 | + uid?: string; |
12 | 19 | }; |
| 20 | + spec?: unknown; |
| 21 | + status?: unknown; |
13 | 22 | }; |
14 | 23 |
|
15 | | -export const removeManagedFieldsProperty = (resourceObject: Resource) => { |
16 | | - if (resourceObject?.metadata?.managedFields) { |
| 24 | +export const removeManagedFieldsProperty = (resourceObject: Resource, showOnlyImportantData: boolean) => { |
| 25 | + if (!resourceObject) { |
| 26 | + return resourceObject; |
| 27 | + } |
| 28 | + const processMetadata = (metadata: Resource['metadata']) => { |
| 29 | + const { managedFields, generation, uid, annotations, ...restMetadata } = metadata || {}; |
| 30 | + |
| 31 | + const newAnnotations = { ...annotations }; |
| 32 | + if (showOnlyImportantData) { |
| 33 | + delete newAnnotations['kubectl.kubernetes.io/last-applied-configuration']; |
| 34 | + } |
| 35 | + |
17 | 36 | return { |
18 | | - ...resourceObject, |
19 | | - metadata: { |
20 | | - ...resourceObject.metadata, |
21 | | - managedFields: undefined, |
22 | | - }, |
| 37 | + ...restMetadata, |
| 38 | + ...(Object.keys(newAnnotations).length > 0 && { |
| 39 | + annotations: newAnnotations, |
| 40 | + }), |
23 | 41 | }; |
24 | | - } |
| 42 | + }; |
| 43 | + |
| 44 | + const processResource = (resource: Omit<Resource, 'items'>) => { |
| 45 | + const { metadata, ...restResource } = resource; |
| 46 | + return { |
| 47 | + ...restResource, |
| 48 | + ...(metadata && { metadata: processMetadata(metadata) }), |
| 49 | + }; |
| 50 | + }; |
| 51 | + |
25 | 52 | if (resourceObject?.items) { |
26 | 53 | return { |
27 | 54 | ...resourceObject, |
28 | | - items: resourceObject.items.map((item) => ({ |
29 | | - ...item, |
30 | | - metadata: { ...item.metadata, managedFields: undefined }, |
31 | | - })), |
| 55 | + items: resourceObject.items.map(processResource), |
32 | 56 | }; |
33 | 57 | } |
34 | 58 |
|
35 | | - return resourceObject; |
| 59 | + return processResource(resourceObject); |
36 | 60 | }; |
0 commit comments