Skip to content

Commit

Permalink
Add compose usage sample (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 committed Mar 18, 2024
1 parent 72b753b commit 6d48337
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 20 deletions.
11 changes: 10 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
FROM golang:1.21-alpine as builder
FROM alpine:3.19 as health-downloader
ENV GRPC_HEALTH_PROBE_VERSION=v0.4.25 \
GRPC_HEALTH_PROBE_URL=https://github.com/grpc-ecosystem/grpc-health-probe/releases/download
RUN apk -U add curl \
&& curl -fLso /bin/grpc_health_probe \
${GRPC_HEALTH_PROBE_URL}/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 \
&& chmod +x /bin/grpc_health_probe

FROM golang:1.22-alpine as builder
RUN apk add \
binutils \
gcc \
Expand All @@ -11,5 +19,6 @@ COPY . .
RUN make server client

FROM alpine:3.19
COPY --from=health-downloader /bin/grpc_health_probe /bin/grpc_health_probe
COPY --from=builder /work/bin/* /
ENTRYPOINT [ "/server" ]
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,34 @@ There is also a `cli` provided in the container which can be used to make calls
docker run -it --rm --entrypoint /cli ghcr.io/metal-stack/go-ipam
```

## Docker Compose example

Ensure you have docker with compose support installed. Then execute the following command:

```bash
docker compose up -d

# check if up and running
docker compose ps

NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
go-ipam-ipam-1 go-ipam "/server postgres" ipam 14 seconds ago Up 13 seconds (healthy) 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp
go-ipam-postgres-1 postgres:alpine "docker-entrypoint.s…" postgres 8 minutes ago Up 13 seconds 5432/tcp


# Then execute the cli to create prefixes and acquire ips

docker compose exec ipam /cli prefix create --cidr 192.168.0.0/16
prefix:"192.168.0.0/16" created

docker compose exec ipam /cli ip acquire --prefix 192.168.0.0/16
ip:"192.168.0.1" acquired

# Queries can also made against the Rest api like so:

curl -v -X POST -d '{}' -H 'Content-Type: application/json' localhost:9090/api.v1.IpamService/ListPrefixes
```

## Supported Databases & Performance

| Database | Acquire Child Prefix | Acquire IP | New Prefix | Prefix Overlap | Production-Ready | Geo-Redundant |
Expand Down
5 changes: 5 additions & 0 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ func (s *server) Run() error {
grpcreflect.NewStaticReflector(apiv1connect.IpamServiceName),
compress.WithAll(compress.LevelBalanced),
))
mux.Handle(grpcreflect.NewHandlerV1Alpha(
grpcreflect.NewStaticReflector(apiv1connect.IpamServiceName),
compress.WithAll(compress.LevelBalanced),
))

server := http.Server{
Addr: s.c.GrpcServerEndpoint,
Expand All @@ -103,6 +107,7 @@ func (s *server) Run() error {
ReadHeaderTimeout: 1 * time.Minute,
}

s.log.Info("started grpc server", "at", server.Addr)
err = server.ListenAndServe()
return err
}
39 changes: 39 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
version: "3.8"

services:
postgres:
image: postgres:alpine
environment:
- POSTGRES_USER=ipam
- POSTGRES_PASSWORD=ipam
- POSTGRES_DB=ipam
restart: always
volumes:
- db_data:/var/lib/postgresql/data
expose:
- 5432
ipam:
image: go-ipam
environment:
- GOIPAM_GRPC_SERVER_ENDPOINT=0.0.0.0:9090
- GOIPAM_PG_HOST=postgres
- GOIPAM_PG_PORT=5432
- GOIPAM_PG_USER=ipam
- GOIPAM_PG_PASSWORD=ipam
- GOIPAM_PG_DBNAME=ipam
restart: always
command:
postgres
ports:
- 9090:9090
healthcheck:
test: ["CMD", "/bin/grpc_health_probe", "-addr","localhost:9090"]
interval: 10s
timeout: 2s
retries: 3
start_period: 10s
depends_on:
- postgres
volumes:
db_data:
4 changes: 2 additions & 2 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (p Prefix) toPrefixJSON() prefixJSON {
}

func (p Prefix) toJSON() ([]byte, error) {
pj, err := json.Marshal(p.toPrefixJSON())
pj, err := json.Marshal(p.toPrefixJSON()) // nolint:musttag
if err != nil {
return nil, fmt.Errorf("unable to marshal prefix:%w", err)
}
Expand All @@ -70,7 +70,7 @@ func (ps Prefixes) toJSON() ([]byte, error) {

func fromJSON(js []byte) (Prefix, error) {
var pre prefixJSON
err := json.Unmarshal(js, &pre)
err := json.Unmarshal(js, &pre) // nolint:musttag
if err != nil {
return Prefix{}, fmt.Errorf("unable to unmarshal prefix:%w", err)
}
Expand Down
35 changes: 20 additions & 15 deletions memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,30 @@ func Test_UpdatePrefix_Concurrent(t *testing.T) {
cidr := calcPrefix24(run) + "/24"
prefix.Cidr = cidr

p, err := m.CreatePrefix(ctx, prefix, defaultNamespace)
require.NoError(t, err)
require.NotNil(t, p)
_, err := m.CreatePrefix(ctx, prefix, defaultNamespace)
if err != nil {
t.Error(t, err)
}

p, err = m.ReadPrefix(ctx, cidr, defaultNamespace)
require.NoError(t, err)
require.NotNil(t, p)
p, err := m.ReadPrefix(ctx, cidr, defaultNamespace)
if err != nil {
t.Error(t, err)
}

p, err = m.UpdatePrefix(ctx, p, defaultNamespace)
require.NoError(t, err)
require.NotNil(t, p)
_, err = m.UpdatePrefix(ctx, p, defaultNamespace)
if err != nil {
t.Error(t, err)
}

p, err = m.ReadPrefix(ctx, cidr, defaultNamespace)
require.NoError(t, err)
require.NotNil(t, p)

p, err = m.DeletePrefix(ctx, p, defaultNamespace)
require.NoError(t, err)
require.NotNil(t, p)
if err != nil {
t.Error(t, err)
}

_, err = m.DeletePrefix(ctx, p, defaultNamespace)
if err != nil {
t.Error(t, err)
}
}(i)
}
}
Expand Down
4 changes: 2 additions & 2 deletions sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func Test_ConcurrentAcquirePrefix(t *testing.T) {
count := 20
prefixes := make(chan string)
for i := 0; i < count; i++ {
go acquirePrefix(t, ctx, db, parentCidr, prefixes)
go acquirePrefix(t, ctx, db, parentCidr, prefixes) // nolint:testifylint
}

prefixMap := make(map[string]bool)
Expand Down Expand Up @@ -220,7 +220,7 @@ func Test_ConcurrentAcquireIP(t *testing.T) {
count := 30
ips := make(chan string)
for i := 0; i < count; i++ {
go acquireIP(t, ctx, db, parentCidr, ips)
go acquireIP(t, ctx, db, parentCidr, ips) // nolint:testifylint
}

ipMap := make(map[string]bool)
Expand Down

0 comments on commit 6d48337

Please sign in to comment.