Advisor: Avoid Watcher (#102120)

This commit is contained in:
Andres Martinez Gotor
2025-03-14 11:14:00 +01:00
committed by GitHub
parent a6c16c52e6
commit da53b3fb5e
2 changed files with 23 additions and 41 deletions
+20 -23
View File
@@ -13,6 +13,7 @@ import (
"github.com/grafana/grafana/apps/advisor/pkg/app/checks"
"github.com/grafana/grafana/apps/advisor/pkg/app/checkscheduler"
"github.com/grafana/grafana/apps/advisor/pkg/app/checktyperegisterer"
"github.com/grafana/grafana/pkg/apimachinery/identity"
"github.com/grafana/grafana/pkg/infra/log"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/klog/v2"
@@ -25,11 +26,6 @@ func New(cfg app.Config) (app.App, error) {
return nil, fmt.Errorf("invalid config type")
}
checkRegistry := specificConfig.CheckRegistry
stackID := specificConfig.StackID
namespace, err := checks.GetNamespace(stackID)
if err != nil {
return nil, err
}
log := log.New("advisor.app")
// Prepare storage client
@@ -59,28 +55,29 @@ func New(cfg app.Config) (app.App, error) {
Validator: &simple.Validator{
ValidateFunc: func(ctx context.Context, req *app.AdmissionRequest) error {
if req.Object != nil {
_, err := getCheck(req.Object, checkMap)
return err
check, err := getCheck(req.Object, checkMap)
if err != nil {
return err
}
if req.Action == resource.AdmissionActionCreate {
go func() {
log.Debug("Processing check", "namespace", req.Object.GetNamespace())
requester, err := identity.GetRequester(ctx)
if err != nil {
log.Error("Error getting requester", "error", err)
return
}
ctx = identity.WithRequester(context.Background(), requester)
err = processCheck(ctx, client, req.Object, check)
if err != nil {
log.Error("Error processing check", "error", err)
}
}()
}
}
return nil
},
},
Watcher: &simple.Watcher{
AddFunc: func(ctx context.Context, obj resource.Object) error {
log.Debug("Adding check", "namespace", obj.GetNamespace())
if obj.GetNamespace() != namespace {
log.Debug("Skipping check in namespace", "namespace", obj.GetNamespace())
return nil
} else {
log.Debug("Processing check in namespace", "namespace", obj.GetNamespace())
}
check, err := getCheck(obj, checkMap)
if err != nil {
return err
}
return processCheck(ctx, client, obj, check)
},
},
},
{
Kind: advisorv0alpha1.CheckTypeKind(),
+3 -18
View File
@@ -6,13 +6,9 @@ import (
"fmt"
"sync"
claims "github.com/grafana/authlib/types"
"github.com/grafana/grafana-app-sdk/resource"
advisorv0alpha1 "github.com/grafana/grafana/apps/advisor/pkg/apis/advisor/v0alpha1"
"github.com/grafana/grafana/apps/advisor/pkg/app/checks"
"github.com/grafana/grafana/pkg/apimachinery/identity"
"github.com/grafana/grafana/pkg/apimachinery/utils"
"github.com/grafana/grafana/pkg/services/user"
)
func getCheck(obj resource.Object, checkMap map[string]checks.Check) (checks.Check, error) {
@@ -39,6 +35,9 @@ func getStatusAnnotation(obj resource.Object) string {
func setStatusAnnotation(ctx context.Context, client resource.Client, obj resource.Object, status string) error {
annotations := obj.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
annotations[checks.StatusAnnotation] = status
return client.PatchInto(ctx, obj.GetStaticMetadata().Identifier(), resource.PatchRequest{
Operations: []resource.PatchOperation{{
@@ -59,20 +58,6 @@ func processCheck(ctx context.Context, client resource.Client, obj resource.Obje
if !ok {
return fmt.Errorf("invalid object type")
}
// Populate ctx with the user that created the check
meta, err := utils.MetaAccessor(obj)
if err != nil {
return err
}
createdBy := meta.GetCreatedBy()
typ, uid, err := claims.ParseTypeID(createdBy)
if err != nil {
return err
}
ctx = identity.WithRequester(ctx, &user.SignedInUser{
UserUID: uid,
FallbackType: typ,
})
// Get the items to check
items, err := check.Items(ctx)
if err != nil {