Skip to content

Commit

Permalink
update netaddr (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Feb 28, 2021
1 parent 0958608 commit 12a934f
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 178 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
run: curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(go env GOPATH)/bin v1.26.0

- name: Run golangci-lint
run: $(go env GOPATH)/bin/golangci-lint run
run: $(go env GOPATH)/bin/golangci-lint run -p bugs

- name: build and test
run: |
Expand Down
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
module github.com/metal-stack/go-ipam

go 1.15
go 1.16

require (
github.com/avast/retry-go v3.0.0+incompatible
github.com/jmoiron/sqlx v1.3.1
github.com/lib/pq v1.9.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
github.com/testcontainers/testcontainers-go v0.9.0
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
inet.af/netaddr v0.0.0-20210129021658-06debf945877
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
inet.af/netaddr v0.0.0-20210222205655-a1ec2b7b8c44
)
31 changes: 6 additions & 25 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ github.com/containerd/containerd v1.4.1 h1:pASeJT3R3YyVn+94qEPk0SnU1OQ20Jd/T+SPK
github.com/containerd/containerd v1.4.1/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtMXFTttgp+kVtyUA=
github.com/containerd/continuity v0.0.0-20190426062206-aaeac12a7ffc h1:TP+534wVlf61smEIq1nwLLAjQVEK2EADoW3CX9AuT+8=
github.com/containerd/continuity v0.0.0-20190426062206-aaeac12a7ffc/go.mod h1:GL3xCUCBDV3CZiTSEKksMWbLE66hEyuu9qyDOOqM47Y=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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=
Expand All @@ -27,7 +26,6 @@ github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5Xh
github.com/docker/go-units v0.3.3 h1:Xk8S3Xj5sLGlG5g67hJmYMmUgXv5N4PhkjJHHqrwnTk=
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/dvyukov/go-fuzz v0.0.0-20201127111758-49e582c6c23d/go.mod h1:11Gm+ccJnvAhCNLlf5+cS9KjtbaD5I5zaZpFMsTHWTw=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
Expand Down Expand Up @@ -55,7 +53,6 @@ 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/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jmoiron/sqlx v1.3.1 h1:aLN7YINNZ7cYOPK3QC83dbM6KT0NMqVMw961TqrejlE=
github.com/jmoiron/sqlx v1.3.1/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ=
Expand All @@ -80,9 +77,7 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c h1:nXxl5PrvVm2L/wCy8dQu6DMTwH4oIuGN8GJDAlqDdVE=
github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.8.0 h1:VkHVNpR4iVnU8XQR6DBm8BqYjN7CRzw+xKUbVVbbW9w=
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo=
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/opencontainers/go-digest v1.0.0-rc1 h1:WzifXhOVOEOuFYOJAW6aQqW0TooG2iki3E3Ii+WN7gQ=
github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
Expand All @@ -99,14 +94,10 @@ github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand All @@ -116,22 +107,19 @@ github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVM
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
go4.org/intern v0.0.0-20210108033219-3eb7198706b2 h1:VFTf+jjIgsldaz/Mr00VaCSswHJrI2hIjQygE/W4IMg=
go4.org/intern v0.0.0-20210108033219-3eb7198706b2/go.mod h1:vLqJ+12kCw61iCWsPto0EOHhBS+o4rO5VIucbc9g2Cc=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20201222175341-b30ae309168e h1:ExUmGi0ZsQmiVo9giDQqXkr7vreeXPMkOGIusfsfbzI=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20201222175341-b30ae309168e/go.mod h1:FftLjUGFEDu5k8lt0ddY+HcrH/qU/0qk+H8j9/nTl3E=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20201222180813-1025295fd063 h1:1tk03FUNpulq2cuWpXZWj649rwJpk0d20rxWiopKRmc=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20201222180813-1025295fd063/go.mod h1:FftLjUGFEDu5k8lt0ddY+HcrH/qU/0qk+H8j9/nTl3E=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 h1:/ZScEX8SfEmUGRHs0gxpqteO5nfNW6axyZbBdw9A12g=
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 h1:003p0dJM77cxMSyCPFphvZf/Y5/NXf5fzg6ufd1/Oew=
golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -140,17 +128,14 @@ golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c h1:fqgJT0MGcGpPgpWU7VRdRjuArfcOvC4AoJmILihzhDg=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand All @@ -165,14 +150,10 @@ google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3
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/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand All @@ -181,5 +162,5 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gotest.tools v0.0.0-20181223230014-1083505acf35 h1:zpdCK+REwbk+rqjJmHhiCN6iBIigrZ39glqSF0P3KF0=
gotest.tools v0.0.0-20181223230014-1083505acf35/go.mod h1:R//lfYlUuTOTfblYI3lGoAAAebUdzjvbmQsuB7Ykd90=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
inet.af/netaddr v0.0.0-20210129021658-06debf945877 h1:kiGwZoOS5kyefrTgIAvPzyfTuVfensOlU73GJN9OQKY=
inet.af/netaddr v0.0.0-20210129021658-06debf945877/go.mod h1:I2i9ONCXRZDnG1+7O8fSuYzjcPxHQXrIfzD/IkR87x4=
inet.af/netaddr v0.0.0-20210222205655-a1ec2b7b8c44 h1:p7fX77zWzZMuNdJUhniBsmN1OvFOrW9SOtvgnzqUZX4=
inet.af/netaddr v0.0.0-20210222205655-a1ec2b7b8c44/go.mod h1:I2i9ONCXRZDnG1+7O8fSuYzjcPxHQXrIfzD/IkR87x4=
4 changes: 1 addition & 3 deletions memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package ipam
import (
"fmt"
"sync"

"github.com/pkg/errors"
)

type memory struct {
Expand Down Expand Up @@ -38,7 +36,7 @@ func (m *memory) ReadPrefix(prefix string) (Prefix, error) {

result, ok := m.prefixes[prefix]
if !ok {
return Prefix{}, errors.Errorf("Prefix %s not found", prefix)
return Prefix{}, fmt.Errorf("prefix %s not found", prefix)
}
return *result.deepCopy(), nil
}
Expand Down
2 changes: 1 addition & 1 deletion memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Test_ReadPrefix(t *testing.T) {
// Prefix
p, err := m.ReadPrefix("12.0.0.0/8")
require.NotNil(t, err)
require.Equal(t, "Prefix 12.0.0.0/8 not found", err.Error())
require.Equal(t, "prefix 12.0.0.0/8 not found", err.Error())
require.Empty(t, p)

prefix := Prefix{Cidr: "12.0.0.0/16"}
Expand Down
2 changes: 1 addition & 1 deletion postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewPostgresStorage(host, port, user, password, dbname string, sslmode SSLMo
func newPostgres(host, port, user, password, dbname string, sslmode SSLMode) (*sql, error) {
db, err := sqlx.Connect("postgres", dataSource(host, port, user, password, dbname, sslmode))
if err != nil {
return nil, fmt.Errorf("unable to connect to database:%v", err)
return nil, fmt.Errorf("unable to connect to database:%w", err)
}
db.MustExec(postgresSchema)
return &sql{
Expand Down
46 changes: 28 additions & 18 deletions prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package ipam
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"math"
"net"
"strings"

"github.com/avast/retry-go"
"github.com/pkg/errors"
"inet.af/netaddr"
)

Expand All @@ -20,6 +20,8 @@ var (
ErrNoIPAvailable NoIPAvailableError
// ErrAlreadyAllocated is returned if the requested address is not available
ErrAlreadyAllocated AlreadyAllocatedError
// ErrOptimisticLockError is returned if insert or update conflicts with the existing data
ErrOptimisticLockError OptimisticLockError
)

// Prefix is a expression of a ip with length and forms a classless network.
Expand Down Expand Up @@ -160,7 +162,7 @@ func (i *ipamer) DeletePrefix(cidr string) (*Prefix, error) {
}
prefix, err := i.storage.DeletePrefix(*p)
if err != nil {
return nil, fmt.Errorf("delete prefix:%s %v", cidr, err)
return nil, fmt.Errorf("delete prefix:%s %w", cidr, err)
}

return &prefix, nil
Expand Down Expand Up @@ -192,7 +194,7 @@ func (i *ipamer) acquireChildPrefixInternal(parentCidr string, length uint8) (*P
return nil, fmt.Errorf("prefix %s has ips, acquire child prefix not possible", parent.Cidr)
}

var ipset netaddr.IPSet
var ipset netaddr.IPSetBuilder
ipset.AddPrefix(ipprefix)
for cp, available := range parent.availableChildPrefixes {
if available {
Expand All @@ -205,9 +207,9 @@ func (i *ipamer) acquireChildPrefixInternal(parentCidr string, length uint8) (*P
ipset.RemovePrefix(cpipprefix)
}

cp, ok := ipset.RemoveFreePrefix(length)
cp, _, ok := ipset.IPSet().RemoveFreePrefix(length)
if !ok {
pfxs := ipset.Prefixes()
pfxs := ipset.IPSet().Prefixes()
if len(pfxs) == 0 {
return nil, fmt.Errorf("no prefix found in %s with length:%d", parentCidr, length)
}
Expand All @@ -234,15 +236,15 @@ func (i *ipamer) acquireChildPrefixInternal(parentCidr string, length uint8) (*P

_, err = i.storage.UpdatePrefix(*parent)
if err != nil {
return nil, errors.Wrapf(err, "unable to update parent prefix:%v", parent)
return nil, fmt.Errorf("unable to update parent prefix:%v error:%w", parent, err)
}
child, err = i.newPrefix(child.Cidr, parentCidr)
if err != nil {
return nil, fmt.Errorf("unable to persist created child:%v", err)
return nil, fmt.Errorf("unable to persist created child:%w", err)
}
_, err = i.storage.CreatePrefix(*child)
if err != nil {
return nil, errors.Wrapf(err, "unable to update parent prefix:%v", child)
return nil, fmt.Errorf("unable to update parent prefix:%v error:%w", child, err)
}

return child, nil
Expand All @@ -268,11 +270,11 @@ func (i *ipamer) releaseChildPrefixInternal(child *Prefix) error {
parent.availableChildPrefixes[child.Cidr] = true
_, err := i.DeletePrefix(child.Cidr)
if err != nil {
return fmt.Errorf("unable to release prefix %v:%v", child, err)
return fmt.Errorf("unable to release prefix %v:%w", child, err)
}
_, err = i.storage.UpdatePrefix(*parent)
if err != nil {
return fmt.Errorf("unable to release prefix %v:%v", child, err)
return fmt.Errorf("unable to release prefix %v:%w", child, err)
}
return nil
}
Expand Down Expand Up @@ -340,7 +342,7 @@ func (i *ipamer) acquireSpecificIPInternal(prefixCidr, specificIP string) (*IP,
prefix.ips[ipstring] = true
_, err := i.storage.UpdatePrefix(*prefix)
if err != nil {
return nil, errors.Wrapf(err, "unable to persist acquired ip:%v", prefix)
return nil, fmt.Errorf("unable to persist acquired ip:%v error:%w", prefix, err)
}
return acquired, nil
}
Expand Down Expand Up @@ -387,12 +389,12 @@ func (i *ipamer) PrefixesOverlapping(existingPrefixes []string, newPrefixes []st
for _, ep := range existingPrefixes {
eip, err := netaddr.ParseIPPrefix(ep)
if err != nil {
return fmt.Errorf("parsing prefix %s failed:%v", ep, err)
return fmt.Errorf("parsing prefix %s failed:%w", ep, err)
}
for _, np := range newPrefixes {
nip, err := netaddr.ParseIPPrefix(np)
if err != nil {
return fmt.Errorf("parsing prefix %s failed:%v", np, err)
return fmt.Errorf("parsing prefix %s failed:%w", np, err)
}
if eip.Overlaps(nip) || nip.Overlaps(eip) {
return fmt.Errorf("%s overlaps %s", np, ep)
Expand All @@ -406,7 +408,7 @@ func (i *ipamer) PrefixesOverlapping(existingPrefixes []string, newPrefixes []st
func (i *ipamer) newPrefix(cidr, parentCidr string) (*Prefix, error) {
ipnet, err := netaddr.ParseIPPrefix(cidr)
if err != nil {
return nil, fmt.Errorf("unable to parse cidr:%s %v", cidr, err)
return nil, fmt.Errorf("unable to parse cidr:%s %w", cidr, err)
}
p := &Prefix{
Cidr: cidr,
Expand Down Expand Up @@ -486,7 +488,7 @@ func (p *Prefix) availablePrefixes() (uint64, []string) {
if err != nil {
return 0, nil
}
var ipset netaddr.IPSet
var ipset netaddr.IPSetBuilder
ipset.AddPrefix(prefix)
for cp, available := range p.availableChildPrefixes {
if available {
Expand All @@ -500,7 +502,7 @@ func (p *Prefix) availablePrefixes() (uint64, []string) {
}
// Only 2 Bit Prefixes are usable, set max bits available 2 less than max in family
maxBits := prefix.IP.BitLen() - 2
pfxs := ipset.Prefixes()
pfxs := ipset.IPSet().Prefixes()
totalAvailable := uint64(0)
availablePrefixes := []string{}
for _, pfx := range pfxs {
Expand Down Expand Up @@ -555,6 +557,15 @@ func (o NotFoundError) Error() string {
return "NotFound"
}

// OptimisticLockError indicates that the operation could not be executed because the dataset to update has changed in the meantime.
// clients can decide to read the current dataset and retry the operation.
type OptimisticLockError struct {
}

func (o OptimisticLockError) Error() string {
return "OptimisticLockError"
}

// AlreadyAllocatedError is raised if the given address is already in use
type AlreadyAllocatedError struct {
}
Expand All @@ -571,8 +582,7 @@ func retryOnOptimisticLock(retryableFunc retry.RetryableFunc) error {
return retry.Do(
retryableFunc,
retry.RetryIf(func(err error) bool {
_, isOptimisticLock := errors.Cause(err).(OptimisticLockError)
return isOptimisticLock
return errors.Is(err, ErrOptimisticLockError)
}),
retry.Attempts(10),
retry.DelayType(retry.CombineDelay(retry.BackOffDelay, retry.RandomDelay)),
Expand Down
Loading

0 comments on commit 12a934f

Please sign in to comment.