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

fix kind export logs #2161

Merged
merged 2 commits into from
Mar 26, 2021
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
16 changes: 0 additions & 16 deletions pkg/cluster/internal/providers/common/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"path/filepath"

"sigs.k8s.io/kind/pkg/cluster/nodes"
"sigs.k8s.io/kind/pkg/cmd/kind/version"
"sigs.k8s.io/kind/pkg/errors"
"sigs.k8s.io/kind/pkg/exec"
)
Expand All @@ -23,24 +22,9 @@ func CollectLogs(n nodes.Node, dir string) error {
return cmd.SetStdout(f).SetStderr(f).Run()
}
}
writeToPathFn := func(s string, path string) func() error {
return func() error {
f, err := FileOnHost(path)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(s)
return err
}
}

return errors.AggregateConcurrent([]func() error{
// record info about the node container
writeToPathFn(
version.DisplayVersion(),
filepath.Join(dir, "kind-version.txt"),
),
execToPathFn(
n.Command("cat", "/kind/version"),
"kubernetes-version.txt",
Expand Down
18 changes: 18 additions & 0 deletions pkg/cluster/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ limitations under the License.
package cluster

import (
"io/ioutil"
"os"
"path/filepath"
"sort"

"sigs.k8s.io/kind/pkg/cmd/kind/version"

"sigs.k8s.io/kind/pkg/cluster/constants"
"sigs.k8s.io/kind/pkg/cluster/nodes"
"sigs.k8s.io/kind/pkg/cluster/nodeutils"
Expand Down Expand Up @@ -226,5 +231,18 @@ func (p *Provider) CollectLogs(name, dir string) error {
if err != nil {
return err
}
// ensure directory
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return errors.Wrap(err, "failed to create logs directory")
}
// write kind version
if err := ioutil.WriteFile(
filepath.Join(dir, "kind-version.txt"),
[]byte(version.DisplayVersion()),
0666, // match os.Create
); err != nil {
return errors.Wrap(err, "failed to write kind-version.txt")
}
// collect and write cluster logs
return p.provider.CollectLogs(dir, n)
}
13 changes: 6 additions & 7 deletions pkg/cmd/kind/export/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,11 @@ func runE(logger log.Logger, streams cmd.IOStreams, flags *flagpole, args []stri
dir = args[0]
}

// collect the logs
if err := provider.CollectLogs(flags.Name, dir); err != nil {
return err
}

logger.V(0).Infof("Exported logs for cluster %q to:", flags.Name)
// NOTE: the path is the output of this command to be captured by calling tools
// whereas "Exporting logs..." is info / debug (stderr)
logger.V(0).Infof("Exporting logs for cluster %q to:", flags.Name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to: %s", dir) ??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, we print the path to stdout for capture by the calling program.
we print the message to stderr.
(see the comment above)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g.:

$ kind export logs
Exporting logs for cluster "kind" to: 
/private/var/folders/rt/xdpktcmj0p7fx3kvwmrgdqyh00fsfv/T/609940926
ERROR: [command "docker exec --privileged kind-control-plane sh -c 'tar --hard-dereference -C /var/log/ -chf - . || (r=$?; [ $r -eq 1 ] || exit $r)'" failed with error: exit status 1, [command "docker exec --privileged kind-control-plane cat /kind/version" failed with error: exit status 1, command "docker exec --privileged kind-control-plane journalctl --no-pager -u kubelet.service" failed with error: exit status 1, command "docker exec --privileged kind-control-plane journalctl --no-pager -u containerd.service" failed with error: exit status 1, command "docker exec --privileged kind-control-plane journalctl --no-pager" failed with error: exit status 1]]

the stdout part is /private/var/folders/rt/xdpktcmj0p7fx3kvwmrgdqyh00fsfv/T/609940926

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: in this commit I basically just moved this up before we actually export (so if we hit an error you can still see where the logs we did collect are) and s/Exported/Exporting/

fmt.Fprintln(streams.Out, dir)
return nil

// collect the logs
return provider.CollectLogs(flags.Name, dir)
}