Skip to content

Commit

Permalink
Upgrade and small code refactor
Browse files Browse the repository at this point in the history
- Upgrade to alpine 3.9 and Go 1.12.4
- Simpler Dockerfile
- Microbadger added to Travis
- Exits with 1 instead of 0 on user parameter error
- Fixed printing of signal on exit
- Replaced panic with log.Fatal
  • Loading branch information
qdm12 committed Apr 27, 2019
1 parent fc2e388 commit cb04904
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ services:
install:
- docker build .
after_script:
#- "curl -X POST https://hooks.microbadger.com/images/qmcgaw/docker-proxy-acl/???"
- "curl -X POST https://hooks.microbadger.com/images/qmcgaw/docker-proxy-acl-alpine/LjSz_drSWg23rkYgs4SL7WeaV_o="
33 changes: 15 additions & 18 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
ARG ALPINE_VERSION=3.8
ARG GO_VERSION=1.11.4
ARG ALPINE_VERSION=3.9
ARG GO_VERSION=1.12.4

FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS builder
ARG BINCOMPRESS
RUN apk --update add git build-base upx
RUN go get -u -v golang.org/x/vgo
WORKDIR /tmp/gobuild
COPY go.mod go.sum ./
RUN go mod download
COPY main.go .
# RUN go test -v
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -ldflags="-s -w" -o app .
RUN [ "${BINCOMPRESS}" == "" ] || (upx -v --best --ultra-brute --overlay=strip app && upx -t app)

FROM scratch AS final
FROM scratch
ARG BUILD_DATE
ARG VCS_REF
LABEL org.label-schema.schema-version="1.0.0-rc1" \
Expand All @@ -21,21 +28,11 @@ LABEL org.label-schema.schema-version="1.0.0-rc1" \
org.label-schema.docker.cmd.devel="docker run -it --rm qmcgaw/REPONAME_DOCKER" \
org.label-schema.docker.params="" \
org.label-schema.version="" \
image-size="MB" \
ram-usage="MB" \
cpu-usage=""
image-size="5.82MB" \
ram-usage="10MB" \
cpu-usage="Low"
ENTRYPOINT ["/dockerproxy"]
# HEALTHCHECK --interval=300s --timeout=5s --start-period=5s --retries=1 CMD ["/healthcheck"]
# USER 1000
ENTRYPOINT ["/dockerproxy"]
CMD -a containers

FROM builder AS builder2
COPY go.mod go.sum ./
RUN go mod download
COPY *.go ./
RUN go test -v
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -ldflags="-s -w" -o app .
# RUN upx -v --best --ultra-brute --overlay=strip app && upx -t app

FROM final
COPY --from=builder2 /tmp/gobuild/app /dockerproxy
COPY --from=builder /tmp/gobuild/app /dockerproxy
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

| Image size | RAM usage | CPU usage |
| --- | --- | --- |
| 5.28MB | 10MB | Low |
| 5.82MB | 10MB | Low |

## Why

