Skip to content

Commit

Permalink
Merge pull request #368 from lucaslorentz/improve-modules
Browse files Browse the repository at this point in the history
Add root module and remove plugin module
  • Loading branch information
lucaslorentz authored May 20, 2022
2 parents 500809a + ae7c332 commit b4adeaa
Show file tree
Hide file tree
Showing 65 changed files with 125 additions and 634 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Caddy-Docker-Proxy
[![Build Status](https://dev.azure.com/lucaslorentzlara/lucaslorentzlara/_apis/build/status/lucaslorentz.caddy-docker-proxy?branchName=master)](https://dev.azure.com/lucaslorentzlara/lucaslorentzlara/_build/latest?definitionId=1) [![Go Report Card](https://goreportcard.com/badge/github.com/lucaslorentz/caddy-docker-proxy)](https://goreportcard.com/report/github.com/lucaslorentz/caddy-docker-proxy)

## NEW MODULE NAME!

We've renamed our go module, from version 2.7.0 forward you should import caddy-docker-proxy using `github.com/lucaslorentz/caddy-docker-proxy/v2` or a specific version `github.com/lucaslorentz/caddy-docker-proxy/[email protected]`.

The old name `github.com/lucaslorentz/caddy-docker-proxy/plugin` will be a available for backwards compatibility, but it will be frozen at version 2.6.0.

## Introduction
This plugin enables Caddy to be used as a reverse proxy for Docker containers via labels.

Expand Down Expand Up @@ -643,4 +649,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.
12 changes: 5 additions & 7 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,35 @@ set -e
echo ==PARAMETERS==
echo ARTIFACTS: "${ARTIFACTS:=./artifacts}"

cd plugin
go vet ./...
go test -race ./...
cd ../

go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest

# AMD64
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
caddy = *.example.com
caddy.1_@foo = host foo.example.com
caddy.1_handle = @foo
caddy.1_handle.reverse_proxy = foo:8080
caddy = *.example.com
caddy.2_@bar = host bar.example.com
caddy.2_handle = @bar
caddy.2_handle.reverse_proxy = bar:8080
----------
*.example.com {
@foo host foo.example.com
@bar host bar.example.com
handle @foo {
reverse_proxy foo:8080
}
handle @bar {
reverse_proxy bar:8080
}
caddy = *.example.com
caddy.1_@foo = host foo.example.com
caddy.1_handle = @foo
caddy.1_handle.reverse_proxy = foo:8080

caddy = *.example.com
caddy.2_@bar = host bar.example.com
caddy.2_handle = @bar
caddy.2_handle.reverse_proxy = bar:8080
----------
*.example.com {
@foo host foo.example.com
@bar host bar.example.com
handle @foo {
reverse_proxy foo:8080
}
handle @bar {
reverse_proxy bar:8080
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions plugin/cmd.go → cmd.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package plugin
package caddydockerproxy

import (
"flag"
Expand All @@ -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/config"
"github.com/lucaslorentz/caddy-docker-proxy/v2/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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion plugin/generator/containers.go → 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/caddyfile"
"go.uber.org/zap"
)

Expand Down
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/config"
)

func TestContainers_TemplateData(t *testing.T) {
Expand Down
34 changes: 17 additions & 17 deletions plugin/generator/generator.go → 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/caddyfile"
"github.com/lucaslorentz/caddy-docker-proxy/v2/config"
"github.com/lucaslorentz/caddy-docker-proxy/v2/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
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/config"
"github.com/lucaslorentz/caddy-docker-proxy/v2/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 → 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/caddyfile"
)

type targetsProvider func() ([]string, error)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion plugin/generator/services.go → 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/caddyfile"

"go.uber.org/zap"
)
Expand Down
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/config"
)

func TestServices_TemplateData(t *testing.T) {
Expand Down
File renamed without changes.
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
Loading

0 comments on commit b4adeaa

Please sign in to comment.