Playlist: Add mutation and validation (#95708)

This commit is contained in:
Todd Treece 2024-11-05 17:45:09 -05:00 committed by GitHub
parent c125ead121
commit 280c27507c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 3 deletions

View File

@ -5,6 +5,8 @@ externalName: {
group: "playlist"
apiResource: {
groupOverride: "playlist.grafana.app"
mutation: operations: ["create","update"]
validation: operations: ["create","update"]
}
codegen: {
frontend: false

View File

@ -28,6 +28,20 @@ var appManifestData = app.ManifestData{
Versions: []app.ManifestKindVersion{
{
Name: "v0alpha1",
Admission: &app.AdmissionCapabilities{
Validation: &app.ValidationCapability{
Operations: []app.AdmissionOperation{
app.AdmissionOperationCreate,
app.AdmissionOperationUpdate,
},
},
Mutation: &app.MutationCapability{
Operations: []app.AdmissionOperation{
app.AdmissionOperationCreate,
app.AdmissionOperationUpdate,
},
},
},
Schema: &versionSchemaPlaylistv0alpha1,
},
},

View File

@ -5,11 +5,11 @@ import (
"fmt"
"github.com/grafana/grafana-app-sdk/app"
"github.com/grafana/grafana-app-sdk/logging"
"github.com/grafana/grafana-app-sdk/operator"
"github.com/grafana/grafana-app-sdk/resource"
"github.com/grafana/grafana-app-sdk/simple"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/klog/v2"
playlistv0alpha1 "github.com/grafana/grafana/apps/playlist/pkg/apis/playlist/v0alpha1"
"github.com/grafana/grafana/apps/playlist/pkg/watchers"
@ -38,13 +38,27 @@ func New(cfg app.Config) (app.App, error) {
KubeConfig: cfg.KubeConfig,
InformerConfig: simple.AppInformerConfig{
ErrorHandler: func(ctx context.Context, err error) {
logging.DefaultLogger.With("error", err).Error("Informer processing error")
klog.ErrorS(err, "Informer processing error")
},
},
ManagedKinds: []simple.AppManagedKind{
{
Kind: playlistv0alpha1.PlaylistKind(),
Watcher: playlistWatcher,
Mutator: &simple.Mutator{
MutateFunc: func(ctx context.Context, req *app.AdmissionRequest) (*app.MutatingResponse, error) {
// modify req.Object if needed
return &app.MutatingResponse{
UpdatedObject: req.Object,
}, nil
},
},
Validator: &simple.Validator{
ValidateFunc: func(ctx context.Context, req *app.AdmissionRequest) error {
// do something here if needed
return nil
},
},
},
},
}