Skip to content

Instantly share code, notes, and snippets.

@charandas
Created November 13, 2024 04:15
Show Gist options
  • Save charandas/db3e382793553a96802de83fe7c457aa to your computer and use it in GitHub Desktop.
Save charandas/db3e382793553a96802de83fe7c457aa to your computer and use it in GitHub Desktop.
app.go generated by grafanaApp target
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(),
}
}
@charandas
Copy link
Author

The extra braces on L32 and L59 are just so variables for any number of kinds don't have to come up with unique names. Is there a better fix for this, or can we live with the extra braces?

I tend to think the only real fix could be that the kinds were iterated here in .go instead of in the template but there is a reason they are iterated over in the template: on this side, they are packages which are way harder to iterate over.

Could always spit more files to make individual ManagedKind for each of the groupversion and then simply import here though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment