-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployer_caas.go
136 lines (117 loc) · 4.2 KB
/
deployer_caas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2023 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package bootstrap
import (
"context"
"fmt"
"github.com/juju/errors"
corebase "github.com/juju/juju/core/base"
"github.com/juju/juju/core/network"
coreunit "github.com/juju/juju/core/unit"
"github.com/juju/juju/core/version"
"github.com/juju/juju/domain/application/service"
"github.com/juju/juju/state"
)
// CloudService is the interface that is used to get the cloud service
// for the controller.
type CloudService interface {
Addresses() network.SpaceAddresses
}
// CloudServiceGetter is the interface that is used to get the cloud service
// for the controller.
type CloudServiceGetter interface {
CloudService(string) (CloudService, error)
}
// OperationApplier is the interface that is used to apply operations.
type OperationApplier interface {
// ApplyOperation applies the given operation.
ApplyOperation(*state.UpdateUnitOperation) error
}
// CAASDeployerConfig holds the configuration for a CAASDeployer.
type CAASDeployerConfig struct {
BaseDeployerConfig
CloudServiceGetter CloudServiceGetter
OperationApplier OperationApplier
UnitPassword string
}
// Validate validates the configuration.
func (c CAASDeployerConfig) Validate() error {
if err := c.BaseDeployerConfig.Validate(); err != nil {
return errors.Trace(err)
}
if c.CloudServiceGetter == nil {
return errors.NotValidf("CloudServiceGetter")
}
if c.OperationApplier == nil {
return errors.NotValidf("OperationApplier")
}
return nil
}
// CAASDeployer is the interface that is used to deploy the controller charm
// for CAAS workloads.
type CAASDeployer struct {
baseDeployer
cloudServiceGetter CloudServiceGetter
operationApplier OperationApplier
unitPassword string
}
// NewCAASDeployer returns a new ControllerCharmDeployer for CAAS workloads.
func NewCAASDeployer(config CAASDeployerConfig) (*CAASDeployer, error) {
if err := config.Validate(); err != nil {
return nil, errors.Trace(err)
}
return &CAASDeployer{
baseDeployer: makeBaseDeployer(config.BaseDeployerConfig),
cloudServiceGetter: config.CloudServiceGetter,
operationApplier: config.OperationApplier,
unitPassword: config.UnitPassword,
}, nil
}
// ControllerAddress returns the address of the controller that should be
// used.
func (d *CAASDeployer) ControllerAddress(context.Context) (string, error) {
s, err := d.cloudServiceGetter.CloudService(d.controllerConfig.ControllerUUID())
if err != nil {
return "", errors.Trace(err)
}
hp := network.SpaceAddressesWithPort(s.Addresses(), 0)
addr := hp.AllMatchingScope(network.ScopeMatchCloudLocal)
var controllerAddress string
if len(addr) > 0 {
controllerAddress = addr[0]
}
d.logger.Debugf("CAAS controller address %v", controllerAddress)
return controllerAddress, nil
}
// ControllerCharmBase returns the base used for deploying the controller
// charm.
func (d *CAASDeployer) ControllerCharmBase() (corebase.Base, error) {
return version.DefaultSupportedLTSBase(), nil
}
// CompleteProcess is called when the bootstrap process is complete.
func (d *CAASDeployer) CompleteProcess(ctx context.Context, controllerUnit Unit) error {
providerID := fmt.Sprintf("controller-%d", controllerUnit.UnitTag().Number())
controllerUnitName, err := coreunit.NewName(controllerUnit.UnitTag().Id())
if err != nil {
return errors.Annotatef(err, "parsing controller unit name %q", controllerUnit.UnitTag().Id())
}
if err := d.applicationService.UpdateCAASUnit(ctx, controllerUnitName, service.UpdateCAASUnitParams{
ProviderId: &providerID,
}); err != nil {
return errors.Annotatef(err, "updating controller unit")
}
if err := d.applicationService.SetUnitPassword(ctx, controllerUnitName, d.unitPassword); err != nil {
return errors.Annotate(err, "setting controller unit password")
}
// TODO(units) - remove dual write to state
op := controllerUnit.UpdateOperation(state.UnitUpdateProperties{
ProviderId: &providerID,
})
if err := d.operationApplier.ApplyOperation(op); err != nil {
return errors.Annotate(err, "cannot update controller unit")
}
if err := controllerUnit.SetPassword(d.unitPassword); err != nil {
return errors.Annotate(err, "cannot set password for controller unit")
}
return nil
}