Skip to content

Commit

Permalink
improve log level usage
Browse files Browse the repository at this point in the history
  • Loading branch information
iwilltry42 committed Oct 2, 2019
1 parent 802038a commit 9aa5af8
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 59 deletions.
10 changes: 5 additions & 5 deletions cli/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ func createKubeConfigFile(cluster string) error {
// get kubeconfig file from container and read contents
reader, _, err := docker.CopyFromContainer(ctx, server[0].ID, "/output/kubeconfig.yaml")
if err != nil {
return fmt.Errorf("ERROR: couldn't copy kubeconfig.yaml from server container %s\n%+v", server[0].ID, err)
return fmt.Errorf(" Couldn't copy kubeconfig.yaml from server container %s\n%+v", server[0].ID, err)
}
defer reader.Close()

readBytes, err := ioutil.ReadAll(reader)
if err != nil {
return fmt.Errorf("ERROR: couldn't read kubeconfig from container\n%+v", err)
return fmt.Errorf(" Couldn't read kubeconfig from container\n%+v", err)
}

// create destination kubeconfig file
Expand All @@ -139,7 +139,7 @@ func createKubeConfigFile(cluster string) error {

kubeconfigfile, err := os.Create(destPath)
if err != nil {
return fmt.Errorf("ERROR: couldn't create kubeconfig file %s\n%+v", destPath, err)
return fmt.Errorf(" Couldn't create kubeconfig file %s\n%+v", destPath, err)
}
defer kubeconfigfile.Close()

Expand All @@ -165,7 +165,7 @@ func createKubeConfigFile(cluster string) error {
}
_, err = kubeconfigfile.Write(trimBytes)
if err != nil {
return fmt.Errorf("ERROR: couldn't write to kubeconfig.yaml\n%+v", err)
return fmt.Errorf(" Couldn't write to kubeconfig.yaml\n%+v", err)
}

return nil
Expand Down Expand Up @@ -255,7 +255,7 @@ func getClusters(all bool, name string) (map[string]cluster, error) {
ctx := context.Background()
docker, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return nil, fmt.Errorf("ERROR: couldn't create docker client\n%+v", err)
return nil, fmt.Errorf(" Couldn't create docker client\n%+v", err)
}

// Prepare docker label filters
Expand Down
17 changes: 8 additions & 9 deletions cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func CheckTools(c *cli.Context) error {
ping, err := docker.Ping(ctx)

if err != nil {
return fmt.Errorf("ERROR: checking docker failed\n%+v", err)
return fmt.Errorf(" Checking docker failed\n%+v", err)
}
log.Printf("SUCCESS: Checking docker succeeded (API: v%s)\n", ping.APIVersion)
return nil
Expand All @@ -53,7 +53,7 @@ func CreateCluster(c *cli.Context) error {
return err
} else if len(cluster) != 0 {
// A cluster exists with the same name. Return with an error.
return fmt.Errorf("ERROR: Cluster %s already exists", c.String("name"))
return fmt.Errorf(" Cluster %s already exists", c.String("name"))
}

// On Error delete the cluster. If there createCluster() encounter any error,
Expand Down Expand Up @@ -159,7 +159,6 @@ func CreateCluster(c *cli.Context) error {
NodeToPortSpecMap: portmap,
PortAutoOffset: c.Int("port-auto-offset"),
ServerArgs: k3sServerArgs,
Verbose: c.GlobalBool("verbose"),
Volumes: volumes,
}

Expand Down Expand Up @@ -197,7 +196,7 @@ func CreateCluster(c *cli.Context) error {
out, err := docker.ContainerLogs(ctx, dockerID, types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true})
if err != nil {
out.Close()
return fmt.Errorf("ERROR: couldn't get docker logs for %s\n%+v", c.String("name"), err)
return fmt.Errorf(" Couldn't get docker logs for %s\n%+v", c.String("name"), err)
}
buf := new(bytes.Buffer)
nRead, _ := buf.ReadFrom(out)
Expand Down Expand Up @@ -258,7 +257,7 @@ func DeleteCluster(c *cli.Context) error {
deleteClusterDir(cluster.name)
log.Println("...Removing server")
if err := removeContainer(cluster.server.ID); err != nil {
return fmt.Errorf("ERROR: Couldn't remove server for cluster %s\n%+v", cluster.name, err)
return fmt.Errorf(" Couldn't remove server for cluster %s\n%+v", cluster.name, err)
}

if err := deleteClusterNetwork(cluster.name); err != nil {
Expand Down Expand Up @@ -287,7 +286,7 @@ func StopCluster(c *cli.Context) error {
ctx := context.Background()
docker, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return fmt.Errorf("ERROR: couldn't create docker client\n%+v", err)
return fmt.Errorf(" Couldn't create docker client\n%+v", err)
}

// remove clusters one by one instead of appending all names to the docker command
Expand All @@ -305,7 +304,7 @@ func StopCluster(c *cli.Context) error {
}
log.Println("...Stopping server")
if err := docker.ContainerStop(ctx, cluster.server.ID, nil); err != nil {
return fmt.Errorf("ERROR: Couldn't stop server for cluster %s\n%+v", cluster.name, err)
return fmt.Errorf(" Couldn't stop server for cluster %s\n%+v", cluster.name, err)
}

log.Infof("Stopped cluster [%s]", cluster.name)
Expand All @@ -325,7 +324,7 @@ func StartCluster(c *cli.Context) error {
ctx := context.Background()
docker, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return fmt.Errorf("ERROR: couldn't create docker client\n%+v", err)
return fmt.Errorf(" Couldn't create docker client\n%+v", err)
}

// remove clusters one by one instead of appending all names to the docker command
Expand All @@ -335,7 +334,7 @@ func StartCluster(c *cli.Context) error {

log.Println("...Starting server")
if err := docker.ContainerStart(ctx, cluster.server.ID, types.ContainerStartOptions{}); err != nil {
return fmt.Errorf("ERROR: Couldn't start server for cluster %s\n%+v", cluster.name, err)
return fmt.Errorf(" Couldn't start server for cluster %s\n%+v", cluster.name, err)
}

if len(cluster.workers) > 0 {
Expand Down
25 changes: 12 additions & 13 deletions cli/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,26 @@ type ClusterSpec struct {
NodeToPortSpecMap map[string][]string
PortAutoOffset int
ServerArgs []string
Verbose bool
Volumes []string
}

func startContainer(verbose bool, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (string, error) {
func startContainer(config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (string, error) {
ctx := context.Background()

docker, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return "", fmt.Errorf("ERROR: couldn't create docker client\n%+v", err)
return "", fmt.Errorf("Couldn't create docker client\n%+v", err)
}

resp, err := docker.ContainerCreate(ctx, config, hostConfig, networkingConfig, containerName)
if client.IsErrNotFound(err) {
log.Printf("Pulling image %s...\n", config.Image)
reader, err := docker.ImagePull(ctx, config.Image, types.ImagePullOptions{})
if err != nil {
return "", fmt.Errorf("ERROR: couldn't pull image %s\n%+v", config.Image, err)
return "", fmt.Errorf("Couldn't pull image %s\n%+v", config.Image, err)
}
defer reader.Close()
if verbose {
if ll := log.GetLevel(); ll == log.DebugLevel {
_, err := io.Copy(os.Stdout, reader)
if err != nil {
log.Warningf("Couldn't get docker output\n%+v", err)
Expand All @@ -63,10 +62,10 @@ func startContainer(verbose bool, config *container.Config, hostConfig *containe
}
resp, err = docker.ContainerCreate(ctx, config, hostConfig, networkingConfig, containerName)
if err != nil {
return "", fmt.Errorf("ERROR: couldn't create container after pull %s\n%+v", containerName, err)
return "", fmt.Errorf(" Couldn't create container after pull %s\n%+v", containerName, err)
}
} else if err != nil {
return "", fmt.Errorf("ERROR: couldn't create container %s\n%+v", containerName, err)
return "", fmt.Errorf(" Couldn't create container %s\n%+v", containerName, err)
}

if err := docker.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
Expand Down Expand Up @@ -139,9 +138,9 @@ func createServer(spec *ClusterSpec) (string, error) {
Env: spec.Env,
Labels: containerLabels,
}
id, err := startContainer(spec.Verbose, config, hostConfig, networkingConfig, containerName)
id, err := startContainer(config, hostConfig, networkingConfig, containerName)
if err != nil {
return "", fmt.Errorf("ERROR: couldn't create container %s\n%+v", containerName, err)
return "", fmt.Errorf(" Couldn't create container %s\n%+v", containerName, err)
}

return id, nil
Expand Down Expand Up @@ -209,9 +208,9 @@ func createWorker(spec *ClusterSpec, postfix int) (string, error) {
ExposedPorts: workerPublishedPorts.ExposedPorts,
}

id, err := startContainer(spec.Verbose, config, hostConfig, networkingConfig, containerName)
id, err := startContainer(config, hostConfig, networkingConfig, containerName)
if err != nil {
return "", fmt.Errorf("ERROR: couldn't start container %s\n%+v", containerName, err)
return "", fmt.Errorf(" Couldn't start container %s\n%+v", containerName, err)
}

return id, nil
Expand All @@ -222,7 +221,7 @@ func removeContainer(ID string) error {
ctx := context.Background()
docker, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return fmt.Errorf("ERROR: couldn't create docker client\n%+v", err)
return fmt.Errorf(" Couldn't create docker client\n%+v", err)
}

options := types.ContainerRemoveOptions{
Expand All @@ -231,7 +230,7 @@ func removeContainer(ID string) error {
}

if err := docker.ContainerRemove(ctx, ID, options); err != nil {
return fmt.Errorf("ERROR: couldn't delete container [%s] -> %+v", ID, err)
return fmt.Errorf(" Couldn't delete container [%s] -> %+v", ID, err)
}
return nil
}
22 changes: 11 additions & 11 deletions cli/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ func importImage(clusterName string, images []string, noRemove bool) error {
ctx := context.Background()
docker, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return fmt.Errorf("ERROR: couldn't create docker client\n%+v", err)
return fmt.Errorf(" Couldn't create docker client\n%+v", err)
}

// get cluster directory to temporarily save the image tarball there
imageVolume, err := getImageVolume(clusterName)
if err != nil {
return fmt.Errorf("ERROR: couldn't get image volume for cluster [%s]\n%+v", clusterName, err)
return fmt.Errorf(" Couldn't get image volume for cluster [%s]\n%+v", clusterName, err)
}

//*** first, save the images using the local docker daemon
Expand Down Expand Up @@ -58,7 +58,7 @@ func importImage(clusterName string, images []string, noRemove bool) error {
},
}

toolsContainerID, err := startContainer(false, &containerConfig, &hostConfig, &network.NetworkingConfig{}, toolsContainerName)
toolsContainerID, err := startContainer(&containerConfig, &hostConfig, &network.NetworkingConfig{}, toolsContainerName)
if err != nil {
return err
}
Expand All @@ -75,14 +75,14 @@ func importImage(clusterName string, images []string, noRemove bool) error {
for {
cont, err := docker.ContainerInspect(ctx, toolsContainerID)
if err != nil {
return fmt.Errorf("ERROR: couldn't get helper container's exit code\n%+v", err)
return fmt.Errorf(" Couldn't get helper container's exit code\n%+v", err)
}
if !cont.State.Running { // container finished...
if cont.State.ExitCode == 0 { // ...successfully
log.Info("Saved images to shared docker volume")
break
} else if cont.State.ExitCode != 0 { // ...failed
errTxt := "ERROR: helper container failed to save images"
errTxt := "Helper container failed to save images"
logReader, err := docker.ContainerLogs(ctx, toolsContainerID, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Expand All @@ -103,7 +103,7 @@ func importImage(clusterName string, images []string, noRemove bool) error {
// Get the container IDs for all containers in the cluster
clusters, err := getClusters(false, clusterName)
if err != nil {
return fmt.Errorf("ERROR: couldn't get cluster by name [%s]\n%+v", clusterName, err)
return fmt.Errorf(" Couldn't get cluster by name [%s]\n%+v", clusterName, err)
}
containerList := []types.Container{clusters[clusterName].server}
containerList = append(containerList, clusters[clusterName].workers...)
Expand Down Expand Up @@ -138,7 +138,7 @@ func importImage(clusterName string, images []string, noRemove bool) error {
// create exec configuration
execResponse, err := docker.ContainerExecCreate(ctx, container.ID, execConfig)
if err != nil {
return fmt.Errorf("ERROR: Failed to create exec command for container [%s]\n%+v", containerName, err)
return fmt.Errorf("Failed to create exec command for container [%s]\n%+v", containerName, err)
}

// attach to exec process in container
Expand All @@ -147,25 +147,25 @@ func importImage(clusterName string, images []string, noRemove bool) error {
Tty: execAttachConfig.Tty,
})
if err != nil {
return fmt.Errorf("ERROR: couldn't attach to container [%s]\n%+v", containerName, err)
return fmt.Errorf(" Couldn't attach to container [%s]\n%+v", containerName, err)
}
defer containerConnection.Close()

// start exec
err = docker.ContainerExecStart(ctx, execResponse.ID, execStartConfig)
if err != nil {
return fmt.Errorf("ERROR: couldn't execute command in container [%s]\n%+v", containerName, err)
return fmt.Errorf(" Couldn't execute command in container [%s]\n%+v", containerName, err)
}

// get output from container
content, err := ioutil.ReadAll(containerConnection.Reader)
if err != nil {
return fmt.Errorf("ERROR: couldn't read output from container [%s]\n%+v", containerName, err)
return fmt.Errorf(" Couldn't read output from container [%s]\n%+v", containerName, err)
}

// example output "unpacking image........ ...done"
if !strings.Contains(string(content), "done") {
return fmt.Errorf("ERROR: seems like something went wrong using `ctr image import` in container [%s]. Full output below:\n%s", containerName, string(content))
return fmt.Errorf("seems like something went wrong using `ctr image import` in container [%s]. Full output below:\n%s", containerName, string(content))
}
}

Expand Down
8 changes: 4 additions & 4 deletions cli/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func createClusterNetwork(clusterName string) (string, error) {
ctx := context.Background()
docker, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return "", fmt.Errorf("ERROR: couldn't create docker client\n%+v", err)
return "", fmt.Errorf(" Couldn't create docker client\n%+v", err)
}

args := filters.NewArgs()
Expand All @@ -47,7 +47,7 @@ func createClusterNetwork(clusterName string) (string, error) {
},
})
if err != nil {
return "", fmt.Errorf("ERROR: couldn't create network\n%+v", err)
return "", fmt.Errorf(" Couldn't create network\n%+v", err)
}

return resp.ID, nil
Expand All @@ -58,7 +58,7 @@ func deleteClusterNetwork(clusterName string) error {
ctx := context.Background()
docker, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return fmt.Errorf("ERROR: couldn't create docker client\n%+v", err)
return fmt.Errorf(" Couldn't create docker client\n%+v", err)
}

filters := filters.NewArgs()
Expand All @@ -69,7 +69,7 @@ func deleteClusterNetwork(clusterName string) error {
Filters: filters,
})
if err != nil {
return fmt.Errorf("ERROR: couldn't find network for cluster %s\n%+v", clusterName, err)
return fmt.Errorf(" Couldn't find network for cluster %s\n%+v", clusterName, err)
}

// there should be only one network that matches the name... but who knows?
Expand Down
4 changes: 2 additions & 2 deletions cli/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ func validatePortSpecs(specs []string) error {
atSplit := strings.Split(spec, "@")
_, err := nat.ParsePortSpec(atSplit[0])
if err != nil {
return fmt.Errorf("ERROR: Invalid port specification [%s] in port mapping [%s]\n%+v", atSplit[0], spec, err)
return fmt.Errorf("Invalid port specification [%s] in port mapping [%s]\n%+v", atSplit[0], spec, err)
}
if len(atSplit) > 0 {
for i := 1; i < len(atSplit); i++ {
if err := ValidateHostname(atSplit[i]); err != nil {
return fmt.Errorf("ERROR: Invalid node-specifier [%s] in port mapping [%s]\n%+v", atSplit[i], spec, err)
return fmt.Errorf("Invalid node-specifier [%s] in port mapping [%s]\n%+v", atSplit[i], spec, err)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cli/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func subShell(cluster, shell, command string) error {
}
}
if !supported {
return fmt.Errorf("ERROR: selected shell [%s] is not supported", shell)
return fmt.Errorf("selected shell [%s] is not supported", shell)
}

// get kubeconfig for selected cluster
Expand Down
Loading

0 comments on commit 9aa5af8

Please sign in to comment.