Skip to content

Commit

Permalink
Add root module and remove plugin module
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslorentz committed May 19, 2022
1 parent 500809a commit 2ac1042
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 603 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -643,4 +643,4 @@ $ docker rm -f caddy whoami0 whoami1

You can build Caddy using [xcaddy](https://github.com/caddyserver/xcaddy) or [caddy docker builder](https://hub.docker.com/_/caddy).

Use module name **github.com/lucaslorentz/caddy-docker-proxy/plugin** to add this plugin to your build.
Use module name **github.com/lucaslorentz/caddy-docker-proxy/v2** to add this plugin to your build.
10 changes: 5 additions & 5 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
CGO_ENABLED=0 GOARCH=amd64 GOOS=linux \
xcaddy build \
--output ${ARTIFACTS}/binaries/linux/amd64/caddy \
--with github.com/lucaslorentz/caddy-docker-proxy/plugin=$PWD/plugin
--with github.com/lucaslorentz/caddy-docker-proxy/v2=$PWD

# ARM
CGO_ENABLED=0 GOARCH=arm GOARM=6 GOOS=linux \
xcaddy build \
--output ${ARTIFACTS}/binaries/linux/arm/v6/caddy \
--with github.com/lucaslorentz/caddy-docker-proxy/plugin=$PWD/plugin
--with github.com/lucaslorentz/caddy-docker-proxy/v2=$PWD

CGO_ENABLED=0 GOARCH=arm GOARM=7 GOOS=linux \
xcaddy build \
--output ${ARTIFACTS}/binaries/linux/arm/v7/caddy \
--with github.com/lucaslorentz/caddy-docker-proxy/plugin=$PWD/plugin
--with github.com/lucaslorentz/caddy-docker-proxy/v2=$PWD

CGO_ENABLED=0 GOARCH=arm64 GOOS=linux \
xcaddy build \
--output ${ARTIFACTS}/binaries/linux/arm64/caddy \
--with github.com/lucaslorentz/caddy-docker-proxy/plugin=$PWD/plugin
--with github.com/lucaslorentz/caddy-docker-proxy/v2=$PWD

# AMD64 WINDOWS
CGO_ENABLED=0 GOARCH=amd64 GOOS=windows \
xcaddy build \
--output ${ARTIFACTS}/binaries/windows/amd64/caddy.exe \
--with github.com/lucaslorentz/caddy-docker-proxy/plugin=$PWD/plugin
--with github.com/lucaslorentz/caddy-docker-proxy/v2=$PWD
14 changes: 6 additions & 8 deletions plugin/go.mod → go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/lucaslorentz/caddy-docker-proxy/plugin
module github.com/lucaslorentz/caddy-docker-proxy/v2

go 1.17

require (
github.com/caddyserver/caddy/v2 v2.5.0
github.com/docker/docker v20.10.14+incompatible
github.com/caddyserver/caddy/v2 v2.5.1
github.com/docker/docker v20.10.16+incompatible
github.com/stretchr/testify v1.7.1
go.uber.org/zap v1.21.0
)
Expand All @@ -16,7 +16,7 @@ require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/Microsoft/go-winio v0.4.14 // indirect
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/antlr/antlr4 v0.0.0-20200503195918-621b933c7a7f // indirect
github.com/aryann/difflib v0.0.0-20210328193216-ff5ff6dc229b // indirect
Expand All @@ -27,7 +27,6 @@ require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cheekybits/genny v1.0.0 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/containerd/containerd v1.6.3 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgraph-io/badger v1.6.2 // indirect
Expand Down Expand Up @@ -75,7 +74,7 @@ require (
github.com/marten-seemann/qtls-go1-18 v0.1.1 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/mholt/acmez v1.0.2 // indirect
github.com/micromdm/scep/v2 v2.1.0 // indirect
Expand All @@ -87,9 +86,8 @@ require (
github.com/morikuni/aec v1.0.0 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/onsi/ginkgo v1.16.4 // indirect
github.com/onsi/gomega v1.15.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
Expand Down
530 changes: 9 additions & 521 deletions plugin/go.sum → go.sum

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package caddydockerproxy

import (
_ "github.com/lucaslorentz/caddy-docker-proxy/v2/plugin"
)
8 changes: 4 additions & 4 deletions plugin/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/caddyserver/caddy/v2"
caddycmd "github.com/caddyserver/caddy/v2/cmd"
"github.com/lucaslorentz/caddy-docker-proxy/plugin/config"
"github.com/lucaslorentz/caddy-docker-proxy/plugin/generator"
"github.com/lucaslorentz/caddy-docker-proxy/v2/plugin/config"
"github.com/lucaslorentz/caddy-docker-proxy/v2/plugin/generator"

"go.uber.org/zap"
)
Expand All @@ -31,13 +31,13 @@ func init() {
"Which mode this instance should run: standalone | controller | server")

fs.String("docker-sockets", "",
"Docker sockets comma separate")
"Docker sockets comma separate")

fs.String("docker-certs-path", "",
"Docker socket certs path comma separate")

fs.String("docker-apis-version", "",
"Docker socket apis version comma separate")
"Docker socket apis version comma separate")

