Created
November 13, 2024 04:15
-
-
Save charandas/db3e382793553a96802de83fe7c457aa to your computer and use it in GitHub Desktop.
Revisions
-
charandas created this gist
Nov 13, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,94 @@ package app import ( "context" "errors" "fmt" "reflect" "github.com/grafana/grafana-app-sdk/app" "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" ) type PlaylistConfig struct { EnableWatchers bool } func New(cfg app.Config) (app.App, error) { playlistConfig, ok := cfg.SpecificConfig.(*PlaylistConfig) if !ok { return nil, errors.New("could not load project's specific config, type assertion failed: type of SpecificConfig=" + reflect.TypeOf(cfg.SpecificConfig).Name()) } managedKinds := make([]simple.AppManagedKind, 0) { managedKind := simple.AppManagedKind{ Kind: playlistv0alpha1.PlaylistKind(), 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 }, }, } managedKinds = append(managedKinds, managedKind) if playlistConfig.EnableWatchers { playlistWatcher, err := watchers.NewPlaylistWatcher() if err != nil { return nil, fmt.Errorf("unable to create PlaylistWatcher: %w", err) } managedKind.Watcher = playlistWatcher } } config := simple.AppConfig{ Name: "playlist", KubeConfig: cfg.KubeConfig, InformerConfig: simple.AppInformerConfig{ ErrorHandler: func(ctx context.Context, err error) { // FIXME: add your own error handling here klog.ErrorS(err, "Informer processing error") }, }, ManagedKinds: managedKinds, } // Create the App a, err := simple.NewApp(config) if err != nil { return nil, err } // Validate the capabilities against the provided manifest to make sure there isn't a mismatch err = a.ValidateManifest(cfg.ManifestData) return a, err } func GetKinds() map[schema.GroupVersion]resource.Kind { gv := schema.GroupVersion{ Group: playlistv0alpha1.PlaylistKind().Group(), Version: playlistv0alpha1.PlaylistKind().Version(), } return map[schema.GroupVersion]resource.Kind{ gv: playlistv0alpha1.PlaylistKind(), } }