Skip to content

Commit

Permalink
add range check
Browse files Browse the repository at this point in the history
Signed-off-by: Rakshit Gondwal <[email protected]>
  • Loading branch information
rakshitgondwal committed Jan 7, 2024
1 parent a92b566 commit 49b5088
Showing 1 changed file with 34 additions and 33 deletions.
67 changes: 34 additions & 33 deletions metrics-operator/controllers/metrics/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (r *KeptnMetricReconciler) Reconcile(ctx context.Context, req ctrl.Request)

value, rawValue, err := r.getResults(ctx, metric, provider, metricProvider)

metric = r.updateMetric(metric, value, rawValue, err)
metric = r.updateMetric(metric, value, rawValue, reconcile, err)

if err := r.Client.Status().Update(ctx, metric); err != nil {
r.Log.Error(err, "Failed to update the Metric status", "requestInfo", requestInfo)
Expand All @@ -116,44 +116,45 @@ func (r *KeptnMetricReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

func (r *KeptnMetricReconciler) updateMetric(metric *metricsapi.KeptnMetric, value string, rawValue []byte, reconcile ctrl.Result, err error) *metricsapi.KeptnMetric {

Check failure on line 118 in metrics-operator/controllers/metrics/controller.go

View workflow job for this annotation

GitHub Actions / golangci-lint (metrics-operator, metrics-operator/)

SA4009: argument reconcile is overwritten before first use (staticcheck)
if metric.Spec.Range.StoredResults > 0 {
if err != nil {
intervalResult := metricsapi.IntervalResult{
LastUpdated: metav1.Time{Time: time.Now().UTC()},
ErrMsg: err.Error(),
}
if len(metric.Status.IntervalResults) >= int(metric.Spec.Range.StoredResults) {
metric.Status.IntervalResults = append(metric.Status.IntervalResults[1:], intervalResult)
if metric.Spec.Range != nil {
if metric.Spec.Range.StoredResults > 0 {
if err != nil {
intervalResult := metricsapi.IntervalResult{
LastUpdated: metav1.Time{Time: time.Now().UTC()},
ErrMsg: err.Error(),
}
if len(metric.Status.IntervalResults) >= int(metric.Spec.Range.StoredResults) {
metric.Status.IntervalResults = append(metric.Status.IntervalResults[1:], intervalResult)
} else {
metric.Status.IntervalResults = append(metric.Status.IntervalResults, intervalResult)
}
} else {
metric.Status.IntervalResults = append(metric.Status.IntervalResults, intervalResult)
intervalResult := metricsapi.IntervalResult{
Value: value,
Range: metric.Spec.Range,
LastUpdated: metav1.Time{Time: time.Now().UTC()},
ErrMsg: err.Error(),
}
if len(metric.Status.IntervalResults) >= int(metric.Spec.Range.StoredResults) {
metric.Status.IntervalResults = append(metric.Status.IntervalResults[1:], intervalResult)
} else {
metric.Status.IntervalResults = append(metric.Status.IntervalResults, intervalResult)
}
}
} else {
intervalResult := metricsapi.IntervalResult{
Value: value,
Range: metric.Spec.Range,
LastUpdated: metav1.Time{Time: time.Now().UTC()},
ErrMsg: err.Error(),
}
if len(metric.Status.IntervalResults) >= int(metric.Spec.Range.StoredResults) {
metric.Status.IntervalResults = append(metric.Status.IntervalResults[1:], intervalResult)
if err != nil {
r.Log.Error(err, "Failed to evaluate the query", "Response from provider was:", (string)(rawValue))
metric.Status.ErrMsg = err.Error()
metric.Status.Value = ""
metric.Status.RawValue = cupSize(rawValue)
metric.Status.LastUpdated = metav1.Time{Time: time.Now()}
reconcile = ctrl.Result{Requeue: false}

Check failure on line 151 in metrics-operator/controllers/metrics/controller.go

View workflow job for this annotation

GitHub Actions / golangci-lint (metrics-operator, metrics-operator/)

ineffectual assignment to reconcile (ineffassign)
} else {
metric.Status.IntervalResults = append(metric.Status.IntervalResults, intervalResult)
metric.Status.Value = value
metric.Status.RawValue = cupSize(rawValue)
metric.Status.LastUpdated = metav1.Time{Time: time.Now()}
}
}

} else {
if err != nil {
r.Log.Error(err, "Failed to evaluate the query", "Response from provider was:", (string)(rawValue))
metric.Status.ErrMsg = err.Error()
metric.Status.Value = ""
metric.Status.RawValue = cupSize(rawValue)
metric.Status.LastUpdated = metav1.Time{Time: time.Now()}
reconcile = ctrl.Result{Requeue: false}
} else {
metric.Status.Value = value
metric.Status.RawValue = cupSize(rawValue)
metric.Status.LastUpdated = metav1.Time{Time: time.Now()}
}
}
return metric
}
Expand Down

0 comments on commit 49b5088

Please sign in to comment.