Skip to content

Commit a06391b

Browse files
committed
pkg/clusterapi: refactor the cluster api system singleton
Previously the package assumed one could run a number of Cluster API systems at the same time. This changeset reworks this assumption around a singleton. The Cluster API system and its controllers should only ever run once for the entire lifecycle of a command. Signed-off-by: Vince Prignano <[email protected]>
1 parent 9cc3209 commit a06391b

File tree

2 files changed

+45
-23
lines changed

2 files changed

+45
-23
lines changed

pkg/clusterapi/localcontrolplane.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@ import (
2828
)
2929

3030
var (
31-
binDir string
31+
// Scheme is the scheme used by the local control plane.
32+
Scheme = scheme.Scheme
3233
)
3334

3435
func init() {
35-
utilruntime.Must(clusterv1alpha3.AddToScheme(scheme.Scheme))
36-
utilruntime.Must(clusterv1alpha4.AddToScheme(scheme.Scheme))
37-
utilruntime.Must(clusterv1.AddToScheme(scheme.Scheme))
38-
utilruntime.Must(capav1beta1.AddToScheme(scheme.Scheme))
39-
utilruntime.Must(capav1.AddToScheme(scheme.Scheme))
40-
utilruntime.Must(capzv1.AddToScheme(scheme.Scheme))
36+
utilruntime.Must(clusterv1alpha3.AddToScheme(Scheme))
37+
utilruntime.Must(clusterv1alpha4.AddToScheme(Scheme))
38+
utilruntime.Must(clusterv1.AddToScheme(Scheme))
39+
utilruntime.Must(capav1beta1.AddToScheme(Scheme))
40+
utilruntime.Must(capav1.AddToScheme(Scheme))
41+
utilruntime.Must(capzv1.AddToScheme(Scheme))
4142
}
4243

4344
// localControlPlane creates a local capi control plane
@@ -64,7 +65,7 @@ func (c *localControlPlane) Run(ctx context.Context) error {
6465
log.SetLogger(klog.NewKlogr())
6566
logrus.Info("Started local control plane with envtest")
6667
c.Env = &envtest.Environment{
67-
Scheme: scheme.Scheme,
68+
Scheme: Scheme,
6869
AttachControlPlaneOutput: false,
6970
BinaryAssetsDirectory: c.BinDir,
7071
ControlPlaneStartTimeout: 10 * time.Second,

pkg/clusterapi/system.go

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,37 @@ import (
2828
"github.com/openshift/installer/pkg/types/vsphere"
2929
)
3030

31-
// System creates a local capi control plane
31+
var (
32+
sys = &system{}
33+
)
34+
35+
// Interface is the interface for the cluster-api system.
36+
type Interface interface {
37+
Run(ctx context.Context, installConfig *installconfig.InstallConfig) error
38+
Client() client.Client
39+
Teardown()
40+
}
41+
42+
// System returns the cluster-api system.
43+
func System() Interface {
44+
return sys
45+
}
46+
47+
// system creates a local capi control plane
3248
// to use as a management cluster.
33-
type System struct {
34-
Client client.Client
49+
type system struct {
50+
client client.Client
3551

3652
componentDir string
3753
lcp *localControlPlane
3854

39-
wg sync.WaitGroup
40-
once sync.Once
41-
cancel context.CancelFunc
55+
wg sync.WaitGroup
56+
teardownOnce sync.Once
57+
cancel context.CancelFunc
4258
}
4359

4460
// Run launches the cluster-api system.
45-
func (c *System) Run(ctx context.Context, installConfig *installconfig.InstallConfig) (err error) {
61+
func (c *system) Run(ctx context.Context, installConfig *installconfig.InstallConfig) error {
4662
// Setup the context with a cancel function.
4763
ctx, cancel := context.WithCancel(ctx)
4864
c.cancel = cancel
@@ -52,7 +68,7 @@ func (c *System) Run(ctx context.Context, installConfig *installconfig.InstallCo
5268
if err := c.lcp.Run(ctx); err != nil {
5369
return fmt.Errorf("failed to run local control plane: %w", err)
5470
}
55-
c.Client = c.lcp.Client
71+
c.client = c.lcp.Client
5672

5773
// Create a temporary directory to unpack the cluster-api assets
5874
// and use it as the working directory for the envtest environment.
@@ -69,7 +85,7 @@ func (c *System) Run(ctx context.Context, installConfig *installconfig.InstallCo
6985
controllers := []*controller{
7086
{
7187
Name: "Cluster API",
72-
Path: fmt.Sprintf("%s/cluster-api", binDir),
88+
Path: fmt.Sprintf("%s/cluster-api", c.lcp.BinDir),
7389
Components: []string{c.componentDir + "/core-components.yaml"},
7490
Args: []string{
7591
"-v=2",
@@ -184,8 +200,13 @@ func (c *System) Run(ctx context.Context, installConfig *installconfig.InstallCo
184200
return nil
185201
}
186202

203+
// Client returns the client for the local control plane.
204+
func (c *system) Client() client.Client {
205+
return c.client
206+
}
207+
187208
// Teardown shuts down the local capi control plane and all its controllers.
188-
func (c *System) Teardown() {
209+
func (c *system) Teardown() {
189210
if c.lcp == nil {
190211
return
191212
}
@@ -194,7 +215,7 @@ func (c *System) Teardown() {
194215
defer os.RemoveAll(c.lcp.BinDir)
195216

196217
// Proceed to shutdown.
197-
c.once.Do(func() {
218+
c.teardownOnce.Do(func() {
198219
c.cancel()
199220
logrus.Info("Shutting down local Cluster API control plane...")
200221
ch := make(chan struct{})
@@ -218,7 +239,7 @@ func (c *System) Teardown() {
218239
// and have the name `cluster-api-provider-<name>`.
219240
//
220241
// While the manifests can be optional, we expect them to be in the manifests directory and named `<name>-infrastructure-components.yaml`.
221-
func (c *System) getInfrastructureController(provider *Provider, args []string, env map[string]string) *controller {
242+
func (c *system) getInfrastructureController(provider *Provider, args []string, env map[string]string) *controller {
222243
manifests := []string{}
223244
defaultManifestPath := filepath.Join(c.componentDir, fmt.Sprintf("/%s-infrastructure-components.yaml", provider.Name))
224245
if _, err := os.Stat(defaultManifestPath); err == nil {
@@ -227,7 +248,7 @@ func (c *System) getInfrastructureController(provider *Provider, args []string,
227248
return &controller{
228249
Provider: provider,
229250
Name: fmt.Sprintf("%s infrastructure provider", provider.Name),
230-
Path: fmt.Sprintf("%s/cluster-api-provider-%s", binDir, provider.Name),
251+
Path: fmt.Sprintf("%s/cluster-api-provider-%s", c.lcp.BinDir, provider.Name),
231252
Components: manifests,
232253
Args: args,
233254
Env: env,
@@ -248,10 +269,10 @@ type controller struct {
248269
}
249270

250271
// runController configures the controller, and waits for it to be ready.
251-
func (c *System) runController(ctx context.Context, ct *controller) error {
272+
func (c *system) runController(ctx context.Context, ct *controller) error {
252273
// If the provider is not empty, we extract it to the binaries directory.
253274
if ct.Provider != nil {
254-
if err := ct.Provider.Extract(binDir); err != nil {
275+
if err := ct.Provider.Extract(c.lcp.BinDir); err != nil {
255276
logrus.Fatal(err)
256277
}
257278
}

0 commit comments

Comments
 (0)