Skip to content

Commit

Permalink
cache operator and kots binaries in the public files s3 bucket (#683)
Browse files Browse the repository at this point in the history
* cache operator binary in the public files s3 bucket

* fail on failing http codes

* update operator import

* use 'Install', not 'AddToScheme'

* update embedded-cluster-kinds

* cache kots bin, include artifact paths in metadata

* back to addtoscheme

* return k0s version

* fix kots binary source

* use correct kots binary

* update operator to revert k0s upgrade

* use existing KOTS_VERSION and KOTS_BINARY_URL_OVERRIDE makefile vars

* fix merge

* test artifact output
  • Loading branch information
laverya authored Jun 12, 2024
1 parent 664d3ae commit 3ed87ba
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 35 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ADMIN_CONSOLE_IMAGE_OVERRIDE =
ADMIN_CONSOLE_MIGRATIONS_IMAGE_OVERRIDE =
EMBEDDED_OPERATOR_CHART_URL = oci://registry.replicated.com/library
EMBEDDED_OPERATOR_CHART_NAME = embedded-cluster-operator
EMBEDDED_OPERATOR_CHART_VERSION = 0.34.9
EMBEDDED_OPERATOR_CHART_VERSION = 0.35.2
EMBEDDED_OPERATOR_UTILS_IMAGE = busybox:1.36.1
EMBEDDED_CLUSTER_OPERATOR_IMAGE_OVERRIDE =
OPENEBS_CHART_URL = https://openebs.github.io/openebs
Expand Down Expand Up @@ -39,7 +39,6 @@ LOCAL_ARTIFACT_MIRROR_IMAGE ?= registry.replicated.com/library/embedded-cluster-
LOCAL_ARTIFACT_MIRROR_IMAGE_LOCATION = ${LOCAL_ARTIFACT_MIRROR_IMAGE}:$(subst +,-,$(VERSION))
LD_FLAGS = -X github.com/replicatedhq/embedded-cluster/pkg/defaults.K0sVersion=$(K0S_VERSION) \
-X github.com/replicatedhq/embedded-cluster/pkg/defaults.Version=$(VERSION) \
-X github.com/replicatedhq/embedded-cluster/pkg/defaults.K0sBinaryURL=$(K0S_BINARY_SOURCE_OVERRIDE) \
-X github.com/replicatedhq/embedded-cluster/pkg/defaults.TroubleshootVersion=$(TROUBLESHOOT_VERSION) \
-X github.com/replicatedhq/embedded-cluster/pkg/defaults.KubectlVersion=$(KUBECTL_VERSION) \
-X github.com/replicatedhq/embedded-cluster/pkg/defaults.LocalArtifactMirrorImage=$(LOCAL_ARTIFACT_MIRROR_IMAGE_LOCATION) \
Expand All @@ -48,6 +47,7 @@ LD_FLAGS = -X github.com/replicatedhq/embedded-cluster/pkg/defaults.K0sVersion=$
-X github.com/replicatedhq/embedded-cluster/pkg/addons/adminconsole.Version=$(ADMIN_CONSOLE_CHART_VERSION) \
-X github.com/replicatedhq/embedded-cluster/pkg/addons/adminconsole.ImageOverride=$(ADMIN_CONSOLE_IMAGE_OVERRIDE) \
-X github.com/replicatedhq/embedded-cluster/pkg/addons/adminconsole.MigrationsImageOverride=$(ADMIN_CONSOLE_MIGRATIONS_IMAGE_OVERRIDE) \
-X github.com/replicatedhq/embedded-cluster/pkg/addons/adminconsole.KotsVersion=$(KOTS_VERSION) \
-X github.com/replicatedhq/embedded-cluster/pkg/addons/embeddedclusteroperator.ChartURL=$(EMBEDDED_OPERATOR_CHART_URL) \
-X github.com/replicatedhq/embedded-cluster/pkg/addons/embeddedclusteroperator.ChartName=$(EMBEDDED_OPERATOR_CHART_NAME) \
-X github.com/replicatedhq/embedded-cluster/pkg/addons/embeddedclusteroperator.Version=$(EMBEDDED_OPERATOR_CHART_VERSION) \
Expand Down
17 changes: 11 additions & 6 deletions cmd/embedded-cluster/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/urfave/cli/v2"

"github.com/replicatedhq/embedded-cluster/pkg/addons"
"github.com/replicatedhq/embedded-cluster/pkg/addons/adminconsole"
"github.com/replicatedhq/embedded-cluster/pkg/addons/embeddedclusteroperator"
"github.com/replicatedhq/embedded-cluster/pkg/config"
"github.com/replicatedhq/embedded-cluster/pkg/defaults"
"github.com/replicatedhq/embedded-cluster/pkg/goods"
Expand Down Expand Up @@ -103,10 +105,16 @@ func gatherVersionMetadata() (*types.ReleaseMetadata, error) {
return nil, fmt.Errorf("unable to get k0s binary sha256: %w", err)
}

artifacts := map[string]string{
"kots": fmt.Sprintf("kots-binaries/%s", adminconsole.KotsVersion),
"operator": fmt.Sprintf("operator-binaries/%s", embeddedclusteroperator.Version),
"local-artifact-mirror-image": defaults.LocalArtifactMirrorImage,
}

meta := types.ReleaseMetadata{
Versions: versions,
K0sSHA: sha,
K0sBinaryURL: defaults.K0sBinaryURL,
Versions: versions,
K0sSHA: sha,
Artifacts: artifacts,
}

chtconfig, repconfig, err := applier.GenerateHelmConfigs(
Expand Down Expand Up @@ -145,9 +153,6 @@ func gatherVersionMetadata() (*types.ReleaseMetadata, error) {
return nil, fmt.Errorf("unable to get airgap images: %w", err)
}
meta.K0sImages = append(meta.K0sImages, additionalImages...)
meta.Artifacts = map[string]string{
"local-artifact-mirror-image": defaults.LocalArtifactMirrorImage,
}

return &meta, nil
}
Expand Down
17 changes: 17 additions & 0 deletions e2e/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,23 @@ func TestVersion(t *testing.T) {
}
}