fs.String("controller-network", "",
"Network allowed to configure caddy server in CIDR notation. Ex: 10.200.200.0/24")
Expand Down
2 changes: 1 addition & 1 deletion plugin/generator/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package generator

import (
"github.com/docker/docker/api/types"
"github.com/lucaslorentz/caddy-docker-proxy/plugin/caddyfile"
"github.com/lucaslorentz/caddy-docker-proxy/v2/plugin/caddyfile"
"go.uber.org/zap"
)

Expand Down
2 changes: 1 addition & 1 deletion plugin/generator/containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/network"
"github.com/lucaslorentz/caddy-docker-proxy/plugin/config"
"github.com/lucaslorentz/caddy-docker-proxy/v2/plugin/config"
)

func TestContainers_TemplateData(t *testing.T) {
Expand Down
34 changes: 17 additions & 17 deletions plugin/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/lucaslorentz/caddy-docker-proxy/plugin/caddyfile"
"github.com/lucaslorentz/caddy-docker-proxy/plugin/config"
"github.com/lucaslorentz/caddy-docker-proxy/plugin/docker"
"github.com/lucaslorentz/caddy-docker-proxy/v2/plugin/caddyfile"
"github.com/lucaslorentz/caddy-docker-proxy/v2/plugin/config"
"github.com/lucaslorentz/caddy-docker-proxy/v2/plugin/docker"

"go.uber.org/zap"
)
Expand All @@ -39,11 +39,11 @@ func CreateGenerator(dockerClients []docker.Client, dockerUtils docker.Utils, op
var labelRegexString = fmt.Sprintf("^%s(_\\d+)?(\\.|$)", options.LabelPrefix)

return &CaddyfileGenerator{
options: options,
labelRegex: regexp.MustCompile(labelRegexString),
dockerClients: dockerClients,
swarmIsAvailable: make([]bool, len(dockerClients)),
dockerUtils: dockerUtils,
options: options,
labelRegex: regexp.MustCompile(labelRegexString),
dockerClients: dockerClients,
swarmIsAvailable: make([]bool, len(dockerClients)),
dockerUtils: dockerUtils,
}
}

Expand Down Expand Up @@ -85,7 +85,7 @@ func (g *CaddyfileGenerator) GenerateCaddyfile(logger *zap.Logger) ([]byte, []st
logger.Debug("Skipping default Caddyfile because no path is set")
}

