Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add envfunc to export kind cluster logs #220

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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