@@ -27,6 +27,7 @@ import (
27
27
28
28
const (
29
29
PLUGIN_SERVER_ADDRESS = "VCLUSTER_PLUGIN_ADDRESS"
30
+ PLUGIN_NAME = "VCLUSTER_PLUGIN_NAME"
30
31
)
31
32
32
33
var defaultManager Manager = & manager {}
@@ -35,18 +36,27 @@ type Options struct {
35
36
// ListenAddress is optional and the address where to contact
36
37
// the vcluster plugin server at. Defaults to localhost:10099
37
38
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
38
48
}
39
49
40
50
type LeaderElectionHook func (ctx context.Context ) error
41
51
42
52
type Manager interface {
43
53
// Init creates a new plugin context and will block until the
44
54
// vcluster container instance could be contacted.
45
- Init (name string ) (* synccontext.RegisterContext , error )
55
+ Init () (* synccontext.RegisterContext , error )
46
56
47
57
// InitWithOptions creates a new plugin context and will block until the
48
58
// vcluster container instance could be contacted.
49
- InitWithOptions (name string , opts Options ) (* synccontext.RegisterContext , error )
59
+ InitWithOptions (opts Options ) (* synccontext.RegisterContext , error )
50
60
51
61
// Register makes sure the syncer will be executed as soon as start
52
62
// is run.
@@ -58,21 +68,21 @@ type Manager interface {
58
68
Start () error
59
69
}
60
70
61
- func MustInit (name string ) * synccontext.RegisterContext {
62
- ctx , err := defaultManager .Init (name )
71
+ func MustInit () * synccontext.RegisterContext {
72
+ ctx , err := defaultManager .Init ()
63
73
if err != nil {
64
74
panic (err )
65
75
}
66
76
67
77
return ctx
68
78
}
69
79
70
- func Init (name string ) (* synccontext.RegisterContext , error ) {
71
- return defaultManager .Init (name )
80
+ func Init () (* synccontext.RegisterContext , error ) {
81
+ return defaultManager .Init ()
72
82
}
73
83
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 )
76
86
}
77
87
78
88
func MustRegister (syncer syncer.Base ) {
@@ -110,13 +120,14 @@ type manager struct {
110
120
hooks []* remote.ClientHook
111
121
}
112
122
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 {})
115
125
}
116
126
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 )
118
129
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 )
120
131
}
121
132
122
133
m .guard .Lock ()
@@ -206,6 +217,8 @@ func (m *manager) InitWithOptions(name string, opts Options) (*synccontext.Regis
206
217
MetricsBindAddress : "0" ,
207
218
LeaderElection : false ,
208
219
Namespace : virtualClusterOptions .TargetNamespace ,
220
+ NewClient : opts .NewClient ,
221
+ NewCache : opts .NewCache ,
209
222
})
210
223
if err != nil {
211
224
return nil , errors .Wrap (err , "create phyiscal manager" )
@@ -214,11 +227,13 @@ func (m *manager) InitWithOptions(name string, opts Options) (*synccontext.Regis
214
227
Scheme : Scheme ,
215
228
MetricsBindAddress : "0" ,
216
229
LeaderElection : false ,
230
+ NewClient : opts .NewClient ,
231
+ NewCache : opts .NewCache ,
217
232
})
218
233
if err != nil {
219
234
return nil , errors .Wrap (err , "create virtual manager" )
220
235
}
221
- currentNamespaceClient , err := newCurrentNamespaceClient (context .Background (), pluginContext .CurrentNamespace , physicalManager , virtualClusterOptions )
236
+ currentNamespaceClient , err := newCurrentNamespaceClient (context .Background (), pluginContext .CurrentNamespace , physicalManager , virtualClusterOptions , opts )
222
237
if err != nil {
223
238
return nil , errors .Wrap (err , "create namespaced client" )
224
239
}
@@ -538,7 +553,7 @@ func (m *manager) start() error {
538
553
return nil
539
554
}
540
555
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 ) {
542
557
var err error
543
558
544
559
// currentNamespaceCache is needed for tasks such as finding out fake kubelet ips
@@ -549,7 +564,11 @@ func newCurrentNamespaceClient(ctx context.Context, currentNamespace string, loc
549
564
// objects from the current namespace.
550
565
currentNamespaceCache := localManager .GetCache ()
551
566
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 {
553
572
Scheme : localManager .GetScheme (),
554
573
Mapper : localManager .GetRESTMapper (),
555
574
Namespace : currentNamespace ,
@@ -571,13 +590,12 @@ func newCurrentNamespaceClient(ctx context.Context, currentNamespace string, loc
571
590
}
572
591
573
592
// 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 {
575
598
Scheme : localManager .GetScheme (),
576
599
Mapper : localManager .GetRESTMapper (),
577
600
})
578
- if err != nil {
579
- return nil , err
580
- }
581
-
582
- return currentNamespaceClient , nil
583
601
}
0 commit comments