From 6d4833734bc32ae4e103233b9491cf7dc7978f75 Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Mon, 18 Mar 2024 08:00:28 +0100 Subject: [PATCH] Add compose usage sample (#139) --- Dockerfile | 11 ++++++++++- README.md | 28 ++++++++++++++++++++++++++++ cmd/server/server.go | 5 +++++ compose.yaml | 39 +++++++++++++++++++++++++++++++++++++++ json.go | 4 ++-- memory_test.go | 35 ++++++++++++++++++++--------------- sql_test.go | 4 ++-- 7 files changed, 106 insertions(+), 20 deletions(-) create mode 100644 compose.yaml diff --git a/Dockerfile b/Dockerfile index 791f7c3..8bb86fd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 \ @@ -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" ] \ No newline at end of file diff --git a/README.md b/README.md index fff2e02..c6b51d6 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/cmd/server/server.go b/cmd/server/server.go index e27cd3d..3c637a5 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -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, @@ -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 } diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..3bc3e71 --- /dev/null +++ b/compose.yaml @@ -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: diff --git a/json.go b/json.go index efef95e..16ae125 100644 --- a/json.go +++ b/json.go @@ -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) } @@ -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) } diff --git a/memory_test.go b/memory_test.go index bde0ae6..37eda33 100644 --- a/memory_test.go +++ b/memory_test.go @@ -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) } } diff --git a/sql_test.go b/sql_test.go index 5f05d8c..e08b4ef 100644 --- a/sql_test.go +++ b/sql_test.go @@ -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) @@ -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)