Skip to content

Commit

Permalink
Merge pull request #3 from SoumaSakaguchi/feature/calicoctl
Browse files Browse the repository at this point in the history
Feature/calicoctl
  • Loading branch information
SoumaSakaguchi authored Jan 3, 2025
2 parents 9d6a99e + 7993702 commit 6646286
Show file tree
Hide file tree
Showing 4 changed files with 640 additions and 18 deletions.
10 changes: 5 additions & 5 deletions calicoctl/calicoctl/commands/node/diags.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func runDiags(logDir string) error {
{"Dumping iptables (IPv4)", "iptables-save -c", "ipv4_tables"},
{"Dumping iptables (IPv6)", "ip6tables-save -c", "ipv6_tables"},
{"Dumping ipsets", "ipset list", "ipsets"},
{"Dumping ipsets (container)", "docker run --rm --privileged --net=host calico/node ipset list", "ipset_container"},
{"Dumping ipsets (container)", "nerdctl run --rm --privileged --net=host calico/node ipset list", "ipset_container"},
{"Copying journal for calico-node.service", "journalctl -u calico-node.service --no-pager", "journalctl_calico_node"},
{"Dumping felix stats", "pkill -SIGUSR1 felix", ""},
}
Expand Down Expand Up @@ -185,9 +185,9 @@ func getNodeContainerLogs(logDir string) {
}

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

Expand All @@ -204,15 +204,15 @@ func getNodeContainerLogs(logDir string) {
fmt.Println("Copying logs from Calico containers")
err = os.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)
fmt.Printf("Could not save output of `nerdctl 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(containers))
for scanner.Scan() {
name := strings.Split(scanner.Text(), ":")[0]
log.Debugf("Getting logs for container %s", name)
cLog, err := exec.Command("docker", "logs", name).CombinedOutput()
cLog, err := exec.Command("nerdctl", "logs", name).CombinedOutput()
if err != nil {
fmt.Printf("Could not pull log for container %s: %s\n", name, err)
continue
Expand Down
28 changes: 15 additions & 13 deletions calicoctl/calicoctl/commands/node/run.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !freebsd

// Copyright (c) 2016-2019 Tigera, Inc. All rights reserved.

// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -125,7 +127,7 @@ Options:
--log-dir=<LOG_DIR> The directory containing Calico logs.
[default: /var/log/calico]
--node-image=<DOCKER_IMAGE_NAME>
Docker image to use for Calico's per-node container.
DOCKER image to use for Calico's per-node container.
[default: quay.io/calico/node:latest]
--backend=(bird|none)
Specify which networking backend to use. When set
Expand Down Expand Up @@ -295,13 +297,13 @@ Description:
vols = append(vols, vol{hostPath: etcdcfg.EtcdCertFile, containerPath: ETCD_CERT_NODE_FILE})
}

// Create the Docker command to execute (or display). Start with the
// Create the nerdctl command to execute (or display). Start with the
// fixed parts. If this is not for an init system, we'll include the
// detach flag (to prevent the command blocking), and use Dockers built
// detach flag (to prevent the command blocking), and use nerdctls built
// in restart mechanism. If this is for an init-system we want the
// command to remain attached and for Docker to remove the dead
// command to remain attached and for nerdctl to remove the dead
// container so that it can be restarted by the init system.
cmd := []string{"docker", "run", "--net=host", "--privileged",
cmd := []string{"nerdctl", "run", "--net=host", "--privileged",
"--name=calico-node"}
if initSystem {
cmd = append(cmd, "--rm")
Expand Down Expand Up @@ -331,7 +333,7 @@ Description:
fmt.Println("to display the appropriate start and stop commands.")
} else {
fmt.Println("Use the following command to stop the calico/node container:")
fmt.Printf("\ndocker stop calico-node\n\n")
fmt.Printf("\nnerdctl stop calico-node\n\n")
}
return nil
}
Expand All @@ -356,17 +358,17 @@ Description:
// Make sure the calico-node is not already running before we attempt
// to start the node.
fmt.Println("Removing old calico-node container (if running).")
err = exec.Command("docker", "rm", "-f", "calico-node").Run()
err = exec.Command("nerdctl", "rm", "-f", "calico-node").Run()
if err != nil {
log.WithError(err).Debug("Unable to remove calico-node container (ok if container was not running)")
}

// Run the docker command.
// Run the nerdctl command.
fmt.Println("Running the following command to start calico-node:")
fmt.Printf("\n%s\n\n", strings.Join(cmd, " "))
fmt.Println("Image may take a short time to download if it is not available locally.")

// Now execute the actual Docker run command and check for the
// Now execute the actual nerdctl run command and check for the
// unable to find image message.
runCmd := exec.Command(cmd[0], cmd[1:]...)
if output, err := runCmd.CombinedOutput(); err != nil {
Expand All @@ -377,9 +379,9 @@ Description:
return fmt.Errorf(errStr)
}

// Create the command to follow the docker logs for the calico/node
// Create the command to follow the nerdctl logs for the calico/node
fmt.Print("Container started, checking progress logs.\n\n")
logCmd := exec.Command("docker", "logs", "--follow", "calico-node")
logCmd := exec.Command("nerdctl", "logs", "--follow", "calico-node")

// Get the stdout pipe
outPipe, err := logCmd.StdoutPipe()
Expand All @@ -394,7 +396,7 @@ Description:
return fmt.Errorf("Error executing command: unable to check calico/node logs: %v", err)
}

// Protect against calico processes taking too long to start, or docker
// Protect against calico processes taking too long to start, or nerdctl
// logs hanging without output.
time.AfterFunc(checkLogTimeout, func() {
err = logCmd.Process.Kill()
Expand Down Expand Up @@ -423,7 +425,7 @@ Description:
// Wait for the logging process to terminate. We expect an error here, because we
// just killed it.
err = logCmd.Wait()
log.WithError(err).Info("Expected error after killing docker logs command")
log.WithError(err).Info("Expected error after killing nerdctl logs command")

// If we didn't successfully start then notify the user.
if outScanner.Err() != nil {
Expand Down
Loading

0 comments on commit 6646286

Please sign in to comment.