diff --git a/src/common/k8s-api/__tests__/ingress.api.ts b/src/common/k8s-api/__tests__/ingress.api.ts new file mode 100644 index 0000000000..3b84c37038 --- /dev/null +++ b/src/common/k8s-api/__tests__/ingress.api.ts @@ -0,0 +1,108 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import { computeRuleDeclarations, Ingress } from "../endpoints"; + +describe("computeRuleDeclarations", () => { + it("given no tls field, should format links as http://", () => { + const ingress = new Ingress({ + apiVersion: "networking.k8s.io/v1", + kind: "Ingress", + metadata: { + name: "foo", + resourceVersion: "1", + uid: "bar", + }, + }); + + const result = computeRuleDeclarations(ingress, { + host: "foo.bar", + http: { + paths: [{ + backend: { + service: { + name: "my-service", + port: { + number: 8080, + }, + }, + }, + }], + }, + }); + + expect(result[0].url).toBe("http://foo.bar/"); + }); + + it("given no tls entries, should format links as http://", () => { + const ingress = new Ingress({ + apiVersion: "networking.k8s.io/v1", + kind: "Ingress", + metadata: { + name: "foo", + resourceVersion: "1", + uid: "bar", + }, + }); + + ingress.spec = { + tls: [], + }; + + const result = computeRuleDeclarations(ingress, { + host: "foo.bar", + http: { + paths: [{ + backend: { + service: { + name: "my-service", + port: { + number: 8080, + }, + }, + }, + }], + }, + }); + + expect(result[0].url).toBe("http://foo.bar/"); + }); + + it("given some tls entries, should format links as https://", () => { + const ingress = new Ingress({ + apiVersion: "networking.k8s.io/v1", + kind: "Ingress", + metadata: { + name: "foo", + resourceVersion: "1", + uid: "bar", + }, + }); + + ingress.spec = { + tls: [{ + secretName: "my-secret", + }], + }; + + const result = computeRuleDeclarations(ingress, { + host: "foo.bar", + http: { + paths: [{ + backend: { + service: { + name: "my-service", + port: { + number: 8080, + }, + }, + }, + }], + }, + }); + + expect(result[0].url).toBe("https://foo.bar/"); + }); +}); diff --git a/src/common/k8s-api/endpoints/ingress.api.ts b/src/common/k8s-api/endpoints/ingress.api.ts index afc29605a0..a78a4783b5 100644 --- a/src/common/k8s-api/endpoints/ingress.api.ts +++ b/src/common/k8s-api/endpoints/ingress.api.ts @@ -42,8 +42,8 @@ export interface ILoadBalancerIngress { // extensions/v1beta1 export interface ExtensionsBackend { - serviceName: string; - servicePort: number | string; + serviceName?: string; + servicePort?: number | string; } // networking.k8s.io/v1 @@ -96,7 +96,7 @@ export interface IngressRule { } export interface Ingress { - spec: { + spec?: { tls?: { secretName: string; }[]; @@ -200,7 +200,7 @@ export class Ingress extends KubeObject { export function computeRuleDeclarations(ingress: Ingress, rule: IngressRule): ComputedIngressRoute[] { const { host = "*", http: { paths } = { paths: [] }} = rule; - const protocol = (ingress.spec.tls?.length ?? 0) > 0 + const protocol = (ingress.spec?.tls?.length ?? 0) === 0 ? "http" : "https";