Skip to content
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
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.22.5
go-version: 1.24.0

- name: Build
run: go build

- name: Test
run: go test
run: go test -race
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.22.5 AS builder
FROM golang:1.24.0 AS builder
WORKDIR /build
# Separate dependency caching from compilation
COPY go.mod go.mod
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ It can be used to implement a control plane for a dynamic set of services, where
errors must be highlighted to the caller rather than ignored.

## Prerequisites
* go >= 1.22
* go >= 1.24
* OR docker

## Instructions
Expand Down
2 changes: 1 addition & 1 deletion build-and-push-images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
## Pushes to PKB private repository, change the registry if you want to push it elsewhere

## Increment this number when you push a new image
VERSION=7
VERSION=8
docker build . -t "europe-docker.pkg.dev/infra-240614/eu.gcr.io/regproxy2:$VERSION"
docker push "europe-docker.pkg.dev/infra-240614/eu.gcr.io/regproxy2:$VERSION"
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
module regproxy2

go 1.22.5
go 1.24.0

require (
github.com/google/uuid v1.6.0
github.com/stretchr/testify v1.7.0
go.mercari.io/go-dnscache v0.0.0-20210517095825-88b046eb94f2
go.uber.org/zap v1.19.1
golang.org/x/sync v0.7.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
)
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -57,6 +59,7 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
66 changes: 63 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func errResp(resp http.ResponseWriter, e error) {

type RegStorage interface {
Put(string, *url.URL) error
Remove(string) error
All() (map[string]*url.URL, error)
}

Expand All @@ -51,6 +52,10 @@ func (m *RegStorageMemory) Put(name string, url *url.URL) error {
m.upstreams[name] = url
return nil
}
func (m *RegStorageMemory) Remove(name string) error {
delete(m.upstreams, name)
return nil
}
func (m *RegStorageMemory) All() (map[string]*url.URL, error) {
return maps.Clone(m.upstreams), nil
}
Expand All @@ -73,7 +78,7 @@ func NewRegStorageFile(fileName string) (*RegStorageFile, error) {
return nil, err
}
for name, u := range upstreams {
ups := upstream{
ups := registerRequest{
Name: name,
Callback: u.String(),
}
Expand All @@ -90,6 +95,14 @@ func (m *RegStorageFile) Put(name string, url *url.URL) error {
mm[name] = url
return m.write(mm)
}
func (m *RegStorageFile) Remove(name string) error {
mm, err := m.All()
if err != nil {
return err
}
delete(mm, name)
return m.write(mm)
}
func (m *RegStorageFile) write(content map[string]*url.URL) error {
sb := strings.Builder{}
for name, u := range content {
Expand Down Expand Up @@ -211,13 +224,13 @@ func (p *RegProxy) proxy(resp http.ResponseWriter, req *http.Request) {
_ = latestSuccess.Write(resp)
}

type upstream struct {
type registerRequest struct {
Name string `json:"name"`
Callback string `json:"callback"`
}

func (p *RegProxy) register(resp http.ResponseWriter, req *http.Request) {
var q upstream
var q registerRequest
err := json.NewDecoder(req.Body).Decode(&q)
if err != nil {
badRequest(resp, err.Error())
Expand All @@ -240,6 +253,51 @@ func (p *RegProxy) register(resp http.ResponseWriter, req *http.Request) {
resp.WriteHeader(204)
}

type deregisterRequest struct {
Name string `json:"name"`
}

func (p *RegProxy) deregister(resp http.ResponseWriter, req *http.Request) {
var q deregisterRequest
err := json.NewDecoder(req.Body).Decode(&q)
if err != nil {
badRequest(resp, err.Error())
return
}
p.writeLock.Lock()
defer p.writeLock.Unlock()
Comment thread
leonyork marked this conversation as resolved.
log.Printf("Removing upstream %v", q)
if err := p.storage.Remove(q.Name); err != nil {
Comment thread
leonyork marked this conversation as resolved.
errResp(resp, err)
return
}
resp.WriteHeader(204)
}

type upstream struct {
Name string `json:"name"`
Callback string `json:"callback"`
}

func (p *RegProxy) list(resp http.ResponseWriter, req *http.Request) {
p.writeLock.Lock()
defer p.writeLock.Unlock()
Comment thread
MFAshby marked this conversation as resolved.
all, err := p.storage.All()
if err != nil {
errResp(resp, err)
return
}
var res []upstream
for k, v := range all {
res = append(res, upstream{Name: k, Callback: v.String()})
}
resp.WriteHeader(200)
err = json.NewEncoder(resp).Encode(res)
if err != nil {
log.Printf("error writing response %v", err)
}
}

func (p *RegProxy) health(resp http.ResponseWriter, _ *http.Request) {
// https://inadarei.github.io/rfc-healthcheck/
resp.WriteHeader(200)
Expand Down Expand Up @@ -288,6 +346,8 @@ func NewRegProxy(
sm := http.NewServeMux()
sm.HandleFunc("/health", rp.health)
sm.HandleFunc("/register", rp.register)
sm.HandleFunc("/deregister", rp.deregister)
sm.HandleFunc("/list", rp.list)
sm.HandleFunc("/", rp.proxy)
rp.handler = sm
return rp
Expand Down
Loading