-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(operator): evaluation controller uses KeptnMetric as SLI provider (
#661) Signed-off-by: odubajDT <ondrej.dubaj@dynatrace.com> Signed-off-by: realanna <anna.reale@dynatrace.com> Co-authored-by: realanna <anna.reale@dynatrace.com>
- Loading branch information
Showing
13 changed files
with
707 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestKeptnMetric_IsStatusSet(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
metric KeptnMetric | ||
want bool | ||
}{ | ||
{ | ||
name: "results set", | ||
metric: KeptnMetric{ | ||
Status: KeptnMetricStatus{ | ||
Value: "val", | ||
}, | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "value empty", | ||
metric: KeptnMetric{ | ||
Status: KeptnMetricStatus{ | ||
Value: "", | ||
}, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "value not set", | ||
metric: KeptnMetric{ | ||
Status: KeptnMetricStatus{}, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "status not set", | ||
metric: KeptnMetric{}, | ||
want: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
require.Equal(t, tt.want, tt.metric.IsStatusSet()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package providers | ||
|
||
import ( | ||
klcv1alpha2 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
const DynatraceProviderName = "dynatrace" | ||
const PrometheusProviderName = "prometheus" | ||
const KeptnMetricProviderName = "keptn-metric" | ||
const KLTNamespace = "keptn-lifecycle-toolkit-system" | ||
|
||
var MetricDefaultProvider = &klcv1alpha2.KeptnEvaluationProvider{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: KeptnMetricProviderName, | ||
Namespace: KLTNamespace, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package providers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/go-logr/logr" | ||
klcv1alpha2 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2" | ||
metricsv1alpha1 "github.com/keptn/lifecycle-toolkit/operator/apis/metrics/v1alpha1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type KeptnMetricProvider struct { | ||
Log logr.Logger | ||
k8sClient client.Client | ||
} | ||
|
||
// EvaluateQuery fetches the SLI values from KeptnMetric resource | ||
func (p *KeptnMetricProvider) EvaluateQuery(ctx context.Context, objective klcv1alpha2.Objective, provider klcv1alpha2.KeptnEvaluationProvider) (string, []byte, error) { | ||
metric := &metricsv1alpha1.KeptnMetric{} | ||
if err := p.k8sClient.Get(ctx, types.NamespacedName{Name: objective.Name, Namespace: provider.Namespace}, metric); err != nil { | ||
p.Log.Error(err, "Could not retrieve KeptnMetric") | ||
return "", nil, err | ||
} | ||
|
||
if !metric.IsStatusSet() { | ||
err := fmt.Errorf("empty value for: %s", metric.Name) | ||
p.Log.Error(err, "KeptnMetric has no value") | ||
return "", nil, err | ||
} | ||
|
||
return metric.Status.Value, metric.Status.RawValue, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package providers | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
klcv1alpha2 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2" | ||
metricsv1alpha1 "github.com/keptn/lifecycle-toolkit/operator/apis/metrics/v1alpha1" | ||
"github.com/stretchr/testify/require" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
) | ||
|
||
func Test_keptnmetric(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
metric *metricsv1alpha1.KeptnMetric | ||
out string | ||
outraw []byte | ||
wantError bool | ||
}{ | ||
{ | ||
name: "no KeptnMetric", | ||
metric: &metricsv1alpha1.KeptnMetric{}, | ||
out: "", | ||
outraw: []byte(nil), | ||
wantError: true, | ||
}, | ||
{ | ||
name: "KeptnMetric without results", | ||
metric: &metricsv1alpha1.KeptnMetric{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "metric", | ||
Namespace: "default", | ||
}, | ||
}, | ||
out: "", | ||
outraw: []byte(nil), | ||
wantError: true, | ||
}, | ||
{ | ||
name: "KeptnMetric with results", | ||
metric: &metricsv1alpha1.KeptnMetric{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "metric", | ||
Namespace: "default", | ||
}, | ||
Status: metricsv1alpha1.KeptnMetricStatus{ | ||
Value: "1", | ||
RawValue: []byte("1"), | ||
}, | ||
}, | ||
out: "1", | ||
outraw: []byte("1"), | ||
wantError: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := metricsv1alpha1.AddToScheme(scheme.Scheme) | ||
require.Nil(t, err) | ||
client := fake.NewClientBuilder().WithObjects(tt.metric).Build() | ||
|
||
kmp := KeptnMetricProvider{ | ||
Log: ctrl.Log.WithName("testytest"), | ||
k8sClient: client, | ||
} | ||
|
||
obj := klcv1alpha2.Objective{ | ||
Name: "metric", | ||
} | ||
|
||
p := klcv1alpha2.KeptnEvaluationProvider{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "provider", | ||
}, | ||
} | ||
|
||
r, raw, e := kmp.EvaluateQuery(context.TODO(), obj, p) | ||
require.Equal(t, tt.out, r) | ||
require.Equal(t, tt.outraw, raw) | ||
if tt.wantError != (e != nil) { | ||
t.Errorf("want error: %t, got: %v", tt.wantError, e) | ||
} | ||
|
||
}) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.