From 30450907999bdb8396824f909bbe305dedaa15d0 Mon Sep 17 00:00:00 2001 From: Nancy <42977925+mantis-toboggan-md@users.noreply.github.com> Date: Fri, 26 Jan 2024 16:33:45 -0800 Subject: [PATCH] fix auto-import action when cluster has no labels (#28) Co-authored-by: Evgeniya Vashkevich --- pkg/capi/index.ts | 56 ++++++++++++++++++------------------ pkg/capi/util/auto-import.ts | 3 ++ 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/pkg/capi/index.ts b/pkg/capi/index.ts index 34b60d5..a59ba5d 100644 --- a/pkg/capi/index.ts +++ b/pkg/capi/index.ts @@ -38,36 +38,36 @@ export default function(plugin: IPlugin): void { ); // add enable auto-import action to namespace table - // plugin.addAction(ActionLocation.TABLE, - // { path: [{ urlPath: '/c/local/explorer/projectsnamespaces', exact: true }, { urlPath: 'cluster.x-k8s.io.cluster', endsWith: true }] }, - // { - // labelKey: 'capi.autoImport.enableAction', - // icon: 'icon-plus', - // enabled(target: any) { - // return target.metadata.labels[LABELS.AUTO_IMPORT] !== 'true'; - // }, - // invoke(opts, resources = []) { - // resources.forEach((ns) => { - // toggleAutoImport(ns); - // }); - // } - // }); + plugin.addAction(ActionLocation.TABLE, + { path: [{ urlPath: '/c/local/explorer/projectsnamespaces', exact: true }, { urlPath: 'cluster.x-k8s.io.cluster', endsWith: true }] }, + { + labelKey: 'capi.autoImport.enableAction', + icon: 'icon-plus', + enabled(target: any) { + return target?.metadata?.labels?.[LABELS.AUTO_IMPORT] !== 'true'; + }, + invoke(opts, resources = []) { + resources.forEach((ns) => { + toggleAutoImport(ns); + }); + } + }); // add disable auto-import action to namespace table - // plugin.addAction(ActionLocation.TABLE, - // { path: [{ urlPath: '/c/local/explorer/projectsnamespaces', exact: true }, { urlPath: 'cluster.x-k8s.io.cluster', endsWith: true }] }, - // { - // labelKey: 'capi.autoImport.disableAction', - // icon: 'icon-minus', - // enabled(target: any) { - // return target.metadata.labels[LABELS.AUTO_IMPORT] === 'true'; - // }, - // invoke(opts, resources = []) { - // resources.forEach((ns) => { - // toggleAutoImport(ns); - // }); - // } - // }); + plugin.addAction(ActionLocation.TABLE, + { path: [{ urlPath: '/c/local/explorer/projectsnamespaces', exact: true }, { urlPath: 'cluster.x-k8s.io.cluster', endsWith: true }] }, + { + labelKey: 'capi.autoImport.disableAction', + icon: 'icon-minus', + enabled(target: any) { + return target?.metadata?.labels?.[LABELS.AUTO_IMPORT] === 'true'; + }, + invoke(opts, resources = []) { + resources.forEach((ns) => { + toggleAutoImport(ns); + }); + } + }); // add column to namespace table plugin.addTableColumn( diff --git a/pkg/capi/util/auto-import.ts b/pkg/capi/util/auto-import.ts index 32bd069..546d8b6 100644 --- a/pkg/capi/util/auto-import.ts +++ b/pkg/capi/util/auto-import.ts @@ -4,6 +4,9 @@ export default function(resource: any) { if (resource?.metadata?.labels?.[LABELS.AUTO_IMPORT] === 'true') { delete resource.metadata.labels[LABELS.AUTO_IMPORT]; } else { + if (!resource.metadata.labels) { + resource.metadata.labels = {}; + } resource.metadata.labels[LABELS.AUTO_IMPORT] = 'true'; } try {