From d09816aacf61bef6b0795d60a5ca76ed034d27dd Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Wed, 25 May 2022 06:00:37 -0700 Subject: [PATCH] Cherry Pick bug fixes from v5.5.0-beta.2 (#5429) --- .../__tests__/{nodes.test.ts => node.test.ts} | 56 ++++++++++++++++++- src/common/k8s-api/endpoints/node.api.ts | 35 +++++++++--- src/main/helm/exec.ts | 2 +- src/main/kubectl/kubectl.ts | 2 +- src/renderer/components/+nodes/store.ts | 4 +- 5 files changed, 86 insertions(+), 13 deletions(-) rename src/common/k8s-api/__tests__/{nodes.test.ts => node.test.ts} (72%) diff --git a/src/common/k8s-api/__tests__/nodes.test.ts b/src/common/k8s-api/__tests__/node.test.ts similarity index 72% rename from src/common/k8s-api/__tests__/nodes.test.ts rename to src/common/k8s-api/__tests__/node.test.ts index e2527cd9c6..53ffc59d79 100644 --- a/src/common/k8s-api/__tests__/nodes.test.ts +++ b/src/common/k8s-api/__tests__/node.test.ts @@ -8,7 +8,61 @@ import { Node } from "../endpoints"; * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ -describe("Nodes tests", () => { +describe("Node tests", () => { + describe("isMasterNode()", () => { + it("given a master node labelled before kubernetes 1.20, should return true", () => { + const node = new Node({ + apiVersion: "foo", + kind: "Node", + metadata: { + name: "bar", + resourceVersion: "1", + uid: "bat", + labels: { + "node-role.kubernetes.io/master": "NoSchedule", + }, + selfLink: "/api/v1/nodes/bar", + }, + }); + + expect(node.isMasterNode()).toBe(true); + }); + + it("given a master node labelled after kubernetes 1.20, should return true", () => { + const node = new Node({ + apiVersion: "foo", + kind: "Node", + metadata: { + name: "bar", + resourceVersion: "1", + uid: "bat", + labels: { + "node-role.kubernetes.io/control-plane": "NoSchedule", + }, + selfLink: "/api/v1/nodes/bar", + }, + }); + + expect(node.isMasterNode()).toBe(true); + }); + + it("given a non master node, should return false", () => { + const node = new Node({ + apiVersion: "foo", + kind: "Node", + metadata: { + name: "bar", + resourceVersion: "1", + uid: "bat", + labels: {}, + selfLink: "/api/v1/nodes/bar", + }, + }); + + expect(node.isMasterNode()).toBe(false); + }); + }); + describe("getRoleLabels()", () => { it("should return empty string if labels is not present", () => { const node = new Node({ diff --git a/src/common/k8s-api/endpoints/node.api.ts b/src/common/k8s-api/endpoints/node.api.ts index 3aab828621..a51db59fa7 100644 --- a/src/common/k8s-api/endpoints/node.api.ts +++ b/src/common/k8s-api/endpoints/node.api.ts @@ -5,7 +5,7 @@ import type { BaseKubeObjectCondition, KubeObjectScope } from "../kube-object"; import { KubeObject } from "../kube-object"; -import { cpuUnitsToNumber, unitsToBytes } from "../../../renderer/utils"; +import { cpuUnitsToNumber, unitsToBytes, isObject } from "../../../renderer/utils"; import type { MetricData } from "./metrics.api"; import { metricsApi } from "./metrics.api"; import type { DerivedKubeApiOptions, IgnoredKubeApiOptions } from "../kube-api"; @@ -69,6 +69,17 @@ export interface NodeCondition extends BaseKubeObjectCondition { lastHeartbeatTime?: string; } +/** + * These role label prefixs are the ones that are for master nodes + * + * The `master` label has been deprecated in Kubernetes 1.20, and will be removed in 1.25 so we + * have to also use the newer `control-plane` label + */ +const masterNodeLabels = [ + "master", + "control-plane", +]; + /** * This regex is used in the `getRoleLabels()` method bellow, but placed here * as factoring out regexes is best practice. @@ -189,15 +200,19 @@ export class Node extends KubeObject masterNodeLabels.includes(roleLabel)); + } + + getRoleLabelItems(): string[] { const { labels } = this.metadata; - - if (!labels || typeof labels !== "object") { - return ""; - } - const roleLabels: string[] = []; + if (!isObject(labels)) { + return roleLabels; + } + for (const labelKey of Object.keys(labels)) { const match = nodeRoleLabelKeyMatcher.match(labelKey); @@ -214,7 +229,11 @@ export class Node extends KubeObject { } @computed get masterNodes() { - return this.items.filter(node => node.getRoleLabels().includes("master")); + return this.items.filter(node => node.isMasterNode()); } @computed get workerNodes() { - return this.items.filter(node => !node.getRoleLabels().includes("master")); + return this.items.filter(node => !node.isMasterNode()); } getWarningsCount(): number {