Class: Bosh::Director::DeploymentPlan::IpProvider

Inherits:
Object
  • Object
show all
Includes:
IpUtil
Defined in:
lib/bosh/director/deployment_plan/ip_provider/ip_provider.rb

Instance Method Summary collapse

Methods included from IpUtil

#each_ip, #format_ip, #ip_to_i, #ip_to_netaddr

Constructor Details

#initialize(ip_repo, networks, logger) ⇒ IpProvider

Returns a new instance of IpProvider.



6
7
8
9
10
# File 'lib/bosh/director/deployment_plan/ip_provider/ip_provider.rb', line 6

def initialize(ip_repo, networks, logger)
  @logger = Bosh::Director::TaggedLogger.new(logger, 'network-configuration')
  @ip_repo = ip_repo
  @networks = networks
end

Instance Method Details

#release(reservation) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/bosh/director/deployment_plan/ip_provider/ip_provider.rb', line 12

def release(reservation)
  if reservation.ip.nil?
    return if reservation.network.is_a?(DynamicNetwork)

    @logger.error("Failed to release IP for manual network '#{reservation.network.name}': IP must be provided")
    raise Bosh::Director::NetworkReservationIpMissing, "Can't release reservation without an IP"
  else
    @ip_repo.delete(reservation.ip, reservation.network.name)
  end
end

#reserve(reservation) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bosh/director/deployment_plan/ip_provider/ip_provider.rb', line 23

def reserve(reservation)
  # We should not be calling reserve on reservations that have already been reserved
  return if reservation.reserved?

  if reservation.network.is_a?(DynamicNetwork)
    reserve_dynamic(reservation)
    return
  end

  if reservation.network.is_a?(VipNetwork)
    reserve_vip(reservation)
    return
  end

  # Reserve IP for Manual Network
  if reservation.ip.nil?
    @logger.debug("Allocating dynamic ip for manual network '#{reservation.network.name}'")

    filter_subnet_by_instance_az(reservation).each do |subnet|
      ip = @ip_repo.allocate_dynamic_ip(reservation, subnet)

      if ip
        @logger.debug("Reserving dynamic IP '#{format_ip(ip)}' for manual network '#{reservation.network.name}'")
        reservation.resolve_ip(ip)
        reservation.resolve_type(:dynamic)
        reservation.mark_reserved
        return
      end
    end

    raise NetworkReservationNotEnoughCapacity,
      "Failed to reserve IP for '#{reservation.instance_model}' for manual network '#{reservation.network.name}': no more available"

  else

    ip_string = format_ip(reservation.ip)
    @logger.debug("Reserving #{reservation.desc} for manual network '#{reservation.network.name}'")

    subnet = reservation.network.find_subnet_containing(reservation.ip)
    if subnet
      if subnet.restricted_ips.include?(reservation.ip.to_i)
        message = "Failed to reserve IP '#{ip_string}' for network '#{subnet.network_name}': IP belongs to reserved range"
        @logger.error(message)
        raise Bosh::Director::NetworkReservationIpReserved, message
      end

      reserve_manual(reservation, subnet)
    else
      raise NetworkReservationIpOutsideSubnet,
        "Provided static IP '#{ip_string}' does not belong to any subnet in network '#{reservation.network.name}'"
    end
  end
end

#reserve_existing_ips(reservation) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bosh/director/deployment_plan/ip_provider/ip_provider.rb', line 77

def reserve_existing_ips(reservation)
  if reservation.network.is_a?(DynamicNetwork)
    if reservation.network_type == 'dynamic'
      # Marking reservation as reserved so that it keeps existing reservation and
      # does not recreate VM
      reserve_dynamic(reservation)
    end

    # If previous network type was not dynamic we should release reservation from DB
    return
  end

  if reservation.network.is_a?(VipNetwork)
    reserve_vip(reservation)
    return
  end

  @logger.debug('Reserving existing ips')
  network, subnet = find_network_and_subnet_containing(reservation.ip, reservation.network.name)
  if subnet
    @logger.debug("Marking existing IP #{format_ip(reservation.ip)} as reserved")
    reservation.resolve_network(network)
    reserve_manual(reservation, subnet)
  end
end