Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into k0s-1-29
Browse files Browse the repository at this point in the history
  • Loading branch information
laverya committed Oct 23, 2024
2 parents 430c1c8 + adc8a2c commit 2eb8fb7
Show file tree
Hide file tree
Showing 38 changed files with 1,528 additions and 94 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
uses: golangci/golangci-lint-action@v6
with:
version: v1.60.3
args: --build-tags exclude_graphdriver_btrfs

test:
name: Unit tests
Expand Down Expand Up @@ -519,6 +520,7 @@ jobs:
- TestMultiNodeReset
- TestCollectSupportBundle
- TestUnsupportedOverrides
- TestHostCollectSupportBundleInCluster
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ go.work.sum
.gomodcache
/local-dev/
*.tmp
.envrc
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ embedded-cluster:
unit-tests:
mkdir -p pkg/goods/bins pkg/goods/internal/bins
touch pkg/goods/bins/BUILD pkg/goods/internal/bins/BUILD # compilation will fail if no files are present
go test -v ./pkg/... ./cmd/...
go test -tags exclude_graphdriver_btrfs -v ./pkg/... ./cmd/...
$(MAKE) -C operator test

.PHONY: vet
vet: static
go vet ./...
go vet -tags exclude_graphdriver_btrfs ./...

.PHONY: e2e-tests
e2e-tests: embedded-release
Expand All @@ -266,11 +266,11 @@ clean:

.PHONY: lint
lint:
golangci-lint run -c .golangci.yml ./...
golangci-lint run -c .golangci.yml ./... --build-tags exclude_graphdriver_btrfs

.PHONY: lint-and-fix
lint-and-fix:
golangci-lint run --fix -c .golangci.yml ./...
golangci-lint run --fix -c .golangci.yml ./... --build-tags exclude_graphdriver_btrfs

.PHONY: scan
scan:
Expand All @@ -285,7 +285,7 @@ scan:
buildtools:
mkdir -p pkg/goods/bins pkg/goods/internal/bins
touch pkg/goods/bins/BUILD pkg/goods/internal/bins/BUILD # compilation will fail if no files are present
go build -o ./output/bin/buildtools ./cmd/buildtools
go build -tags exclude_graphdriver_btrfs -o ./output/bin/buildtools ./cmd/buildtools

.PHONY: list-distros
list-distros:
Expand Down
16 changes: 0 additions & 16 deletions cmd/embedded-cluster/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,3 @@ func getDataDirFlagWithDefault(runtimeConfig *ecv1beta1.RuntimeConfigSpec) *cli.
},
}
}

