Skip to content

Commit 7bdb465

Browse files
authored
Merge pull request #30 from FabianKramm/hooks
refactor: remove name & allow client override
2 parents 1e1bf12 + 5737b7d commit 7bdb465

File tree

1 file changed

+39
-21
lines changed

1 file changed

+39
-21
lines changed

plugin/plugin.go

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
const (
2929
PLUGIN_SERVER_ADDRESS = "VCLUSTER_PLUGIN_ADDRESS"
30+
PLUGIN_NAME = "VCLUSTER_PLUGIN_NAME"
3031
)
3132

3233
var defaultManager Manager = &manager{}
@@ -35,18 +36,27 @@ type Options struct {
3536
// ListenAddress is optional and the address where to contact
3637
// the vcluster plugin server at. Defaults to localhost:10099
3738
ListenAddress string
39+
40+
// NewClient is the func that creates the client to be used by the manager.
41+
// If not set this will create the default DelegatingClient that will
42+
// use the cache for reads and the client for writes.
43+
NewClient cluster.NewClientFunc
44+
45+
// NewCache is the function that will create the cache to be used
46+
// by the manager. If not set this will use the default new cache function.
47+
NewCache cache.NewCacheFunc
3848
}
3949

4050
type LeaderElectionHook func(ctx context.Context) error
4151

4252
type Manager interface {
4353
// Init creates a new plugin context and will block until the
4454
// vcluster container instance could be contacted.
45-
Init(name string) (*synccontext.RegisterContext, error)
55+
Init() (*synccontext.RegisterContext, error)
4656

4757
// InitWithOptions creates a new plugin context and will block until the
4858
// vcluster container instance could be contacted.
49-
InitWithOptions(name string, opts Options) (*synccontext.RegisterContext, error)
59+
InitWithOptions(opts Options) (*synccontext.RegisterContext, error)
5060

5161
// Register makes sure the syncer will be executed as soon as start
5262
// is run.
@@ -58,21 +68,21 @@ type Manager interface {
5868
Start() error
5969
}
6070

61-
func MustInit(name string) *synccontext.RegisterContext {
62-
ctx, err := defaultManager.Init(name)
71+
func MustInit() *synccontext.RegisterContext {
72+
ctx, err := defaultManager.Init()
6373
if err != nil {
6474
panic(err)
6575
}
6676

6777
return ctx
6878
}
6979

70-
func Init(name string) (*synccontext.RegisterContext, error) {
71-
return defaultManager.Init(name)
80+
func Init() (*synccontext.RegisterContext, error) {
81+
return defaultManager.Init()
7282
}
7383

74-
func InitWithOptions(name string, opts Options) (*synccontext.RegisterContext, error) {
75-
return defaultManager.InitWithOptions(name, opts)
84+
func InitWithOptions(opts Options) (*synccontext.RegisterContext, error) {
85+
return defaultManager.InitWithOptions(opts)
7686
}
7787

7888
func MustRegister(syncer syncer.Base) {
@@ -110,13 +120,14 @@ type manager struct {
110120
hooks []*remote.ClientHook
111121
}
112122

113-
func (m *manager) Init(name string) (*synccontext.RegisterContext, error) {
114-
return m.InitWithOptions(name, Options{})
123+
func (m *manager) Init() (*synccontext.RegisterContext, error) {
124+
return m.InitWithOptions(Options{})
115125
}
116126

117-
func (m *manager) InitWithOptions(name string, opts Options) (*synccontext.RegisterContext, error) {
127+
func (m *manager) InitWithOptions(opts Options) (*synccontext.RegisterContext, error) {
128+
name := os.Getenv(PLUGIN_NAME)
118129
if name == "" {
119-
return nil, fmt.Errorf("please provide a plugin name")
130+
return nil, fmt.Errorf("plugin name wasn't found in environment. vcluster-sdk expects the vcluster to set the %s environment variable", PLUGIN_NAME)
120131
}
121132

122133
m.guard.Lock()
@@ -206,6 +217,8 @@ func (m *manager) InitWithOptions(name string, opts Options) (*synccontext.Regis
206217
MetricsBindAddress: "0",
207218
LeaderElection: false,
208219
Namespace: virtualClusterOptions.TargetNamespace,
220+
NewClient: opts.NewClient,
221+
NewCache: opts.NewCache,
209222
})
210223
if err != nil {
211224
return nil, errors.Wrap(err, "create phyiscal manager")
@@ -214,11 +227,13 @@ func (m *manager) InitWithOptions(name string, opts Options) (*synccontext.Regis
214227
Scheme: Scheme,
215228
MetricsBindAddress: "0",
216229
LeaderElection: false,
230+
NewClient: opts.NewClient,
231+
NewCache: opts.NewCache,
217232
})
218233
if err != nil {
219234
return nil, errors.Wrap(err, "create virtual manager")
220235
}
221-
currentNamespaceClient, err := newCurrentNamespaceClient(context.Background(), pluginContext.CurrentNamespace, physicalManager, virtualClusterOptions)
236+
currentNamespaceClient, err := newCurrentNamespaceClient(context.Background(), pluginContext.CurrentNamespace, physicalManager, virtualClusterOptions, opts)
222237
if err != nil {
223238
return nil, errors.Wrap(err, "create namespaced client")
224239
}
@@ -538,7 +553,7 @@ func (m *manager) start() error {
538553
return nil
539554
}
540555

541-
func newCurrentNamespaceClient(ctx context.Context, currentNamespace string, localManager ctrl.Manager, options *synccontext.VirtualClusterOptions) (client.Client, error) {
556+
func newCurrentNamespaceClient(ctx context.Context, currentNamespace string, localManager ctrl.Manager, options *synccontext.VirtualClusterOptions, opts Options) (client.Client, error) {
542557
var err error
543558

544559
// currentNamespaceCache is needed for tasks such as finding out fake kubelet ips
@@ -549,7 +564,11 @@ func newCurrentNamespaceClient(ctx context.Context, currentNamespace string, loc
549564
// objects from the current namespace.
550565
currentNamespaceCache := localManager.GetCache()
551566
if currentNamespace != options.TargetNamespace {
552-
currentNamespaceCache, err = cache.New(localManager.GetConfig(), cache.Options{
567+
if opts.NewCache == nil {
568+
opts.NewCache = cache.New
569+
}
570+
571+
currentNamespaceCache, err = opts.NewCache(localManager.GetConfig(), cache.Options{
553572
Scheme: localManager.GetScheme(),
554573
Mapper: localManager.GetRESTMapper(),
555574
Namespace: currentNamespace,
@@ -571,13 +590,12 @@ func newCurrentNamespaceClient(ctx context.Context, currentNamespace string, loc
571590
}
572591

573592
// create a current namespace client
574-
currentNamespaceClient, err := cluster.DefaultNewClient(currentNamespaceCache, localManager.GetConfig(), client.Options{
593+
if opts.NewClient == nil {
594+
opts.NewClient = cluster.DefaultNewClient
595+
}
596+
597+
return opts.NewClient(currentNamespaceCache, localManager.GetConfig(), client.Options{
575598
Scheme: localManager.GetScheme(),
576599
Mapper: localManager.GetRESTMapper(),
577600
})
578-
if err != nil {
579-
return nil, err
580-
}
581-
582-
return currentNamespaceClient, nil
583601
}

0 commit comments

Comments
 (0)