From 05f51764197871427b51c9c811a1dc64c5f2e862 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Mon, 12 Apr 2021 08:29:36 -0400 Subject: [PATCH] Broadcast failure to list namespaces less frequently (#2501) - Only of three successive (non-overlapping) list failures Signed-off-by: Sebastian Malton --- src/main/cluster.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main/cluster.ts b/src/main/cluster.ts index 7a02c3e0c9..c7a142cc30 100644 --- a/src/main/cluster.ts +++ b/src/main/cluster.ts @@ -675,6 +675,8 @@ export class Cluster implements ClusterModel, ClusterState { }; } + protected getAllowedNamespacesErrorCount = 0; + protected async getAllowedNamespaces() { if (this.accessibleNamespaces.length) { return this.accessibleNamespaces; @@ -683,16 +685,27 @@ export class Cluster implements ClusterModel, ClusterState { const api = (await this.getProxyKubeconfig()).makeApiClient(CoreV1Api); try { - const namespaceList = await api.listNamespace(); + const { body: { items }} = await api.listNamespace(); + const namespaces = items.map(ns => ns.metadata.name); - return namespaceList.body.items.map(ns => ns.metadata.name); + this.getAllowedNamespacesErrorCount = 0; // reset on success + + return namespaces; } catch (error) { const ctx = (await this.getProxyKubeconfig()).getContextObject(this.contextName); const namespaceList = [ctx.namespace].filter(Boolean); if (namespaceList.length === 0 && error instanceof HttpError && error.statusCode === 403) { - logger.info("[CLUSTER]: listing namespaces is forbidden, broadcasting", { clusterId: this.id }); - broadcastMessage(ClusterListNamespaceForbiddenChannel, this.id); + this.getAllowedNamespacesErrorCount += 1; + + if (this.getAllowedNamespacesErrorCount > 3) { + // reset on send + this.getAllowedNamespacesErrorCount = 0; + + // then broadcast, make sure it is 3 successive attempts + logger.info("[CLUSTER]: listing namespaces is forbidden, broadcasting", { clusterId: this.id, error }); + broadcastMessage(ClusterListNamespaceForbiddenChannel, this.id); + } } return namespaceList;