func getDataDirFlag(runtimeConfig *ecv1beta1.RuntimeConfigSpec) *cli.StringFlag {
return &cli.StringFlag{
Name: "data-dir",
Usage: "Path to the data directory (default discovered from the cluster)",
Hidden: false,
Action: func(c *cli.Context, s string) error {
if s == "" {
return nil
}
logrus.Debugf("Setting data dir to %s from flag", s)
runtimeConfig.DataDir = s
return nil
},
}
}
6 changes: 6 additions & 0 deletions cmd/embedded-cluster/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/replicatedhq/embedded-cluster/pkg/addons"
"github.com/replicatedhq/embedded-cluster/pkg/airgap"
"github.com/replicatedhq/embedded-cluster/pkg/config"
"github.com/replicatedhq/embedded-cluster/pkg/configutils"
"github.com/replicatedhq/embedded-cluster/pkg/defaults"
"github.com/replicatedhq/embedded-cluster/pkg/goods"
"github.com/replicatedhq/embedded-cluster/pkg/helpers"
Expand Down Expand Up @@ -707,6 +708,11 @@ func installCommand() *cli.Command {
defer tryRemoveTmpDirContents(provider)

var err error
err = configutils.WriteRuntimeConfig(runtimeConfig)
if err != nil {
return fmt.Errorf("unable to write runtime config: %w", err)
}

proxy, err := getProxySpecFromFlags(c)
if err != nil {
return fmt.Errorf("unable to get proxy spec from flags: %w", err)
Expand Down
6 changes: 6 additions & 0 deletions cmd/embedded-cluster/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/replicatedhq/embedded-cluster/pkg/airgap"
"github.com/replicatedhq/embedded-cluster/pkg/config"
"github.com/replicatedhq/embedded-cluster/pkg/configutils"
"github.com/replicatedhq/embedded-cluster/pkg/defaults"
"github.com/replicatedhq/embedded-cluster/pkg/helpers"
"github.com/replicatedhq/embedded-cluster/pkg/highavailability"
Expand Down Expand Up @@ -190,6 +191,11 @@ var joinCommand = &cli.Command{
return fmt.Errorf("unable to get join token: %w", err)
}

err = configutils.WriteRuntimeConfig(jcmd.InstallationSpec.RuntimeConfig)
if err != nil {
return fmt.Errorf("unable to write runtime config: %w", err)
}

provider := defaults.NewProviderFromRuntimeConfig(jcmd.InstallationSpec.RuntimeConfig)
os.Setenv("KUBECONFIG", provider.PathToKubeConfig())
os.Setenv("TMPDIR", provider.EmbeddedClusterTmpSubDir())
Expand Down
1 change: 1 addition & 0 deletions cmd/embedded-cluster/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func main() {
updateCommand(),
restoreCommand(),
adminConsoleCommand(),
supportBundleCommand(),
},
}
if err := app.RunContext(ctx, os.Args); err != nil {
Expand Down
10 changes: 9 additions & 1 deletion cmd/embedded-cluster/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

ecv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1"
"github.com/replicatedhq/embedded-cluster/pkg/configutils"
"github.com/replicatedhq/embedded-cluster/pkg/defaults"
"github.com/replicatedhq/embedded-cluster/pkg/kubeutils"
)
Expand Down Expand Up @@ -36,14 +37,21 @@ func discoverKubeconfigPath(ctx context.Context, runtimeConfig *ecv1beta1.Runtim
}

// discoverBestProvider discovers the provider from the cluster (if it's up) and will fall back to
// the filesystem, or the default.
// the /etc/embedded-cluster/ec.yaml file, the filesystem, or the default.
func discoverBestProvider(ctx context.Context) *defaults.Provider {
// It's possible that the cluster is not up
provider, err := getProviderFromCluster(ctx)
if err == nil {
return provider
}

// There might be a runtime config file
runtimeConfig, err := configutils.ReadRuntimeConfig()
if err == nil {
provider = defaults.NewProviderFromRuntimeConfig(runtimeConfig)
return provider
}

// Otherwise, fall back to the filesystem
provider, err = defaults.NewProviderFromFilesystem()
if err == nil {
Expand Down
15 changes: 5 additions & 10 deletions cmd/embedded-cluster/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"

ecv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1"
"github.com/replicatedhq/embedded-cluster/pkg/defaults"
"github.com/replicatedhq/embedded-cluster/pkg/helpers"
"github.com/replicatedhq/embedded-cluster/pkg/kubeutils"
Expand Down Expand Up @@ -330,8 +329,6 @@ func maybePrintHAWarning(ctx context.Context, provider *defaults.Provider) error
}

func resetCommand() *cli.Command {
runtimeConfig := ecv1beta1.GetDefaultRuntimeConfig()

return &cli.Command{
Name: "reset",
Before: func(c *cli.Context) error {
Expand All @@ -342,7 +339,6 @@ func resetCommand() *cli.Command {
},
Args: false,
Flags: []cli.Flag{
getDataDirFlag(runtimeConfig),
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Expand All @@ -357,12 +353,7 @@ func resetCommand() *cli.Command {
},
Usage: fmt.Sprintf("Remove %s from the current node", binName),
Action: func(c *cli.Context) error {
var provider *defaults.Provider
if c.IsSet("data-dir") {
provider = defaults.NewProviderFromRuntimeConfig(runtimeConfig)
} else {
provider = discoverBestProvider(c.Context)
}
provider := discoverBestProvider(c.Context)
os.Setenv("KUBECONFIG", provider.PathToKubeConfig())
os.Setenv("TMPDIR", provider.EmbeddedClusterTmpSubDir())

Expand Down Expand Up @@ -502,6 +493,10 @@ func resetCommand() *cli.Command {
return fmt.Errorf("failed to remove k0s binary: %w", err)
}

if err := helpers.RemoveAll(defaults.PathToECConfig()); err != nil {
return fmt.Errorf("failed to remove embedded cluster data config: %w", err)
}

if _, err := exec.Command("reboot").Output(); err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/embedded-cluster/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/replicatedhq/embedded-cluster/pkg/addons/seaweedfs"
"github.com/replicatedhq/embedded-cluster/pkg/airgap"
"github.com/replicatedhq/embedded-cluster/pkg/config"
"github.com/replicatedhq/embedded-cluster/pkg/configutils"
"github.com/replicatedhq/embedded-cluster/pkg/constants"
"github.com/replicatedhq/embedded-cluster/pkg/defaults"
"github.com/replicatedhq/embedded-cluster/pkg/helpers"
Expand Down Expand Up @@ -958,6 +959,11 @@ func restoreCommand() *cli.Command {

defer tryRemoveTmpDirContents(provider)

err := configutils.WriteRuntimeConfig(runtimeConfig)
if err != nil {
return fmt.Errorf("unable to write runtime config: %w", err)
}

proxy, err := getProxySpecFromFlags(c)
if err != nil {
return fmt.Errorf("unable to get proxy spec from flags: %w", err)
Expand Down
13 changes: 1 addition & 12 deletions cmd/embedded-cluster/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/urfave/cli/v2"
"golang.org/x/term"

ecv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1"
"github.com/replicatedhq/embedded-cluster/pkg/defaults"
)

Expand All @@ -37,27 +36,17 @@ func handleResize(ch chan os.Signal, tty *os.File) {
}

func shellCommand() *cli.Command {
runtimeConfig := ecv1beta1.GetDefaultRuntimeConfig()

return &cli.Command{
Name: "shell",
Usage: "Start a shell with access to the cluster",
Flags: []cli.Flag{
getDataDirFlag(runtimeConfig),
},
Before: func(c *cli.Context) error {
if os.Getuid() != 0 {
return fmt.Errorf("shell command must be run as root")
}
return nil
},
Action: func(c *cli.Context) error {
var provider *defaults.Provider
if c.IsSet("data-dir") {
provider = defaults.NewProviderFromRuntimeConfig(runtimeConfig)
} else {
provider = discoverBestProvider(c.Context)
}
provider := discoverBestProvider(c.Context)
os.Setenv("TMPDIR", provider.EmbeddedClusterTmpSubDir())

if _, err := os.Stat(provider.PathToKubeConfig()); err != nil {
Expand Down
67 changes: 67 additions & 0 deletions cmd/embedded-cluster/support_bundle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"bytes"
"fmt"
"io"
"os"

"github.com/urfave/cli/v2"

"github.com/replicatedhq/embedded-cluster/pkg/defaults"
"github.com/replicatedhq/embedded-cluster/pkg/helpers"
"github.com/replicatedhq/embedded-cluster/pkg/spinner"
)

func supportBundleCommand() *cli.Command {
return &cli.Command{
Name: "support-bundle",
Usage: fmt.Sprintf("Generate a %s support bundle", defaults.BinaryName()),
Before: func(c *cli.Context) error {
if os.Getuid() != 0 {
return fmt.Errorf("support-bundle command must be run as root")
}
return nil
},
Action: func(c *cli.Context) error {
provider := discoverBestProvider(c.Context)
os.Setenv("TMPDIR", provider.EmbeddedClusterTmpSubDir())

supportBundle := provider.PathToEmbeddedClusterBinary("kubectl-support_bundle")
if _, err := os.Stat(supportBundle); err != nil {
return fmt.Errorf("unable to find support bundle binary")
}

kubeConfig := provider.PathToKubeConfig()
hostSupportBundle := provider.PathToEmbeddedClusterSupportFile("host-support-bundle.yaml")

spin := spinner.Start()
spin.Infof("Collecting support bundle (this may take a while)")

stdout := bytes.NewBuffer(nil)
stderr := bytes.NewBuffer(nil)
if err := helpers.RunCommandWithOptions(
helpers.RunCommandOptions{
Writer: stdout,
ErrWriter: stderr,
LogOnSuccess: true,
},
supportBundle,
"--interactive=false",
fmt.Sprintf("--kubeconfig=%s", kubeConfig),
"--load-cluster-specs",
hostSupportBundle,
); err != nil {
spin.Infof("Failed to collect support bundle")
spin.CloseWithError()
io.Copy(os.Stdout, stdout)
io.Copy(os.Stderr, stderr)
return ErrNothingElseToAdd
}

spin.Infof("Support bundle collected!")
spin.Close()
return nil
},
}
}
37 changes: 37 additions & 0 deletions e2e/host-support-bundle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package e2e

import (
"testing"
"time"

"github.com/replicatedhq/embedded-cluster/e2e/cluster/docker"
)

func TestHostCollectSupportBundleInCluster(t *testing.T) {
t.Parallel()

RequireEnvVars(t, []string{"SHORT_SHA"})

tc := docker.NewCluster(&docker.ClusterInput{
T: t,
Nodes: 1,
Distro: "debian-bookworm",
LicensePath: "license.yaml",
ECBinaryPath: "../output/bin/embedded-cluster",
})
defer tc.Cleanup()

t.Logf("%s: installing embedded-cluster on node 0", time.Now().Format(time.RFC3339))
line := []string{"single-node-install.sh", "cli"}
if stdout, stderr, err := tc.RunCommandOnNode(0, line); err != nil {
t.Fatalf("fail to install embedded-cluster: %v: %s: %s", err, stdout, stderr)
}

line = []string{"collect-support-bundle-host-in-cluster.sh"}
stdout, stderr, err := tc.RunCommandOnNode(0, line)
if err != nil {
t.Fatalf("fail to collect host support bundle: %v: %s: %s", err, stdout, stderr)
}

t.Logf("%s: test complete", time.Now().Format(time.RFC3339))
}
6 changes: 6 additions & 0 deletions e2e/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,12 @@ func TestUpgradeFromReplicatedApp(t *testing.T) {
t.Fatalf("fail to check postupgrade state: %v: %s: %s", err, stdout, stderr)
}

line = []string{"collect-support-bundle-host-in-cluster.sh"}
stdout, stderr, err := tc.RunCommandOnNode(0, line)
if err != nil {
t.Fatalf("fail to collect host support bundle: %v: %s: %s", err, stdout, stderr)
}

t.Logf("%s: test complete", time.Now().Format(time.RFC3339))
}

Expand Down
Loading

0 comments on commit 2eb8fb7

Please sign in to comment.