Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

amtool: Detect version drift and warn users #2672

Merged
merged 2 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 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 Down Expand Up @@ -93,7 +95,19 @@ 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)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this is now executed for every command, can we an option to skip it?

Something like:

var skipVersionCheck bool

app.Flag("skip.version-check", "Skip version checks when executing commands.").BoolVar(&skipVersionCheck)

// Then, we can do something like
if skipVersionCheck {
	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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we have an option to cancel the check, I don't think we should fail silently. How do you feel about logging that we were unable to compute the version and tell the user that should upgrade/downgrade?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can not run the check, it is because alertmanager does not work at all, and the error will be printed in the actual call later on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking more in the case of Grafana but this is not a concern for us here to be honest.

return c
}

if semver.MajorMinor("v"+*status.Payload.VersionInfo.Version) != semver.MajorMinor("v"+version.Version) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite nice 🎉

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 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