Expand Down Expand Up @@ -88,5 +88,5 @@ services:
- [ ] Change to another router
- [ ] Healthcheck
- [ ] Title icon
- [ ] [group ids coming up for Go 1.7](https://github.com/golang/go/issues/2617)
- [ ] Non root user
- [ ] Title icon
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/qdm12/docker-proxy-acl-alpine

require (
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/mux v1.6.2
github.com/kyokomi/emoji v1.5.1
github.com/namsral/flag v1.7.4-pre
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/mux v1.6.2 h1:Pgr17XVTNXAk3q/r4CpKzC5xBM/qW1uVLV+IhRZpIIk=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/kyokomi/emoji v1.5.1 h1:qp9dub1mW7C4MlvoRENH6EAENb9skEFOvIEbp1Waj38=
Expand Down
78 changes: 39 additions & 39 deletions docker-proxy-acl.go → main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
"github.com/namsral/flag"
)

type UpStream struct {
Name string
type upStream struct {
name string
handle *http.Client
}

func (r UpStream) Pass() func(res http.ResponseWriter, req *http.Request) {
func (r upStream) pass() func(res http.ResponseWriter, req *http.Request) {
return func(res http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
http.Error(res, "400 Bad request ; only GET allowed.", 400)
Expand All @@ -31,12 +31,12 @@ func (r UpStream) Pass() func(res http.ResponseWriter, req *http.Request) {
if len(req.URL.RawQuery) > 0 {
param = "?" + req.URL.RawQuery
}
body, _ := r.Get("http://docker"+req.URL.Path+param, res)
body, _ := r.get("http://docker"+req.URL.Path+param, res)
fmt.Fprintf(res, "%s", body)
}
}

func (r UpStream) Get(url string, res http.ResponseWriter) ([]byte, error) {
func (r upStream) get(url string, res http.ResponseWriter) ([]byte, error) {
req, err := r.handle.Get(url)
if err != nil {
return nil, err
Expand All @@ -49,8 +49,8 @@ func (r UpStream) Get(url string, res http.ResponseWriter) ([]byte, error) {
return ioutil.ReadAll(req.Body)
}

func newProxySocket(socket string) UpStream {
stream := UpStream{Name: socket}
func newProxySocket(socket string) upStream {
stream := upStream{name: socket}
stream.handle = &http.Client{
Transport: &http.Transport{
Dial: func(proto, addr string) (net.Conn, error) {
Expand Down Expand Up @@ -91,8 +91,7 @@ func main() {
fs.Var(&allowed, "a", "Allowed location pattern prefix")
fs.Parse(os.Args[1:])
if len(allowed) < 1 {
log.Println(emoji.Sprint(":x:") + "Need at least 1 argument for -a: [" + strings.Join(allowedOptions, ",") + "]")
os.Exit(0)
log.Fatal(emoji.Sprint(":x:") + "Need at least 1 argument for -a: [" + strings.Join(allowedOptions, ",") + "]")
}
var ok bool
for i := range allowed {
Expand All @@ -104,8 +103,7 @@ func main() {
}
}
if !ok {
log.Println(emoji.Sprint(":x:") + "Argument " + allowed[i] + " not recognized!")
os.Exit(0)
log.Fatal(emoji.Sprint(":x:") + "Argument " + allowed[i] + " not recognized")
}
}
var routers [2]*mux.Router
Expand All @@ -118,55 +116,57 @@ func main() {
switch allowed[i] {
case "containers":
containers := m.PathPrefix("/containers").Subrouter()
containers.HandleFunc("/json", upstream.Pass())
containers.HandleFunc("/{name}/json", upstream.Pass())
containers.HandleFunc("/json", upstream.pass())
containers.HandleFunc("/{name}/json", upstream.pass())
case "images":
containers := m.PathPrefix("/images").Subrouter()
containers.HandleFunc("/json", upstream.Pass())
containers.HandleFunc("/{name}/json", upstream.Pass())
containers.HandleFunc("/{name}/history", upstream.Pass())
images := m.PathPrefix("/images").Subrouter()
images.HandleFunc("/json", upstream.pass())
images.HandleFunc("/{name}/json", upstream.pass())
images.HandleFunc("/{name}/history", upstream.pass())
case "volumes":
m.HandleFunc("/volumes", upstream.Pass())
m.HandleFunc("/volumes/{name}", upstream.Pass())
m.HandleFunc("/volumes", upstream.pass())
m.HandleFunc("/volumes/{name}", upstream.pass())
case "networks":
m.HandleFunc("/networks", upstream.Pass())
m.HandleFunc("/networks/{name}", upstream.Pass())
m.HandleFunc("/networks", upstream.pass())
m.HandleFunc("/networks/{name}", upstream.pass())
case "services":
m.HandleFunc("/services", upstream.Pass())
m.HandleFunc("/services/{name}", upstream.Pass())
m.HandleFunc("/services", upstream.pass())
m.HandleFunc("/services/{name}", upstream.pass())
case "tasks":
m.HandleFunc("/tasks", upstream.Pass())
m.HandleFunc("/tasks/{name}", upstream.Pass())
m.HandleFunc("/tasks", upstream.pass())
m.HandleFunc("/tasks/{name}", upstream.pass())
case "events":
m.HandleFunc("/events", upstream.Pass())
m.HandleFunc("/events", upstream.pass())
case "version":
m.HandleFunc("/version", upstream.Pass())
m.HandleFunc("/version", upstream.pass())
case "info":
m.HandleFunc("/info", upstream.Pass())
m.HandleFunc("/info", upstream.pass())
case "ping":
m.HandleFunc("/_ping", upstream.Pass())
m.HandleFunc("/_ping", upstream.pass())
}
}
}
http.Handle("/", routers[0])
listener, err := net.Listen("unix", *filename)
if err != nil {
log.Fatal(err)
}
os.Chmod(*filename, 0666)
// Looking up group ids coming up for Go 1.7
// https://github.com/golang/go/issues/2617
log.Println("Listening on " + *filename + emoji.Sprint(" :ear:"))
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt, os.Kill, syscall.SIGTERM)
go func(c chan os.Signal) {
sig := <-c
log.Println(emoji.Sprint(":heavy_exclamation_mark:")+"Caught signal %s, shutting down", sig)
listener.Close()
os.Exit(0)
}(sigc)
if err != nil {
panic(err)
}
go waitForSignal(sigc, listener)
err = http.Serve(listener, nil)
if err != nil {
panic(err)
log.Fatal(err)
}
}

func waitForSignal(c chan os.Signal, listener net.Listener) {
sig := <-c
log.Println(emoji.Sprint(":heavy_exclamation_mark:") + "Caught signal '" + sig.String() + "', shutting down")
listener.Close()
os.Exit(0)
}

0 comments on commit cb04904

Please sign in to comment.