Skip to content

Commit

Permalink
feat(metrics-operator): introduce ErrMsg field into KeptnMetric status (
Browse files Browse the repository at this point in the history
#1365)

Signed-off-by: odubajDT <ondrej.dubaj@dynatrace.com>
  • Loading branch information
odubajDT committed May 4, 2023
1 parent be3c2f1 commit 092d284
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 15 deletions.
2 changes: 2 additions & 0 deletions metrics-operator/api/v1alpha3/keptnmetric_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type KeptnMetricStatus struct {
RawValue []byte `json:"rawValue"`
// LastUpdated represents the time when the status data was last updated
LastUpdated metav1.Time `json:"lastUpdated"`
// ErrMsg represents the error details when the query could not be evaluated
ErrMsg string `json:"errMsg,omitempty"`
}

// ProviderRef represents the provider object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ spec:
status:
description: KeptnMetricStatus defines the observed state of KeptnMetric
properties:
errMsg:
description: ErrMsg represents the error details when the query could
not be evaluated
type: string
lastUpdated:
description: LastUpdated represents the time when the status data
was last updated
Expand Down
16 changes: 11 additions & 5 deletions metrics-operator/controllers/metrics/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,27 @@ func (r *KeptnMetricReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{Requeue: false}, err2
}

reconcile := ctrl.Result{Requeue: true, RequeueAfter: 10 * time.Second}
value, rawValue, err := provider.EvaluateQuery(ctx, *metric, *metricProvider)
if err != nil {
r.Log.Error(err, "Failed to evaluate the query")
return ctrl.Result{Requeue: false}, err
metric.Status.ErrMsg = err.Error()
metric.Status.Value = ""
metric.Status.RawValue = []byte{}
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()}
}
metric.Status.Value = value
metric.Status.RawValue = cupSize(rawValue)
metric.Status.LastUpdated = metav1.Time{Time: time.Now()}

if err := r.Client.Status().Update(ctx, metric); err != nil {
r.Log.Error(err, "Failed to update the Metric status")
return ctrl.Result{}, err
}

return ctrl.Result{Requeue: true, RequeueAfter: 10 * time.Second}, nil
return reconcile, err
}

func cupSize(value []byte) []byte {
Expand Down
56 changes: 46 additions & 10 deletions metrics-operator/controllers/metrics/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,21 @@ func TestKeptnMetricReconciler_Reconcile(t *testing.T) {
}

tests := []struct {
name string
ctx context.Context
req controllerruntime.Request
want controllerruntime.Result
wantErr error
name string
ctx context.Context
req controllerruntime.Request
want controllerruntime.Result
wantMetric *metricsapi.KeptnMetric
wantErr error
}{
{
name: "metric not found, ignoring",
ctx: context.TODO(),
req: controllerruntime.Request{
NamespacedName: types.NamespacedName{Namespace: "default", Name: "myunexistingmetric"},
},
want: controllerruntime.Result{},
want: controllerruntime.Result{},
wantMetric: nil,
},

{
Expand All @@ -207,7 +209,8 @@ func TestKeptnMetricReconciler_Reconcile(t *testing.T) {
req: controllerruntime.Request{
NamespacedName: types.NamespacedName{Namespace: "default", Name: "mymetric"},
},
want: controllerruntime.Result{Requeue: true, RequeueAfter: 10 * time.Second},
want: controllerruntime.Result{Requeue: true, RequeueAfter: 10 * time.Second},
wantMetric: nil,
},

{
Expand All @@ -216,7 +219,8 @@ func TestKeptnMetricReconciler_Reconcile(t *testing.T) {
req: controllerruntime.Request{
NamespacedName: types.NamespacedName{Namespace: "default", Name: "mymetric2"},
},
want: controllerruntime.Result{Requeue: true, RequeueAfter: 10 * time.Second},
want: controllerruntime.Result{Requeue: true, RequeueAfter: 10 * time.Second},
wantMetric: nil,
},

{
Expand All @@ -225,8 +229,9 @@ func TestKeptnMetricReconciler_Reconcile(t *testing.T) {
req: controllerruntime.Request{
NamespacedName: types.NamespacedName{Namespace: "default", Name: "mymetric3"},
},
want: controllerruntime.Result{Requeue: false, RequeueAfter: 0},
wantErr: fmt.Errorf("provider unsupported-type not supported"),
want: controllerruntime.Result{Requeue: false, RequeueAfter: 0},
wantErr: fmt.Errorf("provider unsupported-type not supported"),
wantMetric: nil,
},

{
Expand All @@ -237,6 +242,17 @@ func TestKeptnMetricReconciler_Reconcile(t *testing.T) {
},
want: controllerruntime.Result{Requeue: false, RequeueAfter: 0},
wantErr: fmt.Errorf("client_error: client error: 404"),
wantMetric: &metricsapi.KeptnMetric{
ObjectMeta: metav1.ObjectMeta{
Name: "mymetric4",
Namespace: "default",
},
Status: metricsapi.KeptnMetricStatus{
ErrMsg: "client_error: client error: 404",
Value: "",
RawValue: []byte{},
},
},
},

{
Expand All @@ -247,6 +263,17 @@ func TestKeptnMetricReconciler_Reconcile(t *testing.T) {
},
want: controllerruntime.Result{Requeue: false, RequeueAfter: 0},
wantErr: fmt.Errorf("client_error: client error: 404"),
wantMetric: &metricsapi.KeptnMetric{
ObjectMeta: metav1.ObjectMeta{
Name: "mymetric5",
Namespace: "default",
},
Status: metricsapi.KeptnMetricStatus{
ErrMsg: "client_error: client error: 404",
Value: "",
RawValue: []byte{},
},
},
},
}
for _, tt := range tests {
Expand All @@ -263,6 +290,15 @@ func TestKeptnMetricReconciler_Reconcile(t *testing.T) {
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Reconcile() got = %v, want %v", got, tt.want)
}

if tt.wantMetric != nil {
metric := &metricsapi.KeptnMetric{}
err := client.Get(context.TODO(), types.NamespacedName{Namespace: tt.wantMetric.Namespace, Name: tt.wantMetric.Name}, metric)
require.Nil(t, err)
require.Equal(t, tt.wantMetric.Status.ErrMsg, metric.Status.ErrMsg)
require.Equal(t, tt.wantMetric.Status.Value, metric.Status.Value)
require.Equal(t, tt.wantMetric.Status.RawValue, metric.Status.RawValue)
}
})
}
}
Expand Down

0 comments on commit 092d284

Please sign in to comment.