From 666d90dd3c53208b50c9c717b9fb49825d7a63db Mon Sep 17 00:00:00 2001 From: Lauri Nevala Date: Tue, 21 Jun 2022 18:24:02 +0300 Subject: [PATCH] Do not crash if ingress is missing status.loadbalancer.ingress property (#5665) --- .../{ingress.api.ts => ingress.api.test.ts} | 47 +++++++++++++++++++ src/common/k8s-api/endpoints/ingress.api.ts | 4 +- 2 files changed, 49 insertions(+), 2 deletions(-) rename src/common/k8s-api/__tests__/{ingress.api.ts => ingress.api.test.ts} (68%) diff --git a/src/common/k8s-api/__tests__/ingress.api.ts b/src/common/k8s-api/__tests__/ingress.api.test.ts similarity index 68% rename from src/common/k8s-api/__tests__/ingress.api.ts rename to src/common/k8s-api/__tests__/ingress.api.test.ts index d5e4d323ad..acb2a59a68 100644 --- a/src/common/k8s-api/__tests__/ingress.api.ts +++ b/src/common/k8s-api/__tests__/ingress.api.test.ts @@ -5,6 +5,53 @@ import { computeRuleDeclarations, Ingress } from "../endpoints"; +describe("Ingress", () => { + it("given no loadbalancer ingresses in status property, loadbalancers should be an empty array", () => { + const ingress = new Ingress({ + apiVersion: "networking.k8s.io/v1", + kind: "Ingress", + metadata: { + name: "foo", + resourceVersion: "1", + uid: "bar", + namespace: "default", + selfLink: "/apis/networking.k8s.io/v1/ingresses/default/foo", + }, + status: { + loadBalancer: {}, + }, + }); + + expect(ingress.getLoadBalancers()).toEqual([]); + }); + + it("given loadbalancer ingresses in status property, loadbalancers should be flat array of ip adresses and hostnames", () => { + const ingress = new Ingress({ + apiVersion: "networking.k8s.io/v1", + kind: "Ingress", + metadata: { + name: "foo", + resourceVersion: "1", + uid: "bar", + namespace: "default", + selfLink: "/apis/networking.k8s.io/v1/ingresses/default/foo", + }, + status: { + loadBalancer: { + ingress: [{ + ip: "10.0.0.27", + }, + { + hostname: "localhost", + }], + }, + }, + }); + + expect(ingress.getLoadBalancers()).toEqual(["10.0.0.27", "localhost"]); + }); +}); + describe("computeRuleDeclarations", () => { it("given no tls field, should format links as http://", () => { const ingress = new Ingress({ diff --git a/src/common/k8s-api/endpoints/ingress.api.ts b/src/common/k8s-api/endpoints/ingress.api.ts index 4b9c044b0a..0391171e3a 100644 --- a/src/common/k8s-api/endpoints/ingress.api.ts +++ b/src/common/k8s-api/endpoints/ingress.api.ts @@ -136,7 +136,7 @@ export interface IngressSpec { export interface IngressStatus { loadBalancer: { - ingress: ILoadBalancerIngress[]; + ingress?: ILoadBalancerIngress[]; }; } @@ -199,7 +199,7 @@ export class Ingress extends KubeObject ( + return this.status?.loadBalancer?.ingress?.map(address => ( address.hostname || address.ip )) ?? []; }