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 b7182a4
Showing 1 changed file with 23 additions and 4 deletions.
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 b7182a4

Please # to comment.