Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dns truncate #403

Merged
merged 3 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})