expectedArtifacts := []string{"kots", "operator", "local-artifact-mirror-image"}
if len(parsed.Artifacts) != len(expectedArtifacts) {
t.Log(output)
t.Fatalf("found %d artifacts in metadata, expected %d", len(parsed.Artifacts), len(expectedArtifacts))
}

for _, expectedName := range expectedArtifacts {
if _, ok := parsed.Artifacts[expectedName]; !ok {
t.Errorf("failed to find artifact %s in 'metadata' output", expectedName)
failed = true
}
if len(parsed.Artifacts[expectedName]) == 0 {
t.Errorf("artifact %s is empty in 'metadata' output", expectedName)
failed = true
}
}

if failed {
t.Log(output)
t.FailNow()
Expand Down
11 changes: 5 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,15 @@ require (
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
go.etcd.io/etcd/api/v3 v3.5.13 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.13 // indirect
go.etcd.io/etcd/client/v3 v3.5.13 // indirect
go.etcd.io/etcd/api/v3 v3.5.14 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.14 // indirect
go.etcd.io/etcd/client/v3 v3.5.14 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
golang.org/x/sync v0.7.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
google.golang.org/grpc v1.62.1 // indirect
google.golang.org/grpc v1.64.0 // indirect
helm.sh/helm/v3 v3.15.1 // indirect
)

Expand Down
22 changes: 10 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,12 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zitadel/oidc/v2 v2.7.0 h1:IGX4EDk6tegTjUSsZDWeTfLseFU0BdJ/Glf1tgys2lU=
github.com/zitadel/oidc/v2 v2.7.0/go.mod h1:zkUkVJS0sDVy9m0UA9RgO3f8i/C0rtjvXU36UJj7T+0=
go.etcd.io/etcd/api/v3 v3.5.13 h1:8WXU2/NBge6AUF1K1gOexB6e07NgsN1hXK0rSTtgSp4=
go.etcd.io/etcd/api/v3 v3.5.13/go.mod h1:gBqlqkcMMZMVTMm4NDZloEVJzxQOQIls8splbqBDa0c=
go.etcd.io/etcd/client/pkg/v3 v3.5.13 h1:RVZSAnWWWiI5IrYAXjQorajncORbS0zI48LQlE2kQWg=
go.etcd.io/etcd/client/pkg/v3 v3.5.13/go.mod h1:XxHT4u1qU12E2+po+UVPrEeL94Um6zL58ppuJWXSAB8=
go.etcd.io/etcd/client/v3 v3.5.13 h1:o0fHTNJLeO0MyVbc7I3fsCf6nrOqn5d+diSarKnB2js=
go.etcd.io/etcd/client/v3 v3.5.13/go.mod h1:cqiAeY8b5DEEcpxvgWKsbLIWNM/8Wy2xJSDMtioMcoI=
go.etcd.io/etcd/api/v3 v3.5.14 h1:vHObSCxyB9zlF60w7qzAdTcGaglbJOpSj1Xj9+WGxq0=
go.etcd.io/etcd/api/v3 v3.5.14/go.mod h1:BmtWcRlQvwa1h3G2jvKYwIQy4PkHlDej5t7uLMUdJUU=
go.etcd.io/etcd/client/pkg/v3 v3.5.14 h1:SaNH6Y+rVEdxfpA2Jr5wkEvN6Zykme5+YnbCkxvuWxQ=
go.etcd.io/etcd/client/pkg/v3 v3.5.14/go.mod h1:8uMgAokyG1czCtIdsq+AGyYQMvpIKnSvPjFMunkgeZI=
go.etcd.io/etcd/client/v3 v3.5.14 h1:CWfRs4FDaDoSz81giL7zPpZH2Z35tbOrAJkkjMqOupg=
go.etcd.io/etcd/client/v3 v3.5.14/go.mod h1:k3XfdV/VIHy/97rqWjoUzrj9tk7GgJGH9J8L4dNXmAk=
go.mongodb.org/mongo-driver v1.11.3 h1:Ql6K6qYHEzB6xvu4+AU0BoRoqf9vFPcc4o7MUIdPW8Y=
go.mongodb.org/mongo-driver v1.11.3/go.mod h1:PTSz5yu21bkT/wXpkS7WR5f0ddqw5quethTUn9WM+2g=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
Expand Down Expand Up @@ -415,17 +415,15 @@ google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80 h1:KAeGQVN3M9nD0/bQXnr/ClcEMJ968gUXJQ9pwfSynuQ=
google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80/go.mod h1:cc8bqMqtv9gMOr0zHg2Vzff5ULhhL2IXP4sbcn32Dro=
google.golang.org/genproto/googleapis/api v0.0.0-20240123012728-ef4313101c80 h1:Lj5rbfG876hIAYFjqiJnPHfhXbv+nzTWfm04Fg/XSVU=
google.golang.org/genproto/googleapis/api v0.0.0-20240123012728-ef4313101c80/go.mod h1:4jWUdICTdgc3Ibxmr8nAJiiLHwQBY0UI0XZcEMaFKaA=
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237 h1:RFiFrvy37/mpSpdySBDrUdipW/dHwsRwh3J3+A9VgT4=
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237/go.mod h1:Z5Iiy3jtmioajWHDGFk7CeugTyHtPvMHA4UTmUkyalE=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 h1:NnYq6UN9ReLM9/Y01KWNOWyI5xQ9kbIms5GGJVwS/Yc=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
google.golang.org/grpc v1.62.1 h1:B4n+nfKzOICUXMgyrNd19h/I9oH0L1pizfk1d4zSgTk=
google.golang.org/grpc v1.62.1/go.mod h1:IWTG0VlJLCh1SkC58F7np9ka9mx/WNkjl4PGJaiq+QE=
google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY=
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
Expand Down
1 change: 1 addition & 0 deletions pkg/addons/adminconsole/adminconsole.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
Version = "v0.0.0"
ImageOverride = ""
MigrationsImageOverride = ""
KotsVersion = ""
CounterRegex = regexp.MustCompile(`(\d+)/(\d+)`)
Password = ""
)
Expand Down
5 changes: 0 additions & 5 deletions pkg/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ var (
KubectlVersion = "0.0.0"
// provider holds a global reference to the default provider.
provider *Provider
// K0sBinaryURL holds an alternative URL from where to download the k0s
// binary that has been embedded in this version of the binary. If this
// is empty then it means we have shipped the official k0s binary. This
// is set at compile time via ldflags.
K0sBinaryURL = ""
// LocalArtifactMirrorImage holds a reference to where the lam image for
// this version of embedded-cluster is stored. Set at compile time.
LocalArtifactMirrorImage = ""
Expand Down
69 changes: 65 additions & 4 deletions scripts/cache-files.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

set -eo pipefail
set -euo pipefail

function require() {
if [ -z "$2" ]; then
Expand Down Expand Up @@ -51,19 +51,78 @@ function k0sbin() {
return 0
fi

# if the override is set, the binary will have been added to the bucket through another process
# if the override is set, we should download this binary and upload it to the bucket so as not to require end users hit the override url
if [ -n "${k0s_override}" ] && [ "${k0s_override}" != '' ]; then
echo "K0S_BINARY_SOURCE_OVERRIDE is set to '${k0s_override}', using that source"
curl -L -o "${k0s_version}" "${k0s_override}"
curl --fail-with-body -L -o "${k0s_version}" "${k0s_override}"
else
# download the k0s binary from official sources
curl -L -o "${k0s_version}" "https://github.com/k0sproject/k0s/releases/download/${k0s_version}/k0s-${k0s_version}-amd64"
echo "downloading k0s binary from https://github.com/k0sproject/k0s/releases/download/${k0s_version}/k0s-${k0s_version}-amd64"
curl --fail-with-body -L -o "${k0s_version}" "https://github.com/k0sproject/k0s/releases/download/${k0s_version}/k0s-${k0s_version}-amd64"
fi

# upload the binary to the bucket
retry 3 aws s3 cp "${k0s_version}" "s3://${S3_BUCKET}/k0s-binaries/${k0s_version}"
}

function operatorbin() {
# first, figure out what version of operator is in the current build
local operator_version=
operator_version=$(awk '/^EMBEDDED_OPERATOR_CHART_VERSION/{print $3}' Makefile)

# check if the binary already exists in the bucket
local operator_binary_exists=
operator_binary_exists=$(aws s3api head-object --bucket "${S3_BUCKET}" --key "operator-binaries/${operator_version}" || true)

# if the binary already exists, we don't need to upload it again
if [ -n "${operator_binary_exists}" ]; then
echo "operator binary ${operator_version} already exists in bucket ${S3_BUCKET}, skipping upload"
return 0
fi

# download the operator binary from github
echo "downloading embedded cluster operator binary from https://github.com/replicatedhq/embedded-cluster-operator/releases/download/v${operator_version}/manager"
curl --fail-with-body -L -o "${operator_version}" "https://github.com/replicatedhq/embedded-cluster-operator/releases/download/v${operator_version}/manager"

# upload the binary to the bucket
retry 3 aws s3 cp "${operator_version}" "s3://${S3_BUCKET}/operator-binaries/${operator_version}"
}

function kotsbin() {
# first, figure out what version of kots is in the current build
local kots_version=
kots_version=$(awk '/^ADMIN_CONSOLE_CHART_VERSION/{print $3}' Makefile)
kots_version=$(echo "${kots_version}" | sed 's/\([0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/')

local kots_override=
kots_override=$(awk '/^KOTS_BINARY_URL_OVERRIDE/{gsub("\"", "", $3); print $3}' Makefile)

# check if the binary already exists in the bucket
local kots_binary_exists=
kots_binary_exists=$(aws s3api head-object --bucket "${S3_BUCKET}" --key "kots-binaries/${kots_version}" || true)

# if the binary already exists, we don't need to upload it again
if [ -n "${kots_binary_exists}" ]; then
echo "kots binary ${kots_version} already exists in bucket ${S3_BUCKET}, skipping upload"
return 0
fi

if [ -n "${kots_override}" ] && [ "${kots_override}" != '' ]; then
echo "KOTS_BINARY_URL_OVERRIDE is set to '${kots_override}', using that source"
curl --fail-with-body -L -o "kots_linux_amd64.tar.gz" "${kots_override}"
else
# download the kots binary from github
echo "downloading kots binary from https://github.com/replicatedhq/kots/releases/download/v${kots_version}/kots_linux_amd64.tar.gz"
curl --fail-with-body -L -o "kots_linux_amd64.tar.gz" "https://github.com/replicatedhq/kots/releases/download/v${kots_version}/kots_linux_amd64.tar.gz"
fi

# decompress the bundle, as we only care about the binary and not the sbom/license/readme
tar -xvf kots_linux_amd64.tar.gz

# upload the binary to the bucket
retry 3 aws s3 cp "kots" "s3://${S3_BUCKET}/kots-binaries/${kots_version}"
}

function metadata() {
if [ -z "${EC_VERSION}" ]; then
echo "EC_VERSION unset, not uploading metadata.json"
Expand Down Expand Up @@ -98,6 +157,8 @@ function embeddedcluster() {
# the embedded cluster release does not exist for CI builds
function main() {
k0sbin
operatorbin
kotsbin
metadata
embeddedcluster
}
Expand Down

0 comments on commit 3ed87ba

Please sign in to comment.