From 94a23d2acb4e46361056dc54882e5ba9ae2e59a6 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Tue, 20 Jul 2021 09:01:23 -0400 Subject: [PATCH] Set defaults for EndpointSubsets Signed-off-by: Sebastian Malton --- src/renderer/api/endpoints/endpoint.api.ts | 37 ++++++++++------------ 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/renderer/api/endpoints/endpoint.api.ts b/src/renderer/api/endpoints/endpoint.api.ts index 9aa1701c1d..14bc399062 100644 --- a/src/renderer/api/endpoints/endpoint.api.ts +++ b/src/renderer/api/endpoints/endpoint.api.ts @@ -23,6 +23,7 @@ import { autoBind } from "../../utils"; import { KubeObject } from "../kube-object"; import { KubeApi } from "../kube-api"; import type { KubeJsonApiData } from "../kube-json-api"; +import { get } from "lodash"; export interface IEndpointPort { name?: string; @@ -63,6 +64,10 @@ export class EndpointAddress implements IEndpointAddress { resourceVersion: string; }; + static create(data: IEndpointAddress): EndpointAddress { + return new EndpointAddress(data); + } + constructor(data: IEndpointAddress) { Object.assign(this, data); } @@ -90,35 +95,27 @@ export class EndpointSubset implements IEndpointSubset { ports: IEndpointPort[]; constructor(data: IEndpointSubset) { - Object.assign(this, data); + this.addresses = get(data, "addresses", []); + this.notReadyAddresses = get(data, "notReadyAddresses", []); + this.ports = get(data, "ports", []); } getAddresses(): EndpointAddress[] { - const addresses = this.addresses || []; - - return addresses.map(a => new EndpointAddress(a)); + return this.addresses.map(EndpointAddress.create); } getNotReadyAddresses(): EndpointAddress[] { - const notReadyAddresses = this.notReadyAddresses || []; - - return notReadyAddresses.map(a => new EndpointAddress(a)); + return this.notReadyAddresses.map(EndpointAddress.create); } toString(): string { - if(!this.addresses) { - return ""; - } - - return this.addresses.map(address => { - if (!this.ports) { - return address.ip; - } - - return this.ports.map(port => { - return `${address.ip}:${port.port}`; - }).join(", "); - }).join(", "); + return this.addresses + .map(address => ( + this.ports + .map(port => `${address.ip}:${port.port}`) + .join(", ") + )) + .join(", "); } }