Skip to content

Commit

Permalink
Cleanup and trimming container list
Browse files Browse the repository at this point in the history
- Trimmed down the list of containers omitting `k8s-POD` containers
- Found that policy controller is logging to stderr only, fixed up
  gathering of logs to ensure we still get all of it
- Code cleanup
  • Loading branch information
heschlie committed Sep 14, 2017
1 parent 0104c44 commit 7bdadf9
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions calicoctl/commands/node/diags.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import (
"strings"
"time"
"bufio"
"bytes"

"github.com/docopt/docopt-go"
log "github.com/sirupsen/logrus"
shutil "github.com/termie/go-shutil"
"regexp"
)

// diagCmd is a struct to hold a command, cmd info and filename to run diagnostic on
Expand Down Expand Up @@ -165,32 +165,34 @@ func getNodeContainerLogs(logDir string) {
os.Mkdir(logDir, os.ModeDir)

// Get a list of Calico containers running on this Node.
var stderr bytes.Buffer
cmd := exec.Command("docker", "ps", "-a", "--filter", "\"name=calico\"", "--format", "{{.Names}}: {{.CreatedAt}}")
cmd.Stderr = &stderr
containers, err := cmd.Output()
result, err := exec.Command("docker", "ps", "-a", "--filter", "\"name=calico\"", "--format", "{{.Names}}: {{.CreatedAt}}").CombinedOutput()
if err != nil {
fmt.Printf("Could not run docker command: %s\n", stderr.String())
fmt.Printf("Could not run docker command: %s\n", string(result))
return
}

// No Calico containers found.
if string(containers) == "" {
if string(result) == "" {
log.Debug("Did not find any Calico containers")
return
}

// Remove any containers that have "k8s_POD" in them.
re := regexp.MustCompile("(?m)[\r\n]+^.*k8s_POD.*$")
containers := re.ReplaceAllString(string(result), "")

fmt.Println("Copying logs from Calico containers")
err = ioutil.WriteFile(logDir+"/"+"container_creation_time", containers,0666)
err = ioutil.WriteFile(logDir+"/"+"container_creation_time", []byte(containers),0666)
if err != nil {
fmt.Printf("Could not save output of `docker ps` command to container_creation_time: %s\n", err)
}

// Grab the log for each container and write it as <containerName>.log.
scanner := bufio.NewScanner(strings.NewReader(string(containers)))
scanner := bufio.NewScanner(strings.NewReader(containers))
for scanner.Scan() {
name := strings.Split(scanner.Text(), ":")[0]
cLog, err := exec.Command("docker", "logs", name).Output()
log.Debugf("Getting logs for container %s", name)
cLog, err := exec.Command("docker", "logs", name).CombinedOutput()
if err != nil {
fmt.Printf("Could not pull log for container %s: %s\n", name, err)
continue
Expand All @@ -212,13 +214,9 @@ func writeDiags(cmds diagCmd, dir string) {

parts := strings.Fields(cmds.cmd)

var stderr bytes.Buffer
cmd := exec.Command(parts[0], parts[1:]...)
cmd.Stderr = &stderr

content, err := cmd.Output()
content, err := exec.Command(parts[0], parts[1:]...).CombinedOutput()
if err != nil {
fmt.Printf("Failed to run command: %s\nError: %s\n", cmds.cmd, stderr.String())
fmt.Printf("Failed to run command: %s\nError: %s\n", cmds.cmd, string(content))
}

// This is for the commands we want to run but don't want to save the output
Expand Down

0 comments on commit 7bdadf9

Please sign in to comment.