Skip to content

Commit d6a1a8c

Browse files
committed
Revert "Merge pull request juju#12498 from juju/revert-12491-ha-controller-charm"
This reverts commit d275725, reversing changes made to 508ab7a.
1 parent d275725 commit d6a1a8c

File tree

7 files changed

+126
-15
lines changed

7 files changed

+126
-15
lines changed

api/apiclient.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ func (st *state) Context() context.Context {
334334
// returned will apply a 30-second write deadline, so WriteJSON should
335335
// only be called from one goroutine.
336336
func (st *state) ConnectStream(path string, attrs url.Values) (base.Stream, error) {
337-
path, err := apiPath(st.modelTag, path)
337+
path, err := apiPath(st.modelTag.Id(), path)
338338
if err != nil {
339339
return nil, errors.Trace(err)
340340
}
@@ -514,7 +514,7 @@ func (st *state) addCookiesToHeader(h http.Header) error {
514514
// apiEndpoint returns a URL that refers to the given API slash-prefixed
515515
// endpoint path and query parameters.
516516
func (st *state) apiEndpoint(path, query string) (*url.URL, error) {
517-
path, err := apiPath(st.modelTag, path)
517+
path, err := apiPath(st.modelTag.Id(), path)
518518
if err != nil {
519519
return nil, errors.Trace(err)
520520
}
@@ -526,22 +526,42 @@ func (st *state) apiEndpoint(path, query string) (*url.URL, error) {
526526
}, nil
527527
}
528528

529+
// ControllerAPIURL returns the URL to use to connect to the controller API.
530+
func ControllerAPIURL(addr string) string {
531+
urlStr, _ := url.QueryUnescape(apiURL(addr, "").String())
532+
return urlStr
533+
}
534+
535+
// ModelAPITemplateURL returns a URL template to use to connect to a model API.
536+
func ModelAPITemplateURL(addr string) string {
537+
urlStr, _ := url.QueryUnescape(apiURL(addr, "${modelUUID}").String())
538+
return urlStr
539+
}
540+
541+
func apiURL(addr, model string) *url.URL {
542+
path, _ := apiPath(model, "/api")
543+
return &url.URL{
544+
Scheme: "wss",
545+
Host: addr,
546+
Path: path,
547+
}
548+
}
549+
529550
// Ping implements api.Connection.
530551
func (s *state) Ping() error {
531552
return s.APICall("Pinger", s.pingerFacadeVersion, "", "Ping", nil, nil)
532553
}
533554

534555
// apiPath returns the given API endpoint path relative
535-
// to the given model tag.
536-
func apiPath(modelTag names.ModelTag, path string) (string, error) {
556+
// to the given model string.
557+
func apiPath(model, path string) (string, error) {
537558
if !strings.HasPrefix(path, "/") {
538559
return "", errors.Errorf("cannot make API path from non-slash-prefixed path %q", path)
539560
}
540-
modelUUID := modelTag.Id()
541-
if modelUUID == "" {
561+
if model == "" {
542562
return path, nil
543563
}
544-
return modelRoot + modelUUID + path, nil
564+
return modelRoot + model + path, nil
545565
}
546566

547567
// tagToString returns the value of a tag's String method, or "" if the tag is nil.
@@ -615,7 +635,7 @@ func dialAPI(ctx context.Context, info *Info, opts0 DialOpts) (*dialResult, erro
615635
if opts.DNSCache == nil {
616636
opts.DNSCache = nopDNSCache{}
617637
}
618-
path, err := apiPath(info.ModelTag, "/api")
638+
path, err := apiPath(info.ModelTag.Id(), "/api")
619639
if err != nil {
620640
return nil, errors.Trace(err)
621641
}

cmd/juju/commands/bootstrap_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2150,7 +2150,7 @@ func (s *BootstrapSuite) TestBootstrapWithControllerCharm(c *gc.C) {
21502150
err: `--controller-charm ".*" is not a valid charm`,
21512151
}, {
21522152
charmPath: "/invalid/path",
2153-
err: `problem with --controller-charm: stat /invalid/path: no such file or directory`,
2153+
err: `problem with --controller-charm: .* /invalid/path: .*`,
21542154
},
21552155
} {
21562156
var gotArgs bootstrap.BootstrapParams

cmd/jujud/agent/bootstrap.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/juju/juju/agent"
2929
"github.com/juju/juju/agent/agentbootstrap"
3030
agenttools "github.com/juju/juju/agent/tools"
31+
"github.com/juju/juju/api"
3132
"github.com/juju/juju/apiserver"
3233
"github.com/juju/juju/caas"
3334
k8sprovider "github.com/juju/juju/caas/kubernetes/provider"
@@ -485,7 +486,8 @@ func addControllerApplication(st *state.State, curl *charm.URL, m *state.Machine
485486
addr = pa.Value
486487
}
487488
}
488-
cfg["controller-url"] = fmt.Sprintf("https://%s", addr)
489+
cfg["controller-url"] = api.ControllerAPIURL(addr)
490+
cfg["model-url-template"] = api.ModelAPITemplateURL(addr)
489491
app, err := st.AddApplication(state.AddApplicationArgs{
490492
Name: bootstrap.ControllerApplicationName,
491493
Series: curl.Series,

state/application.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2080,7 +2080,7 @@ func (a *Application) addUnitOps(
20802080
if err != nil {
20812081
return "", nil, errors.Trace(err)
20822082
}
2083-
if args.machineID != "" {
2083+
if args.machineID != "" && !strings.HasPrefix(a.doc.CharmURL.Name, "juju-") {
20842084
return "", nil, errors.NotSupportedf("non-empty machineID")
20852085
}
20862086
}

state/enableha.go

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"strconv"
99

1010
"github.com/juju/errors"
11-
"github.com/juju/juju/mongo"
12-
"github.com/juju/juju/tools"
1311
"github.com/juju/names/v4"
1412
"github.com/juju/replicaset"
1513
jujutxn "github.com/juju/txn"
@@ -21,7 +19,10 @@ import (
2119

2220
"github.com/juju/juju/core/constraints"
2321
"github.com/juju/juju/core/instance"
22+
"github.com/juju/juju/environs/bootstrap"
23+
"github.com/juju/juju/mongo"
2424
stateerrors "github.com/juju/juju/state/errors"
25+
"github.com/juju/juju/tools"
2526
)
2627

2728
func isController(mdoc *machineDoc) bool {
@@ -170,9 +171,30 @@ func (st *State) enableHAIntentionOps(
170171
var ops []txn.Op
171172
var change ControllersChanges
172173

174+
// TODO(wallyworld) - only need until we transition away from enable-ha
175+
controllerApp, err := st.Application(bootstrap.ControllerApplicationName)
176+
if err != nil && !errors.IsNotFound(err) {
177+
return nil, ControllersChanges{}, errors.Trace(err)
178+
}
179+
173180
for _, m := range intent.convert {
174181
ops = append(ops, convertControllerOps(m)...)
175182
change.Converted = append(change.Converted, m.Id())
183+
// Add a controller charm unit to the promoted machine.
184+
if controllerApp != nil {
185+
unitName, unitOps, err := controllerApp.addUnitOps("", AddUnitParams{machineID: m.Id()}, nil)
186+
if err != nil {
187+
return nil, ControllersChanges{}, errors.Trace(err)
188+
}
189+
ops = append(ops, unitOps...)
190+
addToMachineOp := txn.Op{
191+
C: machinesC,
192+
Id: m.doc.DocID,
193+
Assert: txn.DocExists,
194+
Update: bson.D{{"$addToSet", bson.D{{"principals", unitName}}}, {"$set", bson.D{{"clean", false}}}},
195+
}
196+
ops = append(ops, addToMachineOp)
197+
}
176198
}
177199

178200
// Use any placement directives that have been provided when adding new
@@ -200,15 +222,36 @@ func (st *State) enableHAIntentionOps(
200222
Constraints: cons,
201223
Placement: placement,
202224
}
225+
// Set up the new controller to have a controller charm unit.
226+
// The unit itself is created below.
227+
var controllerUnitName string
228+
if controllerApp != nil {
229+
controllerUnitName, err = controllerApp.newUnitName()
230+
if err != nil {
231+
return nil, ControllersChanges{}, errors.Trace(err)
232+
}
233+
template.Dirty = true
234+
template.principals = []string{controllerUnitName}
235+
}
203236
mdoc, addOps, err := st.addMachineOps(template)
204237
if err != nil {
205-
return nil, ControllersChanges{}, err
238+
return nil, ControllersChanges{}, errors.Trace(err)
206239
}
207240
if isController(mdoc) {
208241
controllerIds = append(controllerIds, mdoc.Id)
209242
}
210243
ops = append(ops, addOps...)
211244
change.Added = append(change.Added, mdoc.Id)
245+
if controllerApp != nil {
246+
_, unitOps, err := controllerApp.addUnitOps("", AddUnitParams{
247+
UnitName: &controllerUnitName,
248+
machineID: mdoc.Id,
249+
}, nil)
250+
if err != nil {
251+
return nil, ControllersChanges{}, errors.Trace(err)
252+
}
253+
ops = append(ops, unitOps...)
254+
}
212255
}
213256

214257
for _, m := range intent.maintain {

state/enableha_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,50 @@ func (s *EnableHASuite) TestEnableHAAddsNewMachines(c *gc.C) {
105105
s.assertControllerInfo(c, ids, ids, nil)
106106
}
107107

108+
func (s *EnableHASuite) TestEnableHAAddsControllerCharm(c *gc.C) {
109+
state.AddTestingApplicationForSeries(c, s.State, "focal", "controller",
110+
state.AddTestingCharmMultiSeries(c, s.State, "juju-controller"))
111+
changes, err := s.State.EnableHA(3, constraints.Value{}, "bionic", nil)
112+
c.Assert(err, jc.ErrorIsNil)
113+
c.Assert(changes.Added, gc.HasLen, 3)
114+
for i := 0; i < 3; i++ {
115+
unitName := fmt.Sprintf("controller/%d", i)
116+
m, err := s.State.Machine(fmt.Sprint(i))
117+
c.Assert(err, jc.ErrorIsNil)
118+
c.Assert(m.Principals(), jc.DeepEquals, []string{unitName})
119+
u, err := s.State.Unit(unitName)
120+
c.Assert(err, jc.ErrorIsNil)
121+
mID, err := u.AssignedMachineId()
122+
c.Assert(err, jc.ErrorIsNil)
123+
c.Assert(mID, gc.Equals, fmt.Sprint(i))
124+
}
125+
}
126+
127+
func (s *EnableHASuite) TestEnableHAAddsControllerCharmToPromoted(c *gc.C) {
128+
state.AddTestingApplicationForSeries(c, s.State, "focal", "controller",
129+
state.AddTestingCharmMultiSeries(c, s.State, "juju-controller"))
130+
m0, err := s.State.AddMachine("bionic", state.JobHostUnits)
131+
c.Assert(err, jc.ErrorIsNil)
132+
changes, err := s.State.EnableHA(3, constraints.Value{}, "bionic", []string{"0"})
133+
c.Assert(err, jc.ErrorIsNil)
134+
c.Assert(changes.Added, gc.HasLen, 2)
135+
c.Assert(changes.Converted, gc.HasLen, 1)
136+
for i := 0; i < 3; i++ {
137+
unitName := fmt.Sprintf("controller/%d", i)
138+
m, err := s.State.Machine(fmt.Sprint(i))
139+
c.Assert(err, jc.ErrorIsNil)
140+
c.Assert(m.Principals(), jc.DeepEquals, []string{unitName})
141+
u, err := s.State.Unit(unitName)
142+
c.Assert(err, jc.ErrorIsNil)
143+
mID, err := u.AssignedMachineId()
144+
c.Assert(err, jc.ErrorIsNil)
145+
c.Assert(mID, gc.Equals, fmt.Sprint(i))
146+
}
147+
err = m0.Refresh()
148+
c.Assert(err, jc.ErrorIsNil)
149+
c.Assert(m0.Principals(), gc.DeepEquals, []string{"controller/0"})
150+
}
151+
108152
func (s *EnableHASuite) TestEnableHATo(c *gc.C) {
109153
ids := make([]string, 3)
110154
m0, err := s.State.AddMachine("bionic", state.JobHostUnits, state.JobManageModel)

state/machine.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"gopkg.in/mgo.v2/bson"
2121
"gopkg.in/mgo.v2/txn"
2222

23+
"github.com/juju/juju/api"
2324
"github.com/juju/juju/core/actions"
2425
"github.com/juju/juju/core/constraints"
2526
corecontainer "github.com/juju/juju/core/container"
@@ -1768,7 +1769,8 @@ func (st *State) maybeUpdateControllerCharm(publicAddr string) error {
17681769
return errors.Trace(err)
17691770
}
17701771
return controllerApp.UpdateCharmConfig(model.GenerationMaster, charm.Settings{
1771-
"controller-url": fmt.Sprintf("https://%s", publicAddr),
1772+
"controller-url": api.ControllerAPIURL(publicAddr),
1773+
"model-url-template": api.ModelAPITemplateURL(publicAddr),
17721774
})
17731775
}
17741776

0 commit comments

Comments
 (0)