From ac43ee869c152a24771c18da4b5e65d6e79b1d3c Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 25 Jul 2024 17:10:05 +0000 Subject: [PATCH 1/6] build-sys: update build sys to darvaza.org/x's latest Signed-off-by: Alejandro Mery --- Makefile | 30 +++++---- go.mod | 25 +++----- go.sum | 39 +----------- pkg/tools/gen_index.sh | 3 +- pkg/tools/gen_mk.sh | 140 +++++++++++++++++++++++++++++------------ pkg/tools/revive.toml | 2 +- pkg/tools/tools.go | 6 +- 7 files changed, 127 insertions(+), 118 deletions(-) diff --git a/Makefile b/Makefile index de1bcf3..71d1a27 100644 --- a/Makefile +++ b/Makefile @@ -6,20 +6,20 @@ GOFMT ?= gofmt GOFMT_FLAGS = -w -l -s GOGENERATE_FLAGS = -v -GOPATH ?= $(shell $(GO) env GOPATH) -GOBIN ?= $(GOPATH)/bin - TOOLSDIR := $(CURDIR)/pkg/tools -TMPDIR ?= .tmp +TMPDIR ?= $(CURDIR)/.tmp +OUTDIR ?= $(TMPDIR) -REVIVE ?= $(GOBIN)/revive -REVIVE_CONF ?= $(TOOLSDIR)/revive.toml -REVIVE_RUN_ARGS ?= -config $(REVIVE_CONF) -formatter friendly +GOLANGCI_LINT_VERSION ?= v1.59.1 REVIVE_VERSION ?= v1.3.7 -REVIVE_INSTALL_URL ?= github.com/mgechev/revive@$(REVIVE_VERSION) -GO_INSTALL_URLS = \ - $(REVIVE_INSTALL_URL) \ +GOLANGCI_LINT_URL ?= github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION) +GOLANGCI_LINT ?= $(GO) run $(GOLANGCI_LINT_URL) + +REVIVE_CONF ?= $(TOOLSDIR)/revive.toml +REVIVE_RUN_ARGS ?= -config $(REVIVE_CONF) -formatter friendly +REVIVE_URL ?= github.com/mgechev/revive@$(REVIVE_VERSION) +REVIVE ?= $(GO) run $(REVIVE_URL) V = 0 Q = $(if $(filter 1,$V),,@) @@ -30,12 +30,13 @@ GO_BUILD_CMD = $(GO_BUILD) -o "$(OUTDIR)" all: get generate tidy build -install: - $Q $(GO) install -v ./cmd/... - clean: ; $(info $(M) cleaning…) rm -rf $(TMPDIR) +install: ; $(info $(M) cleaning…) + $Q $(GO) install -v ./cmd/... + + $(TMPDIR)/index: $(TOOLSDIR)/gen_index.sh Makefile FORCE ; $(info $(M) generating index…) $Q mkdir -p $(@D) $Q $< > $@~ @@ -55,6 +56,3 @@ tidy: fmt generate: ; $(info $(M) running go:generate…) $Q git grep -l '^//go:generate' | sort -uV | xargs -r -n1 $(GO) generate $(GOGENERATE_FLAGS) - -$(REVIVE): - $Q $(GO) install -v $(REVIVE_INSTALL_URL) diff --git a/go.mod b/go.mod index 7aaf513..e110a63 100644 --- a/go.mod +++ b/go.mod @@ -1,19 +1,25 @@ module git.jpi.io/amery/jpictl -go 1.19 +go 1.21 require ( + asciigoat.org/core v0.3.9 // indirect asciigoat.org/ini v0.2.5 + darvaza.org/cache/x/simplelru v0.1.8 // indirect darvaza.org/core v0.14.2 darvaza.org/resolver v0.9.2 darvaza.org/sidecar v0.4.0 darvaza.org/slog v0.5.7 darvaza.org/slog/handlers/discard v0.4.11 + darvaza.org/slog/handlers/filter v0.4.9 // indirect + darvaza.org/slog/handlers/zerolog v0.4.9 // indirect +) + +require ( github.com/gofrs/uuid/v5 v5.2.0 github.com/hack-pad/hackpadfs v0.2.1 github.com/libdns/cloudflare v0.1.1 github.com/libdns/libdns v0.2.2 - github.com/mgechev/revive v1.3.7 github.com/spf13/cobra v1.8.0 golang.org/x/crypto v0.25.0 golang.org/x/net v0.27.0 @@ -21,28 +27,13 @@ require ( ) require ( - asciigoat.org/core v0.3.9 // indirect - darvaza.org/cache/x/simplelru v0.1.8 // indirect - darvaza.org/slog/handlers/filter v0.4.9 // indirect - darvaza.org/slog/handlers/zerolog v0.4.9 // indirect - github.com/BurntSushi/toml v1.4.0 // indirect - github.com/chavacava/garif v0.1.0 // indirect - github.com/fatih/color v1.17.0 // indirect - github.com/fatih/structtag v1.2.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/kr/pretty v0.3.1 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect - github.com/mattn/go-runewidth v0.0.15 // indirect - github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 // indirect github.com/miekg/dns v1.1.59 // indirect - github.com/mitchellh/go-homedir v1.1.0 // indirect - github.com/olekukonko/tablewriter v0.0.5 // indirect - github.com/pkg/errors v0.9.1 // indirect - github.com/rivo/uniseg v0.4.7 // indirect github.com/rogpeppe/go-internal v1.11.0 // indirect github.com/rs/zerolog v1.33.0 // indirect - github.com/spf13/afero v1.11.0 // indirect github.com/spf13/pflag v1.0.5 // indirect golang.org/x/mod v0.17.0 // indirect golang.org/x/sync v0.7.0 // indirect diff --git a/go.sum b/go.sum index 566e364..bfddbbd 100644 --- a/go.sum +++ b/go.sum @@ -18,20 +18,9 @@ darvaza.org/slog/handlers/filter v0.4.9 h1:xD8OBwlJytpiwTSDDZqUuNSOsJuaManXQiOj9 darvaza.org/slog/handlers/filter v0.4.9/go.mod h1:t+sjcf1c46kAdf1TRiQmop91xlkteZrC4WDXoVwHgP8= darvaza.org/slog/handlers/zerolog v0.4.9 h1:08FjRnwRGtJsLLBnbgxVorb/bkgm5QEM/LXD2cxeCbM= darvaza.org/slog/handlers/zerolog v0.4.9/go.mod h1:PZYfx6eOxQfD+cXJQp52iwKgcD30QVYHoXxOCojAOdw= -github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= -github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= -github.com/chavacava/garif v0.1.0 h1:2JHa3hbYf5D9dsgseMKAmc/MZ109otzgNFk5s87H9Pc= -github.com/chavacava/garif v0.1.0/go.mod h1:XMyYCkEL58DF0oyW4qDjjnPWONs2HBqYKI+UIPD+Gww= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -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= -github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4= -github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI= -github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4= -github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gofrs/uuid/v5 v5.2.0 h1:qw1GMx6/y8vhVsx626ImfKMuS5CvJmhIKKtuyvfajMM= github.com/gofrs/uuid/v5 v5.2.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8= @@ -53,27 +42,10 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/ github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= -github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= -github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 h1:zpIH83+oKzcpryru8ceC6BxnoG8TBrhgAvRg8obzup0= -github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg= -github.com/mgechev/revive v1.3.7 h1:502QY0vQGe9KtYJ9FpxMz9rL+Fc/P13CI5POL4uHCcE= -github.com/mgechev/revive v1.3.7/go.mod h1:RJ16jUbF0OWC3co/+XTxmFNgEpUPwnnA0BRllX2aDNA= github.com/miekg/dns v1.1.59 h1:C9EXc/UToRwKLhK5wKU/I4QVsBUc8kE6MkHBkeypWZs= github.com/miekg/dns v1.1.59/go.mod h1:nZpewl5p6IvctfgrckopVx2OlSEHPRO/U4SYkRklrEk= -github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= -github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= -github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= -github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= -github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= @@ -81,19 +53,10 @@ github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= -github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= @@ -113,6 +76,6 @@ golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJ golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/tools/gen_index.sh b/pkg/tools/gen_index.sh index 18770b5..3cff51c 100755 --- a/pkg/tools/gen_index.sh +++ b/pkg/tools/gen_index.sh @@ -5,8 +5,7 @@ set -eu : ${GO:=go} MODULES=$(find * -name go.mod -exec dirname '{}' \;) -GROUPS="pkg cmd" -BASE="$PWD" +GROUPS="" mod() { local d="${1:-.}" diff --git a/pkg/tools/gen_mk.sh b/pkg/tools/gen_mk.sh index 3c034e1..0efcaa1 100755 --- a/pkg/tools/gen_mk.sh +++ b/pkg/tools/gen_mk.sh @@ -7,6 +7,12 @@ INDEX="$1" PROJECTS="$(cut -d':' -f1 "$INDEX")" COMMANDS="tidy get build test up" +TAB=$(printf "\t") + +escape_dir() { + echo "$1" | sed -e 's|/|\\/|g' -e 's|\.|\\.|g' +} + expand() { local prefix="$1" suffix="$2" local x= out= @@ -41,12 +47,6 @@ packed_oneline() { packed | tr '\n' ';' | sed -e 's|;$||' -e 's|then;|then |g' -e 's|;[ \t]*|; |g' } -gen_install_tools() { - cat < Date: Thu, 25 Jul 2024 17:16:02 +0000 Subject: [PATCH 2/6] chore: clear lint warnings Signed-off-by: Alejandro Mery --- cmd/jpictl/gateway.go | 6 +++--- pkg/cluster/ceph_scan.go | 5 +---- pkg/wireguard/config_parser.go | 2 -- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/cmd/jpictl/gateway.go b/cmd/jpictl/gateway.go index d093972..94683e0 100644 --- a/cmd/jpictl/gateway.go +++ b/cmd/jpictl/gateway.go @@ -43,7 +43,7 @@ func gatewaySet(zi cluster.ZoneIterator, gw string) error { zi.ForEachZone(func(z *cluster.Zone) bool { for _, m := range z.Machines { if m.Name == gw { - z.SetGateway(m.ID, true) + _ = z.SetGateway(m.ID, true) return true } } @@ -79,8 +79,8 @@ func gatewayUnset(zi cluster.ZoneIterator, ngw string) error { zi.ForEachZone(func(z *cluster.Zone) bool { for _, m := range z.Machines { if m.Name == ngw && m.IsGateway() { - z.SetGateway(m.ID, false) - m.RemoveWireguardConfig(0) + _ = z.SetGateway(m.ID, false) + _ = m.RemoveWireguardConfig(0) return true } } diff --git a/pkg/cluster/ceph_scan.go b/pkg/cluster/ceph_scan.go index 59e5edd..f9d1d97 100644 --- a/pkg/cluster/ceph_scan.go +++ b/pkg/cluster/ceph_scan.go @@ -73,10 +73,7 @@ func newCephScanTODO(cfg *ceph.Config) *cephScanTODO { func (m *Cluster) scanCephMonitors(opts *ScanOptions) error { cfg, err := m.GetCephConfig() - switch { - case os.IsNotExist(err): - err = nil - case err != nil: + if err != nil && !os.IsNotExist(err) { return err } diff --git a/pkg/wireguard/config_parser.go b/pkg/wireguard/config_parser.go index e07a78e..7a282d3 100644 --- a/pkg/wireguard/config_parser.go +++ b/pkg/wireguard/config_parser.go @@ -8,8 +8,6 @@ import ( "darvaza.org/core" ) -type sectionHandler func(*Config, *basic.Section) error - var sectionMap = map[string]func(*Config, *basic.Section) error{ "Interface": loadInterfaceConfSection, "Peer": loadPeerConfSection, From 0c21d74c15f53259959cb2d4df9215282c71e9a6 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 25 Jul 2024 17:17:57 +0000 Subject: [PATCH 3/6] rings: make unsafe address factories public Signed-off-by: Alejandro Mery --- pkg/rings/encode.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/pkg/rings/encode.go b/pkg/rings/encode.go index 7a083eb..86b7404 100644 --- a/pkg/rings/encode.go +++ b/pkg/rings/encode.go @@ -14,7 +14,7 @@ func RingZeroPrefix(region RegionID, zone ZoneID) (cidr netip.Prefix, err error) case !zone.Valid(): err = ErrOutOfRange(zone, "zone") default: - addr := unsafeRingZeroAddress(region, zone, 0) + addr := UnsafeRingZeroAddress(region, zone, 0) cidr = netip.PrefixFrom(addr, RingZeroBits) } @@ -33,7 +33,7 @@ func RingZeroAddress(region RegionID, zone ZoneID, node NodeID) (addr netip.Addr case !node.ValidZero(): err = ErrOutOfRange(node, "node") default: - addr = unsafeRingZeroAddress(region, zone, node) + addr = UnsafeRingZeroAddress(region, zone, node) } return addr, err @@ -52,7 +52,7 @@ func RingOnePrefix(region RegionID, zone ZoneID) (cidr netip.Prefix, err error) case !zone.Valid(): err = ErrOutOfRange(zone, "zone") default: - addr := unsafeRingOneAddress(region, zone, 0) + addr := UnsafeRingOneAddress(region, zone, 0) cidr = netip.PrefixFrom(addr, RingOneBits) } return cidr, err @@ -71,7 +71,7 @@ func RingOneAddress(region RegionID, zone ZoneID, node NodeID) (addr netip.Addr, case !node.Valid(): err = ErrOutOfRange(node, "node") default: - addr = unsafeRingOneAddress(region, zone, node) + addr = UnsafeRingOneAddress(region, zone, node) } return addr, err } @@ -86,7 +86,7 @@ func RingTwoPrefix(region RegionID) (cidr netip.Prefix, err error) { case !region.Valid(): err = ErrOutOfRange(region, "region") default: - addr := unsafeRingTwoAddress(region, 0) + addr := UnsafeRingTwoAddress(region, 0) cidr = netip.PrefixFrom(addr, RingTwoBits) } return cidr, err @@ -101,20 +101,25 @@ func RingThreePrefix(region RegionID) (subnet netip.Prefix, err error) { case !region.Valid(): err = ErrOutOfRange(region, "region") default: - addr := unsafeRingThreeAddress(region, 0) + addr := UnsafeRingThreeAddress(region, 0) subnet = netip.PrefixFrom(addr, RingThreeBits) } return subnet, err } -func unsafeRingZeroAddress(region RegionID, zone ZoneID, node NodeID) netip.Addr { +// UnsafeRingZeroAddress is equivalent ot RingZeroAddress but without validating +// the input. +func UnsafeRingZeroAddress(region RegionID, zone ZoneID, node NodeID) netip.Addr { r := uint(region) z := uint(zone) n := uint(node) return AddrFrom4(10, 0, r<<4+z, n) } -func unsafeRingOneAddress(region RegionID, zone ZoneID, node NodeID) netip.Addr { + +// UnsafeRingOneAddress is equivalent ot RingOneAddress but without validating +// the input. +func UnsafeRingOneAddress(region RegionID, zone ZoneID, node NodeID) netip.Addr { r := uint(region) z := uint(zone) n := uint(node) @@ -125,7 +130,9 @@ func unsafeRingOneAddress(region RegionID, zone ZoneID, node NodeID) netip.Addr return AddrFrom4(10, r, z<<4+n1, n0) } -func unsafeRingTwoAddress(region RegionID, n uint) netip.Addr { +// UnsafeRingTwoAddress is equivalent ot RingTwoAddress but without validating +// the input. +func UnsafeRingTwoAddress(region RegionID, n uint) netip.Addr { r := uint(region) n1 := n >> 8 @@ -134,7 +141,9 @@ func unsafeRingTwoAddress(region RegionID, n uint) netip.Addr { return AddrFrom4(10, r, n1, n0) } -func unsafeRingThreeAddress(region RegionID, n uint) netip.Addr { +// UnsafeRingThreeAddress is equivalent ot RingThreeAddress but without validating +// the input. +func UnsafeRingThreeAddress(region RegionID, n uint) netip.Addr { r := uint(region) n2 := n >> 16 From 05538e6925638ebf5d91162acf21724a9d538888 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 25 Jul 2024 17:27:28 +0000 Subject: [PATCH 4/6] tools: introduce LazyBuffer abstraction of bytes.Buffer Signed-off-by: Alejandro Mery --- pkg/tools/buffer.go | 72 +++++++++++++++++++++++++++++++++++++++++++++ pkg/tools/tools.go | 5 ++-- 2 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 pkg/tools/buffer.go diff --git a/pkg/tools/buffer.go b/pkg/tools/buffer.go new file mode 100644 index 0000000..b368a36 --- /dev/null +++ b/pkg/tools/buffer.go @@ -0,0 +1,72 @@ +package tools + +import ( + "bytes" + "fmt" + "io" +) + +// LazyBuffer is a [bytes.Buffer] that minimizes counting and error checks. +type LazyBuffer bytes.Buffer + +// Sys returns the underlying [bytes.Buffer]. +func (buf *LazyBuffer) Sys() *bytes.Buffer { + if buf == nil { + return nil + } + return (*bytes.Buffer)(buf) +} + +// Len tells the size in bytes of the currently stored data. +func (buf *LazyBuffer) Len() int { return buf.Sys().Len() } + +// String returns the stored data as string. +func (buf *LazyBuffer) String() string { return buf.Sys().String() } + +// Bytes returns the stored data as a bytes slice. +func (buf *LazyBuffer) Bytes() []byte { return buf.Sys().Bytes() } + +// Write implements the standard io.Writer interface. +func (buf *LazyBuffer) Write(b []byte) (int, error) { return buf.Sys().Write(b) } + +// WriteTo implements the standard WriteTo() interface. +func (buf *LazyBuffer) WriteTo(out io.Writer) (int64, error) { return buf.Sys().WriteTo(out) } + +// Print appends the [fmt.Print] equivalent to the buffer. +func (buf *LazyBuffer) Print(a ...any) error { + _, err := fmt.Fprint(buf.Sys(), a...) + return err +} + +// Println appends the [fmt.Println] equivalent to the buffer. +func (buf *LazyBuffer) Println(a ...any) error { + _, err := fmt.Fprintln(buf.Sys(), a...) + return err +} + +// Printf appends the [fmt.Printf] equivalent to the buffer. +func (buf *LazyBuffer) Printf(format string, a ...any) error { + _, err := fmt.Fprintf(buf.Sys(), format, a...) + return err +} + +// WriteRunes appends the given runes as UTF-8 characters to the buffer. +func (buf *LazyBuffer) WriteRunes(runes ...rune) { + for _, r := range runes { + _, _ = buf.Sys().WriteRune(r) + } +} + +// WriteBytes writes the given byte arrays to the buffer. +func (buf *LazyBuffer) WriteBytes(s ...[]byte) { + for _, b := range s { + _, _ = buf.Sys().Write(b) + } +} + +// WriteStrings writes the given strings as UTF-8 to the buffer. +func (buf *LazyBuffer) WriteStrings(strings ...string) { + for _, s := range strings { + _, _ = buf.Sys().WriteString(s) + } +} diff --git a/pkg/tools/tools.go b/pkg/tools/tools.go index f6747e9..172f24f 100644 --- a/pkg/tools/tools.go +++ b/pkg/tools/tools.go @@ -1,3 +1,2 @@ -//go:build tools - -package build +// Package tools contains helpers +package tools From f0cef6c19f661207f8042a266c4deb456f9f74dd Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 25 Jul 2024 17:28:56 +0000 Subject: [PATCH 5/6] tools: introduce LazyClose() Signed-off-by: Alejandro Mery --- pkg/tools/tools.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/tools/tools.go b/pkg/tools/tools.go index 172f24f..a4f4ab2 100644 --- a/pkg/tools/tools.go +++ b/pkg/tools/tools.go @@ -1,2 +1,11 @@ // Package tools contains helpers package tools + +import "io" + +// LazyClose closes an [io.Closer] and discards the error +func LazyClose(p io.Closer) { + if p != nil { + _ = p.Close() + } +} From c20bd3d41f24bcdf7cdec4cbac87cc81bed33c04 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 25 Jul 2024 17:36:59 +0000 Subject: [PATCH 6/6] jpictl: introduce initial `jpictl list` to see the different networks and addresses currently limited to rings zero and one. Signed-off-by: Alejandro Mery --- cmd/jpictl/list.go | 200 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 cmd/jpictl/list.go diff --git a/cmd/jpictl/list.go b/cmd/jpictl/list.go new file mode 100644 index 0000000..c024329 --- /dev/null +++ b/cmd/jpictl/list.go @@ -0,0 +1,200 @@ +package main + +import ( + "bytes" + "io" + "net/netip" + "os" + + "darvaza.org/core" + "github.com/spf13/cobra" + + "git.jpi.io/amery/jpictl/pkg/cluster" + "git.jpi.io/amery/jpictl/pkg/rings" + "git.jpi.io/amery/jpictl/pkg/tools" +) + +type inventory struct { + r []*cluster.Region + z [][]*cluster.Zone +} + +func (g *inventory) renderRingZero(out *tools.LazyBuffer) error { + ring0 := netip.PrefixFrom(rings.UnsafeRingZeroAddress(0, 0, 0), rings.RingZeroBits) + from, to, _ := rings.PrefixToRange(ring0) + + _ = out.Printf("; wg%v\n", 0) + _ = out.Printf("%s\t%s-%s\n", ring0, from, to) + + if err := g.renderRingZeroRegions(out); err != nil { + return err + } + + return g.renderRingZeroZones(out) +} + +func (g *inventory) renderRingZeroRegions(out *tools.LazyBuffer) error { + for _, r := range g.r { + if err := g.renderRingZeroRegion(out, r); err != nil { + return err + } + } + return nil +} + +func (*inventory) renderRingZeroRegion(out *tools.LazyBuffer, r *cluster.Region) error { + addr := rings.UnsafeRingZeroAddress(r.ID, 0, 0) + ring0r := netip.PrefixFrom(addr, rings.RingZeroBits+4) + from, to, _ := rings.PrefixToRange(ring0r) + + _ = out.Printf("%s\t%s-%s\t# %s\n", ring0r, from, to, r.Name) + return nil +} + +func (g *inventory) renderRingZeroZones(out *tools.LazyBuffer) error { + for i, r := range g.r { + for _, z := range g.z[i] { + if err := g.renderRingZeroZone(out, r, z); err != nil { + return err + } + } + } + + return nil +} +func (*inventory) renderRingZeroZone(out *tools.LazyBuffer, r *cluster.Region, z *cluster.Zone) error { + addr := rings.UnsafeRingZeroAddress(r.ID, z.ID, 0) + ring0rz := netip.PrefixFrom(addr, rings.RingZeroBits+4+4) + from, to, _ := rings.PrefixToRange(ring0rz) + + _ = out.Printf("; wg%v: %s (%s)\n", 0, z.Name, r.Name) + _ = out.Printf("%s\t%s-%s\t%s\n", ring0rz, from, to, z.Name) + + z.ForEachMachine(func(m *cluster.Machine) bool { + if m.IsGateway() { + addr, _ := m.RingZeroAddress() + cidr := netip.PrefixFrom(addr, 32) + _ = out.Printf("%s\t\t%s-%v\n", cidr, m.Name, 0) + } + + return false + }) + + return nil +} + +func (g *inventory) renderRingOne(out *tools.LazyBuffer) error { + for i, r := range g.r { + for _, z := range g.z[i] { + if err := g.renderRingOneZone(out, r, z); err != nil { + return err + } + } + } + return nil +} + +func (*inventory) renderRingOneZone(out *tools.LazyBuffer, r *cluster.Region, z *cluster.Zone) error { + ring1, err := rings.RingOnePrefix(r.ID, z.ID) + if err != nil { + return err + } + + from, to, _ := rings.PrefixToRange(ring1) + + _ = out.Printf("; wg%v: %s (%s)\n", 1, z.Name, r.Name) + _ = out.Printf("%s\t%s-%s\t%s\n", ring1, from, to, z.Name) + + z.ForEachMachine(func(m *cluster.Machine) bool { + addr := m.RingOneAddress() + cidr := netip.PrefixFrom(addr, 32) + _ = out.Printf("%s\t\t%s-%v\n", cidr, m.Name, 1) + return false + }) + return nil +} + +func (g *inventory) Marshal() ([]byte, error) { + var buf tools.LazyBuffer + + if err := g.renderRingZero(&buf); err != nil { + return nil, err + } + + if err := g.renderRingOne(&buf); err != nil { + return nil, err + } + + return buf.Bytes(), nil +} + +func (g *inventory) WriteTo(out io.Writer) (int64, error) { + b, err := g.Marshal() + if err != nil { + return 0, err + } + + buf := bytes.NewBuffer(b) + return buf.WriteTo(out) +} + +func genInventory(m *cluster.Cluster) (*inventory, error) { + g := new(inventory) + + g.populateRegions(m) + g.populateZones() + + return g, nil +} + +func (g *inventory) populateRegions(m *cluster.Cluster) { + m.ForEachRegion(func(r *cluster.Region) bool { + if r.IsPrimary() { + g.r = append(g.r, r) + } + return false + }) + + core.SliceSortFn(g.r, func(a, b *cluster.Region) bool { + return a.ID < b.ID + }) +} + +func (g *inventory) populateZones() { + g.z = make([][]*cluster.Zone, len(g.r)) + for i, r := range g.r { + r.ForEachZone(func(z *cluster.Zone) bool { + g.z[i] = append(g.z[i], z) + return false + }) + + core.SliceSortFn(g.z[i], func(a, b *cluster.Zone) bool { + return a.ID < b.ID + }) + } +} + +// Command +var listCmd = &cobra.Command{ + Use: "list", + Short: "list shows the IP/CIDR inventory", + PreRun: setVerbosity, + RunE: func(_ *cobra.Command, _ []string) error { + m, err := cfg.LoadZones(false) + if err != nil { + return err + } + + out, err := genInventory(m) + if err != nil { + return err + } + + _, err = out.WriteTo(os.Stdout) + return err + }, +} + +func init() { + rootCmd.AddCommand(listCmd) +}