Skip to content

Re-organize the project structure, change the name and clean the dependencies. #362

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

Merged
merged 5 commits into from
Aug 8, 2021
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
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
.git
.github
docker-gen
dist
examples
LICENSE
Makefile
README.md
templates
*.gz
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ jobs:
run: make check-gofmt

- name: Run tests
run: go test -v
run: go test -v ./internal/dockergen
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
docker-gen
!cmd/docker-gen
dist
*.gz
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ get-deps:
go mod download

check-gofmt:
if [ -n "$(shell gofmt -l .)" ]; then \
if [ -n "$(shell gofmt -l ./cmd/docker-gen)" ]; then \
echo 1>&2 'The following files need to be formatted:'; \
gofmt -l .; \
gofmt -l ./cmd/docker-gen; \
exit 1; \
fi
if [ -n "$(shell gofmt -l ./internal/dockergen)" ]; then \
echo 1>&2 'The following files need to be formatted:'; \
gofmt -l ./internal/dockergen; \
exit 1; \
fi

test:
go test
go test ./internal/dockergen
7 changes: 2 additions & 5 deletions cmd/docker-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"log"
"os"
"path/filepath"
"sync"

"github.com/BurntSushi/toml"
docker "github.com/fsouza/go-dockerclient"
"github.com/jwilder/docker-gen"
"github.com/nginx-proxy/docker-gen/internal/dockergen"
)

type stringslice []string
Expand All @@ -36,8 +35,6 @@ var (
tlsKey string
tlsCaCert string
tlsVerify bool
tlsCertPath string
wg sync.WaitGroup
)

func (strings *stringslice) String() string {
Expand Down Expand Up @@ -69,7 +66,7 @@ Environment Variables:
DOCKER_CERT_PATH - directory path containing key.pem, cert.pem and ca.pem
DOCKER_TLS_VERIFY - enable client TLS verification
`)
println(`For more information, see https://github.com/jwilder/docker-gen`)
println(`For more information, see https://github.com/nginx-proxy/docker-gen`)
}

