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

add a field to indicate spotInstances in node.conditions metric #1928

Merged
merged 1 commit into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/monitor/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/Azure/go-autorest/autorest/azure"
configv1 "github.com/openshift/api/config/v1"
configclient "github.com/openshift/client-go/config/clientset/versioned"
maoclient "github.com/openshift/machine-api-operator/pkg/generated/clientset/versioned"
mcoclient "github.com/openshift/machine-config-operator/pkg/generated/clientset/versioned"
"github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
Expand All @@ -34,6 +35,7 @@ type Monitor struct {
restconfig *rest.Config
cli kubernetes.Interface
configcli configclient.Interface
maocli maoclient.Interface
mcocli mcoclient.Interface
m metrics.Interface
arocli aroclient.Interface
Expand Down Expand Up @@ -70,6 +72,11 @@ func NewMonitor(ctx context.Context, log *logrus.Entry, restConfig *rest.Config,
return nil, err
}

maocli, err := maoclient.NewForConfig(restConfig)
if err != nil {
return nil, err
}

mcocli, err := mcoclient.NewForConfig(restConfig)
if err != nil {
return nil, err
Expand All @@ -90,6 +97,7 @@ func NewMonitor(ctx context.Context, log *logrus.Entry, restConfig *rest.Config,
restconfig: restConfig,
cli: cli,
configcli: configcli,
maocli: maocli,
mcocli: mcocli,
arocli: arocli,
m: m,
Expand Down
54 changes: 46 additions & 8 deletions pkg/monitor/cluster/nodeconditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ package cluster

import (
"context"
"encoding/json"
"strconv"

"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
azureproviderv1beta1 "sigs.k8s.io/cluster-api-provider-azure/pkg/apis/azureprovider/v1beta1"
)

var nodeConditionsExpected = map[corev1.NodeConditionType]corev1.ConditionStatus{
Expand All @@ -23,27 +27,34 @@ func (mon *Monitor) emitNodeConditions(ctx context.Context) error {
return err
}

spotInstances := mon.getSpotInstances(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

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

this makes one api call to MAO to list machines to determine spot instance type for all VMs in a cluster


mon.emitGauge("node.count", int64(len(ns.Items)), nil)

for _, n := range ns.Items {

for _, c := range n.Status.Conditions {
if c.Status == nodeConditionsExpected[c.Type] {
continue
}

_, isSpotInstance := spotInstances[n.Name]

mon.emitGauge("node.conditions", 1, map[string]string{
"nodeName": n.Name,
"status": string(c.Status),
"type": string(c.Type),
"nodeName": n.Name,
"status": string(c.Status),
"type": string(c.Type),
"spotInstance": strconv.FormatBool(isSpotInstance),
})

if mon.hourlyRun {
mon.log.WithFields(logrus.Fields{
"metric": "node.conditions",
"name": n.Name,
"status": c.Status,
"type": c.Type,
"message": c.Message,
"metric": "node.conditions",
"name": n.Name,
"status": c.Status,
"type": c.Type,
"message": c.Message,
"spotInstance": isSpotInstance,
}).Print()
}
}
Expand All @@ -57,3 +68,30 @@ func (mon *Monitor) emitNodeConditions(ctx context.Context) error {

return nil
}

// getSpotInstances returns a map where the keys are the machine name and only exist if the machine is a spot instance
func (mon *Monitor) getSpotInstances(ctx context.Context) map[string]struct{} {
spotInstances := make(map[string]struct{})
machines, err := mon.maocli.MachineV1beta1().Machines("openshift-machine-api").List(ctx, metav1.ListOptions{})

if err != nil {
// when this call fails we may report spot vms as non spot until the next successful call
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to error here and exit from emitNodeConditions? In this case we will not submit metrics on error at all.

I'm on the fence here, but I think having incorrect data is worse than missing data. What do you and others think?

Same question appleis to the code below (errors from json.Unmarshal).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm on the fence as well and am open to suggestions for this. I agree that incorrect data is worse than missing data, but do we have an alert based on missing data? the worst case with incorrect data here is that we might fire a nodeready alert on a spot vm where as the worst case of missing data is we will not fire any alert when we would want one for nodeready.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After giving this more thought, I believe we should continue to not bail of out sending node.conditions when we fail to call the machine api operator. When we fail to call the mao there could be something wrong with some nodes, if we then don't report node.conditions then we may never know via this metric. By continuing to report node.conditions with the possibility that spotInstance is false rather than true, we keep the current functionality of our alerting system rather than create a new scenario of missing metrics.

mon.log.Error(err)
return spotInstances
}

for _, machine := range machines.Items {
var spec azureproviderv1beta1.AzureMachineProviderSpec
err = json.Unmarshal(machine.Spec.ProviderSpec.Value.Raw, &spec)
if err != nil {
mon.log.Error(err)
continue
}

if spec.SpotVMOptions != nil {
spotInstances[machine.Name] = struct{}{}
}
}

return spotInstances
}
Loading