Skip to content

Commit

Permalink
Introduces new types to replace the Address type in core/network.
Browse files Browse the repository at this point in the history
These are:
- SpaceAddress, for storage in state, which holds a space ID.
- ProviderAddress, for client-side space-aware addresses.
- MachineAddress, for addresses that are not space-aware.
Each of these has corresponding types to replace `HostPorts` in core/network.

New types for slices of these entities exist with methods to do selection/ordering/filtering.
They replace previous exported package methods and for scope matching methods move the burden of matcher choice to the caller.
  • Loading branch information
manadart committed Sep 20, 2019
1 parent 2f1e6cf commit 857bf73
Show file tree
Hide file tree
Showing 259 changed files with 4,062 additions and 3,271 deletions.
8 changes: 4 additions & 4 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type APIHostPortsSetter struct {
}

// SetAPIHostPorts is the APIAddressSetter interface.
func (s APIHostPortsSetter) SetAPIHostPorts(servers [][]network.HostPort) error {
func (s APIHostPortsSetter) SetAPIHostPorts(servers []network.HostPorts) error {
return s.ChangeConfig(func(c ConfigSetter) error {
c.SetAPIHostPorts(servers)
return nil
Expand Down Expand Up @@ -295,7 +295,7 @@ type configSetterOnly interface {
SetUpgradedToVersion(newVersion version.Number)

// SetAPIHostPorts sets the API host/port addresses to connect to.
SetAPIHostPorts(servers [][]network.HostPort)
SetAPIHostPorts(servers []network.HostPorts)

// SetCACert sets the CA cert used for validating API connections.
SetCACert(string)
Expand Down Expand Up @@ -568,13 +568,13 @@ func (c *configInternal) SetUpgradedToVersion(newVersion version.Number) {
c.upgradedToVersion = newVersion
}

func (c *configInternal) SetAPIHostPorts(servers [][]network.HostPort) {
func (c *configInternal) SetAPIHostPorts(servers []network.HostPorts) {
if c.apiDetails == nil {
return
}
var addrs []string
for _, serverHostPorts := range servers {
hps := network.PrioritizeInternalHostPorts(serverHostPorts, false)
hps := serverHostPorts.PrioritizedForScope(network.ScopeMatchCloudLocal)
addrs = append(addrs, hps...)
}
c.apiDetails.addresses = addrs
Expand Down
18 changes: 9 additions & 9 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,27 +590,27 @@ func (*suite) TestSetAPIHostPorts(c *gc.C) {
//
// If a server has only machine-local addresses, or none
// at all, then it will be excluded.
server1 := network.NewAddresses("0.1.0.1", "0.1.0.2", "host.com")
server1 := network.NewSpaceAddresses("0.1.0.1", "0.1.0.2", "host.com")
server1[0].Scope = network.ScopeCloudLocal
server1[1].Scope = network.ScopeCloudLocal
server1[2].Scope = network.ScopePublic

server2 := network.NewAddresses("0.2.0.1", "0.2.0.2")
server2 := network.NewSpaceAddresses("0.2.0.1", "0.2.0.2")
server2[0].Scope = network.ScopePublic
server2[1].Scope = network.ScopePublic

server3 := network.NewAddresses("127.0.0.1")
server3 := network.NewSpaceAddresses("127.0.0.1")
server3[0].Scope = network.ScopeMachineLocal

server4 := network.NewAddresses("0.4.0.1", "elsewhere.net")
server4 := network.NewSpaceAddresses("0.4.0.1", "elsewhere.net")
server4[0].Scope = network.ScopeUnknown
server4[1].Scope = network.ScopeUnknown

conf.SetAPIHostPorts([][]network.HostPort{
network.AddressesWithPort(server1, 1111),
network.AddressesWithPort(server2, 2222),
network.AddressesWithPort(server3, 3333),
network.AddressesWithPort(server4, 4444),
conf.SetAPIHostPorts([]network.HostPorts{
network.SpaceAddressesWithPort(server1, 1111).HostPorts(),
network.SpaceAddressesWithPort(server2, 2222).HostPorts(),
network.SpaceAddressesWithPort(server3, 3333).HostPorts(),
network.SpaceAddressesWithPort(server4, 4444).HostPorts(),
})
addrs, err = conf.APIAddresses()
c.Assert(err, jc.ErrorIsNil)
Expand Down
41 changes: 29 additions & 12 deletions agent/agentbootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type InitializeStateParams struct {
instancecfg.StateInitializationParams

// BootstrapMachineAddresses holds the bootstrap machine's addresses.
BootstrapMachineAddresses []corenetwork.Address
BootstrapMachineAddresses corenetwork.ProviderAddresses

// BootstrapMachineJobs holds the jobs that the bootstrap machine
// agent will run.
Expand Down Expand Up @@ -162,6 +162,7 @@ func InitializeState(

// Filter out any LXC or LXD bridge addresses from the machine addresses.
args.BootstrapMachineAddresses = network.FilterBridgeAddresses(args.BootstrapMachineAddresses)

st := ctrl.SystemState()

// Fetch spaces from substrate.
Expand All @@ -176,6 +177,9 @@ func InitializeState(
}
}

// Convert the provider addresses that we got from the bootstrap instance
// to space ID decorated addresses.

if err = initAPIHostPorts(st, args.BootstrapMachineAddresses, servingInfo.APIPort); err != nil {
return nil, err
}
Expand Down Expand Up @@ -207,7 +211,7 @@ func InitializeState(
return nil, errors.Annotate(err, "cannot initialize cloud service")
}
} else {
if controllerNode, err = initBootstrapMachine(c, st, args); err != nil {
if controllerNode, err = initBootstrapMachine(st, args); err != nil {
return nil, errors.Annotate(err, "cannot initialize bootstrap machine")
}
}
Expand Down Expand Up @@ -402,11 +406,7 @@ func initMongo(info mongo.Info, dialOpts mongo.DialOpts, password string) (*mgo.
}

// initBootstrapMachine initializes the initial bootstrap machine in state.
func initBootstrapMachine(
c agent.ConfigSetter,
st *state.State,
args InitializeStateParams,
) (bootstrapController, error) {
func initBootstrapMachine(st *state.State, args InitializeStateParams) (bootstrapController, error) {
model, err := st.Model()
if err != nil {
return nil, errors.Trace(err)
Expand All @@ -425,12 +425,19 @@ func initBootstrapMachine(
if args.BootstrapMachineHardwareCharacteristics != nil {
hardware = *args.BootstrapMachineHardwareCharacteristics
}

hostSeries, err := series.HostSeries()
if err != nil {
return nil, errors.Trace(err)
}

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

m, err := st.AddOneMachine(state.MachineTemplate{
Addresses: args.BootstrapMachineAddresses,
Addresses: spaceAddrs,
Series: hostSeries,
Nonce: agent.BootstrapNonce,
Constraints: args.BootstrapMachineConstraints,
Expand Down Expand Up @@ -485,25 +492,35 @@ func initControllerCloudService(
if err != nil {
return errors.Trace(err)
}

if len(svc.Addresses) == 0 {
// this should never happen because we have already checked in k8s controller bootstrap stacker.
return errors.NotProvisionedf("k8s controller service %q address", svc.Id)
}
addrs, err := svc.Addresses.ToSpaceAddresses(st)
if err != nil {
return errors.Trace(err)
}

svcId := controllerUUID
logger.Infof("creating cloud service for k8s controller %q", svcId)
cloudSvc, err := st.SaveCloudService(state.SaveCloudServiceArgs{
Id: svcId,
ProviderId: svc.Id,
Addresses: svc.Addresses,
Addresses: addrs,
})
logger.Debugf("created cloud service %v for controller", cloudSvc)
return errors.Trace(err)
}

// initAPIHostPorts sets the initial API host/port addresses in state.
func initAPIHostPorts(st *state.State, addrs []corenetwork.Address, apiPort int) error {
hostPorts := corenetwork.AddressesWithPort(addrs, apiPort)
return st.SetAPIHostPorts([][]corenetwork.HostPort{hostPorts})
func initAPIHostPorts(st *state.State, pAddrs corenetwork.ProviderAddresses, apiPort int) error {
addrs, err := pAddrs.ToSpaceAddresses(st)
if err != nil {
return errors.Trace(err)
}
hostPorts := corenetwork.SpaceAddressesWithPort(addrs, apiPort)
return st.SetAPIHostPorts([]corenetwork.SpaceHostPorts{hostPorts})
}

// machineJobFromParams returns the job corresponding to params.MachineJob.
Expand Down
8 changes: 4 additions & 4 deletions agent/agentbootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ LXC_BRIDGE="ignored"`[1:])
expectBootstrapConstraints := constraints.MustParse("mem=1024M")
expectModelConstraints := constraints.MustParse("mem=512M")
expectHW := instance.MustParseHardware("mem=2048M")
initialAddrs := corenetwork.NewAddresses(
initialAddrs := corenetwork.NewProviderAddresses(
"zeroonetwothree",
"0.1.2.3",
"10.0.3.1", // lxc bridge address filtered.
Expand All @@ -132,7 +132,7 @@ LXC_BRIDGE="ignored"`[1:])
"10.0.4.4", // lxd bridge address filtered.
"10.0.4.5", // not an lxd bridge address
)
filteredAddrs := corenetwork.NewAddresses(
filteredAddrs := corenetwork.NewSpaceAddresses(
"zeroonetwothree",
"0.1.2.3",
"10.0.3.3",
Expand Down Expand Up @@ -293,8 +293,8 @@ LXC_BRIDGE="ignored"`[1:])
// Check that the API host ports are initialised correctly.
apiHostPorts, err := st.APIHostPortsForClients()
c.Assert(err, jc.ErrorIsNil)
c.Assert(apiHostPorts, jc.DeepEquals, [][]corenetwork.HostPort{
corenetwork.AddressesWithPort(filteredAddrs, 1234),
c.Assert(apiHostPorts, jc.DeepEquals, []corenetwork.SpaceHostPorts{
corenetwork.SpaceAddressesWithPort(filteredAddrs, 1234),
})

// Check that the state serving info is initialised correctly.
Expand Down
14 changes: 7 additions & 7 deletions api/apiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ type state struct {
serverVersion version.Number

// hostPorts is the API server addresses returned from Login,
// which the client may cache and use for failover.
hostPorts [][]network.HostPort
// which the client may cache and use for fail-over.
hostPorts []network.MachineHostPorts

// publicDNSName is the public host name returned from Login
// which the client can use to make a connection verified
Expand Down Expand Up @@ -160,7 +160,7 @@ type state struct {
type RedirectError struct {
// Servers holds the sets of addresses of the redirected
// servers.
Servers [][]network.HostPort
Servers []network.MachineHostPorts

// CACert holds the certificate of the remote server.
CACert string
Expand Down Expand Up @@ -1257,12 +1257,12 @@ func (s *state) ControllerTag() names.ControllerTag {
// Juju CLI, all addresses must be attempted, as the CLI may
// be invoked both within and outside the model (think
// private clouds).
func (s *state) APIHostPorts() [][]network.HostPort {
func (s *state) APIHostPorts() []network.MachineHostPorts {
// NOTE: We're making a copy of s.hostPorts before returning it,
// for safety.
hostPorts := make([][]network.HostPort, len(s.hostPorts))
for i, server := range s.hostPorts {
hostPorts[i] = append([]network.HostPort{}, server...)
hostPorts := make([]network.MachineHostPorts, len(s.hostPorts))
for i, servers := range s.hostPorts {
hostPorts[i] = append(network.MachineHostPorts{}, servers...)
}
return hostPorts
}
Expand Down
15 changes: 11 additions & 4 deletions api/apiclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,9 +687,15 @@ func (s *apiclientSuite) TestOpenWithRedirect(c *gc.C) {
}, api.DialOpts{})
c.Assert(err, gc.ErrorMatches, `redirection to alternative server required`)

hps, _ := network.ParseHostPorts(redirectToHosts...)
hps := make(network.MachineHostPorts, len(redirectToHosts))
for i, addr := range redirectToHosts {
hp, err := network.ParseMachineHostPort(addr)
c.Assert(err, jc.ErrorIsNil)
hps[i] = *hp
}

c.Assert(errors.Cause(err), jc.DeepEquals, &api.RedirectError{
Servers: [][]network.HostPort{hps},
Servers: []network.MachineHostPorts{hps},
CACert: redirectToCACert,
FollowRedirect: true,
})
Expand Down Expand Up @@ -1340,12 +1346,13 @@ func (a *redirectAPIAdmin) RedirectInfo() (params.RedirectInfoResult, error) {
if !a.r.redirected {
return params.RedirectInfoResult{}, errors.New("not redirected")
}
hps, err := network.ParseHostPorts(a.r.redirectToHosts...)

hps, err := network.ParseProviderHostPorts(a.r.redirectToHosts...)
if err != nil {
panic(err)
}
return params.RedirectInfoResult{
Servers: [][]params.HostPort{params.FromNetworkHostPorts(hps)},
Servers: [][]params.HostPort{params.FromProviderHostPorts(hps)},
CACert: a.r.redirectToCACert,
}, nil
}
Expand Down
6 changes: 3 additions & 3 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,13 +594,13 @@ func (c *Client) httpPost(content io.ReadSeeker, endpoint, contentType string, r
return nil
}

// APIHostPorts returns a slice of network.HostPort for each API server.
func (c *Client) APIHostPorts() ([][]network.HostPort, error) {
// APIHostPorts returns a slice of network.MachineHostPort for each API server.
func (c *Client) APIHostPorts() ([]network.MachineHostPorts, error) {
var result params.APIHostPortsResult
if err := c.facade.FacadeCall("APIHostPorts", nil, &result); err != nil {
return nil, err
}
return result.NetworkHostsPorts(), nil
return result.MachineHostsPorts(), nil
}

// AgentVersion reports the version number of the api server.
Expand Down
4 changes: 2 additions & 2 deletions api/common/apiaddresser.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ func (a *APIAddresser) ModelUUID() (string, error) {
}

// APIHostPorts returns the host/port addresses of the API servers.
func (a *APIAddresser) APIHostPorts() ([][]network.HostPort, error) {
func (a *APIAddresser) APIHostPorts() ([]network.ProviderHostPorts, error) {
var result params.APIHostPortsResult
err := a.facade.FacadeCall("APIHostPorts", nil, &result)
if err != nil {
return nil, err
}
return result.NetworkHostsPorts(), nil
return params.ToProviderHostsPorts(result.Servers), nil
}

// WatchAPIHostPorts watches the host/port addresses of the API servers.
Expand Down
2 changes: 1 addition & 1 deletion api/deployer/deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var _ = gc.Suite(&deployerSuite{})
func (s *deployerSuite) SetUpTest(c *gc.C) {
s.JujuConnSuite.SetUpTest(c)
s.stateAPI, s.machine = s.OpenAPIAsNewMachine(c, state.JobManageModel, state.JobHostUnits)
err := s.machine.SetProviderAddresses(network.NewAddress("0.1.2.3"))
err := s.machine.SetProviderAddresses(network.NewSpaceAddress("0.1.2.3"))
c.Assert(err, jc.ErrorIsNil)

// Create the needed applications and relate them.
Expand Down
2 changes: 1 addition & 1 deletion api/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func UnderlyingConn(c Connection) jsoncodec.JSONConn {
type TestingStateParams struct {
Address string
ModelTag string
APIHostPorts [][]network.HostPort
APIHostPorts []network.MachineHostPorts
FacadeVersions map[string][]int
ServerScheme string
ServerRoot string
Expand Down
6 changes: 3 additions & 3 deletions api/highavailability/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ func assertEnableHA(c *gc.C, s *jujutesting.JujuConnSuite) {
defer assertKill(c, pingerA)

err = m.SetMachineAddresses(
network.NewScopedAddress("127.0.0.1", network.ScopeMachineLocal),
network.NewScopedAddress("cloud-local0.internal", network.ScopeCloudLocal),
network.NewScopedAddress("fc00::1", network.ScopePublic),
network.NewScopedSpaceAddress("127.0.0.1", network.ScopeMachineLocal),
network.NewScopedSpaceAddress("cloud-local0.internal", network.ScopeCloudLocal),
network.NewScopedSpaceAddress("fc00::1", network.ScopePublic),
)
c.Assert(err, jc.ErrorIsNil)

Expand Down
16 changes: 8 additions & 8 deletions api/instancepoller/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ func (m *Machine) SetInstanceStatus(status status.Status, message string, data m
return result.OneError()
}

// ProviderAddresses returns all addresses of the machine known to the
// cloud provider.
func (m *Machine) ProviderAddresses() ([]network.Address, error) {
// ProviderAddresses returns all addresses of the machine
// known to the cloud provider.
func (m *Machine) ProviderAddresses() (network.ProviderAddresses, error) {
var results params.MachineAddressesResults
args := params.Entities{Entities: []params.Entity{
{Tag: m.tag.String()},
Expand All @@ -170,17 +170,17 @@ func (m *Machine) ProviderAddresses() ([]network.Address, error) {
if result.Error != nil {
return nil, result.Error
}
return params.NetworkAddresses(result.Addresses...), nil
return params.ToProviderAddresses(result.Addresses...), nil
}

// SetProviderAddresses sets the cached provider addresses for the
// machine.
func (m *Machine) SetProviderAddresses(addrs ...network.Address) error {
// SetProviderAddresses sets the cached provider
// addresses for the machine.
func (m *Machine) SetProviderAddresses(addrs ...network.ProviderAddress) error {
var result params.ErrorResults
args := params.SetMachinesAddresses{
MachineAddresses: []params.MachineAddresses{{
Tag: m.tag.String(),
Addresses: params.FromNetworkAddresses(addrs...),
Addresses: params.FromProviderAddresses(addrs...),
}}}
err := m.facade.FacadeCall("SetProviderAddresses", args, &result)
if err != nil {
Expand Down
Loading

0 comments on commit 857bf73

Please sign in to comment.