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

[WIP] Replace log with logrus #112

Merged
merged 4 commits into from
Oct 1, 2019
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
14 changes: 7 additions & 7 deletions cli/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strconv"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/docker/docker/client"
homedir "github.com/mitchellh/go-homedir"
"github.com/olekukonko/tablewriter"
log "github.com/sirupsen/logrus"
)

const (
Expand Down Expand Up @@ -65,27 +65,27 @@ func createDirIfNotExists(path string) error {
func createClusterDir(name string) {
clusterPath, _ := getClusterDir(name)
if err := createDirIfNotExists(clusterPath); err != nil {
log.Fatalf("ERROR: couldn't create cluster directory [%s] -> %+v", clusterPath, err)
log.Fatalf("Couldn't create cluster directory [%s] -> %+v", clusterPath, err)
}
// create subdir for sharing container images
if err := createDirIfNotExists(clusterPath + "/images"); err != nil {
log.Fatalf("ERROR: couldn't create cluster sub-directory [%s] -> %+v", clusterPath+"/images", err)
log.Fatalf("Couldn't create cluster sub-directory [%s] -> %+v", clusterPath+"/images", err)
}
}

// deleteClusterDir contrary to createClusterDir, this deletes the cluster directory under $HOME/.config/k3d/<cluster_name>
func deleteClusterDir(name string) {
clusterPath, _ := getClusterDir(name)
if err := os.RemoveAll(clusterPath); err != nil {
log.Printf("WARNING: couldn't delete cluster directory [%s]. You might want to delete it manually.", clusterPath)
log.Warningf("Couldn't delete cluster directory [%s]. You might want to delete it manually.", clusterPath)
}
}

// getClusterDir returns the path to the cluster directory which is $HOME/.config/k3d/<cluster_name>
func getClusterDir(name string) (string, error) {
homeDir, err := homedir.Dir()
if err != nil {
log.Printf("ERROR: Couldn't get user's home directory")
log.Error("Couldn't get user's home directory")
return "", err
}
return path.Join(homeDir, ".config", "k3d", name), nil
Expand Down Expand Up @@ -202,7 +202,7 @@ func getKubeConfig(cluster string) (string, error) {
func printClusters() {
clusters, err := getClusters(true, "")
if err != nil {
log.Fatalf("ERROR: Couldn't list clusters\n%+v", err)
log.Fatalf("Couldn't list clusters\n%+v", err)
}
if len(clusters) == 0 {
log.Printf("No clusters found!")
Expand Down Expand Up @@ -295,7 +295,7 @@ func getClusters(all bool, name string) (map[string]cluster, error) {
Filters: filters,
})
if err != nil {
log.Printf("WARNING: couldn't get worker containers for cluster %s\n%+v", clusterName, err)
log.Warningf("Couldn't get worker containers for cluster %s\n%+v", clusterName, err)
}

// save cluster information
Expand Down
20 changes: 10 additions & 10 deletions cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"context"
"errors"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -69,10 +69,10 @@ func CreateCluster(c *cli.Context) error {
image := c.String("image")
if c.IsSet("version") {
// TODO: --version to be deprecated
log.Println("[WARNING] The `--version` flag will be deprecated soon, please use `--image rancher/k3s:<version>` instead")
log.Warning("The `--version` flag will be deprecated soon, please use `--image rancher/k3s:<version>` instead")
if c.IsSet("image") {
// version specified, custom image = error (to push deprecation of version flag)
log.Fatalln("[ERROR] Please use `--image <image>:<version>` instead of --image and --version")
log.Fatalln("Please use `--image <image>:<version>` instead of --image and --version")
} else {
// version specified, default image = ok (until deprecation of version flag)
image = fmt.Sprintf("%s:%s", strings.Split(image, ":")[0], c.String("version"))
Expand All @@ -98,7 +98,7 @@ func CreateCluster(c *cli.Context) error {
// k3s server arguments
// TODO: --port will soon be --api-port since we want to re-use --port for arbitrary port mappings
if c.IsSet("port") {
log.Println("INFO: As of v2.0.0 --port will be used for arbitrary port mapping. Please use --api-port/-a instead for configuring the Api Port")
log.Info("As of v2.0.0 --port will be used for arbitrary port mapping. Please use --api-port/-a instead for configuring the Api Port")
}
apiPort, err := parseAPIPort(c.String("api-port"))
if err != nil {
Expand All @@ -116,7 +116,7 @@ func CreateCluster(c *cli.Context) error {
// In case of error, Log a warning message, and continue on. Since it more likely caused by a miss configured
// DOCKER_MACHINE_NAME environment variable.
if err != nil {
log.Printf("WARNING: Failed to get docker machine IP address, ignoring the DOCKER_MACHINE_NAME environment variable setting.\n")
log.Warning("Failed to get docker machine IP address, ignoring the DOCKER_MACHINE_NAME environment variable setting.")
}
}

Expand Down Expand Up @@ -262,15 +262,15 @@ func DeleteCluster(c *cli.Context) error {
}

if err := deleteClusterNetwork(cluster.name); err != nil {
log.Printf("WARNING: couldn't delete cluster network for cluster %s\n%+v", cluster.name, err)
log.Warningf("Couldn't delete cluster network for cluster %s\n%+v", cluster.name, err)
}

log.Println("...Removing docker image volume")
if err := deleteImageVolume(cluster.name); err != nil {
log.Printf("WARNING: couldn't delete image docker volume for cluster %s\n%+v", cluster.name, err)
log.Warningf("Couldn't delete image docker volume for cluster %s\n%+v", cluster.name, err)
}

log.Printf("SUCCESS: removed cluster [%s]", cluster.name)
log.Infof("Removed cluster [%s]", cluster.name)
}

return nil
Expand Down Expand Up @@ -308,7 +308,7 @@ func StopCluster(c *cli.Context) error {
return fmt.Errorf("ERROR: Couldn't stop server for cluster %s\n%+v", cluster.name, err)
}

log.Printf("SUCCESS: Stopped cluster [%s]", cluster.name)
log.Infof("Stopped cluster [%s]", cluster.name)
}

return nil
Expand Down Expand Up @@ -357,7 +357,7 @@ func StartCluster(c *cli.Context) error {
// ListClusters prints a list of created clusters
func ListClusters(c *cli.Context) error {
if c.IsSet("all") {
log.Println("INFO: --all is on by default, thus no longer required. This option will be removed in v2.0.0")
log.Info("--all is on by default, thus no longer required. This option will be removed in v2.0.0")

}
printClusters()
Expand Down
6 changes: 3 additions & 3 deletions cli/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
log "github.com/sirupsen/logrus"
)

type ClusterSpec struct {
Expand Down Expand Up @@ -53,12 +53,12 @@ func startContainer(verbose bool, config *container.Config, hostConfig *containe
if verbose {
_, err := io.Copy(os.Stdout, reader)
if err != nil {
log.Printf("WARNING: couldn't get docker output\n%+v", err)
log.Warningf("Couldn't get docker output\n%+v", err)
}
} else {
_, err := io.Copy(ioutil.Discard, reader)
if err != nil {
log.Printf("WARNING: couldn't get docker output\n%+v", err)
log.Warningf("Couldn't get docker output\n%+v", err)
}
}
resp, err = docker.ContainerCreate(ctx, config, hostConfig, networkingConfig, containerName)
Expand Down
3 changes: 2 additions & 1 deletion cli/docker-machine.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package run

import (
"log"
"os"
"os/exec"
"strings"

log "github.com/sirupsen/logrus"
)

func getDockerMachineIp() (string, error) {
Expand Down
26 changes: 13 additions & 13 deletions cli/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"
"fmt"
"io/ioutil"
"log"
"strings"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
log "github.com/sirupsen/logrus"
)

const (
Expand All @@ -34,7 +34,7 @@ func importImage(clusterName string, images []string, noRemove bool) error {
}

//*** first, save the images using the local docker daemon
log.Printf("INFO: Saving images %s from local docker daemon...", images)
log.Infof("Saving images %s from local docker daemon...", images)
toolsContainerName := fmt.Sprintf("k3d-%s-tools", clusterName)
tarFileName := fmt.Sprintf("%s/k3d-%s-images-%s.tar", imageBasePathRemote, clusterName, time.Now().Format("20060102150405"))

Expand Down Expand Up @@ -67,7 +67,7 @@ func importImage(clusterName string, images []string, noRemove bool) error {
if err = docker.ContainerRemove(ctx, toolsContainerID, types.ContainerRemoveOptions{
Force: true,
}); err != nil {
log.Println(fmt.Errorf("WARN: couldn't remove tools container\n%+v", err))
log.Warningf("Couldn't remove tools container\n%+v", err)
}
}()

Expand All @@ -79,7 +79,7 @@ func importImage(clusterName string, images []string, noRemove bool) error {
}
if !cont.State.Running { // container finished...
if cont.State.ExitCode == 0 { // ...successfully
log.Println("INFO: saved images to shared docker volume")
log.Info("Saved images to shared docker volume")
break
} else if cont.State.ExitCode != 0 { // ...failed
errTxt := "ERROR: helper container failed to save images"
Expand Down Expand Up @@ -133,7 +133,7 @@ func importImage(clusterName string, images []string, noRemove bool) error {
for _, container := range containerList {

containerName := container.Names[0][1:] // trimming the leading "/" from name
log.Printf("INFO: Importing images %s in container [%s]", images, containerName)
log.Infof("Importing images %s in container [%s]", images, containerName)

// create exec configuration
execResponse, err := docker.ContainerExecCreate(ctx, container.ID, execConfig)
Expand Down Expand Up @@ -169,44 +169,44 @@ func importImage(clusterName string, images []string, noRemove bool) error {
}
}

log.Printf("INFO: Successfully imported images %s in all nodes of cluster [%s]", images, clusterName)
log.Infof("Successfully imported images %s in all nodes of cluster [%s]", images, clusterName)

// remove tarball from inside the server container
if !noRemove {
log.Println("INFO: Cleaning up tarball")
log.Info("Cleaning up tarball")

execID, err := docker.ContainerExecCreate(ctx, clusters[clusterName].server.ID, types.ExecConfig{
Cmd: []string{"rm", "-f", tarFileName},
})
if err != nil {
log.Printf("WARN: failed to delete tarball: couldn't create remove in container [%s]\n%+v", clusters[clusterName].server.ID, err)
log.Warningf("Failed to delete tarball: couldn't create remove in container [%s]\n%+v", clusters[clusterName].server.ID, err)
}
err = docker.ContainerExecStart(ctx, execID.ID, types.ExecStartCheck{
Detach: true,
})
if err != nil {
log.Printf("WARN: couldn't start tarball deletion action\n%+v", err)
log.Warningf("Couldn't start tarball deletion action\n%+v", err)
}

for {
execInspect, err := docker.ContainerExecInspect(ctx, execID.ID)
if err != nil {
log.Printf("WARN: couldn't verify deletion of tarball\n%+v", err)
log.Warningf("Couldn't verify deletion of tarball\n%+v", err)
}

if !execInspect.Running {
if execInspect.ExitCode == 0 {
log.Println("INFO: deleted tarball")
log.Info("Deleted tarball")
break
} else {
log.Println("WARN: failed to delete tarball")
log.Warning("Failed to delete tarball")
break
}
}
}
}

log.Println("INFO: ...Done")
log.Info("...Done")

return nil
}
6 changes: 3 additions & 3 deletions cli/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package run
import (
"context"
"fmt"
"log"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
log "github.com/sirupsen/logrus"
)

func k3dNetworkName(clusterName string) string {
Expand All @@ -32,7 +32,7 @@ func createClusterNetwork(clusterName string) (string, error) {
}

if len(nl) > 1 {
log.Printf("WARNING: Found %d networks for %s when we only expect 1\n", len(nl), clusterName)
log.Warningf("Found %d networks for %s when we only expect 1\n", len(nl), clusterName)
}

if len(nl) > 0 {
Expand Down Expand Up @@ -75,7 +75,7 @@ func deleteClusterNetwork(clusterName string) error {
// there should be only one network that matches the name... but who knows?
for _, network := range networks {
if err := docker.NetworkRemove(ctx, network.ID); err != nil {
log.Printf("WARNING: couldn't remove network for cluster %s\n%+v", clusterName, err)
log.Warningf("couldn't remove network for cluster %s\n%+v", clusterName, err)
continue
}
}
Expand Down
4 changes: 2 additions & 2 deletions cli/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package run

import (
"fmt"
"log"
"strings"

"github.com/docker/go-connections/nat"
log "github.com/sirupsen/logrus"
)

// PublishedPorts is a struct used for exposing container ports on the host system
Expand Down Expand Up @@ -54,7 +54,7 @@ func mapNodesToPortSpecs(specs []string, createdNodes []string) (map[string][]st
}
}
if !nodeFound {
log.Printf("WARNING: Unknown node-specifier [%s] in port mapping entry [%s]", node, spec)
log.Warningf("Unknown node-specifier [%s] in port mapping entry [%s]", node, spec)
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package main

import (
"fmt"
"log"
"os"

log "github.com/sirupsen/logrus"
"github.com/urfave/cli"

run "github.com/rancher/k3d/cli"
"github.com/rancher/k3d/version"
"github.com/urfave/cli"
)

// defaultK3sImage specifies the default image being used for server and workers
Expand Down