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