Skip to content

Commit 1ecfafd

Browse files
committed
Add olm operator plug-in framework
Signed-off-by: perdasilva <[email protected]> Upstream-repository: perdasilva Upstream-commit: bca9d5ddd849847a6c9fa92af38779ab2909c4d6
1 parent f8c466a commit 1ecfafd

File tree

11 files changed

+261
-20
lines changed

11 files changed

+261
-20
lines changed

staging/operator-lifecycle-manager/pkg/controller/operators/olm/config.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ type operatorConfig struct {
3737
configClient configv1client.Interface
3838
}
3939

40+
func (o *operatorConfig) OperatorClient() operatorclient.ClientInterface {
41+
return o.operatorClient
42+
}
43+
44+
func (o *operatorConfig) ExternalClient() versioned.Interface {
45+
return o.externalClient
46+
}
47+
48+
func (o *operatorConfig) ResyncPeriod() func() time.Duration {
49+
return o.resyncPeriod
50+
}
51+
52+
func (o *operatorConfig) WatchedNamespaces() []string {
53+
return o.watchedNamespaces
54+
}
55+
56+
func (o *operatorConfig) Logger() *logrus.Logger {
57+
return o.logger
58+
}
59+
4060
func (o *operatorConfig) apply(options []OperatorOption) {
4161
for _, option := range options {
4262
option(o)

staging/operator-lifecycle-manager/pkg/controller/operators/olm/operator.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88
"time"
99

10+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm/plugins"
1011
"github.com/sirupsen/logrus"
1112
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
1213
corev1 "k8s.io/api/core/v1"
@@ -60,6 +61,10 @@ var (
6061
ErrAPIServiceOwnerConflict = errors.New("unable to adopt APIService")
6162
)
6263

64+
// this unexported operator plugin slice provides an entrypoint for
65+
// downstream to inject its own plugins to augment the controller behavior
66+
var operatorPlugInFactoryFuncs []plugins.OperatorPlugInFactoryFunc
67+
6368
type Operator struct {
6469
queueinformer.Operator
6570

@@ -90,6 +95,7 @@ type Operator struct {
9095
clientAttenuator *scoped.ClientAttenuator
9196
serviceAccountQuerier *scoped.UserDefinedServiceAccountQuerier
9297
clientFactory clients.Factory
98+
plugins []plugins.OperatorPlugin
9399
}
94100

95101
func NewOperator(ctx context.Context, options ...OperatorOption) (*Operator, error) {
@@ -587,6 +593,31 @@ func newOperatorWithConfig(ctx context.Context, config *operatorConfig) (*Operat
587593
OverridesBuilderFunc: overridesBuilderFunc.GetDeploymentInitializer,
588594
}
589595

596+
// initialize plugins
597+
for _, makePlugIn := range operatorPlugInFactoryFuncs {
598+
plugin, err := makePlugIn(ctx, config, op)
599+
if err != nil {
600+
return nil, fmt.Errorf("error creating plugin: %s", err)
601+
}
602+
op.plugins = append(op.plugins, plugin)
603+
}
604+
605+
if len(operatorPlugInFactoryFuncs) > 0 {
606+
go func() {
607+
// block until operator is done
608+
<-op.Done()
609+
610+
// shutdown plug-ins
611+
for _, plugin := range op.plugins {
612+
if err := plugin.Shutdown(); err != nil {
613+
if op.logger != nil {
614+
op.logger.Warnf("error shutting down plug-in: %s", err)
615+
}
616+
}
617+
}
618+
}()
619+
}
620+
590621
return op, nil
591622
}
592623

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package olm
2+
3+
import (
4+
"context"
5+
6+
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/queueinformer"
7+
)
8+
9+
// HostOperator is an extensible and observable operator that hosts the plug-in, i.e. which the plug-in is extending
10+
type HostOperator interface {
11+
queueinformer.ObservableOperator
12+
queueinformer.ExtensibleOperator
13+
}
14+
15+
// OperatorPlugin provides a simple interface
16+
// that can be used to extend the olm operator's functionality
17+
type OperatorPlugin interface {
18+
// Shutdown is called once the host operator is done
19+
// to give the plug-in a change to clean up resources if necessary
20+
Shutdown() error
21+
}
22+
23+
// OperatorPlugInFactoryFunc factory function that returns a new instance of a plug-in
24+
type OperatorPlugInFactoryFunc func(ctx context.Context, config *OperatorConfig, hostOperator HostOperator) (OperatorPlugin, error)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package plugins
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
8+
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
9+
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/queueinformer"
10+
"github.com/sirupsen/logrus"
11+
)
12+
13+
// HostOperator is an extensible and observable operator that hosts the plug-in, i.e. which the plug-in is extending
14+
type HostOperator interface {
15+
queueinformer.ObservableOperator
16+
queueinformer.ExtensibleOperator
17+
}
18+
19+
// OperatorConfig gives access to required configuration from the host operator
20+
type OperatorConfig interface {
21+
OperatorClient() operatorclient.ClientInterface
22+
ExternalClient() versioned.Interface
23+
ResyncPeriod() func() time.Duration
24+
WatchedNamespaces() []string
25+
Logger() *logrus.Logger
26+
}
27+
28+
// OperatorPlugin provides a simple interface
29+
// that can be used to extend the olm operator's functionality
30+
type OperatorPlugin interface {
31+
// Shutdown is called once the host operator is done
32+
// to give the plug-in a change to clean up resources if necessary
33+
Shutdown() error
34+
}
35+
36+
// OperatorPlugInFactoryFunc factory function that returns a new instance of a plug-in
37+
type OperatorPlugInFactoryFunc func(ctx context.Context, config OperatorConfig, hostOperator HostOperator) (OperatorPlugin, error)

staging/operator-lifecycle-manager/pkg/lib/queueinformer/queueinformer_operator.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,19 @@ import (
1313
"k8s.io/client-go/tools/cache"
1414
)
1515

16-
// Operator describes a Reconciler that manages a set of QueueInformers.
17-
type Operator interface {
16+
// ExtensibleOperator describes a Reconciler that can be extended with additional informers and queue informers
17+
type ExtensibleOperator interface {
18+
// RegisterQueueInformer registers the given QueueInformer with the Operator.
19+
// This method returns an error if the Operator has already been started.
20+
RegisterQueueInformer(queueInformer *QueueInformer) error
21+
22+
// RegisterInformer registers an informer with the Operator.
23+
// This method returns an error if the Operator has already been started.
24+
RegisterInformer(cache.SharedIndexInformer) error
25+
}
26+
27+
// ObservableOperator describes a Reconciler whose state can be queried
28+
type ObservableOperator interface {
1829
// Ready returns a channel that is closed when the Operator is ready to run.
1930
Ready() <-chan struct{}
2031

@@ -29,15 +40,12 @@ type Operator interface {
2940

3041
// HasSynced returns true if the Operator's Informers have synced, false otherwise.
3142
HasSynced() bool
43+
}
3244

33-
// RegisterQueueInformer registers the given QueueInformer with the Operator.
34-
// This method returns an error if the Operator has already been started.
35-
RegisterQueueInformer(queueInformer *QueueInformer) error
36-
37-
// RegisterInformer registers an informer with the Operator.
38-
// This method returns an error if the Operator has already been started.
39-
RegisterInformer(cache.SharedIndexInformer) error
40-
45+
// Operator describes a Reconciler that manages a set of QueueInformers.
46+
type Operator interface {
47+
ObservableOperator
48+
ExtensibleOperator
4149
// RunInformers starts the Operator's underlying Informers.
4250
RunInformers(ctx context.Context)
4351

vendor/github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm/config.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm/operator.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm/operator_plugin.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm/plugins/operator_plugin.go

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/operator-framework/operator-lifecycle-manager/pkg/lib/queueinformer/queueinformer_operator.go

Lines changed: 18 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,7 @@ github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operator
689689
github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm
690690
github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm/overrides
691691
github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm/overrides/inject
692+
github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm/plugins
692693
github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/openshift
693694
github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry
694695
github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/grpc

0 commit comments

Comments
 (0)