Skip to content

Commit

Permalink
refactor to 'fail early' structure
Browse files Browse the repository at this point in the history
  • Loading branch information
cjerad committed Sep 24, 2021
1 parent cb3a828 commit 3e579f1
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions pkg/controllers/metrics/node/counter/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,20 @@ func handleUpdatedNode(ctx context.Context, name types.NamespacedName, currLabel

func getLabels(node *v1.Node) prometheus.Labels {
labels := make(prometheus.Labels)
if node != nil {
labels[metrics.ProvisionerLabel] = node.Labels["karpenter.sh/provisioner-name"]
labels[metricArchLabel] = node.Labels["kubernetes.io/arch"]
labels[metricInstanceTypeLabel] = node.Labels["node.kubernetes.io/instance-type"]
labels[metricTopologyRegionLabel] = node.Labels["topology.kubernetes.io/region"]
labels[metricTopologyZoneLabel] = node.Labels["topology.kubernetes.io/zone"]
labels[metricOsLabel] = node.Labels["kubernetes.io/os"]

for _, c := range node.Status.Conditions {
if labelName, found := conditionTypeToMetricLabel[c.Type]; found {
labels[labelName] = strings.ToLower(string(c.Status))
}
if node == nil {
return labels
}

labels[metrics.ProvisionerLabel] = node.Labels["karpenter.sh/provisioner-name"]
labels[metricArchLabel] = node.Labels["kubernetes.io/arch"]
labels[metricInstanceTypeLabel] = node.Labels["node.kubernetes.io/instance-type"]
labels[metricTopologyRegionLabel] = node.Labels["topology.kubernetes.io/region"]
labels[metricTopologyZoneLabel] = node.Labels["topology.kubernetes.io/zone"]
labels[metricOsLabel] = node.Labels["kubernetes.io/os"]

for _, c := range node.Status.Conditions {
if labelName, found := conditionTypeToMetricLabel[c.Type]; found {
labels[labelName] = strings.ToLower(string(c.Status))
}
}

Expand All @@ -154,8 +156,10 @@ func decrementNodeCount(labels prometheus.Labels) error {

func updateNodeCount(labels prometheus.Labels, update func(prometheus.Gauge)) error {
nodeCount, err := nodeCountGaugeVec.GetMetricWith(labels)
if err == nil {
update(nodeCount)
if err != nil {
return err
}
return err

update(nodeCount)
return nil
}

0 comments on commit 3e579f1

Please sign in to comment.