Skip to content

Commit

Permalink
fix: dns truncate (#403)
Browse files Browse the repository at this point in the history
Co-authored-by: Julien Bouquillon <[email protected]>
  • Loading branch information
devthejo and Julien Bouquillon authored Aug 30, 2023
1 parent 65dc733 commit d7e9ffe
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 22 deletions.
49 changes: 27 additions & 22 deletions plugins/contrib/patches/03-dns-truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,36 @@ module.exports = (manifests, _options, context) => {
const { utils } = context
const { yaml, slug, logger } = utils

const subdomainsToTruncate = []
for (const manifest of manifests) {
if (manifest.kind !== "Ingress") {
continue
}
const rules = manifest.spec?.rules || []
for (const { host } of rules) {
const domainParts = host.split(".")
for (const subdomain of domainParts) {
if (subdomain.length > MAX_DNS_LENGTH) {
subdomainsToTruncate.push(subdomain)
let subdomainsToTruncate = []
do {
subdomainsToTruncate = []
for (const manifest of manifests) {
if (manifest.kind !== "Ingress") {
continue
}
const rules = manifest.spec?.rules || []
for (const { host } of rules) {
const domainParts = host.split(".")
for (const subdomain of domainParts) {
if (subdomain.length > MAX_DNS_LENGTH) {
subdomainsToTruncate.push(subdomain)
}
}
}
}
}
if (subdomainsToTruncate.length > 0) {
let inline = yaml.dump(manifests)
for (const subdomain of subdomainsToTruncate) {
const slugSubdomain = slug(subdomain)
logger.debug(
`replacing too long subdomain "${subdomain}" -> "${slugSubdomain}"`
)
inline = inline.replaceAll(subdomain, slugSubdomain)

if (subdomainsToTruncate.length > 0) {
let inline = yaml.dump(manifests)
for (const subdomain of subdomainsToTruncate) {
const slugSubdomain = slug(subdomain)
logger.debug(
`replacing too long subdomain "${subdomain}" -> "${slugSubdomain}"`
)
inline = inline.replaceAll(subdomain, slugSubdomain)
}
manifests = yaml.load(inline)
}
manifests = yaml.load(inline)
}
} while (subdomainsToTruncate.length > 0)

return manifests
}
79 changes: 79 additions & 0 deletions plugins/contrib/patches/tests/dns-truncate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const dnsTruncate = require("../03-dns-truncate")

const samples = [
{
title: "should strip invalid DNS chars",
options: {},
manifests: [
{
kind: "Ingress",
spec: { rules: [{ host: "some-host.something.com" }] },
},
{
kind: "Ingress",
spec: { rules: [{ host: "something-else-some-host.com" }] },
},
{
kind: "Ingress",
spec: { rules: [{ host: "something.something.some.where.fr" }] },
},
],
expected: [
"some-host.something.com",
"something-else-some-host.com",
"something.something.some.where.fr",
],
},
{
title: "should cut long DNS",
options: {},
manifests: [
{
kind: "Ingress",
spec: {
rules: [
{
host: "there-are-no-big-problems-there-are-just-a-lot-of-little-problems.something.somewhere.around.com",
},
],
},
},
{
kind: "Ingress",
spec: {
rules: [
{
host: "perfection-is-achieved-not-when-there-is-nothing-more-to-add-but-when-there-is-nothing-left-to-take-away.something.somewhere.around.com",
},
],
},
},
],
expected: [
"there-are-no-big-problems-there-are-just-a-lot-of-lit-fjdkhln2.something.somewhere.around.com",
"perfection-is-achieved-not-when-there-is-nothing-more-p4f2qo44.something.somewhere.around.com",
],
},
]

samples.forEach((sample) => {
test(`${sample.title}`, async () => {
const ctx = require("~common/ctx")
const utils = require("~common/utils")

const res = dnsTruncate(sample.manifests, sample.options, {
config: { environment: "dev" },
ctx,
utils,
logger: {
trace: () => {},
},
})

const hosts = res
.filter((k) => k.kind === "Ingress")
.flatMap((ing) => ing.spec.rules.map((rule) => rule.host))

expect(hosts).toEqual(sample.expected)
})
})

0 comments on commit d7e9ffe

Please sign in to comment.