func loadConfig(file string) error {
Expand Down
File renamed without changes.
24 changes: 3 additions & 21 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
module github.com/jwilder/docker-gen
module github.com/nginx-proxy/docker-gen

go 1.11
go 1.16

require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/BurntSushi/toml v0.3.1
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/containerd/continuity v0.0.0-20210315143101-93e15499afd5 // indirect
github.com/docker/docker v1.4.2-0.20171014114940-f2afa2623594 // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.3.2 // indirect
github.com/fsouza/go-dockerclient v0.0.0-20171009031830-d2a6d0596004
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/mux v0.0.0-20160718151158-d391bea3118c // indirect
github.com/gotestyourself/gotestyourself v2.2.0+incompatible // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/opencontainers/runc v0.1.1 // indirect
github.com/opencontainers/selinux v1.8.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/fsouza/go-dockerclient v1.7.2
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20210331175145-43e1dd70ce54 // indirect
gotest.tools v2.2.0+incompatible // indirect
)
207 changes: 96 additions & 111 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion config.go → internal/dockergen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func ParseWait(s string) (*Wait, error) {
return nil, err
}
if max < min {
return nil, errors.New("Invalid wait interval: max must be larger than min")
return nil, errors.New("invalid wait interval: max must be larger than min")
}
} else {
max = 4 * min
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 5 additions & 18 deletions docker_client.go → internal/dockergen/docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dockergen
import (
"errors"
"fmt"
"os"
"strconv"
"strings"

Expand Down Expand Up @@ -48,7 +47,7 @@ func parseHost(addr string) (string, string, error) {
addr = strings.TrimSpace(addr)
switch {
case addr == "tcp://":
return "", "", fmt.Errorf("Invalid bind address format: %s", addr)
return "", "", fmt.Errorf("invalid bind address format: %s", addr)
case strings.HasPrefix(addr, "unix://"):
proto = "unix"
addr = strings.TrimPrefix(addr, "unix://")
Expand All @@ -65,15 +64,15 @@ func parseHost(addr string) (string, string, error) {
addr = "/var/run/docker.sock"
default:
if strings.Contains(addr, "://") {
return "", "", fmt.Errorf("Invalid bind address protocol: %s", addr)
return "", "", fmt.Errorf("invalid bind address protocol: %s", addr)
}
proto = "tcp"
}

if proto != "unix" && strings.Contains(addr, ":") {
hostParts := strings.Split(addr, ":")
if len(hostParts) != 2 {
return "", "", fmt.Errorf("Invalid bind address format: %s", addr)
return "", "", fmt.Errorf("invalid bind address format: %s", addr)
}
if hostParts[0] != "" {
host = hostParts[0]
Expand All @@ -84,11 +83,11 @@ func parseHost(addr string) (string, string, error) {
if p, err := strconv.Atoi(hostParts[1]); err == nil && p != 0 {
port = p
} else {
return "", "", fmt.Errorf("Invalid bind address format: %s", addr)
return "", "", fmt.Errorf("invalid bind address format: %s", addr)
}

} else if proto == "tcp" && !strings.Contains(addr, ":") {
return "", "", fmt.Errorf("Invalid bind address format: %s", addr)
return "", "", fmt.Errorf("invalid bind address format: %s", addr)
} else {
host = addr
}
Expand Down Expand Up @@ -118,15 +117,3 @@ func splitDockerImage(img string) (string, string, string) {

return registry, repository, tag
}

// pathExists returns whether the given file or directory exists or not
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
File renamed without changes.
13 changes: 7 additions & 6 deletions generator.go → internal/dockergen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"syscall"
"time"

"github.com/fsouza/go-dockerclient"
docker "github.com/fsouza/go-dockerclient"
)

type generator struct {
Expand Down Expand Up @@ -41,12 +41,12 @@ type GeneratorConfig struct {
func NewGenerator(gc GeneratorConfig) (*generator, error) {
endpoint, err := GetEndpoint(gc.Endpoint)
if err != nil {
return nil, fmt.Errorf("Bad endpoint: %s", err)
return nil, fmt.Errorf("bad endpoint: %s", err)
}

client, err := NewDockerClient(endpoint, gc.TLSVerify, gc.TLSCert, gc.TLSCACert, gc.TLSKey)
if err != nil {
return nil, fmt.Errorf("Unable to create docker client: %s", err)
return nil, fmt.Errorf("unable to create docker client: %s", err)
}

apiVersion, err := client.Version()
Expand Down Expand Up @@ -191,7 +191,7 @@ func (g *generator) generateFromEvents() {
watchers = append(watchers, watcher)

debouncedChan := newDebounceChannel(watcher, config.Wait)
for _ = range debouncedChan {
for range debouncedChan {
containers, err := g.getContainers()
if err != nil {
log.Printf("Error listing containers: %s\n", err)
Expand Down Expand Up @@ -367,7 +367,8 @@ func (g *generator) getContainers() ([]*RuntimeContainer, error) {

containers := []*RuntimeContainer{}
for _, apiContainer := range apiContainers {
container, err := g.Client.InspectContainer(apiContainer.ID)
opts := docker.InspectContainerOptions{ID: apiContainer.ID}
container, err := g.Client.InspectContainerWithOptions(opts)
if err != nil {
log.Printf("Error inspecting container: %s: %s\n", apiContainer.ID, err)
continue
Expand Down Expand Up @@ -465,7 +466,7 @@ func (g *generator) getContainers() ([]*RuntimeContainer, error) {

func newSignalChannel() <-chan os.Signal {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGKILL)
signal.Notify(sig, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

return sig
}
Expand Down
14 changes: 7 additions & 7 deletions generator_test.go → internal/dockergen/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"testing"
"time"

"github.com/fsouza/go-dockerclient"
docker "github.com/fsouza/go-dockerclient"
dockertest "github.com/fsouza/go-dockerclient/testing"
)

Expand Down Expand Up @@ -49,7 +49,7 @@ func TestGenerateFromEvents(t *testing.T) {
}))
server.CustomHandler("/containers/json", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
result := []docker.APIContainers{
docker.APIContainers{
{
ID: containerID,
Image: "base:latest",
Command: "/bin/sh",
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestGenerateFromEvents(t *testing.T) {
},
Image: "0ff407d5a7d9ed36acdf3e75de8cc127afecc9af234d05486be2981cdc01a38d",
NetworkSettings: &docker.NetworkSettings{
IPAddress: fmt.Sprintf("10.0.0.10"),
IPAddress: "10.0.0.10",
IPPrefixLen: 24,
Gateway: "10.0.0.1",
Bridge: "docker0",
Expand Down Expand Up @@ -147,24 +147,24 @@ func TestGenerateFromEvents(t *testing.T) {
Endpoint: serverURL,
Configs: ConfigFile{
[]Config{
Config{
{
Template: tmplFile.Name(),
Dest: destFiles[0].Name(),
Watch: false,
},
Config{
{
Template: tmplFile.Name(),
Dest: destFiles[1].Name(),
Watch: true,
Wait: &Wait{0, 0},
},
Config{
{
Template: tmplFile.Name(),
Dest: destFiles[2].Name(),
Watch: true,
Wait: &Wait{20 * time.Millisecond, 25 * time.Millisecond},
},
Config{
{
Template: tmplFile.Name(),
Dest: destFiles[3].Name(),
Watch: true,
Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions reflect_test.go → internal/dockergen/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ func TestDeepGetNoPath(t *testing.T) {
t.Fail()
}

var returned RuntimeContainer
returned = value.(RuntimeContainer)
returned := value.(RuntimeContainer)
if !returned.Equals(item) {
t.Fail()
}
Expand Down
23 changes: 6 additions & 17 deletions template.go → internal/dockergen/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,6 @@ import (
"text/template"
)

func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}

func getArrayValues(funcName string, entries interface{}) (*reflect.Value, error) {
entriesVal := reflect.ValueOf(entries)

Expand All @@ -45,7 +34,7 @@ func getArrayValues(funcName string, entries interface{}) (*reflect.Value, error
case reflect.Array, reflect.Slice:
break
default:
return nil, fmt.Errorf("Must pass an array or slice to '%v'; received %v; kind %v", funcName, entries, kind)
return nil, fmt.Errorf("must pass an array or slice to '%v'; received %v; kind %v", funcName, entries, kind)
}
return &entriesVal, nil
}
Expand Down Expand Up @@ -121,7 +110,7 @@ func groupByLabel(entries interface{}, label string) (map[string][]interface{},
}
return nil, nil
}
return nil, fmt.Errorf("Must pass an array or slice of RuntimeContainer to 'groupByLabel'; received %v", v)
return nil, fmt.Errorf("must pass an array or slice of RuntimeContainer to 'groupByLabel'; received %v", v)
}
return generalizedGroupBy("groupByLabel", entries, getLabel, func(groups map[string][]interface{}, value interface{}, v interface{}) {
groups[value.(string)] = append(groups[value.(string)], v)
Expand Down Expand Up @@ -261,7 +250,7 @@ func keys(input interface{}) (interface{}, error) {

val := reflect.ValueOf(input)
if val.Kind() != reflect.Map {
return nil, fmt.Errorf("Cannot call keys on a non-map value: %v", input)
return nil, fmt.Errorf("cannot call keys on a non-map value: %v", input)
}

vk := val.MapKeys()
Expand Down Expand Up @@ -435,7 +424,7 @@ func newTemplate(name string) *template.Template {
"contains": contains,
"dict": dict,
"dir": dirList,
"exists": exists,
"exists": pathExists,
"first": arrayFirst,
"groupBy": groupBy,
"groupByKeys": groupByKeys,
Expand Down Expand Up @@ -534,7 +523,7 @@ func GenerateFile(config Config, containers Context) bool {
log.Fatalf("Unable to create empty destination file: %s\n", err)
} else {
emptyFile.Close()
fi, err = os.Stat(config.Dest)
fi, _ = os.Stat(config.Dest)
}
}
if err := dest.Chmod(fi.Mode()); err != nil {
Expand All @@ -549,7 +538,7 @@ func GenerateFile(config Config, containers Context) bool {
}
}

if bytes.Compare(oldContents, contents) != 0 {
if !bytes.Equal(oldContents, contents) {
err = os.Rename(dest.Name(), config.Dest)
if err != nil {
log.Fatalf("Unable to create dest file %s: %s\n", config.Dest, err)
Expand Down
Loading