Skip to content

Commit

Permalink
Do not error out if no ip is available in a prefix (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Jul 24, 2024
1 parent 89978cc commit d5e5fcf
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 61 deletions.
11 changes: 7 additions & 4 deletions cmd/metal-api/internal/service/async-actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"fmt"
"log/slog"

"connectrpc.com/connect"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/headscale"

ipamer "github.com/metal-stack/go-ipam"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/datastore"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/ipam"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/metal"
Expand Down Expand Up @@ -216,12 +216,15 @@ func (a *asyncActor) releaseIP(ip metal.IP) error {

// now the IP should not exist any more in our datastore
// so cleanup the ipam

ctx := context.Background()
err = a.ReleaseIP(ctx, ip)
if err != nil {
if errors.Is(err, ipamer.ErrNotFound) {
return nil
var connectErr *connect.Error
if errors.As(err, &connectErr) {
if connectErr.Code() == connect.CodeNotFound {
return nil
}
}
return fmt.Errorf("cannot release IP %q: %w", ip, err)
}
Expand Down
11 changes: 6 additions & 5 deletions cmd/metal-api/internal/service/ip-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (

v1 "github.com/metal-stack/metal-api/cmd/metal-api/internal/service/v1"

goipam "github.com/metal-stack/go-ipam"

restfulspec "github.com/emicklei/go-restful-openapi/v2"
restful "github.com/emicklei/go-restful/v3"
"github.com/metal-stack/metal-lib/httperrors"
Expand Down Expand Up @@ -439,10 +437,13 @@ func allocateSpecificIP(ctx context.Context, parent *metal.Network, specificIP s
func allocateRandomIP(ctx context.Context, parent *metal.Network, ipamer ipam.IPAMer) (ipAddress, parentPrefixCidr string, err error) {
for _, prefix := range parent.Prefixes {
ipAddress, err = ipamer.AllocateIP(ctx, prefix)
if err != nil && errors.Is(err, goipam.ErrNoIPAvailable) {
continue
}
if err != nil {
var connectErr *connect.Error
if errors.As(err, &connectErr) {
if connectErr.Code() == connect.CodeNotFound {
continue
}
}
return "", "", err
}

Expand Down
33 changes: 17 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/juanfont/headscale v0.22.3
github.com/klauspost/connect-compress/v2 v2.0.0
github.com/looplab/fsm v1.0.2
github.com/metal-stack/go-ipam v1.14.1
github.com/metal-stack/go-ipam v1.14.3
github.com/metal-stack/masterdata-api v0.11.4
github.com/metal-stack/metal-lib v0.17.2
github.com/metal-stack/security v0.8.0
Expand All @@ -31,9 +31,9 @@ require (
github.com/stretchr/testify v1.9.0
github.com/testcontainers/testcontainers-go v0.31.0
github.com/undefinedlabs/go-mpatch v1.0.7
golang.org/x/crypto v0.24.0
golang.org/x/crypto v0.25.0
golang.org/x/sync v0.7.0
google.golang.org/grpc v1.64.0
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2
gopkg.in/rethinkdb/rethinkdb-go.v6 v6.2.2
)
Expand Down Expand Up @@ -63,9 +63,9 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/containerd/containerd v1.7.18 // indirect
github.com/containerd/errdefs v0.1.0 // indirect
github.com/containerd/containerd v1.7.19 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/platforms v0.2.1 // indirect
github.com/coreos/go-oidc/v3 v3.10.0 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
Expand Down Expand Up @@ -147,6 +147,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
Expand All @@ -159,10 +160,10 @@ require (
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.54.0 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/puzpuzpuz/xsync/v2 v2.5.1 // indirect
github.com/redis/go-redis/v9 v9.5.3 // indirect
github.com/redis/go-redis/v9 v9.5.4 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rs/zerolog v1.33.0 // indirect
github.com/sagikazarmark/locafero v0.6.0 // indirect
Expand Down Expand Up @@ -191,24 +192,24 @@ require (
go.etcd.io/etcd/api/v3 v3.5.14 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.14 // indirect
go.etcd.io/etcd/client/v3 v3.5.14 // indirect
go.mongodb.org/mongo-driver v1.15.1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.52.0 // indirect
go.opentelemetry.io/otel v1.27.0 // indirect
go.opentelemetry.io/otel/metric v1.27.0 // indirect
go.opentelemetry.io/otel/trace v1.27.0
go.mongodb.org/mongo-driver v1.16.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 // indirect
go.opentelemetry.io/otel v1.28.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
go4.org/mem v0.0.0-20240501181205-ae6ca9944745 // indirect
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
golang.org/x/net v0.26.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect; indirecct
golang.zx2c4.com/wireguard/windows v0.5.3 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240617180043-68d350f18fd4 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240617180043-68d350f18fd4 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240711142825-46eb208f015d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240711142825-46eb208f015d // indirect
gopkg.in/cenkalti/backoff.v2 v2.2.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
Loading

0 comments on commit d5e5fcf

Please sign in to comment.