Skip to content

Commit

Permalink
amtool: Detect version drift and warn users (#2672)
Browse files Browse the repository at this point in the history
* amtool: Detect version drift and warn users

This change detects the alertmanager version when initiating the client.
It ignores most errors since I expect amtool to fail later.

If amtool is not compiled with proper version, we do not do anything
either.

We use MajorMinor for now as we have not reach 1.0, but we still allow
the bugfix version number (Z in x.y.Z) to differ.

Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>

* Add version check

Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
  • Loading branch information
roidelapluie authored Aug 9, 2021
1 parent c72c4d7 commit f684896
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
22 changes: 21 additions & 1 deletion cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package cli

import (
"crypto/tls"
"fmt"
"net/http"
"net/url"
"os"
Expand All @@ -23,6 +24,7 @@ import (

"github.com/go-openapi/strfmt"
"github.com/prometheus/common/version"
"golang.org/x/mod/semver"
kingpin "gopkg.in/alecthomas/kingpin.v2"

"github.com/prometheus/alertmanager/api/v2/client"
Expand All @@ -38,6 +40,7 @@ var (
output string
timeout time.Duration
tlsInsecureSkipVerify bool
versionCheck bool

configFiles = []string{os.ExpandEnv("$HOME/.config/amtool/config.yml"), "/etc/amtool/config.yml"}
legacyFlags = map[string]string{"comment_required": "require-comment"}
Expand Down Expand Up @@ -93,7 +96,23 @@ func NewAlertmanagerClient(amURL *url.URL) *client.Alertmanager {
cr.DefaultAuthentication = clientruntime.BasicAuth(amURL.User.Username(), password)
}

return client.New(cr, strfmt.Default)
c := client.New(cr, strfmt.Default)

if !versionCheck {
return c
}

status, err := c.General.GetStatus(nil)
if err != nil || status.Payload.VersionInfo == nil || version.Version == "" {
// We can not get version info, or we do not know our own version. Let amtool continue.
return c
}

if semver.MajorMinor("v"+*status.Payload.VersionInfo.Version) != semver.MajorMinor("v"+version.Version) {
fmt.Fprintf(os.Stderr, "Warning: amtool version (%s) and alertmanager version (%s) are different.\n", version.Version, *status.Payload.VersionInfo.Version)
}

return c
}

// Execute is the main function for the amtool command
Expand All @@ -109,6 +128,7 @@ func Execute() {
app.Flag("output", "Output formatter (simple, extended, json)").Short('o').Default("simple").EnumVar(&output, "simple", "extended", "json")
app.Flag("timeout", "Timeout for the executed command").Default("30s").DurationVar(&timeout)
app.Flag("tls.insecure.skip.verify", "Skip TLS certificate verification").BoolVar(&tlsInsecureSkipVerify)
app.Flag("version-check", "Check alertmanager version. Use --no-version-check to disable.").Default("true").BoolVar(&versionCheck)

app.Version(version.Print("amtool"))
app.GetFlag("help").Short('h')
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/stretchr/testify v1.7.0
github.com/xlab/treeprint v1.1.0
go.uber.org/atomic v1.9.0
golang.org/x/mod v0.4.2
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985
golang.org/x/tools v0.1.5
gopkg.in/alecthomas/kingpin.v2 v2.2.6
Expand Down

0 comments on commit f684896

Please sign in to comment.