Skip to content

Commit

Permalink
provider: enable k3d cluster log export helper
Browse files Browse the repository at this point in the history
  • Loading branch information
harshanarayana committed Jan 17, 2025
1 parent 6e42505 commit edabcee
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23.0
toolchain go1.23.4

require (
github.com/blang/semver/v4 v4.0.0
github.com/stretchr/testify v1.10.0
github.com/vladimirvivien/gexe v0.4.1
k8s.io/api v0.32.1
Expand All @@ -18,7 +19,6 @@ require (

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
Expand Down
27 changes: 23 additions & 4 deletions third_party/k3d/k3d.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"strings"

"github.com/blang/semver/v4"
"k8s.io/apimachinery/pkg/util/json"

"k8s.io/client-go/rest"
Expand All @@ -37,7 +38,8 @@ import (
log "k8s.io/klog/v2"
)

var k3dVersion = "v5.7.2"
var k3dVersion = "v5.8.0"
var k3dWithExportLogSupport = "v5.8.0"

type Cluster struct {
path string
Expand Down Expand Up @@ -88,7 +90,7 @@ func WithImage(image string) support.ClusterOpts {
}

func NewCluster(name string) *Cluster {
return &Cluster{name: name}
return &Cluster{name: name, version: k3dVersion}
}

func NewProvider() support.E2EClusterProvider {
Expand Down Expand Up @@ -251,8 +253,25 @@ func (c *Cluster) GetKubectlContext() string {
}

func (c *Cluster) ExportLogs(ctx context.Context, dest string) error {
log.Warning("ExportLogs not implemented for k3d. Please use regular kubectl like commands to extract the logs from the cluster")
return nil
reqVersion, err := semver.Parse(c.version)
supVersion, _ := semver.Parse(k3dWithExportLogSupport)
if err != nil {
log.ErrorS(err, "failed to determine the k3d version to decide if the current version supporst the log export helpers. Please use regular kubectl like commands to extract the logs from the cluster")
return nil
}
var stdout, stderr bytes.Buffer
if reqVersion.GE(supVersion) {
p := utils.RunCommandWithCustomWriter(fmt.Sprintf("%s debug export-logs %s --path %s", c.path, c.name, dest), &stdout, &stderr)
err = p.Err()
if err != nil {
log.ErrorS(err, "failed to export cluster logs due to an error", "stdout", stdout.String(), "stderr", stderr.String(), "result", p.Result())
return err
}
return nil
} else {
log.Warning("ExportLogs not implemented for k3d. Please use regular kubectl like commands to extract the logs from the cluster")
return nil
}
}

func (c *Cluster) Destroy(ctx context.Context) error {
Expand Down

0 comments on commit edabcee

Please sign in to comment.