Skip to content

Commit e287078

Browse files
Merge pull request #8350 from patrickdillon/capi-restore-etcd
CORS-3471: standalone destroy bootstrap support with capi
2 parents 7279a0e + e5c396e commit e287078

File tree

8 files changed

+73
-47
lines changed

8 files changed

+73
-47
lines changed

pkg/clusterapi/localcontrolplane.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ func (c *localControlPlane) Run(ctx context.Context) error {
7676
BinaryAssetsDirectory: c.BinDir,
7777
ControlPlaneStartTimeout: 10 * time.Second,
7878
ControlPlaneStopTimeout: 10 * time.Second,
79+
ControlPlane: envtest.ControlPlane{
80+
Etcd: &envtest.Etcd{
81+
DataDir: c.BinDir,
82+
},
83+
},
7984
}
8085
var err error
8186
c.Cfg, err = c.Env.Start()

pkg/clusterapi/system.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import (
1717
"sigs.k8s.io/controller-runtime/pkg/client"
1818
"sigs.k8s.io/controller-runtime/pkg/envtest"
1919

20+
"github.com/openshift/installer/cmd/openshift-install/command"
2021
"github.com/openshift/installer/data"
21-
"github.com/openshift/installer/pkg/asset/installconfig"
22+
"github.com/openshift/installer/pkg/asset/cluster/metadata"
23+
azic "github.com/openshift/installer/pkg/asset/installconfig/azure"
2224
gcpic "github.com/openshift/installer/pkg/asset/installconfig/gcp"
2325
powervsic "github.com/openshift/installer/pkg/asset/installconfig/powervs"
2426
"github.com/openshift/installer/pkg/clusterapi/internal/process"
@@ -49,7 +51,7 @@ const (
4951

5052
// Interface is the interface for the cluster-api system.
5153
type Interface interface {
52-
Run(ctx context.Context, installConfig *installconfig.InstallConfig) error
54+
Run(ctx context.Context) error
5355
State() SystemState
5456
Client() client.Client
5557
Teardown()
@@ -78,7 +80,7 @@ type system struct {
7880
}
7981

8082
// Run launches the cluster-api system.
81-
func (c *system) Run(ctx context.Context, installConfig *installconfig.InstallConfig) error {
83+
func (c *system) Run(ctx context.Context) error {
8284
c.Lock()
8385
defer c.Unlock()
8486

@@ -121,9 +123,19 @@ func (c *system) Run(ctx context.Context, installConfig *installconfig.InstallCo
121123
},
122124
}
123125

126+
metadata, err := metadata.Load(command.RootOpts.Dir)
127+
if err != nil {
128+
return fmt.Errorf("failed to load metadata: %w", err)
129+
}
130+
131+
platform := metadata.Platform()
132+
if platform == "" {
133+
return fmt.Errorf("no platform configured in metadata")
134+
}
135+
124136
// Create the infrastructure controllers.
125137
// Only add the controllers for the platform we are deploying to.
126-
switch platform := installConfig.Config.Platform.Name(); platform {
138+
switch platform {
127139
case aws.Name:
128140
controller := c.getInfrastructureController(
129141
&AWS,
@@ -137,7 +149,7 @@ func (c *system) Run(ctx context.Context, installConfig *installconfig.InstallCo
137149
},
138150
map[string]string{},
139151
)
140-
if cfg := installConfig.Config.AWS; cfg != nil && len(cfg.ServiceEndpoints) > 0 {
152+
if cfg := metadata.AWS; cfg != nil && len(cfg.ServiceEndpoints) > 0 {
141153
endpoints := make([]string, 0, len(cfg.ServiceEndpoints))
142154
// CAPA expects name=url pairs of service endpoints
143155
for _, endpoint := range cfg.ServiceEndpoints {
@@ -147,9 +159,13 @@ func (c *system) Run(ctx context.Context, installConfig *installconfig.InstallCo
147159
}
148160
controllers = append(controllers, controller)
149161
case azure.Name:
150-
session, err := installConfig.Azure.Session()
162+
cloudName := metadata.Azure.CloudName
163+
if cloudName == "" {
164+
cloudName = azure.PublicCloud
165+
}
166+
session, err := azic.GetSession(cloudName, metadata.Azure.ARMEndpoint)
151167
if err != nil {
152-
return fmt.Errorf("failed to create azure session: %w", err)
168+
return fmt.Errorf("unable to retrieve azure session: %w", err)
153169
}
154170

155171
controllers = append(controllers,

pkg/destroy/bootstrap/bootstrap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func Destroy(ctx context.Context, dir string) (err error) {
8484
return fmt.Errorf("error getting infrastructure provider: %w", err)
8585
}
8686

87-
if err := provider.DestroyBootstrap(dir); err != nil {
87+
if err := provider.DestroyBootstrap(ctx, dir); err != nil {
8888
return fmt.Errorf("error destroying bootstrap resources %w", err)
8989
}
9090

pkg/infrastructure/aws/sdk/aws.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func (a InfraProvider) Provision(ctx context.Context, dir string, parents asset.
303303
}
304304

305305
// DestroyBootstrap destroys the temporary bootstrap resources.
306-
func (a InfraProvider) DestroyBootstrap(dir string) error {
306+
func (a InfraProvider) DestroyBootstrap(ctx context.Context, dir string) error {
307307
// Unmarshall input from tf variables, so we can use it along with
308308
// installConfig and other assets as the contractual input regardless of
309309
// the implementation.
@@ -341,7 +341,7 @@ func (a InfraProvider) DestroyBootstrap(dir string) error {
341341
Fn: request.MakeAddToUserAgentHandler("OpenShift/4.x Destroyer", version.Raw),
342342
})
343343

344-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
344+
ctx, cancel := context.WithTimeout(ctx, 30*time.Minute)
345345
defer cancel()
346346

347347
logger := logrus.StandardLogger()

pkg/infrastructure/baremetal/baremetal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (a Provider) Provision(ctx context.Context, dir string, parents asset.Paren
3333
}
3434

3535
// DestroyBootstrap destroys the temporary bootstrap resources.
36-
func (a Provider) DestroyBootstrap(dir string) error {
36+
func (a Provider) DestroyBootstrap(ctx context.Context, dir string) error {
3737
config, err := getConfig(dir)
3838
if err != nil {
3939
return fmt.Errorf("failed to get baremetal platform config: %w", err)

pkg/infrastructure/clusterapi/clusterapi.go

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (i *InfraProvider) Provision(ctx context.Context, dir string, parents asset
132132
// Run the CAPI system.
133133
timer.StartTimer(infrastructureStage)
134134
capiSystem := clusterapi.System()
135-
if err := capiSystem.Run(ctx, installConfig); err != nil {
135+
if err := capiSystem.Run(ctx); err != nil {
136136
return fileList, fmt.Errorf("failed to run cluster api system: %w", err)
137137
}
138138

@@ -362,49 +362,54 @@ func (i *InfraProvider) Provision(ctx context.Context, dir string, parents asset
362362
}
363363

364364
// DestroyBootstrap destroys the temporary bootstrap resources.
365-
func (i *InfraProvider) DestroyBootstrap(dir string) error {
365+
func (i *InfraProvider) DestroyBootstrap(ctx context.Context, dir string) error {
366366
metadata, err := metadata.Load(dir)
367367
if err != nil {
368368
return err
369369
}
370370

371-
// TODO(padillon): start system if not running
372-
if sys := clusterapi.System(); sys.State() == clusterapi.SystemStateRunning {
373-
machineName := capiutils.GenerateBoostrapMachineName(metadata.InfraID)
374-
machineNamespace := capiutils.Namespace
375-
if err := sys.Client().Delete(context.TODO(), &clusterv1.Machine{
376-
ObjectMeta: metav1.ObjectMeta{
377-
Name: machineName,
378-
Namespace: machineNamespace,
379-
},
380-
}); err != nil {
381-
return fmt.Errorf("failed to delete bootstrap machine: %w", err)
371+
sys := clusterapi.System()
372+
if sys.State() != clusterapi.SystemStateRunning {
373+
if err := sys.Run(ctx); err != nil {
374+
return fmt.Errorf("failed to run capi system: %w", err)
382375
}
376+
}
383377

384-
machineDeletionTimeout := 2 * time.Minute
385-
logrus.Infof("Waiting up to %v for bootstrap machine deletion %s/%s...", machineDeletionTimeout, machineNamespace, machineName)
386-
machineContext, cancel := context.WithTimeout(context.TODO(), machineDeletionTimeout)
387-
wait.Until(func() {
388-
err := sys.Client().Get(context.TODO(), client.ObjectKey{
389-
Name: machineName,
390-
Namespace: machineNamespace,
391-
}, &clusterv1.Machine{})
392-
if err != nil {
393-
if apierrors.IsNotFound(err) {
394-
logrus.Debugf("Machine deleted: %s", machineName)
395-
cancel()
396-
} else {
397-
logrus.Debugf("Error when deleting bootstrap machine: %s", err)
398-
}
399-
}
400-
}, 2*time.Second, machineContext.Done())
378+
machineName := capiutils.GenerateBoostrapMachineName(metadata.InfraID)
379+
machineNamespace := capiutils.Namespace
380+
if err := sys.Client().Delete(ctx, &clusterv1.Machine{
381+
ObjectMeta: metav1.ObjectMeta{
382+
Name: machineName,
383+
Namespace: machineNamespace,
384+
},
385+
}); err != nil {
386+
return fmt.Errorf("failed to delete bootstrap machine: %w", err)
387+
}
401388

402-
err = machineContext.Err()
403-
if err != nil && !errors.Is(err, context.Canceled) {
404-
logrus.Infof("Timeout deleting bootstrap machine: %s", err)
389+
machineDeletionTimeout := 5 * time.Minute
390+
logrus.Infof("Waiting up to %v for bootstrap machine deletion %s/%s...", machineDeletionTimeout, machineNamespace, machineName)
391+
ctx, cancel := context.WithTimeout(ctx, machineDeletionTimeout)
392+
wait.UntilWithContext(ctx, func(context.Context) {
393+
err := sys.Client().Get(ctx, client.ObjectKey{
394+
Name: machineName,
395+
Namespace: machineNamespace,
396+
}, &clusterv1.Machine{})
397+
if err != nil {
398+
if apierrors.IsNotFound(err) {
399+
logrus.Debugf("Machine deleted: %s", machineName)
400+
cancel()
401+
} else {
402+
logrus.Debugf("Error when deleting bootstrap machine: %s", err)
403+
}
405404
}
405+
}, 2*time.Second)
406+
407+
err = ctx.Err()
408+
if err != nil && !errors.Is(err, context.Canceled) {
409+
logrus.Warnf("Timeout deleting bootstrap machine: %s", err)
410+
} else {
411+
logrus.Infof("Finished destroying bootstrap resources")
406412
}
407-
logrus.Infof("Finished destroying bootstrap resources")
408413
clusterapi.System().Teardown()
409414

410415
return nil

pkg/infrastructure/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type Provider interface {
1818
Provision(ctx context.Context, dir string, parents asset.Parents) ([]*asset.File, error)
1919

2020
// DestroyBootstrap destroys the temporary bootstrap resources.
21-
DestroyBootstrap(dir string) error
21+
DestroyBootstrap(ctx context.Context, dir string) error
2222

2323
// ExtractHostAddresses extracts the IPs of the bootstrap and control plane machines.
2424
ExtractHostAddresses(dir string, config *types.InstallConfig, ha *HostAddresses) error

pkg/terraform/terraform.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (p *Provider) Provision(_ context.Context, dir string, parents asset.Parent
8181
// DestroyBootstrap implements pkg/infrastructure/provider.DestroyBootstrap.
8282
// DestroyBootstrap iterates through each stage, and will run the destroy
8383
// command when defined on a stage.
84-
func (p *Provider) DestroyBootstrap(dir string) error {
84+
func (p *Provider) DestroyBootstrap(ctx context.Context, dir string) error {
8585
varFiles := []string{tfVarsFileName, tfPlatformVarsFileName}
8686
for _, stage := range p.stages {
8787
varFiles = append(varFiles, stage.OutputsFilename())

0 commit comments

Comments
 (0)