Skip to content

Commit 3dbaf5e

Browse files
committed
Modifies filtering for HA and management spaces to use SpaceInfo.
1 parent 3b06a59 commit 3dbaf5e

9 files changed

+57
-25
lines changed

state/address.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,19 @@ func (st *State) getOpsForHostPortsChange(
190190
func (st *State) filterHostPortsForManagementSpace(apiHostPorts [][]network.HostPort) ([][]network.HostPort, error) {
191191
config, err := st.ControllerConfig()
192192
if err != nil {
193-
return nil, err
193+
return nil, errors.Trace(err)
194194
}
195195

196196
var hostPortsForAgents [][]network.HostPort
197-
if mgmtSpace := config.JujuManagementSpace(); mgmtSpace != "" {
197+
if mgmtSpace := config.JujuManagementSpace(); mgmtSpace != network.DefaultSpaceName {
198+
sp, err := st.Space(mgmtSpace)
199+
if err != nil {
200+
return nil, errors.Trace(err)
201+
}
202+
198203
hostPortsForAgents = make([][]network.HostPort, len(apiHostPorts))
199-
sp := network.SpaceName(mgmtSpace)
200204
for i := range apiHostPorts {
201-
if filtered, ok := network.SelectHostPortsBySpaceNames(apiHostPorts[i], sp); ok {
205+
if filtered, ok := network.SelectHostPortsBySpaces(apiHostPorts[i], sp.NetworkSpace()); ok {
202206
hostPortsForAgents[i] = filtered
203207
} else {
204208
hostPortsForAgents[i] = apiHostPorts[i]
@@ -207,6 +211,7 @@ func (st *State) filterHostPortsForManagementSpace(apiHostPorts [][]network.Host
207211
} else {
208212
hostPortsForAgents = apiHostPorts
209213
}
214+
210215
return hostPortsForAgents, nil
211216
}
212217

state/address_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (s *ControllerAddressesSuite) TestControllerModel(c *gc.C) {
6868

6969
func (s *ControllerAddressesSuite) TestOtherModel(c *gc.C) {
7070
st := s.Factory.MakeModel(c, nil)
71-
defer st.Close()
71+
defer func() { _ = st.Close() }()
7272
addresses, err := st.Addresses()
7373
c.Assert(err, jc.ErrorIsNil)
7474
c.Assert(addresses, jc.SameContents, []string{"10.0.1.2:1234"})
@@ -238,6 +238,9 @@ func (s *ControllerAddressesSuite) TestSetAPIHostPortsNoMgmtSpaceConcurrentDiffe
238238
}
239239

240240
func (s *ControllerAddressesSuite) TestSetAPIHostPortsWithMgmtSpace(c *gc.C) {
241+
_, err := s.State.AddSpace("mgmt01", "", nil, false)
242+
c.Assert(err, jc.ErrorIsNil)
243+
241244
s.SetJujuManagementSpace(c, "mgmt01")
242245

243246
addrs, err := s.State.APIHostPortsForClients()
@@ -364,6 +367,9 @@ func (s *ControllerAddressesSuite) TestWatchAPIHostPortsForClients(c *gc.C) {
364367
}
365368

366369
func (s *ControllerAddressesSuite) TestWatchAPIHostPortsForAgents(c *gc.C) {
370+
_, err := s.State.AddSpace("mgmt01", "", nil, false)
371+
c.Assert(err, jc.ErrorIsNil)
372+
367373
s.SetJujuManagementSpace(c, "mgmt01")
368374

369375
w := s.State.WatchAPIHostPortsForAgents()
@@ -383,7 +389,7 @@ func (s *ControllerAddressesSuite) TestWatchAPIHostPortsForAgents(c *gc.C) {
383389
Port: 2,
384390
}
385391

386-
err := s.State.SetAPIHostPorts([][]network.HostPort{{
392+
err = s.State.SetAPIHostPorts([][]network.HostPort{{
387393
mgmtHP,
388394
}})
389395
c.Assert(err, jc.ErrorIsNil)

state/controller.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,24 @@ func checkUpdateControllerConfig(name string) error {
157157

158158
// checkSpaceIsAvailableToAllControllers checks if each controller machine has
159159
// at least one address in the input space. If not, an error is returned.
160-
func (st *State) checkSpaceIsAvailableToAllControllers(configSpace string) error {
160+
func (st *State) checkSpaceIsAvailableToAllControllers(spaceName string) error {
161161
info, err := st.ControllerInfo()
162162
if err != nil {
163163
return errors.Annotate(err, "cannot get controller info")
164164
}
165165

166+
space, err := st.Space(spaceName)
167+
if err != nil {
168+
return errors.Trace(err)
169+
}
170+
166171
var missing []string
167-
spaceName := network.SpaceName(configSpace)
168172
for _, id := range info.MachineIds {
169173
m, err := st.Machine(id)
170174
if err != nil {
171175
return errors.Annotate(err, "cannot get machine")
172176
}
173-
if _, ok := network.SelectAddressesBySpaceNames(m.Addresses(), spaceName); !ok {
177+
if _, ok := network.SelectAddressesBySpaces(m.Addresses(), space.NetworkSpace()); !ok {
174178
missing = append(missing, id)
175179
}
176180
}

state/controller_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ func (s *ControllerSuite) TestRemovingUnknownName(c *gc.C) {
161161
}
162162

163163
func (s *ControllerSuite) TestUpdateControllerConfigRejectsSpaceWithoutAddresses(c *gc.C) {
164+
_, err := s.State.AddSpace("mgmt-space", "", nil, false)
165+
c.Assert(err, jc.ErrorIsNil)
166+
164167
m, err := s.State.AddMachine("quantal", state.JobManageModel, state.JobHostUnits)
165168
c.Assert(err, jc.ErrorIsNil)
166169
c.Assert(m.SetMachineAddresses(network.NewAddress("192.168.9.9")), jc.ErrorIsNil)
@@ -173,6 +176,9 @@ func (s *ControllerSuite) TestUpdateControllerConfigRejectsSpaceWithoutAddresses
173176
}
174177

175178
func (s *ControllerSuite) TestUpdateControllerConfigAcceptsSpaceWithAddresses(c *gc.C) {
179+
_, err := s.State.AddSpace("mgmt-space", "", nil, false)
180+
c.Assert(err, jc.ErrorIsNil)
181+
176182
m, err := s.State.AddMachine("quantal", state.JobManageModel, state.JobHostUnits)
177183
c.Assert(err, jc.ErrorIsNil)
178184
c.Assert(m.SetProviderAddresses(network.NewAddressOnSpace("mgmt-space", "192.168.9.9")), jc.ErrorIsNil)

state/remoteapplication.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ func (p AddRemoteApplicationParams) Validate() error {
774774
}
775775
spaceNames := set.NewStrings()
776776
for _, space := range p.Spaces {
777-
spaceNames.Add(space.Name)
777+
spaceNames.Add(string(space.Name))
778778
}
779779
for endpoint, space := range p.Bindings {
780780
if !spaceNames.Contains(space) {
@@ -836,7 +836,7 @@ func (st *State) AddRemoteApplication(args AddRemoteApplicationParams) (_ *Remot
836836
for i, space := range args.Spaces {
837837
spaces[i] = remoteSpaceDoc{
838838
CloudType: space.CloudType,
839-
Name: space.Name,
839+
Name: string(space.Name),
840840
ProviderId: string(space.ProviderId),
841841
ProviderAttributes: space.ProviderAttributes,
842842
}

state/spaces.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ func (s *Space) Subnets() ([]*Subnet, error) {
7171
var doc subnetDoc
7272
var results []*Subnet
7373
// We ignore space-name field for FAN subnets...
74-
iter := subnetsCollection.Find(bson.D{{"space-id", id}, bson.DocElem{"fan-local-underlay", bson.D{{"$exists", false}}}}).Iter()
74+
iter := subnetsCollection.Find(
75+
bson.D{{"space-id", id}, bson.DocElem{Name: "fan-local-underlay", Value: bson.D{{"$exists", false}}}}).Iter()
7576
defer iter.Close()
7677
for iter.Next(&doc) {
7778
subnet := &Subnet{s.st, doc, id}
@@ -89,6 +90,15 @@ func (s *Space) Subnets() ([]*Subnet, error) {
8990
return results, nil
9091
}
9192

93+
func (s *Space) NetworkSpace() network.SpaceInfo {
94+
return network.SpaceInfo{
95+
Name: network.SpaceName(s.Name()),
96+
ProviderId: s.ProviderId(),
97+
// TODO (manadart 2019-08-13): Populate these after subnet refactor.
98+
Subnets: nil,
99+
}
100+
}
101+
92102
// AddSpace creates and returns a new space.
93103
func (st *State) AddSpace(
94104
name string, providerId network.Id, subnets []string, isPublic bool) (newSpace *Space, err error,

state/spacesdiscovery.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,31 +158,26 @@ func (st *State) SaveSpacesFromProvider(providerSpaces []corenetwork.SpaceInfo)
158158
// TODO(mfoord): we need to delete spaces and subnets that no longer
159159
// exist, so long as they're not in use.
160160
for _, space := range providerSpaces {
161-
// Check if the space is already in state, in which case we know
162-
// its name.
161+
// Check if the space is already in state,
162+
// in which case we know its name.
163163
stateSpace, ok := modelSpaceMap[space.ProviderId]
164164
var spaceTag names.SpaceTag
165165
if ok {
166166
spaceName := stateSpace.Name()
167167
if !names.IsValidSpace(spaceName) {
168-
// Can only happen if an invalid name is stored
169-
// in state.
168+
// Can only happen if an invalid name is stored in state.
170169
logger.Errorf("space %q has an invalid name, ignoring", spaceName)
171170
continue
172171

173172
}
174173
spaceTag = names.NewSpaceTag(spaceName)
175174

176175
} else {
177-
// The space is new, we need to create a valid name for it
178-
// in state.
179-
spaceName := space.Name
180-
// Convert the name into a valid name that isn't already in
181-
// use.
182-
spaceName = network.ConvertSpaceName(spaceName, spaceNames)
176+
// The space is new, we need to create a valid name for it in state.
177+
// Convert the name into a valid name that is not already in use.
178+
spaceName := network.ConvertSpaceName(string(space.Name), spaceNames)
183179
spaceNames.Add(spaceName)
184180
spaceTag = names.NewSpaceTag(spaceName)
185-
// We need to create the space.
186181

187182
logger.Debugf("Adding space %s from provider %s", spaceTag.String(), string(space.ProviderId))
188183
_, err = st.AddSpace(spaceTag.Id(), space.ProviderId, []string{}, false)

state/spacesdiscovery_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func checkSpacesEqual(c *gc.C, spaces []*state.Space, spaceInfos []network.Space
200200
c.Assert(len(spaceInfos), gc.Equals, len(filtered))
201201
for i, spaceInfo := range spaceInfos {
202202
space := filtered[i]
203-
c.Check(spaceInfo.Name, gc.Equals, space.Name())
203+
c.Check(string(spaceInfo.Name), gc.Equals, space.Name())
204204
c.Check(spaceInfo.ProviderId, gc.Equals, space.ProviderId())
205205
subnets, err := space.Subnets()
206206
c.Assert(err, jc.ErrorIsNil)

state/state_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4286,6 +4286,9 @@ func (s *StateSuite) TestSetAPIHostPortsNoMgmtSpaceConcurrentDifferent(c *gc.C)
42864286
}
42874287

42884288
func (s *StateSuite) TestSetAPIHostPortsWithMgmtSpace(c *gc.C) {
4289+
_, err := s.State.AddSpace("mgmt01", "", nil, false)
4290+
c.Assert(err, jc.ErrorIsNil)
4291+
42894292
s.SetJujuManagementSpace(c, "mgmt01")
42904293

42914294
addrs, err := s.State.APIHostPortsForClients()
@@ -4412,6 +4415,9 @@ func (s *StateSuite) TestWatchAPIHostPortsForClients(c *gc.C) {
44124415
}
44134416

44144417
func (s *StateSuite) TestWatchAPIHostPortsForAgents(c *gc.C) {
4418+
_, err := s.State.AddSpace("mgmt01", "", nil, false)
4419+
c.Assert(err, jc.ErrorIsNil)
4420+
44154421
s.SetJujuManagementSpace(c, "mgmt01")
44164422

44174423
w := s.State.WatchAPIHostPortsForAgents()
@@ -4431,7 +4437,7 @@ func (s *StateSuite) TestWatchAPIHostPortsForAgents(c *gc.C) {
44314437
Port: 2,
44324438
}
44334439

4434-
err := s.State.SetAPIHostPorts([][]network.HostPort{{
4440+
err = s.State.SetAPIHostPorts([][]network.HostPort{{
44354441
mgmtHP,
44364442
}})
44374443
c.Assert(err, jc.ErrorIsNil)

0 commit comments

Comments
 (0)