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

Further re-organize the project structure #381

Merged
merged 2 commits into from
Oct 26, 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
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 ./internal/dockergen
run: go test -v ./internal/...
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ get-deps:
go mod download

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

test:
go test ./internal/dockergen
go test ./internal/...
17 changes: 9 additions & 8 deletions cmd/docker-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (

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

type stringslice []string
Expand All @@ -27,7 +28,7 @@ var (
onlyPublished bool
includeStopped bool
configFiles stringslice
configs dockergen.ConfigFile
configs config.ConfigFile
interval int
keepBlankLines bool
endpoint string
Expand Down Expand Up @@ -133,11 +134,11 @@ func main() {
}
}
} else {
w, err := dockergen.ParseWait(wait)
w, err := config.ParseWait(wait)
if err != nil {
log.Fatalf("Error parsing wait interval: %s\n", err)
}
config := dockergen.Config{
cfg := config.Config{
Template: flag.Arg(0),
Dest: flag.Arg(1),
Watch: watch,
Expand All @@ -152,10 +153,10 @@ func main() {
KeepBlankLines: keepBlankLines,
}
if notifyContainerID != "" {
config.NotifyContainers[notifyContainerID] = notifyContainerSignal
cfg.NotifyContainers[notifyContainerID] = notifyContainerSignal
}
configs = dockergen.ConfigFile{
Config: []dockergen.Config{config}}
configs = config.ConfigFile{
Config: []config.Config{cfg}}
}

all := true
Expand All @@ -165,7 +166,7 @@ func main() {
}
}

generator, err := dockergen.NewGenerator(dockergen.GeneratorConfig{
generator, err := generator.NewGenerator(generator.GeneratorConfig{
Endpoint: endpoint,
TLSKey: tlsKey,
TLSCert: tlsCert,
Expand Down
2 changes: 1 addition & 1 deletion internal/dockergen/config.go → internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dockergen
package config

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
package dockergen
package config

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFilterWatches(t *testing.T) {
testConfigFile := &ConfigFile{
Config: []Config{
{Template: "foo", Watch: true},
{Template: "bar"},
{Template: "baz", Watch: true},
},
}

expected := []Config{
{Template: "foo", Watch: true},
{Template: "baz", Watch: true},
}

configFile := testConfigFile.FilterWatches()
assert.Equal(t, expected, configFile.Config)
}

func TestParseWait(t *testing.T) {
incorrectIntervals := []string{
"500x", // Incorrect min interval
Expand Down
5 changes: 3 additions & 2 deletions internal/dockergen/context.go → internal/context/context.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dockergen
package context

import (
"bufio"
Expand All @@ -8,6 +8,7 @@ import (
"sync"

docker "github.com/fsouza/go-dockerclient"
"github.com/nginx-proxy/docker-gen/internal/utils"
)

var (
Expand All @@ -19,7 +20,7 @@ var (
type Context []*RuntimeContainer

func (c *Context) Env() map[string]string {
return splitKeyValueSlice(os.Environ())
return utils.SplitKeyValueSlice(os.Environ())
}

func (c *Context) Docker() Docker {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dockergen
package context

import (
"fmt"
Expand Down Expand Up @@ -163,3 +163,39 @@ func TestPublishedAddresses(t *testing.T) {

assert.ElementsMatch(t, expected, container.PublishedAddresses())
}

func TestRuntimeContainerEquals(t *testing.T) {
rc1 := &RuntimeContainer{
ID: "baz",
Image: DockerImage{
Registry: "foo/bar",
},
}
rc2 := &RuntimeContainer{
ID: "baz",
Name: "qux",
Image: DockerImage{
Registry: "foo/bar",
},
}
assert.True(t, rc1.Equals(*rc2))
assert.True(t, rc2.Equals(*rc1))

rc2.Image.Tag = "quux"
assert.False(t, rc1.Equals(*rc2))
assert.False(t, rc2.Equals(*rc1))
}

func TestDockerImageString(t *testing.T) {
image := &DockerImage{Repository: "foo/bar"}
assert.Equal(t, "foo/bar", image.String())

image.Registry = "baz.io"
assert.Equal(t, "baz.io/foo/bar", image.String())

image.Tag = "qux"
assert.Equal(t, "baz.io/foo/bar:qux", image.String())

image.Registry = ""
assert.Equal(t, "foo/bar:qux", image.String())
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
package dockergen
package dockerclient

import (
"errors"
"fmt"
"os"
"strconv"
"strings"

docker "github.com/fsouza/go-dockerclient"
"github.com/nginx-proxy/docker-gen/internal/utils"
)

func GetEndpoint(endpoint string) (string, error) {
defaultEndpoint := "unix:///var/run/docker.sock"
if os.Getenv("DOCKER_HOST") != "" {
defaultEndpoint = os.Getenv("DOCKER_HOST")
}

if endpoint != "" {
defaultEndpoint = endpoint
}

_, _, err := parseHost(defaultEndpoint)
if err != nil {
return "", err
}

return defaultEndpoint, nil
}

func NewDockerClient(endpoint string, tlsVerify bool, tlsCert, tlsCaCert, tlsKey string) (*docker.Client, error) {
if strings.HasPrefix(endpoint, "unix:") {
return docker.NewClient(endpoint)
} else if tlsVerify || tlsEnabled(tlsCert, tlsCaCert, tlsKey) {
if tlsVerify {
if e, err := pathExists(tlsCaCert); !e || err != nil {
if e, err := utils.PathExists(tlsCaCert); !e || err != nil {
return nil, errors.New("TLS verification was requested, but CA cert does not exist")
}
}
Expand All @@ -26,7 +46,7 @@ func NewDockerClient(endpoint string, tlsVerify bool, tlsCert, tlsCaCert, tlsKey

func tlsEnabled(tlsCert, tlsCaCert, tlsKey string) bool {
for _, v := range []string{tlsCert, tlsCaCert, tlsKey} {
if e, err := pathExists(v); e && err == nil {
if e, err := utils.PathExists(v); e && err == nil {
return true
}
}
Expand Down Expand Up @@ -98,7 +118,7 @@ func parseHost(addr string) (string, string, error) {
return proto, fmt.Sprintf("%s:%d", host, port), nil
}

func splitDockerImage(img string) (string, string, string) {
func SplitDockerImage(img string) (string, string, string) {
index := 0
repository := img
var registry, tag string
Expand Down
Loading