Skip to content

Commit 3fa5404

Browse files
committed
Fix download redirects with functions
This resolves an issue introduced in the redirect prep for v3 that broke our download redirects. The problem is that v3 releases are `kubebuilder_${os}_${arch}`, whereas v2 & v1 releases are `kubebuilder_${version}_${os}_${arch}.tar.gz`. Since netlify can't handle wildcards that are part of a path component (like `/2.:minorversion`) instead of a whole one (`:version`), we can't select on major version directly in our redirects. Instead, we introduce a function that handles this logic for us, and then use netlify 200-pseudo-redirects to "mount the function on the downloads part of the releases endpoint.
1 parent 33ca7eb commit 3fa5404

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function notFound(info) {
2+
return {
3+
statusCode: 404,
4+
headers: {'content-type': 'text/html'},
5+
body: ("<h1>Not Found</h1>"+
6+
"<p>You shouldn't see this page, please file a bug</p>"+
7+
`<details><summary>debug details</summary><pre><code>${JSON.stringify(info)}</code></pre></details>`
8+
),
9+
};
10+
}
11+
12+
function redirectToDownload(version, file) {
13+
const loc = `https://github.com/kubernetes-sigs/kubebuilder/releases/download/v${version}/${file}`;
14+
return {
15+
statusCode: 302,
16+
headers: {'location': loc},
17+
};
18+
}
19+
20+
21+
exports.handler = async function(evt, ctx) {
22+
// grab the prefix too to check for coherence
23+
const [prefix, version, os, arch] = evt.path.split("/").slice(-4);
24+
if (prefix !== 'releases' || !version || !os || !arch) {
25+
return notFound({version: version, os: os, arch: arch, prefix: constPrefix, rawPath: evt.path});
26+
}
27+
28+
switch(version[0]) {
29+
case '1':
30+
// fallthrough
31+
case '2':
32+
return redirectToDownload(version, `kubebuilder_${version}_${os}_${arch}.tar.gz`);
33+
default:
34+
return redirectToDownload(version, `kubebuilder_${os}_${arch}`);
35+
}
36+
}

netlify.toml

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
command = "./install-and-build.sh"
44
publish = "docs/book/book"
55

6+
# used to handle the split between v2 and v3+ download links
7+
[functions]
8+
directory = "functions"
69

710
# Standard Netlify redirects
811
[[redirects]]
@@ -63,21 +66,7 @@
6366
status = 302
6467
force = true
6568

66-
# v1 redirects.
67-
[[redirects]]
68-
from = "https://go.kubebuilder.io/releases/1.:minorpatch/:os/:arch"
69-
to = "https://github.com/kubernetes-sigs/kubebuilder/releases/download/v1.:minorpatch/kubebuilder_1.:minorpatch_:os_:arch.tar.gz"
70-
status = 302
71-
force = true
72-
73-
# v2 redirects.
74-
[[redirects]]
75-
from = "https://go.kubebuilder.io/releases/2.:minorpatch/:os/:arch"
76-
to = "https://github.com/kubernetes-sigs/kubebuilder/releases/download/v2.:minorpatch/kubebuilder_2.:minorpatch_:os_:arch.tar.gz"
77-
status = 302
78-
force = true
79-
80-
# v3+ redirects.
69+
# general release redirects
8170
[[redirects]]
8271
from = "https://go.kubebuilder.io/releases/:version"
8372
to = "https://github.com/kubernetes-sigs/kubebuilder/releases/v:version"
@@ -90,10 +79,15 @@
9079
status = 302
9180
force = true
9281

82+
# release download redirect
9383
[[redirects]]
9484
from = "https://go.kubebuilder.io/releases/:version/:os/:arch"
95-
to = "https://github.com/kubernetes-sigs/kubebuilder/releases/download/v:version/kubebuilder_:os_:arch"
96-
status = 302
85+
# I don't quite know why, but netlify (or at least the dev mode) *insists*
86+
# on eating every other query parameter, so just use paths instead
87+
to = "/.netlify/functions/handle-version/:version/:os/:arch"
88+
# 200 --> don't redirect to the the function then to whereever it says,
89+
# just pretend like the function is mounted directly here
90+
status = 200
9791
force = true
9892

9993
# Tools redirects.

0 commit comments

Comments
 (0)