Skip to content

Commit

Permalink
Add option for custom status readers
Browse files Browse the repository at this point in the history
This commit adds the `CustomStatusReadersFactoryFunc` option to the
StatusPoller's `Poll` method options so that consumers can provide
custom status readers for certain GroupKinds (esp. CRDs).

It also changes the `NewGenericStatusReader` func so that it accepts a
statusFunc parameter so that consumers only have to implement that
single func and use the genericStatusReader for all the boilerplate
stuff (like fetching an object from the API).

refs #382
  • Loading branch information
Max Jonas Werner committed Jul 5, 2021
1 parent c1a7c2d commit 5b7b824
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
17 changes: 15 additions & 2 deletions pkg/kstatus/polling/polling.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/engine"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/event"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/statusreaders"
"sigs.k8s.io/cli-utils/pkg/kstatus/status"
"sigs.k8s.io/cli-utils/pkg/object"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand All @@ -38,10 +39,20 @@ type StatusPoller struct {
// back on the event channel returned. The statusPollerRunner can be cancelled at any time by cancelling the
// context passed in.
func (s *StatusPoller) Poll(ctx context.Context, identifiers []object.ObjMetadata, options Options) <-chan event.Event {
statusReaderFactory := createStatusReaders
if options.CustomStatusReadersFactoryFunc != nil {
statusReaderFactory = func(reader engine.ClusterReader, mapper meta.RESTMapper) (map[schema.GroupKind]engine.StatusReader, engine.StatusReader) {
readers, defaultReader := createStatusReaders(reader, mapper)
for gk, r := range options.CustomStatusReadersFactoryFunc(reader, mapper) {
readers[gk] = r
}
return readers, defaultReader
}
}
return s.engine.Poll(ctx, identifiers, engine.Options{
PollInterval: options.PollInterval,
ClusterReaderFactoryFunc: clusterReaderFactoryFunc(options.UseCache),
StatusReadersFactoryFunc: createStatusReaders,
StatusReadersFactoryFunc: statusReaderFactory,
})
}

Expand All @@ -56,6 +67,8 @@ type Options struct {
// all needed resources before each polling cycle. If this is set to false,
// then each resource will be fetched when needed with GET calls.
UseCache bool

CustomStatusReadersFactoryFunc func(engine.ClusterReader, meta.RESTMapper) map[schema.GroupKind]engine.StatusReader
}

// createStatusReaders creates an instance of all the statusreaders. This includes a set of statusreaders for
Expand All @@ -64,7 +77,7 @@ type Options struct {
// TODO: We should consider making the registration more automatic instead of having to create each of them
// here. Also, it might be worth creating them on demand.
func createStatusReaders(reader engine.ClusterReader, mapper meta.RESTMapper) (map[schema.GroupKind]engine.StatusReader, engine.StatusReader) {
defaultStatusReader := statusreaders.NewGenericStatusReader(reader, mapper)
defaultStatusReader := statusreaders.NewGenericStatusReader(reader, mapper, status.Compute)

replicaSetStatusReader := statusreaders.NewReplicaSetStatusReader(reader, mapper, defaultStatusReader)
deploymentStatusReader := statusreaders.NewDeploymentResourceReader(reader, mapper, replicaSetStatusReader)
Expand Down
8 changes: 5 additions & 3 deletions pkg/kstatus/polling/statusreaders/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ import (
"sigs.k8s.io/cli-utils/pkg/object"
)

func NewGenericStatusReader(reader engine.ClusterReader, mapper meta.RESTMapper) engine.StatusReader {
type StatusFunc func(u *unstructured.Unstructured) (*status.Result, error)

func NewGenericStatusReader(reader engine.ClusterReader, mapper meta.RESTMapper, statusFunc StatusFunc) engine.StatusReader {
return &baseStatusReader{
reader: reader,
mapper: mapper,
resourceStatusReader: &genericStatusReader{
reader: reader,
mapper: mapper,
statusFunc: status.Compute,
statusFunc: statusFunc,
},
}
}
Expand All @@ -36,7 +38,7 @@ type genericStatusReader struct {
reader engine.ClusterReader
mapper meta.RESTMapper

statusFunc func(u *unstructured.Unstructured) (*status.Result, error)
statusFunc StatusFunc
}

var _ resourceTypeStatusReader = &genericStatusReader{}
Expand Down

0 comments on commit 5b7b824

Please sign in to comment.