Skip to content

Commit

Permalink
Merge branch '3.4' into 3.4-3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Aflynn50 committed Jun 10, 2024
2 parents eacd805 + a26b259 commit 5cfda08
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 65 deletions.
6 changes: 0 additions & 6 deletions agent/agentbootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,17 +451,11 @@ func initBootstrapMachine(st *state.State, args InitializeStateParams) (bootstra
return nil, errors.Trace(err)
}

spaceAddrs, err := args.BootstrapMachineAddresses.ToSpaceAddresses(st)
if err != nil {
return nil, errors.Trace(err)
}

base, err := corebase.GetBaseFromSeries(hostSeries)
if err != nil {
return nil, errors.Trace(err)
}
m, err := st.AddOneMachine(state.MachineTemplate{
Addresses: spaceAddrs,
Base: state.Base{OS: base.OS, Channel: base.Channel.String()},
Nonce: agent.BootstrapNonce,
Constraints: args.BootstrapMachineConstraints,
Expand Down
54 changes: 29 additions & 25 deletions agent/agentbootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,21 +198,21 @@ func (s *bootstrapSuite) TestInitializeState(c *gc.C) {
// Check that the model has been set up.
model, err := st.Model()
c.Assert(err, jc.ErrorIsNil)
c.Assert(model.UUID(), gc.Equals, modelCfg.UUID())
c.Assert(model.EnvironVersion(), gc.Equals, 666)
c.Check(model.UUID(), gc.Equals, modelCfg.UUID())
c.Check(model.EnvironVersion(), gc.Equals, 666)

// Check that initial admin user has been set up correctly.
modelTag := model.Tag().(names.ModelTag)
controllerTag := names.NewControllerTag(controllerCfg.ControllerUUID())
s.assertCanLogInAsAdmin(c, modelTag, controllerTag, testing.DefaultMongoPassword)
user, err := st.User(model.Owner())
c.Assert(err, jc.ErrorIsNil)
c.Assert(user.PasswordValid(testing.DefaultMongoPassword), jc.IsTrue)
c.Check(user.PasswordValid(testing.DefaultMongoPassword), jc.IsTrue)

// Check controller config
controllerCfg, err = st.ControllerConfig()
c.Assert(err, jc.ErrorIsNil)
c.Assert(controllerCfg, jc.DeepEquals, controller.Config{
c.Check(controllerCfg, jc.DeepEquals, controller.Config{
"controller-uuid": testing.ControllerTag.Id(),
"ca-cert": testing.CACert,
"state-port": 1234,
Expand Down Expand Up @@ -243,11 +243,11 @@ func (s *bootstrapSuite) TestInitializeState(c *gc.C) {
expectedAttrs := expectedCfg.AllAttrs()
expectedAttrs["apt-mirror"] = "http://mirror"
expectedAttrs["no-proxy"] = "value"
c.Assert(newModelCfg.AllAttrs(), jc.DeepEquals, expectedAttrs)
c.Check(newModelCfg.AllAttrs(), jc.DeepEquals, expectedAttrs)

gotModelConstraints, err := st.ModelConstraints()
c.Assert(err, jc.ErrorIsNil)
c.Assert(gotModelConstraints, gc.DeepEquals, expectModelConstraints)
c.Check(gotModelConstraints, gc.DeepEquals, expectModelConstraints)

// Check that the hosted model has been added, model constraints
// set, and its config contains the same authorized-keys as the
Expand All @@ -257,47 +257,50 @@ func (s *bootstrapSuite) TestInitializeState(c *gc.C) {
defer initialModelSt.Release()
gotModelConstraints, err = initialModelSt.ModelConstraints()
c.Assert(err, jc.ErrorIsNil)
c.Assert(gotModelConstraints, gc.DeepEquals, expectModelConstraints)
c.Check(gotModelConstraints, gc.DeepEquals, expectModelConstraints)

initialModel, err := initialModelSt.Model()
c.Assert(err, jc.ErrorIsNil)
c.Assert(initialModel.Name(), gc.Equals, "hosted")
c.Assert(initialModel.CloudRegion(), gc.Equals, "dummy-region")
c.Assert(initialModel.EnvironVersion(), gc.Equals, 123)
c.Check(initialModel.Name(), gc.Equals, "hosted")
c.Check(initialModel.CloudRegion(), gc.Equals, "dummy-region")
c.Check(initialModel.EnvironVersion(), gc.Equals, 123)

hostedCfg, err := initialModel.ModelConfig()
c.Assert(err, jc.ErrorIsNil)
_, hasUnexpected := hostedCfg.AllAttrs()["not-for-hosted"]
c.Assert(hasUnexpected, jc.IsFalse)
c.Assert(hostedCfg.AuthorizedKeys(), gc.Equals, newModelCfg.AuthorizedKeys())
c.Check(hasUnexpected, jc.IsFalse)
c.Check(hostedCfg.AuthorizedKeys(), gc.Equals, newModelCfg.AuthorizedKeys())

// Check that the bootstrap machine looks correct.
m, err := st.Machine("0")
c.Assert(err, jc.ErrorIsNil)
c.Assert(m.Id(), gc.Equals, "0")
c.Assert(m.Jobs(), gc.DeepEquals, []state.MachineJob{state.JobManageModel})
c.Check(m.Id(), gc.Equals, "0")
c.Check(m.Jobs(), gc.DeepEquals, []state.MachineJob{state.JobManageModel})

base, err := corebase.ParseBase(m.Base().OS, m.Base().Channel)
c.Assert(err, jc.ErrorIsNil)
c.Assert(m.Base().String(), gc.Equals, base.String())
c.Assert(m.CheckProvisioned(agent.BootstrapNonce), jc.IsTrue)
c.Assert(m.Addresses(), jc.DeepEquals, filteredAddrs)
c.Check(m.Base().String(), gc.Equals, base.String())
c.Check(m.CheckProvisioned(agent.BootstrapNonce), jc.IsTrue)

gotBootstrapConstraints, err := m.Constraints()
c.Assert(err, jc.ErrorIsNil)
c.Assert(gotBootstrapConstraints, gc.DeepEquals, expectBootstrapConstraints)
c.Assert(err, jc.ErrorIsNil)
c.Check(gotBootstrapConstraints, gc.DeepEquals, expectBootstrapConstraints)

gotHW, err := m.HardwareCharacteristics()
c.Assert(err, jc.ErrorIsNil)
c.Assert(*gotHW, gc.DeepEquals, expectHW)
c.Check(*gotHW, gc.DeepEquals, expectHW)

// Check that the API host ports are initialised correctly.
apiHostPorts, err := st.APIHostPortsForClients()
c.Assert(err, jc.ErrorIsNil)
c.Assert(apiHostPorts, jc.DeepEquals, []corenetwork.SpaceHostPorts{
c.Check(apiHostPorts, jc.DeepEquals, []corenetwork.SpaceHostPorts{
corenetwork.SpaceAddressesWithPort(filteredAddrs, 1234),
})

// Check that the state serving info is initialised correctly.
stateServingInfo, err := st.StateServingInfo()
c.Assert(err, jc.ErrorIsNil)
c.Assert(stateServingInfo, jc.DeepEquals, controller.StateServingInfo{
c.Check(stateServingInfo, jc.DeepEquals, controller.StateServingInfo{
APIPort: 1234,
StatePort: s.mgoInst.Port(),
Cert: testing.ServerCert,
Expand All @@ -313,17 +316,18 @@ func (s *bootstrapSuite) TestInitializeState(c *gc.C) {
c.Assert(err, jc.ErrorIsNil)
expectedStorageCfg, err := storage.NewConfig("spool", "loop", map[string]interface{}{"foo": "bar"})
c.Assert(err, jc.ErrorIsNil)
c.Assert(storageCfg, jc.DeepEquals, expectedStorageCfg)
c.Check(storageCfg, jc.DeepEquals, expectedStorageCfg)

// Check that the machine agent's config has been written
// and that we can use it to connect to mongo.
machine0 := names.NewMachineTag("0")
newCfg, err := agent.ReadConfig(agent.ConfigPath(dataDir, machine0))
c.Assert(err, jc.ErrorIsNil)
c.Assert(newCfg.Tag(), gc.Equals, machine0)
c.Check(newCfg.Tag(), gc.Equals, machine0)

info, ok := cfg.MongoInfo()
c.Assert(ok, jc.IsTrue)
c.Assert(info.Password, gc.Not(gc.Equals), testing.DefaultMongoPassword)
c.Check(info.Password, gc.Not(gc.Equals), testing.DefaultMongoPassword)

session, err := mongo.DialWithInfo(*info, mongotest.DialOpts())
c.Assert(err, jc.ErrorIsNil)
Expand Down
31 changes: 20 additions & 11 deletions apiserver/facades/agent/provisioner/provisioninginfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
apiservererrors "github.com/juju/juju/apiserver/errors"
"github.com/juju/juju/cloudconfig/instancecfg"
corebase "github.com/juju/juju/core/base"
"github.com/juju/juju/core/constraints"
"github.com/juju/juju/core/lxdprofile"
"github.com/juju/juju/core/model"
"github.com/juju/juju/core/network"
Expand Down Expand Up @@ -86,12 +87,16 @@ func (api *ProvisionerAPI) getProvisioningInfo(m *state.Machine,
return nil, errors.Trace(err)
}

machineSpaces, err := api.machineSpaces(m, spaceInfos, endpointBindings)
cons, err := m.Constraints()
if err != nil {
return nil, errors.Annotate(err, "retrieving machine constraints")
}
machineSpaces, err := api.machineSpaces(cons, spaceInfos, endpointBindings)
if err != nil {
return nil, errors.Trace(err)
}

if result.ProvisioningNetworkTopology, err = api.machineSpaceTopology(m.Id(), machineSpaces); err != nil {
if result.ProvisioningNetworkTopology, err = api.machineSpaceTopology(m.Id(), cons, machineSpaces); err != nil {
return nil, errors.Annotate(err, "matching subnets to zones")
}

Expand Down Expand Up @@ -310,14 +315,11 @@ func (api *ProvisionerAPI) machineTags(m *state.Machine, isController bool) (map
//
// It is the responsibility of the provider to negotiate this information
// appropriately.
func (api *ProvisionerAPI) machineSpaces(m *state.Machine,
func (api *ProvisionerAPI) machineSpaces(
cons constraints.Value,
allSpaceInfos network.SpaceInfos,
endpointBindings map[string]*state.Bindings,
) ([]string, error) {
cons, err := m.Constraints()
if err != nil {
return nil, errors.Annotate(err, "retrieving machine constraints")
}

includeSpaces := set.NewStrings(cons.IncludeSpaces()...)
excludeSpaces := set.NewStrings(cons.ExcludeSpaces()...)
Expand All @@ -340,13 +342,20 @@ func (api *ProvisionerAPI) machineSpaces(m *state.Machine,
return includeSpaces.SortedValues(), nil
}

func (api *ProvisionerAPI) machineSpaceTopology(machineID string, spaceNames []string) (params.ProvisioningNetworkTopology, error) {
func (api *ProvisionerAPI) machineSpaceTopology(
machineID string,
cons constraints.Value,
spaceNames []string,
) (params.ProvisioningNetworkTopology, error) {
var topology params.ProvisioningNetworkTopology

// If there are no space names, or if there is only one space
// name and that's the alpha space, we don't bother setting a
// topology that constrains provisioning.
if len(spaceNames) < 1 || (len(spaceNames) == 1 && spaceNames[0] == network.AlphaSpaceName) {
// name and that's the alpha space unless it was explicitly set as a
// constraint, we don't bother setting a topology that constrains
// provisioning.
consHasOnlyAlpha := len(cons.IncludeSpaces()) == 1 && cons.IncludeSpaces()[0] == network.AlphaSpaceName
if len(spaceNames) < 1 ||
((len(spaceNames) == 1 && spaceNames[0] == network.AlphaSpaceName) && !consHasOnlyAlpha) {
return topology, nil
}

Expand Down
77 changes: 77 additions & 0 deletions apiserver/facades/agent/provisioner/provisioninginfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,83 @@ func (s *withoutControllerSuite) TestConflictingNegativeConstraintWithBindingErr
c.Assert(result, jc.DeepEquals, expected)
}

func (s *withoutControllerSuite) TestNoSpaceConstraintsProvidedSpaceTopologyEmpty(c *gc.C) {
wordpressMachine, err := s.State.AddOneMachine(state.MachineTemplate{
Base: state.UbuntuBase("12.10"),
Jobs: []state.MachineJob{state.JobHostUnits},
})
c.Assert(err, jc.ErrorIsNil)

// Simulates running `juju deploy --bind "..."`.
bindings := map[string]string{
"url": "alpha",
"db": "alpha",
}
wordpressCharm := s.AddTestingCharm(c, "wordpress")
wordpressService := s.AddTestingApplicationWithBindings(c, "wordpress", wordpressCharm, bindings)
wordpressUnit, err := wordpressService.AddUnit(state.AddUnitParams{})
c.Assert(err, jc.ErrorIsNil)
err = wordpressUnit.AssignToMachine(wordpressMachine)
c.Assert(err, jc.ErrorIsNil)

args := params.Entities{Entities: []params.Entity{
{Tag: wordpressMachine.Tag().String()},
}}
result, err := s.provisioner.ProvisioningInfo(args)
c.Assert(err, jc.ErrorIsNil)

c.Assert(result.Results, gc.HasLen, 1)
c.Assert(result.Results[0].Error, gc.IsNil)
c.Assert(result.Results[0].Result.ProvisioningNetworkTopology.SubnetAZs, gc.IsNil)
c.Assert(result.Results[0].Result.ProvisioningNetworkTopology.SpaceSubnets, gc.IsNil)
}

func (s *withoutControllerSuite) TestAlphaSpaceConstraintsProvidedExplicitly(c *gc.C) {
s.addSpacesAndSubnets(c)

alphaSpace, err := s.State.SpaceByName("alpha")
c.Assert(err, jc.ErrorIsNil)

testing.AddSubnetsWithTemplate(c, s.State, 1, network.SubnetInfo{
CIDR: "10.10.{{add 4 .}}.0/24",
ProviderId: "subnet-alpha",
AvailabilityZones: []string{"zone-alpha"},
SpaceID: alphaSpace.Id(),
VLANTag: 43,
})

cons := constraints.MustParse("spaces=alpha")
wordpressMachine, err := s.State.AddOneMachine(state.MachineTemplate{
Base: state.UbuntuBase("12.10"),
Jobs: []state.MachineJob{state.JobHostUnits},
Constraints: cons,
})
c.Assert(err, jc.ErrorIsNil)

// Simulates running `juju deploy --bind "..."`.
bindings := map[string]string{
"url": "alpha",
"db": "alpha",
}
wordpressCharm := s.AddTestingCharm(c, "wordpress")
wordpressService := s.AddTestingApplicationWithBindings(c, "wordpress", wordpressCharm, bindings)
wordpressUnit, err := wordpressService.AddUnit(state.AddUnitParams{})
c.Assert(err, jc.ErrorIsNil)
err = wordpressUnit.AssignToMachine(wordpressMachine)
c.Assert(err, jc.ErrorIsNil)

args := params.Entities{Entities: []params.Entity{
{Tag: wordpressMachine.Tag().String()},
}}
result, err := s.provisioner.ProvisioningInfo(args)
c.Assert(err, jc.ErrorIsNil)

c.Assert(result.Results, gc.HasLen, 1)
c.Assert(result.Results[0].Error, gc.IsNil)
c.Assert(result.Results[0].Result.ProvisioningNetworkTopology.SubnetAZs, gc.DeepEquals, map[string][]string{"subnet-alpha": {"zone-alpha"}})
c.Assert(result.Results[0].Result.ProvisioningNetworkTopology.SpaceSubnets, gc.DeepEquals, map[string][]string{"alpha": {"subnet-alpha"}})
}

func (s *withoutControllerSuite) addSpacesAndSubnets(c *gc.C) {
// Add a couple of spaces.
space1, err := s.State.AddSpace("space1", "first space id", nil, true)
Expand Down
Loading

0 comments on commit 5cfda08

Please sign in to comment.