Skip to content
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

fix: 🐛 fix Unstructured metadata types #47

Merged
merged 1 commit into from
May 7, 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
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
Loading