Skip to content

Commit

Permalink
Merge pull request #48 from gnufied/report-vsphere-api-versions
Browse files Browse the repository at this point in the history
Bug 2000294: Report esxi api versions
  • Loading branch information
openshift-merge-robot committed Sep 3, 2021
2 parents ecca095 + 417d319 commit 646689c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 30 deletions.
3 changes: 2 additions & 1 deletion pkg/check/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func customizeVM(ctx *CheckContext, node *v1.Node, spec *types.VirtualMachineCon

}

func customizeHostVersion(hostSystemId string, version string) error {
func customizeHostVersion(hostSystemId string, version string, apiVersion string) error {
hsRef := simulator.Map.Get(types.ManagedObjectReference{
Type: "HostSystem",
Value: hostSystemId,
Expand All @@ -215,5 +215,6 @@ func customizeHostVersion(hostSystemId string, version string) error {

hs := hsRef.(*simulator.HostSystem)
hs.Config.Product.Version = version
hs.Config.Product.ApiVersion = apiVersion
return nil
}
10 changes: 6 additions & 4 deletions pkg/check/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
)

const (
versionLabel = "version"
vCenterUUID = "uuid"
versionLabel = "version"
apiVersionLabel = "api_version"
vCenterUUID = "uuid"
)

var (
Expand All @@ -17,7 +18,7 @@ var (
Help: "Information about vSphere vCenter.",
StabilityLevel: metrics.ALPHA,
},
[]string{versionLabel, vCenterUUID},
[]string{versionLabel, apiVersionLabel, vCenterUUID},
)
)

Expand All @@ -34,6 +35,7 @@ func CollectClusterInfo(ctx *CheckContext) error {

func collectVCenterInfo(ctx *CheckContext) {
version := ctx.VMClient.ServiceContent.About.Version
apiVersion := ctx.VMClient.ServiceContent.About.ApiVersion
uuid := ctx.VMClient.ServiceContent.About.InstanceUuid
vCenterInfoMetric.WithLabelValues(version, uuid).Set(1.0)
vCenterInfoMetric.WithLabelValues(version, apiVersion, uuid).Set(1.0)
}
2 changes: 1 addition & 1 deletion pkg/check/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestInfo(t *testing.T) {
expectedMetric := `
# HELP vsphere_vcenter_info [ALPHA] Information about vSphere vCenter.
# TYPE vsphere_vcenter_info gauge
vsphere_vcenter_info{uuid="dbed6e0c-bd88-4ef6-b594-21283e1c677f",version="6.5.0"} 1
vsphere_vcenter_info{api_version="6.5", uuid="dbed6e0c-bd88-4ef6-b594-21283e1c677f",version="6.5.0"} 1
`

if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(expectedMetric), "vsphere_vcenter_info"); err != nil {
Expand Down
41 changes: 24 additions & 17 deletions pkg/check/node_esxi_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
type CollectNodeESXiVersion struct {
// map ESXI host name ("host-12345", not hostname nor IP address!) -> version
// Version "" means that a CheckNode call is retrieving it right now.
esxiVersions map[string]string
esxiVersions map[string]esxiVersionInfo
esxiVersionsLock sync.Mutex
}

Expand All @@ -30,10 +30,15 @@ var (
Help: "Number of ESXi hosts with given version.",
StabilityLevel: metrics.ALPHA,
},
[]string{versionLabel},
[]string{versionLabel, apiVersionLabel},
)
)

type esxiVersionInfo struct {
version string
apiVersion string
}

func init() {
legacyregistry.MustRegister(esxiVersionMetric)
}
Expand All @@ -43,7 +48,7 @@ func (c *CollectNodeESXiVersion) Name() string {
}

func (c *CollectNodeESXiVersion) StartCheck() error {
c.esxiVersions = make(map[string]string)
c.esxiVersions = make(map[string]esxiVersionInfo)
return nil
}

Expand All @@ -64,7 +69,7 @@ func (c *CollectNodeESXiVersion) CheckNode(ctx *CheckContext, node *v1.Node, vm
defer cancel()
var o mo.HostSystem

err := host.Properties(tctx, host.Reference(), []string{"name", "config.product.version"}, &o)
err := host.Properties(tctx, host.Reference(), []string{"name", "config.product"}, &o)
if err != nil {
return fmt.Errorf("failed to load ESXi host %s for node %s: %s", hostName, node.Name, err)
}
Expand All @@ -73,23 +78,24 @@ func (c *CollectNodeESXiVersion) CheckNode(ctx *CheckContext, node *v1.Node, vm
return fmt.Errorf("error getting ESXi host version for node %s: host.config is nil", node.Name)
}
version := o.Config.Product.Version
apiVersion := o.Config.Product.ApiVersion
realHostName := o.Name // "10.0.0.2" or other user-friendly name of the host.
klog.V(2).Infof("Node %s runs on host %s (%s) with ESXi version: %s", node.Name, hostName, realHostName, version)
c.setHostVersion(hostName, version)
c.setHostVersion(hostName, version, apiVersion)

return nil
}

func (c *CollectNodeESXiVersion) FinishCheck(ctx *CheckContext) {
// Count the versions
versions := make(map[string]int)
for _, version := range c.esxiVersions {
versions[version]++
versions := make(map[esxiVersionInfo]int)
for _, esxiVersion := range c.esxiVersions {
versions[esxiVersion]++
}

// Report the count
for version, count := range versions {
esxiVersionMetric.WithLabelValues(version).Set(float64(count))
for esxiVersion, count := range versions {
esxiVersionMetric.WithLabelValues(esxiVersion.version, esxiVersion.apiVersion).Set(float64(count))
}
return
}
Expand All @@ -102,20 +108,21 @@ func (c *CollectNodeESXiVersion) FinishCheck(ctx *CheckContext) {
func (c *CollectNodeESXiVersion) checkOrMarkHostProcessing(hostName string) (string, bool) {
c.esxiVersionsLock.Lock()
defer c.esxiVersionsLock.Unlock()
var esxiVersion string
ver, found := c.esxiVersions[hostName]
if ver == "" {
ver = "<in progress>"
if ver.version == "" {
esxiVersion = "<in progress>"
}
if found {
return ver, true
return ver.version, true
}
// Mark the hostName as in progress
c.esxiVersions[hostName] = ""
return ver, false
c.esxiVersions[hostName] = esxiVersionInfo{"", ""}
return esxiVersion, false
}

func (c *CollectNodeESXiVersion) setHostVersion(hostName string, version string) {
func (c *CollectNodeESXiVersion) setHostVersion(hostName string, version string, apiVersion string) {
c.esxiVersionsLock.Lock()
defer c.esxiVersionsLock.Unlock()
c.esxiVersions[hostName] = version
c.esxiVersions[hostName] = esxiVersionInfo{version, apiVersion}
}
17 changes: 10 additions & 7 deletions pkg/check/node_esxi_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,27 @@ func TestCollectNodeESXiVersion(t *testing.T) {
tests := []struct {
name string
esxiVersion string
esxiApiversion string
expectedMetrics string
}{
{
name: "esxi 6.7.0",
esxiVersion: "6.7.0",
name: "esxi 6.7.0",
esxiVersion: "6.7.0",
esxiApiversion: "6.7.3",
expectedMetrics: `
# HELP vsphere_esxi_version_total [ALPHA] Number of ESXi hosts with given version.
# TYPE vsphere_esxi_version_total gauge
vsphere_esxi_version_total{version="6.7.0"} 1
vsphere_esxi_version_total{api_version="6.7.3", version="6.7.0"} 1
`,
},
{
name: "esxi 7.0.0",
esxiVersion: "7.0.0",
name: "esxi 7.0.0",
esxiVersion: "7.0.0",
esxiApiversion: "7.0.0-1",
expectedMetrics: `
# HELP vsphere_esxi_version_total [ALPHA] Number of ESXi hosts with given version.
# TYPE vsphere_esxi_version_total gauge
vsphere_esxi_version_total{version="7.0.0"} 1
vsphere_esxi_version_total{api_version="7.0.0-1", version="7.0.0"} 1
`,
},
}
Expand All @@ -48,7 +51,7 @@ func TestCollectNodeESXiVersion(t *testing.T) {
defer cleanup()

// Set esxi version of the only host.
err = customizeHostVersion(defaultHostId, test.esxiVersion)
err = customizeHostVersion(defaultHostId, test.esxiVersion, test.esxiApiversion)
if err != nil {
t.Fatalf("Failed to customize host: %s", err)
}
Expand Down

0 comments on commit 646689c

Please sign in to comment.