Skip to content

Commit

Permalink
Add module versions to /version info
Browse files Browse the repository at this point in the history
This commit adds module version info to the /version endpoint. Due to an
open Go bug, the commit is not testable yet. A unit test has been added
anyways, with a canary that should die when Go fixes the bug on their
end.

Signed-off-by: Eric Chlebek <eric@sensu.io>
  • Loading branch information
echlebek committed Jan 25, 2022
1 parent 7c66d37 commit b2f11e3
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Versioning](http://semver.org/spec/v2.0.0.html).

### Added
- Added metric threshold service checks.
- Added API group version information to the /version endpoint.

### Changed
- Agent now persists prometheus HELP messages as a metric tag.
Expand Down
1 change: 1 addition & 0 deletions api/core/v2/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type Version struct {
Etcd *etcdVersion.Versions `json:"etcd"`
SensuBackend string `json:"sensu_backend"`
APIGroups map[string]string `json:"api_groups"`
}

// FixtureVersion returns a Version fixture for testing.
Expand Down
2 changes: 2 additions & 0 deletions backend/apid/actions/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package actions

import (
corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/sensu/sensu-go/types"
"github.com/sensu/sensu-go/version"
etcdVersion "go.etcd.io/etcd/api/v3/version"
"golang.org/x/net/context"
Expand All @@ -27,5 +28,6 @@ func (v VersionController) GetVersion(ctx context.Context) *corev2.Version {
Cluster: v.clusterVersion,
},
SensuBackend: version.Semver(),
APIGroups: types.APIModuleVersions(),
}
}
41 changes: 41 additions & 0 deletions types/versions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package types

import (
"path"
"runtime/debug"
"strings"
"sync"
)

var (
apiModuleVersions = map[string]string{}
apiModuleVersionsOnce sync.Once
)

// APIModuleVersions returns a map of Sensu API modules that are compiled into
// the product.
func APIModuleVersions() map[string]string {
apiModuleVersionsOnce.Do(loadAPIModuleVersions)
mapCopy := make(map[string]string, len(apiModuleVersions))
for k, v := range apiModuleVersions {
mapCopy[k] = v
}
return mapCopy
}

func loadAPIModuleVersions() {
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
return
}
packageMapMu.Lock()
defer packageMapMu.Unlock()
for k := range packageMap {
for _, mod := range buildInfo.Deps {
if strings.HasSuffix(mod.Path, path.Join("api", k)) {
apiModuleVersions[k] = mod.Version
break
}
}
}
}
26 changes: 26 additions & 0 deletions types/versions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package types_test

import (
"testing"

"runtime/debug"

types "github.com/sensu/sensu-go/types"
)

// TODO(eric): This test doesn't work yet because of https://github.com/golang/go/issues/33976
func TestAPIModuleVersions(t *testing.T) {
_, ok := debug.ReadBuildInfo()
if ok {
t.Fatal("remove this if block, the test works now")
} else {
t.Skip()
}
modVersions := types.APIModuleVersions()
if _, ok := modVersions["core/v2"]; !ok {
t.Errorf("missing core/v2 module version")
}
if _, ok := modVersions["core/v3"]; !ok {
t.Errorf("missing core/v3 module version")
}
}

0 comments on commit b2f11e3

Please sign in to comment.