Correlations: Add labels to CREATE and UPDATE operations for searching (#117053)

add label mutator to api
This commit is contained in:
Kristina 2026-01-29 08:51:54 -06:00 committed by GitHub
parent 9cae78ca0f
commit 7ce5a80f5d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 63 additions and 3 deletions

View file

@ -3,6 +3,12 @@ package kinds
correlationsv0alpha1: {
kind: "Correlation" // note: must be uppercase
pluralName: "Correlations"
mutation: {
operations: [
"CREATE",
"UPDATE",
]
}
schema: {
spec: {
type: CorrelationType

View file

@ -23,6 +23,12 @@ type Correlation struct {
Spec CorrelationSpec `json:"spec" yaml:"spec"`
}
func NewCorrelation() *Correlation {
return &Correlation{
Spec: *NewCorrelationSpec(),
}
}
func (o *Correlation) GetSpec() any {
return o.Spec
}

View file

@ -10,7 +10,7 @@ import (
// schema is unexported to prevent accidental overwrites
var (
schemaCorrelation = resource.NewSimpleSchema("correlations.grafana.app", "v0alpha1", &Correlation{}, &CorrelationList{}, resource.WithKind("Correlation"),
schemaCorrelation = resource.NewSimpleSchema("correlations.grafana.app", "v0alpha1", NewCorrelation(), &CorrelationList{}, resource.WithKind("Correlation"),
resource.WithPlural("correlations"), resource.WithScope(resource.NamespacedScope))
kindCorrelation = resource.Kind{
Schema: schemaCorrelation,

View file

@ -39,7 +39,16 @@ var appManifestData = app.ManifestData{
Plural: "Correlations",
Scope: "Namespaced",
Conversion: false,
Schema: &versionSchemaCorrelationv0alpha1,
Admission: &app.AdmissionCapabilities{
Mutation: &app.MutationCapability{
Operations: []app.AdmissionOperation{
app.AdmissionOperationCreate,
app.AdmissionOperationUpdate,
},
},
},
Schema: &versionSchemaCorrelationv0alpha1,
SelectableFields: []string{
"spec.datasource.name",
},

View file

@ -2,6 +2,7 @@ package app
import (
"context"
"fmt"
"github.com/grafana/grafana-app-sdk/app"
"github.com/grafana/grafana-app-sdk/logging"
@ -26,7 +27,8 @@ func New(cfg app.Config) (app.App, error) {
},
ManagedKinds: []simple.AppManagedKind{
{
Kind: correlationsv0alpha1.CorrelationKind(),
Kind: correlationsv0alpha1.CorrelationKind(),
Mutator: DataSourceMutator(),
},
},
}
@ -53,3 +55,40 @@ func GetKinds() map[schema.GroupVersion][]resource.Kind {
gv: {correlationsv0alpha1.CorrelationKind()},
}
}
const (
// SourceRefLabelKey is the label key for the composite source reference (group.name)
SourceRefLabelKey = "correlations.grafana.app/sourceDS-ref"
// TargetRefLabelKey is the label key for the composite target reference (group.name)
TargetRefLabelKey = "correlations.grafana.app/targetDS-ref"
)
func DataSourceMutator() *simple.Mutator {
return &simple.Mutator{
MutateFunc: func(ctx context.Context, req *app.AdmissionRequest) (*app.MutatingResponse, error) {
c, ok := req.Object.(*correlationsv0alpha1.Correlation)
if !ok || c == nil {
return nil, nil
}
if c.Labels == nil {
c.Labels = make(map[string]string)
}
// Derive source label: "group.name" format
c.Labels[SourceRefLabelKey] = fmt.Sprintf("%s.%s",
c.Spec.Source.Group,
c.Spec.Source.Name)
// Derive target label if target is present
if c.Spec.Target != nil {
c.Labels[TargetRefLabelKey] = fmt.Sprintf("%s.%s",
c.Spec.Target.Group,
c.Spec.Target.Name)
}
return &app.MutatingResponse{UpdatedObject: c}, nil
},
}
}