本文整理汇总了Golang中github.com/juju/juju/state.Subnet.PickNewAddress方法的典型用法代码示例。如果您正苦于以下问题:Golang Subnet.PickNewAddress方法的具体用法?Golang Subnet.PickNewAddress怎么用?Golang Subnet.PickNewAddress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/juju/juju/state.Subnet
的用法示例。
在下文中一共展示了Subnet.PickNewAddress方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: allocateAddress
// allocateAddress tries to pick an address out of the given subnet and
// allocates it to the container.
func (p *ProvisionerAPI) allocateAddress(
environ environs.NetworkingEnviron,
subnet *state.Subnet,
host, container *state.Machine,
instId instance.Id,
macAddress string,
) (*state.IPAddress, error) {
subnetId := network.Id(subnet.ProviderId())
name := names.NewMachineTag(container.Id()).String()
for {
addr, err := subnet.PickNewAddress()
if err != nil {
return nil, err
}
logger.Tracef("picked new address %q on subnet %q", addr.String(), subnetId)
// Attempt to allocate with environ.
err = environ.AllocateAddress(instId, subnetId, addr.Address(), macAddress, name)
if err != nil {
logger.Warningf(
"allocating address %q on instance %q and subnet %q failed: %v (retrying)",
addr.String(), instId, subnetId, err,
)
// It's as good as unavailable for us, so mark it as
// such.
err = setAddrState(addr, state.AddressStateUnavailable)
if err != nil {
logger.Warningf(
"cannot set address %q to %q: %v (ignoring and retrying)",
addr.String(), state.AddressStateUnavailable, err,
)
continue
}
logger.Tracef(
"setting address %q to %q and retrying",
addr.String(), state.AddressStateUnavailable,
)
continue
}
logger.Infof(
"allocated address %q on instance %q and subnet %q",
addr.String(), instId, subnetId,
)
err = p.setAllocatedOrRelease(addr, environ, instId, container, subnetId, macAddress)
if err != nil {
// Something went wrong - retry.
continue
}
return addr, nil
}
}
示例2: allocateAddress
// allocateAddress tries to pick an address out of the given subnet and
// allocates it to the container.
func (p *ProvisionerAPI) allocateAddress(
environ environs.NetworkingEnviron,
subnet *state.Subnet,
host, container *state.Machine,
instId instance.Id,
macAddress string,
) (*state.IPAddress, error) {
hostname := containerHostname(container.Tag())
if !environs.AddressAllocationEnabled() {
// Even if the address allocation feature flag is not enabled, we might
// be running on MAAS 1.8+ with devices support, which we can use to
// register containers getting IPs via DHCP. However, most of the usual
// allocation code can be bypassed, we just need the parent instance ID
// and a MAC address (no subnet or IP address).
allocatedAddress := network.Address{}
err := environ.AllocateAddress(instId, network.AnySubnet, &allocatedAddress, macAddress, hostname)
if err != nil {
// Not using MAAS 1.8+ or some other error.
return nil, errors.Trace(err)
}
logger.Infof(
"allocated address %q on instance %q for container %q",
allocatedAddress.String(), instId, hostname,
)
// Add the address to state, so we can look it up later by MAC address.
stateAddr, err := p.st.AddIPAddress(allocatedAddress, string(network.AnySubnet))
if err != nil {
return nil, errors.Annotatef(err, "failed to save address %q", allocatedAddress)
}
err = p.setAllocatedOrRelease(stateAddr, environ, instId, container, network.AnySubnet, macAddress)
if err != nil {
return nil, errors.Trace(err)
}
return stateAddr, nil
}
subnetId := network.Id(subnet.ProviderId())
for {
addr, err := subnet.PickNewAddress()
if err != nil {
return nil, err
}
netAddr := addr.Address()
logger.Tracef("picked new address %q on subnet %q", addr.String(), subnetId)
// Attempt to allocate with environ.
err = environ.AllocateAddress(instId, subnetId, &netAddr, macAddress, hostname)
if err != nil {
logger.Warningf(
"allocating address %q on instance %q and subnet %q failed: %v (retrying)",
addr.String(), instId, subnetId, err,
)
// It's as good as unavailable for us, so mark it as
// such.
err = setAddrState(addr, state.AddressStateUnavailable)
if err != nil {
logger.Warningf(
"cannot set address %q to %q: %v (ignoring and retrying)",
addr.String(), state.AddressStateUnavailable, err,
)
continue
}
logger.Tracef(
"setting address %q to %q and retrying",
addr.String(), state.AddressStateUnavailable,
)
continue
}
logger.Infof(
"allocated address %q on instance %q and subnet %q",
addr.String(), instId, subnetId,
)
err = p.setAllocatedOrRelease(addr, environ, instId, container, subnetId, macAddress)
if err != nil {
// Something went wrong - retry.
continue
}
return addr, nil
}
}