From e622f1987b5ad122e0b6dac1e3091f6b0a56cb96 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 20 May 2021 12:52:58 +0300 Subject: [PATCH 1/3] Makefile: deduplicate binaries Signed-off-by: Roman Khimov --- Makefile | 23 ++++++----------------- cmd/{gate => s3-gw}/app-healthy.go | 0 cmd/{gate => s3-gw}/app-metrics.go | 0 cmd/{gate => s3-gw}/app-profiler.go | 0 cmd/{gate => s3-gw}/app-settings.go | 0 cmd/{gate => s3-gw}/app.go | 0 cmd/{gate => s3-gw}/app_router.go | 0 cmd/{gate => s3-gw}/main.go | 0 8 files changed, 6 insertions(+), 17 deletions(-) rename cmd/{gate => s3-gw}/app-healthy.go (100%) rename cmd/{gate => s3-gw}/app-metrics.go (100%) rename cmd/{gate => s3-gw}/app-profiler.go (100%) rename cmd/{gate => s3-gw}/app-settings.go (100%) rename cmd/{gate => s3-gw}/app.go (100%) rename cmd/{gate => s3-gw}/app_router.go (100%) rename cmd/{gate => s3-gw}/main.go (100%) diff --git a/Makefile b/Makefile index 1c8b863..aaad91c 100644 --- a/Makefile +++ b/Makefile @@ -5,36 +5,25 @@ REPO ?= $(shell go list -m) VERSION ?= "$(shell git describe --tags 2>/dev/null || git rev-parse --short HEAD | sed 's/^v//')" BINDIR = bin -# s3 gw variables -BIN_NAME=neofs-s3-gw -BIN = "$(BINDIR)/$(BIN_NAME)" +# Binaries to build +CMDS = $(addprefix neofs-, $(notdir $(wildcard cmd/*))) +BINS = $(addprefix $(BINDIR)/, $(CMDS)) # Variables for docker HUB_IMAGE ?= "nspccdev/$(BIN_NAME)" HUB_TAG ?= "$(shell echo ${VERSION} | sed 's/^v//')" -# Authmate variables -AUTHMATE_BIN_NAME=neofs-authmate -AUTHMATE_BIN = "$(BINDIR)/$(AUTHMATE_BIN_NAME)" - .PHONY: help all dep clean format test cover lint docker/lint image-push image dirty-image # Make all binaries -all: $(BIN) $(AUTHMATE_BIN) +all: $(BINS) -$(BIN): $(BINDIR) dep +$(BINS): $(BINDIR) dep @echo "⇒ Build $@" CGO_ENABLED=0 \ go build -v -trimpath \ -ldflags "-X main.Version=$(VERSION)" \ - -o $@ ./cmd/gate - -$(AUTHMATE_BIN): $(BINDIR) dep - @echo "⇒ Build $@" - CGO_ENABLED=0 \ - go build -v -trimpath \ - -ldflags "-X main.Version=$(VERSION)" \ - -o $@ ./cmd/authmate + -o $@ ./cmd/$(subst neofs-,,$(notdir $@)) $(BINDIR): @echo "⇒ Ensure dir: $@" diff --git a/cmd/gate/app-healthy.go b/cmd/s3-gw/app-healthy.go similarity index 100% rename from cmd/gate/app-healthy.go rename to cmd/s3-gw/app-healthy.go diff --git a/cmd/gate/app-metrics.go b/cmd/s3-gw/app-metrics.go similarity index 100% rename from cmd/gate/app-metrics.go rename to cmd/s3-gw/app-metrics.go diff --git a/cmd/gate/app-profiler.go b/cmd/s3-gw/app-profiler.go similarity index 100% rename from cmd/gate/app-profiler.go rename to cmd/s3-gw/app-profiler.go diff --git a/cmd/gate/app-settings.go b/cmd/s3-gw/app-settings.go similarity index 100% rename from cmd/gate/app-settings.go rename to cmd/s3-gw/app-settings.go diff --git a/cmd/gate/app.go b/cmd/s3-gw/app.go similarity index 100% rename from cmd/gate/app.go rename to cmd/s3-gw/app.go diff --git a/cmd/gate/app_router.go b/cmd/s3-gw/app_router.go similarity index 100% rename from cmd/gate/app_router.go rename to cmd/s3-gw/app_router.go diff --git a/cmd/gate/main.go b/cmd/s3-gw/main.go similarity index 100% rename from cmd/gate/main.go rename to cmd/s3-gw/main.go From ef8684c11d2c09c2a4b33418fd7fde62c8d2875d Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 20 May 2021 13:01:12 +0300 Subject: [PATCH 2/3] s3-gw: drop build time setting It's not properly set anyway and it's a bad practice for build reproducibility. Signed-off-by: Roman Khimov --- api/metrics/collector.go | 4 +--- cmd/s3-gw/app-settings.go | 5 ++--- misc/build.go | 2 -- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/api/metrics/collector.go b/api/metrics/collector.go index c6f89c3..c7a7d25 100644 --- a/api/metrics/collector.go +++ b/api/metrics/collector.go @@ -19,8 +19,6 @@ var ( []string{ // current version "version", - // build time of the current version - "build_time", }, ) @@ -62,7 +60,7 @@ func (s *stats) Describe(ch chan<- *prometheus.Desc) { func (s *stats) Collect(ch chan<- prometheus.Metric) { // Expose current version information - versionInfo.WithLabelValues(misc.Version, misc.Build).Set(1.0) + versionInfo.WithLabelValues(misc.Version).Set(1.0) // connect collectors collectHTTPMetrics(ch) diff --git a/cmd/s3-gw/app-settings.go b/cmd/s3-gw/app-settings.go index 87f9538..57f7a5d 100644 --- a/cmd/s3-gw/app-settings.go +++ b/cmd/s3-gw/app-settings.go @@ -181,7 +181,6 @@ func newSettings() *viper.Viper { // set prefers: v.Set(cfgApplicationName, misc.ApplicationName) v.Set(cfgApplicationVersion, misc.Version) - v.Set(cfgApplicationBuildTime, misc.Build) // set defaults: @@ -229,7 +228,7 @@ func newSettings() *viper.Viper { switch { case help != nil && *help: - fmt.Printf("NeoFS S3 Gateway %s (%s)\n", misc.Version, misc.Build) + fmt.Printf("NeoFS S3 Gateway %s\n", misc.Version) flags.PrintDefaults() fmt.Println() @@ -256,7 +255,7 @@ func newSettings() *viper.Viper { os.Exit(0) case version != nil && *version: - fmt.Printf("NeoFS S3 Gateway %s (%s)\n", misc.Version, misc.Build) + fmt.Printf("NeoFS S3 Gateway %s\n", misc.Version) os.Exit(0) case ttl != nil && ttl.Minutes() < minimumTTLInMinutes: fmt.Printf("connection ttl should not be less than %s", defaultTTL) diff --git a/misc/build.go b/misc/build.go index a66717c..867aff9 100644 --- a/misc/build.go +++ b/misc/build.go @@ -9,8 +9,6 @@ const ( ) var ( - // Build holds build timestamp. - Build = "now" // Version contains application version. Version = "dev" ) From 92c8cce93369a1ef108f3ad8b25f659c6824f5a0 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 20 May 2021 15:41:32 +0300 Subject: [PATCH 3/3] misc: move to internal/version, use for all binaries No libmisc, please. Signed-off-by: Roman Khimov --- Makefile | 2 +- api/metrics/collector.go | 4 ++-- api/response.go | 4 ++-- cmd/authmate/main.go | 8 ++------ cmd/s3-gw/app-settings.go | 28 +++++++++++++++++----------- internal/version/version.go | 9 +++++++++ misc/build.go | 14 -------------- 7 files changed, 33 insertions(+), 36 deletions(-) create mode 100644 internal/version/version.go delete mode 100644 misc/build.go diff --git a/Makefile b/Makefile index aaad91c..1c73d75 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ $(BINS): $(BINDIR) dep @echo "⇒ Build $@" CGO_ENABLED=0 \ go build -v -trimpath \ - -ldflags "-X main.Version=$(VERSION)" \ + -ldflags "-X $(REPO)/internal/version.Version=$(VERSION)" \ -o $@ ./cmd/$(subst neofs-,,$(notdir $@)) $(BINDIR): diff --git a/api/metrics/collector.go b/api/metrics/collector.go index c7a7d25..f8dfe00 100644 --- a/api/metrics/collector.go +++ b/api/metrics/collector.go @@ -1,7 +1,7 @@ package metrics import ( - "github.com/nspcc-dev/neofs-s3-gw/misc" + "github.com/nspcc-dev/neofs-s3-gw/internal/version" "github.com/prometheus/client_golang/prometheus" ) @@ -60,7 +60,7 @@ func (s *stats) Describe(ch chan<- *prometheus.Desc) { func (s *stats) Collect(ch chan<- prometheus.Metric) { // Expose current version information - versionInfo.WithLabelValues(misc.Version).Set(1.0) + versionInfo.WithLabelValues(version.Version).Set(1.0) // connect collectors collectHTTPMetrics(ch) diff --git a/api/response.go b/api/response.go index da4b05f..88b961d 100644 --- a/api/response.go +++ b/api/response.go @@ -10,7 +10,7 @@ import ( "strconv" "github.com/google/uuid" - "github.com/nspcc-dev/neofs-s3-gw/misc" + "github.com/nspcc-dev/neofs-s3-gw/internal/version" ) type ( @@ -153,7 +153,7 @@ func errorResponseHandler(w http.ResponseWriter, r *http.Request) { // Write http common headers. func setCommonHeaders(w http.ResponseWriter) { - w.Header().Set(hdrServerInfo, "NeoFS-S3-GW/"+misc.Version) + w.Header().Set(hdrServerInfo, version.Server) w.Header().Set(hdrAcceptRanges, "bytes") // Remove sensitive information diff --git a/cmd/authmate/main.go b/cmd/authmate/main.go index 9adc3b7..af0a526 100644 --- a/cmd/authmate/main.go +++ b/cmd/authmate/main.go @@ -15,6 +15,7 @@ import ( "github.com/nspcc-dev/cdn-sdk/pool" "github.com/nspcc-dev/neofs-api-go/pkg/container" "github.com/nspcc-dev/neofs-s3-gw/authmate" + "github.com/nspcc-dev/neofs-s3-gw/internal/version" "github.com/urfave/cli/v2" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -30,11 +31,6 @@ const ( poolRequestTimeout = 5 * time.Second ) -var ( - // Version of the program. - Version = "dev" -) - var ( neoFSKeyPathFlag string peerAddressFlag string @@ -89,7 +85,7 @@ func main() { app := &cli.App{ Name: "NeoFS gate authentication manager", Usage: "Helps manage delegated access via gates to data stored in NeoFS network", - Version: Version, + Version: version.Version, Flags: appFlags(), Commands: appCommands(), } diff --git a/cmd/s3-gw/app-settings.go b/cmd/s3-gw/app-settings.go index 57f7a5d..0ab9638 100644 --- a/cmd/s3-gw/app-settings.go +++ b/cmd/s3-gw/app-settings.go @@ -9,7 +9,7 @@ import ( "strings" "time" - "github.com/nspcc-dev/neofs-s3-gw/misc" + "github.com/nspcc-dev/neofs-s3-gw/internal/version" "github.com/spf13/pflag" "github.com/spf13/viper" "go.uber.org/zap" @@ -87,6 +87,12 @@ const ( // Settings. // Command line args. cmdHelp = "help" cmdVersion = "version" + + // applicationName is gateway name. + applicationName = "neofs-s3-gw" + + // envPrefix is environment variables prefix used for configuration. + envPrefix = "S3_GW" ) type empty int @@ -145,7 +151,7 @@ func newSettings() *viper.Viper { v := viper.New() v.AutomaticEnv() - v.SetEnvPrefix(misc.Prefix) + v.SetEnvPrefix(envPrefix) v.SetConfigType("yaml") v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) @@ -158,7 +164,7 @@ func newSettings() *viper.Viper { flags.Bool(cfgEnableMetrics, false, "enable prometheus metrics") help := flags.BoolP(cmdHelp, "h", false, "show help") - version := flags.BoolP(cmdVersion, "v", false, "show version") + versionFlag := flags.BoolP(cmdVersion, "v", false, "show version") flags.String(cfgNeoFSPrivateKey, "", "set value to hex string, WIF string, or path to NeoFS private key file") flags.String(cfgGateAuthPrivateKey, "", "set path to file with auth (curve25519) private key to use in auth scheme") @@ -179,8 +185,8 @@ func newSettings() *viper.Viper { domains := flags.StringArrayP(cfgListenDomains, "d", nil, "set domains to be listened") // set prefers: - v.Set(cfgApplicationName, misc.ApplicationName) - v.Set(cfgApplicationVersion, misc.Version) + v.Set(cfgApplicationName, applicationName) + v.Set(cfgApplicationVersion, version.Version) // set defaults: @@ -228,7 +234,7 @@ func newSettings() *viper.Viper { switch { case help != nil && *help: - fmt.Printf("NeoFS S3 Gateway %s\n", misc.Version) + fmt.Printf("NeoFS S3 gateway %s\n", version.Version) flags.PrintDefaults() fmt.Println() @@ -243,19 +249,19 @@ func newSettings() *viper.Viper { } k := strings.Replace(keys[i], ".", "_", -1) - fmt.Printf("%s_%s = %v\n", misc.Prefix, strings.ToUpper(k), v.Get(keys[i])) + fmt.Printf("%s_%s = %v\n", envPrefix, strings.ToUpper(k), v.Get(keys[i])) } fmt.Println() fmt.Println("Peers preset:") fmt.Println() - fmt.Printf("%s_%s_[N]_ADDRESS = string\n", misc.Prefix, strings.ToUpper(cfgPeers)) - fmt.Printf("%s_%s_[N]_WEIGHT = 0..1 (float)\n", misc.Prefix, strings.ToUpper(cfgPeers)) + fmt.Printf("%s_%s_[N]_ADDRESS = string\n", envPrefix, strings.ToUpper(cfgPeers)) + fmt.Printf("%s_%s_[N]_WEIGHT = 0..1 (float)\n", envPrefix, strings.ToUpper(cfgPeers)) os.Exit(0) - case version != nil && *version: - fmt.Printf("NeoFS S3 Gateway %s\n", misc.Version) + case versionFlag != nil && *versionFlag: + fmt.Printf("NeoFS S3 gateway %s\n", version.Version) os.Exit(0) case ttl != nil && ttl.Minutes() < minimumTTLInMinutes: fmt.Printf("connection ttl should not be less than %s", defaultTTL) diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 0000000..6f3d47a --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,9 @@ +package version + +var ( + // Version contains application version. + Version = "dev" + + // Server contains server identification string. + Server = "NeoFS-S3-GW/" + Version +) diff --git a/misc/build.go b/misc/build.go deleted file mode 100644 index 867aff9..0000000 --- a/misc/build.go +++ /dev/null @@ -1,14 +0,0 @@ -package misc - -const ( - // ApplicationName is gateway name. - ApplicationName = "neofs-s3-gw" - - // Prefix is configuration environment variables prefix. - Prefix = "S3_GW" -) - -var ( - // Version contains application version. - Version = "dev" -)