Fix: handle possible null reference in k8s gateway api (#4752)

Co-authored-by: shamoon <4887959+shamoon@users.noreply.github.com>
pull/4760/head
Brett Dudo 2 weeks ago committed by GitHub
parent e46377f461
commit 277fbe8051
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -14,25 +14,29 @@ import * as shvl from "utils/config/shvl";
const logger = createLogger("resource-helpers"); const logger = createLogger("resource-helpers");
const kc = getKubeConfig(); const kc = getKubeConfig();
const getSchemaFromGateway = async (gatewayRef) => { const getSchemaFromGateway = async (parentRef) => {
const crd = kc.makeApiClient(CustomObjectsApi); const crd = kc.makeApiClient(CustomObjectsApi);
const schema = await crd const schema = await crd
.getNamespacedCustomObject({ .getNamespacedCustomObject({
group: HTTPROUTE_API_GROUP, group: HTTPROUTE_API_GROUP,
version: HTTPROUTE_API_VERSION, version: HTTPROUTE_API_VERSION,
namespace: gatewayRef.namespace, namespace: parentRef.namespace,
plural: "gateways", plural: "gateways",
name: gatewayRef.name, name: parentRef.name,
}) })
.then((response) => { .then((response) => {
const listner = response.spec.listeners.filter((listener) => listener.name === gatewayRef.sectionName)[0]; const listener =
return listner.protocol.toLowerCase(); response.spec.listeners.find((l) => l.name === parentRef.sectionName) ?? response.spec.listeners[0];
return listener.protocol.toLowerCase();
}) })
.catch((error) => { .catch((error) => {
logger.error("Error getting gateways: %d %s %s", error.statusCode, error.body, error.response); logger.error("Error getting gateways: %d %s %s", error.statusCode, error.body, error.response);
logger.debug(error); logger.debug(error);
return "";
return "http";
}); });
return schema; return schema;
}; };
@ -44,10 +48,11 @@ async function getUrlFromHttpRoute(resource) {
if (resource.spec.rules[0].matches[0].path.type !== "RegularExpression") { if (resource.spec.rules[0].matches[0].path.type !== "RegularExpression") {
const urlHost = resource.spec.hostnames[0]; const urlHost = resource.spec.hostnames[0];
const urlPath = resource.spec.rules[0].matches[0].path.value; const urlPath = resource.spec.rules[0].matches[0].path.value;
const urlSchema = (await getSchemaFromGateway(resource.spec.parentRefs[0])) ? "https" : "http"; const urlSchema = await getSchemaFromGateway(resource.spec.parentRefs[0]);
url = `${urlSchema}://${urlHost}${urlPath}`; url = `${urlSchema}://${urlHost}${urlPath}`;
} }
} }
return url; return url;
} }
@ -55,6 +60,7 @@ function getUrlFromIngress(resource) {
const urlHost = resource.spec.rules[0].host; const urlHost = resource.spec.rules[0].host;
const urlPath = resource.spec.rules[0].http.paths[0].path; const urlPath = resource.spec.rules[0].http.paths[0].path;
const urlSchema = resource.spec.tls ? "https" : "http"; const urlSchema = resource.spec.tls ? "https" : "http";
return `${urlSchema}://${urlHost}${urlPath}`; return `${urlSchema}://${urlHost}${urlPath}`;
} }
@ -66,6 +72,7 @@ async function getUrlSchema(resource) {
} else { } else {
urlSchema = getUrlFromIngress(resource); urlSchema = getUrlFromIngress(resource);
} }
return urlSchema; return urlSchema;
} }

Loading…
Cancel
Save