mirror of https://github.com/grafana/grafana
Playlists: convert to use reconcilers instead (#98075)
parent
c172bbba50
commit
24bf337c56
@ -0,0 +1,46 @@ |
||||
package reconcilers |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"k8s.io/klog/v2" |
||||
|
||||
"github.com/grafana/grafana-app-sdk/operator" |
||||
playlist "github.com/grafana/grafana/apps/playlist/pkg/apis/playlist/v0alpha1" |
||||
) |
||||
|
||||
func NewPlaylistReconciler(patchClient operator.PatchClient) (operator.Reconciler, error) { |
||||
inner := operator.TypedReconciler[*playlist.Playlist]{} |
||||
|
||||
inner.ReconcileFunc = func(ctx context.Context, request operator.TypedReconcileRequest[*playlist.Playlist]) (operator.ReconcileResult, error) { |
||||
switch request.Action { |
||||
case operator.ReconcileActionCreated: |
||||
klog.InfoS("Added resource", "name", request.Object.GetStaticMetadata().Identifier().Name) |
||||
return operator.ReconcileResult{}, nil |
||||
case operator.ReconcileActionUpdated: |
||||
klog.InfoS("Updated resource", "name", request.Object.GetStaticMetadata().Identifier().Name) |
||||
return operator.ReconcileResult{}, nil |
||||
case operator.ReconcileActionDeleted: |
||||
klog.InfoS("Deleted resource", "name", request.Object.GetStaticMetadata().Identifier().Name) |
||||
return operator.ReconcileResult{}, nil |
||||
case operator.ReconcileActionResynced: |
||||
klog.InfoS("Possibly updated resource", "name", request.Object.GetStaticMetadata().Identifier().Name) |
||||
return operator.ReconcileResult{}, nil |
||||
case operator.ReconcileActionUnknown: |
||||
klog.InfoS("error reconciling unknown action for Playlist", "action", request.Action, "object", request.Object) |
||||
return operator.ReconcileResult{}, nil |
||||
} |
||||
|
||||
klog.InfoS("error reconciling invalid action for Playlist", "action", request.Action, "object", request.Object) |
||||
return operator.ReconcileResult{}, nil |
||||
} |
||||
|
||||
// prefixing the finalizer with <group>-<kind> similar to how OpinionatedWatcher does
|
||||
reconciler, err := operator.NewOpinionatedReconciler(patchClient, "playlist-playlists-finalizer") |
||||
if err != nil { |
||||
klog.ErrorS(err, "Error creating opinionated reconciler for playlists") |
||||
return nil, err |
||||
} |
||||
reconciler.Reconciler = &inner |
||||
return reconciler, nil |
||||
} |
@ -1,75 +0,0 @@ |
||||
package watchers |
||||
|
||||
import ( |
||||
"context" |
||||
"fmt" |
||||
|
||||
"github.com/grafana/grafana-app-sdk/operator" |
||||
"github.com/grafana/grafana-app-sdk/resource" |
||||
"k8s.io/klog/v2" |
||||
|
||||
playlist "github.com/grafana/grafana/apps/playlist/pkg/apis/playlist/v0alpha1" |
||||
) |
||||
|
||||
var _ operator.ResourceWatcher = &PlaylistWatcher{} |
||||
|
||||
type PlaylistWatcher struct{} |
||||
|
||||
func NewPlaylistWatcher() (*PlaylistWatcher, error) { |
||||
return &PlaylistWatcher{}, nil |
||||
} |
||||
|
||||
// Add handles add events for playlist.Playlist resources.
|
||||
func (s *PlaylistWatcher) Add(ctx context.Context, rObj resource.Object) error { |
||||
object, ok := rObj.(*playlist.Playlist) |
||||
if !ok { |
||||
return fmt.Errorf("provided object is not of type *playlist.Playlist (name=%s, namespace=%s, kind=%s)", |
||||
rObj.GetStaticMetadata().Name, rObj.GetStaticMetadata().Namespace, rObj.GetStaticMetadata().Kind) |
||||
} |
||||
|
||||
klog.InfoS("Added resource", "name", object.GetStaticMetadata().Identifier().Name) |
||||
return nil |
||||
} |
||||
|
||||
// Update handles update events for playlist.Playlist resources.
|
||||
func (s *PlaylistWatcher) Update(ctx context.Context, rOld resource.Object, rNew resource.Object) error { |
||||
oldObject, ok := rOld.(*playlist.Playlist) |
||||
if !ok { |
||||
return fmt.Errorf("provided object is not of type *playlist.Playlist (name=%s, namespace=%s, kind=%s)", |
||||
rOld.GetStaticMetadata().Name, rOld.GetStaticMetadata().Namespace, rOld.GetStaticMetadata().Kind) |
||||
} |
||||
|
||||
_, ok = rNew.(*playlist.Playlist) |
||||
if !ok { |
||||
return fmt.Errorf("provided object is not of type *playlist.Playlist (name=%s, namespace=%s, kind=%s)", |
||||
rNew.GetStaticMetadata().Name, rNew.GetStaticMetadata().Namespace, rNew.GetStaticMetadata().Kind) |
||||
} |
||||
|
||||
klog.InfoS("Updated resource", "name", oldObject.GetStaticMetadata().Identifier().Name) |
||||
return nil |
||||
} |
||||
|
||||
// Delete handles delete events for playlist.Playlist resources.
|
||||
func (s *PlaylistWatcher) Delete(ctx context.Context, rObj resource.Object) error { |
||||
object, ok := rObj.(*playlist.Playlist) |
||||
if !ok { |
||||
return fmt.Errorf("provided object is not of type *playlist.Playlist (name=%s, namespace=%s, kind=%s)", |
||||
rObj.GetStaticMetadata().Name, rObj.GetStaticMetadata().Namespace, rObj.GetStaticMetadata().Kind) |
||||
} |
||||
|
||||
klog.InfoS("Deleted resource", "name", object.GetStaticMetadata().Identifier().Name) |
||||
return nil |
||||
} |
||||
|
||||
// Sync is not a standard resource.Watcher function, but is used when wrapping this watcher in an operator.OpinionatedWatcher.
|
||||
// It handles resources which MAY have been updated during an outage period where the watcher was not able to consume events.
|
||||
func (s *PlaylistWatcher) Sync(ctx context.Context, rObj resource.Object) error { |
||||
object, ok := rObj.(*playlist.Playlist) |
||||
if !ok { |
||||
return fmt.Errorf("provided object is not of type *playlist.Playlist (name=%s, namespace=%s, kind=%s)", |
||||
rObj.GetStaticMetadata().Name, rObj.GetStaticMetadata().Namespace, rObj.GetStaticMetadata().Kind) |
||||
} |
||||
|
||||
klog.InfoS("Possible resource update", "name", object.GetStaticMetadata().Identifier().Name) |
||||
return nil |
||||
} |
|
Loading…
Reference in new issue