Skip to content

Commit

Permalink
client: add version information to user agent
Browse files Browse the repository at this point in the history
Add version information to all rest clients, so that we can distinguish
the originator of calls in server logs.
  • Loading branch information
WanzenBug committed Jun 11, 2024
1 parent dbc2d90 commit fc34f08
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 48 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ linstor-gateway: $(GOSOURCES) version.env
[ -n "$(GOOS)" ] && NAME="$${NAME}-$(GOOS)"; \
[ -n "$(GOARCH)" ] && NAME="$${NAME}-$(GOARCH)"; \
go build -o "$$NAME" \
-ldflags "-X github.com/LINBIT/linstor-gateway/cmd.version=$${VERSION} \
-X 'github.com/LINBIT/linstor-gateway/cmd.builddate=$(shell LC_ALL=C date --utc)' \
-X github.com/LINBIT/linstor-gateway/cmd.githash=$${GITHASH}"
-ldflags "-X github.com/LINBIT/linstor-gateway/pkg/version.Version=$${VERSION} \
-X 'github.com/LINBIT/linstor-gateway/pkg/version.BuildDate=$(shell LC_ALL=C date --utc)' \
-X github.com/LINBIT/linstor-gateway/pkg/version.GitCommit=$${GITHASH}"

.PHONY: install
install:
Expand Down
10 changes: 8 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/LINBIT/linstor-gateway/pkg/rest"
"github.com/moul/http2curl"
"io"
"log"
"net/http"
"net/url"
"os"

"github.com/moul/http2curl"

"github.com/LINBIT/linstor-gateway/pkg/rest"
)

type Client struct {
httpClient *http.Client
baseURL *url.URL
log interface{} // must be either Logger, TestLogger, or LeveledLogger
userAgent string

Iscsi *ISCSIService
Nfs *NFSService
Expand Down Expand Up @@ -131,6 +134,9 @@ func (c *Client) newRequest(method, path string, body interface{}) (*http.Reques
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
if c.userAgent != "" {
req.Header.Set("User-Agent", c.userAgent)
}
req.Header.Set("Accept", "application/json")

return req, nil
Expand Down
6 changes: 2 additions & 4 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package client

import (
"errors"
"net/http"
"net/url"
)

Expand All @@ -17,10 +16,9 @@ func BaseURL(URL *url.URL) Option {
}
}

// HTTPClient is a Client's option to set a specific http.Client.
func HTTPClient(httpClient *http.Client) Option {
func UserAgent(userAgent string) Option {
return func(c *Client) error {
c.httpClient = httpClient
c.userAgent = userAgent
return nil
}
}
Expand Down
16 changes: 10 additions & 6 deletions cmd/iscsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,33 @@ package cmd
import (
"context"
"fmt"
"os"
"strconv"
"strings"

"github.com/LINBIT/linstor-gateway/pkg/linstorcontrol"
"github.com/LINBIT/linstor-gateway/pkg/prompt"
"github.com/LINBIT/linstor-gateway/pkg/upgrade"
"github.com/LINBIT/linstor-gateway/pkg/version"

"github.com/fatih/color"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"os"
"strconv"
"strings"

"github.com/LINBIT/linstor-gateway/pkg/common"
"github.com/LINBIT/linstor-gateway/pkg/iscsi"
"github.com/olekukonko/tablewriter"
"github.com/rck/unit"
"github.com/spf13/cobra"

"github.com/LINBIT/linstor-gateway/pkg/common"
"github.com/LINBIT/linstor-gateway/pkg/iscsi"
)

var bold = color.New(color.Bold).SprintfFunc()

func iscsiCommands() *cobra.Command {
var rootCmd = &cobra.Command{
Use: "iscsi",
Version: version,
Version: version.Version,
Short: "Manages Highly-Available iSCSI targets",
Long: `linstor-gateway iscsi manages highly available iSCSI targets by leveraging
LINSTOR and drbd-reactor. Setting up LINSTOR, including storage pools and resource groups,
Expand Down
11 changes: 7 additions & 4 deletions cmd/nfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,29 @@ package cmd
import (
"context"
"fmt"
"net"
"os"
"path/filepath"

"github.com/LINBIT/linstor-gateway/pkg/common"
"github.com/LINBIT/linstor-gateway/pkg/linstorcontrol"
"github.com/LINBIT/linstor-gateway/pkg/nfs"
"github.com/LINBIT/linstor-gateway/pkg/prompt"
"github.com/LINBIT/linstor-gateway/pkg/upgrade"
"github.com/LINBIT/linstor-gateway/pkg/version"

"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
"github.com/rck/unit"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"net"
"os"
"path/filepath"
)

func nfsCommands() *cobra.Command {
var rootCmd = &cobra.Command{
Use: "nfs",
Version: version,
Version: version.Version,
Short: "Manages Highly-Available NFS exports",
Long: `linstor-gateway nfs manages highly available NFS exports by leveraging LINSTOR
and drbd-reactor. A running LINSTOR cluster including storage pools and resource groups
Expand Down
11 changes: 7 additions & 4 deletions cmd/nvme.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ package cmd
import (
"context"
"fmt"
"os"
"strconv"
"strings"

"github.com/LINBIT/linstor-gateway/client"
"github.com/LINBIT/linstor-gateway/pkg/linstorcontrol"
"github.com/LINBIT/linstor-gateway/pkg/prompt"
"github.com/LINBIT/linstor-gateway/pkg/upgrade"
"github.com/LINBIT/linstor-gateway/pkg/version"

"github.com/fatih/color"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"os"
"strconv"
"strings"

"github.com/olekukonko/tablewriter"
"github.com/rck/unit"
Expand All @@ -25,7 +28,7 @@ import (
func nvmeCommands() *cobra.Command {
var rootCmd = &cobra.Command{
Use: "nvme",
Version: version,
Version: version.Version,
Short: "Manages Highly-Available NVME targets",
Long: `nvme manages highly available NVME targets by leveraging LINSTOR and DRBD.`,
Args: cobra.NoArgs,
Expand Down
19 changes: 9 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@ package cmd

import (
"fmt"
"github.com/LINBIT/linstor-gateway/client"
"net/url"
"os"
"strconv"
"strings"

"github.com/LINBIT/linstor-gateway/client"
"github.com/LINBIT/linstor-gateway/pkg/version"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// (potentially) injected by makefile
var (
version string
builddate string
githash string
)

var (
cfgFile string
loglevel string
Expand Down Expand Up @@ -80,7 +75,7 @@ func rootCommand() *cobra.Command {

rootCmd := &cobra.Command{
Use: "linstor-gateway",
Version: version,
Version: version.Version,
Short: "Manage linstor-gateway targets and exports",
Args: cobra.NoArgs,
SilenceUsage: true,
Expand All @@ -96,7 +91,11 @@ func rootCommand() *cobra.Command {
if err != nil {
return err
}
cli, err = client.NewClient(client.BaseURL(base), client.Log(log.StandardLogger()))
cli, err = client.NewClient(
client.BaseURL(base),
client.Log(log.StandardLogger()),
client.UserAgent(version.UserAgent()),
)
if err != nil {
return fmt.Errorf("failed to connect to LINSTOR Gateway server: %w", err)
}
Expand Down
18 changes: 5 additions & 13 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,18 @@ import (
"fmt"

"github.com/spf13/cobra"

"github.com/LINBIT/linstor-gateway/pkg/version"
)

func versionCommand() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print version information of LINSTOR Gateway",
Run: func(cmd *cobra.Command, args []string) {
if version == "" {
version = "DEV"
}
if builddate == "" {
builddate = "DEV"
}
if githash == "" {
githash = "DEV"
}
fmt.Printf("LINSTOR Gateway version %s\n", version)
fmt.Printf("Built at %s\n", builddate)
fmt.Printf("Version control hash: %s\n", githash)

fmt.Printf("LINSTOR Gateway version %s\n", version.Version)
fmt.Printf("Built at %s\n", version.BuildDate)
fmt.Printf("Version control hash: %s\n", version.GitCommit)
},
}
}
10 changes: 8 additions & 2 deletions pkg/linstorcontrol/linstorcontrol.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import (
"context"
"errors"
"fmt"
"github.com/icza/gog"
"sort"

"github.com/icza/gog"

apiconsts "github.com/LINBIT/golinstor"
"github.com/LINBIT/golinstor/client"
log "github.com/sirupsen/logrus"

"github.com/LINBIT/linstor-gateway/pkg/common"
"github.com/LINBIT/linstor-gateway/pkg/version"
)

// Linstor is a struct containing the configuration that is needed to create or delete a LINSTOR resource.
Expand Down Expand Up @@ -125,7 +127,11 @@ func StatusFromResources(serviceCfgPath string, definition *client.ResourceDefin
}

func Default(controllers []string) (*Linstor, error) {
cli, err := client.NewClient(client.Log(log.StandardLogger()), client.Controllers(controllers))
cli, err := client.NewClient(
client.Log(log.StandardLogger()),
client.Controllers(controllers),
client.UserAgent(version.UserAgent()),
)
if err != nil {
return nil, err
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package version

import "fmt"

// (potentially) set by makefile
var (
Version = "unknown"
GitCommit = "unknown"
BuildDate = "unknown"
)

func UserAgent() string {
return fmt.Sprintf("linstor-gateway/%s-g%s", Version, GitCommit)
}

0 comments on commit fc34f08

Please sign in to comment.