for i, dockerClient := range(g.dockerClients){
for i, dockerClient := range g.dockerClients {

// Add Caddyfile from swarm configs
if g.swarmIsAvailable[i] {
Expand All @@ -96,7 +96,7 @@ func (g *CaddyfileGenerator) GenerateCaddyfile(logger *zap.Logger) ([]byte, []st
fullConfig, _, err := dockerClient.ConfigInspectWithRaw(context.Background(), config.ID)
if err != nil {
logger.Error("Failed to inspect Swarm Config", zap.String("config", config.Spec.Name), zap.Error(err))

} else {
block, err := caddyfile.Unmarshal(fullConfig.Spec.Data)
if err != nil {
Expand All @@ -113,7 +113,7 @@ func (g *CaddyfileGenerator) GenerateCaddyfile(logger *zap.Logger) ([]byte, []st
} else {
logger.Debug("Skipping swarm config caddyfiles because swarm is not available")
}

// Add containers
containers, err := dockerClient.ContainerList(context.Background(), types.ContainerListOptions{})
if err == nil {
Expand All @@ -140,14 +140,14 @@ func (g *CaddyfileGenerator) GenerateCaddyfile(logger *zap.Logger) ([]byte, []st
} else {
logger.Error("Failed to get ContainerList", zap.Error(err))
}

// Add services
if g.swarmIsAvailable[i] {
services, err := dockerClient.ServiceList(context.Background(), types.ServiceListOptions{})
if err == nil {
for _, service := range services {
logger.Debug("Swarm service", zap.String("service", service.Spec.Name))

if _, isControlledServer := service.Spec.Labels[g.options.ControlledServersLabel]; isControlledServer {
ips, err := g.getServiceTasksIps(&service, logger, false)
if err != nil {
Expand All @@ -160,7 +160,7 @@ func (g *CaddyfileGenerator) GenerateCaddyfile(logger *zap.Logger) ([]byte, []st
}
}
}

// caddy. labels based config
serviceCaddyfile, err := g.getServiceCaddyfile(&service, logger)
if err == nil {
Expand All @@ -174,7 +174,7 @@ func (g *CaddyfileGenerator) GenerateCaddyfile(logger *zap.Logger) ([]byte, []st
}
} else {
logger.Debug("Skipping swarm services because swarm is not available")
}
}
}

// Write global blocks first
Expand Down Expand Up @@ -213,7 +213,7 @@ func (g *CaddyfileGenerator) GenerateCaddyfile(logger *zap.Logger) ([]byte, []st

func (g *CaddyfileGenerator) checkSwarmAvailability(logger *zap.Logger, isFirstCheck bool) {

for i, dockerClient := range(g.dockerClients){
for i, dockerClient := range g.dockerClients {
info, err := dockerClient.Info(context.Background())
if err == nil {
newSwarmIsAvailable := info.Swarm.LocalNodeState == swarm.LocalNodeStateActive
Expand All @@ -231,7 +231,7 @@ func (g *CaddyfileGenerator) checkSwarmAvailability(logger *zap.Logger, isFirstC
func (g *CaddyfileGenerator) getIngressNetworks(logger *zap.Logger) (map[string]bool, error) {
ingressNetworks := map[string]bool{}

for _, dockerClient := range(g.dockerClients){
for _, dockerClient := range g.dockerClients {
if len(g.options.IngressNetworks) > 0 {
networks, err := dockerClient.NetworkList(context.Background(), types.NetworkListOptions{})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions plugin/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/swarm"
"github.com/lucaslorentz/caddy-docker-proxy/plugin/config"
"github.com/lucaslorentz/caddy-docker-proxy/plugin/docker"
"github.com/lucaslorentz/caddy-docker-proxy/v2/plugin/config"
"github.com/lucaslorentz/caddy-docker-proxy/v2/plugin/docker"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down
2 changes: 1 addition & 1 deletion plugin/generator/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"text/template"

"github.com/lucaslorentz/caddy-docker-proxy/plugin/caddyfile"
"github.com/lucaslorentz/caddy-docker-proxy/v2/plugin/caddyfile"
)

type targetsProvider func() ([]string, error)
Expand Down
2 changes: 1 addition & 1 deletion plugin/generator/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/swarm"
"github.com/lucaslorentz/caddy-docker-proxy/plugin/caddyfile"
"github.com/lucaslorentz/caddy-docker-proxy/v2/plugin/caddyfile"

"go.uber.org/zap"
)
Expand Down
2 changes: 1 addition & 1 deletion plugin/generator/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/lucaslorentz/caddy-docker-proxy/plugin/config"
"github.com/lucaslorentz/caddy-docker-proxy/v2/plugin/config"
)

func TestServices_TemplateData(t *testing.T) {
Expand Down
Loading

0 comments on commit 2ac1042

Please sign in to comment.