forked from kubernetes-sigs/kubebuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
33ca7eb
commit 3fa5404
Showing
2 changed files
with
47 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
function notFound(info) { | ||
return { | ||
statusCode: 404, | ||
headers: {'content-type': 'text/html'}, | ||
body: ("<h1>Not Found</h1>"+ | ||
"<p>You shouldn't see this page, please file a bug</p>"+ | ||
`<details><summary>debug details</summary><pre><code>${JSON.stringify(info)}</code></pre></details>` | ||
), | ||
}; | ||
} | ||
|
||
function redirectToDownload(version, file) { | ||
const loc = `https://github.com/kubernetes-sigs/kubebuilder/releases/download/v${version}/${file}`; | ||
return { | ||
statusCode: 302, | ||
headers: {'location': loc}, | ||
}; | ||
} | ||
|
||
|
||
exports.handler = async function(evt, ctx) { | ||
// grab the prefix too to check for coherence | ||
const [prefix, version, os, arch] = evt.path.split("/").slice(-4); | ||
if (prefix !== 'releases' || !version || !os || !arch) { | ||
return notFound({version: version, os: os, arch: arch, prefix: constPrefix, rawPath: evt.path}); | ||
} | ||
|
||
switch(version[0]) { | ||
case '1': | ||
// fallthrough | ||
case '2': | ||
return redirectToDownload(version, `kubebuilder_${version}_${os}_${arch}.tar.gz`); | ||
default: | ||
return redirectToDownload(version, `kubebuilder_${os}_${arch}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters