Skip to content

Commit b088d4e

Browse files
authored
Merge pull request #2169 from DirectXMan12/docs/netlify-functions-cherrypick
📖 Netlify functions for download redirects
2 parents 33ca7eb + a735bb3 commit b088d4e

File tree

2 files changed

+54
-17
lines changed

2 files changed

+54
-17
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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, 'content-type': 'text/plain'},
17+
body: `Redirecting to ${loc}`,
18+
};
19+
}
20+
21+
22+
exports.handler = async function(evt, ctx) {
23+
// grab the prefix too to check for coherence
24+
const [prefix, version, os, arch] = evt.path.split("/").slice(-4);
25+
if (prefix !== 'releases' || !version || !os || !arch) {
26+
return notFound({version: version, os: os, arch: arch, prefix: prefix, rawPath: evt.path});
27+
}
28+
29+
switch(version[0]) {
30+
case '1':
31+
// fallthrough
32+
case '2':
33+
return redirectToDownload(version, `kubebuilder_${version}_${os}_${arch}.tar.gz`);
34+
default:
35+
return redirectToDownload(version, `kubebuilder_${os}_${arch}`);
36+
}
37+
}

netlify.toml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22
base = "docs/book"
33
command = "./install-and-build.sh"
44
publish = "docs/book/book"
5+
functions = "docs/book/functions"
56

7+
# TODO(directxman12): I don't know why, but this (functions) stanza is in the
8+
# docs and local `netlify dev`, but the above one (under build) is used by the
9+
# online version :-/
10+
11+
# used to handle the split between v2 and v3+ download links
12+
[functions]
13+
# relative to base directory
14+
directory = "functions"
615

716
# Standard Netlify redirects
817
[[redirects]]
@@ -63,21 +72,7 @@
6372
status = 302
6473
force = true
6574

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.
75+
# general release redirects
8176
[[redirects]]
8277
from = "https://go.kubebuilder.io/releases/:version"
8378
to = "https://github.com/kubernetes-sigs/kubebuilder/releases/v:version"
@@ -90,10 +85,15 @@
9085
status = 302
9186
force = true
9287

88+
# release download redirect
9389
[[redirects]]
9490
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
91+
# I don't quite know why, but netlify (or at least the dev mode) *insists*
92+
# on eating every other query parameter, so just use paths instead
93+
to = "/.netlify/functions/handle-version/:version/:os/:arch"
94+
# 200 --> don't redirect to the the function then to whereever it says,
95+
# just pretend like the function is mounted directly here
96+
status = 200
9797
force = true
9898

9999
# Tools redirects.

0 commit comments

Comments
 (0)