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

Integrating k3d-tools repo as Go submodule #275

Merged
merged 2 commits into from
Jun 10, 2020
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
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.github/
.local/
bin/
_dist/
tools/
proxy/
site/
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*.dylib

# Output folders
tools/bin/
tools/_dist/
bin/
_dist/
site/
Expand Down
11 changes: 11 additions & 0 deletions tools/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:1.14 as builder
WORKDIR /app
COPY . .
ENV GO111MODULE=on
ENV CGO_ENABLED=0
RUN make build

FROM busybox:1.31
WORKDIR /app
COPY --from=builder /app/bin/k3d-tools .
ENTRYPOINT [ "/app/k3d-tools"]
77 changes: 77 additions & 0 deletions tools/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
SHELL := /bin/bash

# get git tag
GIT_TAG := $(shell git describe --tags)
ifeq ($(GIT_TAG),)
GIT_TAG := $(shell git describe --always)
endif

# Go options
GO ?= go
PKG := $(shell go mod vendor)
TAGS :=
TESTS := .
TESTFLAGS :=
LDFLAGS := -w -s -X github.com/iwilltry42/k3d-tools/version.Version=${GIT_TAG}
GOFLAGS :=
BINDIR := $(CURDIR)/bin
BINARIES := k3d-tools

# Go Package required
PKG_GOX := github.com/mitchellh/gox
PKG_GOLANGCI_LINT := github.com/golangci/golangci-lint/cmd/golangci-lint

export GO111MODULE=on
export CGO_ENABLED=0

# go source directories.
# DIRS defines a single level directly, we only look at *.go in this directory.
# REC_DIRS defines a source code tree. All go files are analyzed recursively.
DIRS := .
REC_DIRS := cmd

# Rules for finding all go source files using 'DIRS' and 'REC_DIRS'
GO_SRC := $(foreach dir,$(DIRS),$(wildcard $(dir)/*.go))
GO_SRC += $(foreach dir,$(REC_DIRS),$(shell find $(dir) -name "*.go"))

# Rules for directory list as input for the golangci-lint program
LINT_DIRS := $(DIRS) $(foreach dir,$(REC_DIRS),$(dir)/...)

.PHONY: all

all: clean fmt check build

build:
$(GO) build -i $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' -o '$(BINDIR)/$(BINARIES)'

clean:
@rm -rf $(BINDIR) _dist/

extra-clean: clean
go clean -i $(PKG_GOX)
go clean -i $(PKG_GOLANGCI_LINT)

# fmt will fix the golang source style in place.
fmt:
@gofmt -s -l -w $(GO_SRC)

# check-fmt returns an error code if any source code contains format error.
check-fmt:
@test -z $(shell gofmt -s -l $(GO_SRC) | tee /dev/stderr) || echo "[WARN] Fix formatting issues with 'make fmt'"

lint:
@golangci-lint run $(LINT_DIRS)

check: check-fmt lint

# Check for required executables
HAS_GOX := $(shell command -v gox 2> /dev/null)
HAS_GOLANGCI := $(shell command -v golangci-lint 2> /dev/null)

install-tools:
ifndef HAS_GOX
(export GO111MODULE=off; go get -u $(PKG_GOX))
endif
ifndef HAS_GOLANGCI
(export GO111MODULE=off; go get -u $(PKG_GOLANGCI_LINT))
endif
7 changes: 7 additions & 0 deletions tools/cmd/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package run

import "github.com/urfave/cli"

func ImageSave(c *cli.Context) error {
return imageSave(c.Args(), c.String("destination"), c.String("cluster"))
}
49 changes: 49 additions & 0 deletions tools/cmd/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package run

import (
"context"
"fmt"
"io"
"log"
"os"
"strings"
"time"

"github.com/docker/docker/client"
)

func imageSave(images []string, dest, clusterName string) error {
// get a docker client
ctx := context.Background()
docker, err := client.NewEnvClient()
if err != nil {
return fmt.Errorf("ERROR: couldn't create docker client\n%+v", err)
}

imageReader, err := docker.ImageSave(ctx, images)
if err != nil {
return fmt.Errorf("ERROR: couldn't save images %s\n%+v", images, err)
}
defer imageReader.Close()

tarFileName := dest
if !strings.HasSuffix(dest, ".tar") {
if !strings.HasSuffix(dest, "/") {
dest = dest + "/"
}
tarFileName = fmt.Sprintf("%sk3d-%s-images-%s.tar", dest, clusterName, time.Now().Format("20060102150405"))
}
tarFile, err := os.Create(tarFileName)
if err != nil {
return fmt.Errorf("ERROR: couldn't create tarfile [%s]\n%+v", tarFileName, err)
}
defer tarFile.Close()

if _, err = io.Copy(tarFile, imageReader); err != nil {
return fmt.Errorf("ERROR: couldn't save image stream to tarfile\n%+v", err)
}

log.Printf("INFO: saved images %s to [%s]", images, tarFileName)

return nil
}
16 changes: 16 additions & 0 deletions tools/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/rancher/k3d/tools

go 1.14

require (
github.com/Microsoft/go-winio v0.4.12 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v1.13.1
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/stretchr/testify v1.3.0 // indirect
github.com/urfave/cli v1.20.0
golang.org/x/net v0.0.0-20190628185345-da137c7871d7 // indirect
)
29 changes: 29 additions & 0 deletions tools/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
github.com/Microsoft/go-winio v0.4.12 h1:xAfWHN1IrQ0NJ9TBC0KBZoqLjzDTr1ML+4MywiUOryc=
github.com/Microsoft/go-winio v0.4.12/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/distribution v2.7.1+incompatible h1:a5mlkVzth6W5A4fOsS3D2EO5BUmsJpcB+cRlLU7cSug=
github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/docker/docker v1.13.1 h1:IkZjBSIc8hBjLpqeAbeE5mca5mNgeatLHBy3GO78BWo=
github.com/docker/docker v1.13.1/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ=
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/opencontainers/go-digest v1.0.0-rc1 h1:WzifXhOVOEOuFYOJAW6aQqW0TooG2iki3E3Ii+WN7gQ=
github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190628185345-da137c7871d7 h1:rTIdg5QFRR7XCaK4LCjBiPbx8j4DQRpdYMnGn/bJUEU=
golang.org/x/net v0.0.0-20190628185345-da137c7871d7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
61 changes: 61 additions & 0 deletions tools/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"fmt"
"log"
"os"
"time"

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

// main represents the CLI application
func main() {

// App Details
app := cli.NewApp()
app.Name = "k3d-tools"
app.Usage = "Tools to help running k3d successfully!"
app.Version = version.GetVersion()

// commands that you can execute
app.Commands = []cli.Command{
{
// save-image
Name: "save-image",
Aliases: []string{"save"},
Usage: "Save images to tarball",
Flags: []cli.Flag{
cli.StringFlag{
Name: "destination, dest, d",
Value: "/images",
Usage: "destination tar-file (optional)",
},
cli.StringFlag{
Name: "cluster, c",
Value: "k3s-default",
Usage: "name of the k3d cluster",
},
},
Action: run.ImageSave,
},
{
Name: "noop",
Usage: "Don't do anything and sleep forever",
Action: func(c *cli.Context) {
for {
fmt.Println("Sleeping for 12h")
time.Sleep(12 * time.Hour)
}
},
},
}

// run the whole thing
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
1 change: 1 addition & 0 deletions tools/vendor/github.com/Microsoft/go-winio/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions tools/vendor/github.com/Microsoft/go-winio/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions tools/vendor/github.com/Microsoft/go-winio/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading