mirror of https://github.com/grafana/grafana.git
Store ignore step annotation in checks
This commit is contained in:
parent
a1ca480ec1
commit
9089a22933
|
@ -3,6 +3,7 @@ package checks
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"maps"
|
||||
"strconv"
|
||||
|
||||
"github.com/grafana/authlib/types"
|
||||
|
@ -55,12 +56,28 @@ func GetRetryAnnotation(obj resource.Object) string {
|
|||
return obj.GetAnnotations()[RetryAnnotation]
|
||||
}
|
||||
|
||||
func SetStatusAnnotation(ctx context.Context, client resource.Client, obj resource.Object, status string) error {
|
||||
annotations := obj.GetAnnotations()
|
||||
if annotations == nil {
|
||||
annotations = map[string]string{}
|
||||
func AddAnnotations(ctx context.Context, obj resource.Object, annotations map[string]string) map[string]string {
|
||||
existingAnnotations := obj.GetAnnotations()
|
||||
if existingAnnotations == nil {
|
||||
existingAnnotations = map[string]string{}
|
||||
}
|
||||
annotations[StatusAnnotation] = status
|
||||
maps.Copy(existingAnnotations, annotations)
|
||||
return existingAnnotations
|
||||
}
|
||||
|
||||
func DeleteAnnotations(ctx context.Context, obj resource.Object, annotations []string) map[string]string {
|
||||
existingAnnotations := obj.GetAnnotations()
|
||||
if existingAnnotations == nil {
|
||||
existingAnnotations = map[string]string{}
|
||||
}
|
||||
for _, annotation := range annotations {
|
||||
delete(existingAnnotations, annotation)
|
||||
}
|
||||
return existingAnnotations
|
||||
}
|
||||
|
||||
func SetStatusAnnotation(ctx context.Context, client resource.Client, obj resource.Object, status string) error {
|
||||
annotations := AddAnnotations(ctx, obj, map[string]string{StatusAnnotation: status})
|
||||
return client.PatchInto(ctx, obj.GetStaticMetadata().Identifier(), resource.PatchRequest{
|
||||
Operations: []resource.PatchOperation{{
|
||||
Operation: resource.PatchOpAdd,
|
||||
|
|
|
@ -50,8 +50,17 @@ func processCheck(ctx context.Context, client resource.Client, typesClient resou
|
|||
}
|
||||
return fmt.Errorf("error initializing check: %w", err)
|
||||
}
|
||||
// Get the check type
|
||||
var checkType resource.Object
|
||||
checkType, err = typesClient.Get(ctx, resource.Identifier{
|
||||
Namespace: obj.GetNamespace(),
|
||||
Name: check.ID(),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Run the steps
|
||||
steps, err := filterSteps(ctx, typesClient, check.ID(), obj.GetNamespace(), check.Steps())
|
||||
steps, err := filterSteps(checkType, check.Steps())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -68,16 +77,23 @@ func processCheck(ctx context.Context, client resource.Client, typesClient resou
|
|||
Failures: failures,
|
||||
Count: int64(len(items)),
|
||||
}
|
||||
err = checks.SetStatusAnnotation(ctx, client, obj, checks.StatusAnnotationProcessed)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Set the status annotation to processed and annotate the steps ignored
|
||||
annotations := checks.AddAnnotations(ctx, obj, map[string]string{
|
||||
checks.StatusAnnotation: checks.StatusAnnotationProcessed,
|
||||
checks.IgnoreStepsAnnotation: checkType.GetAnnotations()[checks.IgnoreStepsAnnotation],
|
||||
})
|
||||
return client.PatchInto(ctx, obj.GetStaticMetadata().Identifier(), resource.PatchRequest{
|
||||
Operations: []resource.PatchOperation{{
|
||||
Operation: resource.PatchOpAdd,
|
||||
Path: "/status/report",
|
||||
Value: *report,
|
||||
}},
|
||||
Operations: []resource.PatchOperation{
|
||||
{
|
||||
Operation: resource.PatchOpAdd,
|
||||
Path: "/status/report",
|
||||
Value: *report,
|
||||
}, {
|
||||
Operation: resource.PatchOpAdd,
|
||||
Path: "/metadata/annotations",
|
||||
Value: annotations,
|
||||
},
|
||||
},
|
||||
}, resource.PatchOptions{}, obj)
|
||||
}
|
||||
|
||||
|
@ -106,8 +122,17 @@ func processCheckRetry(ctx context.Context, client resource.Client, typesClient
|
|||
}
|
||||
return fmt.Errorf("error initializing check: %w", err)
|
||||
}
|
||||
// Get the check type
|
||||
var checkType resource.Object
|
||||
checkType, err = typesClient.Get(ctx, resource.Identifier{
|
||||
Namespace: obj.GetNamespace(),
|
||||
Name: check.ID(),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Run the steps
|
||||
steps, err := filterSteps(ctx, typesClient, check.ID(), obj.GetNamespace(), check.Steps())
|
||||
steps, err := filterSteps(checkType, check.Steps())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -135,8 +160,7 @@ func processCheckRetry(ctx context.Context, client resource.Client, typesClient
|
|||
return false
|
||||
})
|
||||
// Delete the retry annotation to mark the check as processed
|
||||
annotations := obj.GetAnnotations()
|
||||
delete(annotations, checks.RetryAnnotation)
|
||||
annotations := checks.DeleteAnnotations(ctx, obj, []string{checks.RetryAnnotation})
|
||||
return client.PatchInto(ctx, obj.GetStaticMetadata().Identifier(), resource.PatchRequest{
|
||||
Operations: []resource.PatchOperation{{
|
||||
Operation: resource.PatchOpAdd,
|
||||
|
@ -182,16 +206,8 @@ func runStepsInParallel(ctx context.Context, spec *advisorv0alpha1.CheckSpec, st
|
|||
return reportFailures, internalErr
|
||||
}
|
||||
|
||||
func filterSteps(ctx context.Context, typesClient resource.Client, checkID string, namespace string, steps []checks.Step) ([]checks.Step, error) {
|
||||
identifier := resource.Identifier{
|
||||
Namespace: namespace,
|
||||
Name: checkID,
|
||||
}
|
||||
obj, err := typesClient.Get(ctx, identifier)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ignoreSteps := obj.GetAnnotations()[checks.IgnoreStepsAnnotation]
|
||||
func filterSteps(checkType resource.Object, steps []checks.Step) ([]checks.Step, error) {
|
||||
ignoreSteps := checkType.GetAnnotations()[checks.IgnoreStepsAnnotation]
|
||||
if ignoreSteps != "" && ignoreSteps != "1" { // 1 is the default value for the annotation
|
||||
filteredSteps := []checks.Step{}
|
||||
ignoreStepsList := strings.Split(ignoreSteps, ",")
|
||||
|
|
|
@ -156,6 +156,7 @@ func TestProcessCheck_IgnoreSteps(t *testing.T) {
|
|||
err = processCheck(ctx, client, typesClient, obj, check)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "processed", obj.GetAnnotations()[checks.StatusAnnotation])
|
||||
assert.Equal(t, "mock", obj.GetAnnotations()[checks.IgnoreStepsAnnotation])
|
||||
}
|
||||
|
||||
func TestProcessCheckRetry_NoRetry(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue