Skip to content

Commit

Permalink
feat: start publishing release metadata json file (#199)
Browse files Browse the repository at this point in the history
whenever a release is built we now publish a metadata json file with a
content similar to this:

```json
{
        "Versions": {
                "AdminConsole": "v1.104.3",
                "EmbeddedClusterOperator": "v0.4.1",
                "Installer": "v1.28.4+ec.0-dirty",
                "Kubernetes": "v1.28.4+k0s.0",
                "OpenEBS": "v3.9.0"
        },
        "K0sSHA": "926bcb68478f0eb5bb8479bf01d39524102346d9f24b9f536aeddf71fdd5a975"
}
```

a new hidden topic has been added to the version topic, this is how we
generate the version (or release) metadata:

```
$ embedded-cluster version metadata
{
        "Versions": {
                "AdminConsole": "v1.104.3",
                "EmbeddedClusterOperator": "v0.4.1",
                "Installer": "v1.28.4+ec.0-dirty",
                "Kubernetes": "v1.28.4+k0s.0",
                "OpenEBS": "v3.9.0"
        },
        "K0sSHA": "926bcb68478f0eb5bb8479bf01d39524102346d9f24b9f536aeddf71fdd5a975"
}
$
```
  • Loading branch information
ricardomaraschini authored Dec 1, 2023
1 parent 06bce22 commit 259eb0b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/release-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
run: |
make embedded-cluster-linux-amd64
tar -C output/bin -czvf embedded-cluster-linux-amd64.tgz embedded-cluster
./output/bin/embedded-cluster version metadata > metadata.json
make clean
- name: Build darwin-amd64
run: |
Expand All @@ -41,3 +42,4 @@ jobs:
title: Development Release Build
files: |
*.tgz
metadata.json
2 changes: 2 additions & 0 deletions .github/workflows/release-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
run: |
make embedded-cluster-linux-amd64 VERSION=$TAG_NAME
tar -C output/bin -czvf embedded-cluster-linux-amd64.tgz embedded-cluster
./output/bin/embedded-cluster version metadata > metadata.json
make clean
- name: Build darwin-amd64
run: |
Expand All @@ -41,3 +42,4 @@ jobs:
prerelease: false
files: |
*.tgz
metadata.json
40 changes: 38 additions & 2 deletions cmd/embedded-cluster/version.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"fmt"
"strings"

Expand All @@ -9,11 +10,13 @@ import (

"github.com/replicatedhq/embedded-cluster/pkg/addons"
"github.com/replicatedhq/embedded-cluster/pkg/defaults"
"github.com/replicatedhq/embedded-cluster/pkg/goods"
)

var versionCommand = &cli.Command{
Name: "version",
Usage: fmt.Sprintf("Shows the %s installer version", defaults.BinaryName()),
Name: "version",
Usage: fmt.Sprintf("Shows the %s installer version", defaults.BinaryName()),
Subcommands: []*cli.Command{metadataCommand},
Action: func(c *cli.Context) error {
opts := []addons.Option{addons.Quiet(), addons.WithoutPrompt()}
versions, err := addons.NewApplier(opts...).Versions()
Expand All @@ -34,3 +37,36 @@ var versionCommand = &cli.Command{
return nil
},
}

// ReleaseMetadata holds the metadata about a specific release, including addons and
// their versions.
type ReleaseMetadata struct {
Versions map[string]string
K0sSHA string
}

var metadataCommand = &cli.Command{
Name: "metadata",
Usage: "Print metadata about this release",
Hidden: true,
Action: func(c *cli.Context) error {
opts := []addons.Option{addons.Quiet(), addons.WithoutPrompt()}
versions, err := addons.NewApplier(opts...).Versions()
if err != nil {
return fmt.Errorf("unable to get versions: %w", err)
}
versions["Kubernetes"] = defaults.K0sVersion
versions["Installer"] = defaults.Version
sha, err := goods.K0sBinarySHA256()
if err != nil {
return fmt.Errorf("unable to get k0s binary sha256: %w", err)
}
meta := ReleaseMetadata{Versions: versions, K0sSHA: sha}
data, err := json.MarshalIndent(meta, "", "\t")
if err != nil {
return fmt.Errorf("unable to marshal versions: %w", err)
}
fmt.Println(string(data))
return nil
},
}
20 changes: 20 additions & 0 deletions pkg/goods/goods.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
package goods

import (
"crypto/sha256"
"embed"
"encoding/hex"
"fmt"
"io"
"os"
"path"
"runtime"
"strings"

Expand All @@ -15,6 +19,22 @@ import (
//go:embed bins/*
var binfs embed.FS

// K0sBinarySHA256 returns the SHA256 checksum of the embedded k0s binary.
func K0sBinarySHA256() (string, error) {
fname := fmt.Sprintf("k0s-%s", defaults.K0sVersion)
binpath := path.Join("bins", "k0sctl", fname)
fp, err := binfs.Open(binpath)
if err != nil {
return "", fmt.Errorf("unable to open embedded k0s binary: %w", err)
}
defer fp.Close()
hasher := sha256.New()
if _, err := io.Copy(hasher, fp); err != nil {
return "", fmt.Errorf("unable to copy embedded k0s binary: %w", err)
}
return hex.EncodeToString(hasher.Sum(nil)), nil
}

// Materialize writes to disk embed assets.
func Materialize() error {
entries, err := binfs.ReadDir("bins/k0sctl")
Expand Down

0 comments on commit 259eb0b

Please sign in to comment.