Fix http(s) formatting and add some tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton committed 2022-03-23 08:32:30 -04:00
1 parent 722bd4a666
commit c4f17a3138
2 files changed
+112 -4

No files matched your search

+108
View File
@@ -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/");
});
});
+4 -4
View File
@@ -42,8 +42,8 @@ export interface ILoadBalancerIngress {
// extensions/v1beta1 // extensions/v1beta1
export interface ExtensionsBackend { export interface ExtensionsBackend {
serviceName: string; serviceName?: string;
servicePort: number | string; servicePort?: number | string;
} }
// networking.k8s.io/v1 // networking.k8s.io/v1
@@ -96,7 +96,7 @@ export interface IngressRule {
} }
export interface Ingress { export interface Ingress {
spec: { spec?: {
tls?: { tls?: {
secretName: string; secretName: string;
}[]; }[];
@@ -200,7 +200,7 @@ export class Ingress extends KubeObject {
export function computeRuleDeclarations(ingress: Ingress, rule: IngressRule): ComputedIngressRoute[] { export function computeRuleDeclarations(ingress: Ingress, rule: IngressRule): ComputedIngressRoute[] {
const { host = "*", http: { paths } = { paths: [] }} = rule; const { host = "*", http: { paths } = { paths: [] }} = rule;
const protocol = (ingress.spec.tls?.length ?? 0) > 0 const protocol = (ingress.spec?.tls?.length ?? 0) === 0
? "http" ? "http"
: "https"; : "https";