Skip to content

Commit

Permalink
Merge pull request #47 from webzard-io/xingyu/fix_types
Browse files Browse the repository at this point in the history
fix: 🐛 fix Unstructured metadata types
  • Loading branch information
self-transition authored May 7, 2024
2 parents dd2b0ac + d68e790 commit 875d022
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/data-provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const dataProvider = (
const { items } = await globalStore.get(resource, meta);
const data = items.find(
item =>
item.metadata.name === name && item.metadata.namespace === namespace
item.metadata?.name === name && item.metadata.namespace === namespace
);
if (!data) {
throw new Error(`resource: ${resource} not include id: ${id}`);
Expand All @@ -61,7 +61,7 @@ export const dataProvider = (
let { items } = await globalStore.get<TData>(resource, meta);

if (meta?.namespace) {
items = items.filter((item: Unstructured) => item.metadata.namespace === meta.namespace);
items = items.filter((item: Unstructured) => item.metadata?.namespace === meta.namespace);
}

if (filters) {
Expand Down
22 changes: 12 additions & 10 deletions src/kube-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type Unstructured = {
id: string;
apiVersion?: string;
kind?: string;
metadata: ObjectMeta;
metadata?: ObjectMeta;
};

type KubeApiQueryParams = {
Expand Down Expand Up @@ -359,8 +359,8 @@ export class KubeApi<T extends UnstructuredList> {

informerLog('INFORMER', event);

const name = event.object.metadata.name;
const namespace = event.object.metadata.namespace;
const name = event.object.metadata?.name;
const namespace = event.object.metadata?.namespace;

switch (event.type) {
case 'ADDED': {
Expand All @@ -370,8 +370,8 @@ export class KubeApi<T extends UnstructuredList> {
items = items
.map(item => {
if (
item.metadata.name === name &&
item.metadata.namespace === namespace
item.metadata?.name === name &&
item.metadata?.namespace === namespace
) {
exist = true;
return event.object;
Expand All @@ -386,8 +386,8 @@ export class KubeApi<T extends UnstructuredList> {
case 'MODIFIED':
items = items.map(item => {
if (
item.metadata.name === name &&
item.metadata.namespace === namespace
item.metadata?.name === name &&
item.metadata?.namespace === namespace
) {
return event.object;
}
Expand All @@ -397,8 +397,8 @@ export class KubeApi<T extends UnstructuredList> {
case 'DELETED':
items = items.filter(
item =>
item.metadata.name !== name ||
item.metadata.namespace !== namespace
item.metadata?.name !== name ||
item.metadata?.namespace !== namespace
);
break;
default:
Expand Down Expand Up @@ -695,6 +695,7 @@ export class KubeSdk {
for (const index in validSpecs) {
const originSpec = validSpecs[index];
const spec = cloneDeep(originSpec);
spec.metadata = spec.metadata || {};
spec.metadata.annotations = spec.metadata.annotations || {};

const response = await this.create(spec);
Expand Down Expand Up @@ -729,10 +730,11 @@ export class KubeSdk {
for (const index in restoredSpecs) {
const originSpec = validSpecs[index];
const spec = cloneDeep(originSpec);
spec.metadata = spec.metadata || {};
spec.metadata.annotations = spec.metadata.annotations || {};

if (strategy === 'application/apply-patch+yaml') {
delete spec.metadata.managedFields;
delete spec.metadata?.managedFields;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (spec as any).metadata.resourceVersion;
}
Expand Down

0 comments on commit 875d022

Please sign in to comment.