Skip to content

Commit

Permalink
Add envfunc to export kind cluster logs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrevilain committed Mar 16, 2023
1 parent ed091d6 commit f450f87
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/kind/kind_with_config/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestMain(m *testing.M) {

testenv.Finish(
envfuncs.DeleteNamespace(namespace),
envfuncs.ExportKindClusterLogs(kindClusterName, "./logs"),
envfuncs.DestroyKindCluster(kindClusterName),
)
os.Exit(testenv.Run(m))
Expand Down
23 changes: 23 additions & 0 deletions pkg/envfuncs/kind_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,26 @@ func LoadImageArchiveToCluster(name, imageArchive string) env.Func {
return ctx, nil
}
}

// ExportKindClusterLogs returns an EnvFunc that
// retrieves a previously saved kind Cluster in the context (using the name), and then export cluster logs
// in the provided destination.
func ExportKindClusterLogs(name, dest string) env.Func {
return func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
clusterVal := ctx.Value(kindContextKey(name))
if clusterVal == nil {
return ctx, fmt.Errorf("export kind cluster logs: context cluster is nil")
}

cluster, ok := clusterVal.(*kind.Cluster)
if !ok {
return ctx, fmt.Errorf("export kind cluster logs: unexpected type for cluster value")
}

if err := cluster.ExportLogs(dest); err != nil {
return ctx, fmt.Errorf("load image archive: %w", err)
}

return ctx, nil
}
}
15 changes: 15 additions & 0 deletions support/kind/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ func (k *Cluster) GetKubeCtlContext() string {
return fmt.Sprintf("kind-%s", k.name)
}

// ExportLogs export all cluster logs to the provided path.
func (k *Cluster) ExportLogs(dest string) error {
log.V(4).Info("Exporting kind cluster logs to ", dest)
if err := k.findOrInstallKind(k.e); err != nil {
return err
}

p := k.e.RunProc(fmt.Sprintf(`kind export logs %s --name %s`, dest, k.name))
if p.Err() != nil {
return fmt.Errorf("kind: export cluster logs failed: %s: %s", p.Err(), p.Result())
}

return nil
}

func (k *Cluster) Destroy() error {
log.V(4).Info("Destroying kind cluster ", k.name)
if err := k.findOrInstallKind(k.e); err != nil {
Expand Down

0 comments on commit f450f87

Please sign in to comment.