Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an ability to get mtu value for transformation #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions lib/puppet/parser/functions/get_network_role_property.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
cidr -- CIDR-notated IP addr and mask for the network_role
netmask -- string, contains dotted nemmask
ipaddr_netmask_pair -- list of ipaddr and netmask
phys_dev -- physical device name mapped to the network with the selected network_role

Returns NIL if role not found.

Expand Down Expand Up @@ -69,7 +70,11 @@
ipaddr_cidr = ep[:IP][0] ? ep[:IP][0] : nil
when "String"
Puppet::debug("get_network_role_property(...): Can't determine dynamic or empty IP address for endpoint '#{interface}' (#{ep[:IP]}).")
return nil
if mode != 'PHYS_DEV'
return nil
end
when "NilClass"
ipaddr_cidr = nil
else
Puppet::debug("get_network_role_property(...): invalid IP address for endpoint '#{interface}'.")
return nil
Expand All @@ -80,14 +85,26 @@
when 'CIDR'
rv = ipaddr_cidr
when 'NETMASK'
rv = IPAddr.new('255.255.255.255').mask(prepare_cidr(ipaddr_cidr)[1]).to_s
rv = (ipaddr_cidr.nil? ? nil : IPAddr.new('255.255.255.255').mask(prepare_cidr(ipaddr_cidr)[1]).to_s)
when 'IPADDR'
rv = prepare_cidr(ipaddr_cidr)[0].to_s
rv = (ipaddr_cidr.nil? ? nil : prepare_cidr(ipaddr_cidr)[0].to_s)
when 'IPADDR_NETMASK_PAIR'
rv = prepare_cidr(ipaddr_cidr)[0].to_s, IPAddr.new('255.255.255.255').mask(prepare_cidr(ipaddr_cidr)[1]).to_s
rv = (ipaddr_cidr.nil? ? [nil,nil] : [prepare_cidr(ipaddr_cidr)[0].to_s, IPAddr.new('255.255.255.255').mask(prepare_cidr(ipaddr_cidr)[1]).to_s])
when 'PHYS_DEV'
devices = L23network::Scheme.get_phys_dev_by_endpoint(interface, cfg[:interfaces], cfg[:transformations])
if devices.any? { |dev| /^bond/ =~ dev }
for i in 0..cfg[:transformations].size-1
transform = cfg[:transformations][i]
name = transform[:name]
if transform[:name] == devices[0]
devices.push(transform[:interfaces])
end
end
end
rv = devices.flatten
end

rv
end

# vim: set ts=2 sw=2 et :
# vim: set ts=2 sw=2 et :
65 changes: 65 additions & 0 deletions lib/puppet/parser/functions/get_transformation_property.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'puppetx/l23_network_scheme'

Puppet::Parser::Functions::newfunction(:get_transformation_property, :type => :rvalue, :doc => <<-EOS
This function gets an endpoint properties from transformations --
and returns information about the selected property

ex: get_transformation_property('mtu','eth0')

You can use following modes:
mtu -- mtu value for the selected transformation.

Returns NIL if a device is not found or mtu is not set

EOS
) do |argv|
if argv.size > 1
mode = argv[0].to_s().upcase()
argv.shift
else
raise(Puppet::ParseError, "get_transformation_property(...): Wrong number of arguments.")
end

cfg = L23network::Scheme.get_config(lookupvar('l3_fqdn_hostname'))
if cfg.nil?
raise(Puppet::ParseError, "get_transformation_property(...): You must call prepare_network_config(...) first!")
end

if !cfg[:roles] || !cfg[:endpoints] || cfg[:roles].class.to_s() != "Hash" || cfg[:endpoints].class.to_s() != "Hash"
raise(Puppet::ParseError, "get_transformation_property(...): Invalid cfg_hash format.")
end

transforms = cfg[:transformations]
ifaces = cfg[:interfaces]
all_ifaces = ifaces.keys.map(&:to_s)
devices = argv.flatten
mtu = []

rv = nil

case mode
when 'MTU'
devices.each do |device|
if all_ifaces.include? device and ifaces[device.to_sym][:mtu] != nil
mtu.push(ifaces[device.to_sym][:mtu])
else
for i in 0..transforms.size-1 do
transform = cfg[:transformations][i]
if transform[:name] == device and transform[:mtu] != nil
mtu.push(transform[:mtu])
end
end
end
if mtu.empty?
Puppet::debug("get_transformation_property(...): MTU value is not set for interface '#{device}'.")
rv = nil
else
rv = mtu.min
end
end
end

rv
end

# vim: set ts=2 sw=2 et :
32 changes: 31 additions & 1 deletion lib/puppetx/l23_network_scheme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,36 @@ def self.set_config(h, v)
def self.get_config(h)
@network_scheme_hash[h.to_sym]
end
def self.get_phys_dev_by_endpoint(endpoint, interfaces, transformations)
rv = []
all_ifaces = interfaces.keys.map(&:to_s)
ifaces = []

if all_ifaces.include? endpoint
ifaces.push(endpoint)
return ifaces
end

for i in 0..transformations.size-1 do
transform = transformations[i]
if transform[:name] != nil and transform[:name].include? endpoint
if transform[:vlandev] != nil
ifaces.push(transform[:vlandev])
else
endpoint = transform[:name].split(".")[0]
if all_ifaces.include? endpoint
ifaces.push(endpoint.to_s)
end
end
elsif transform[:bridge] == endpoint
ifaces.push(transform[:name].split(".")[0])
elsif transform[:bridges] != nil and transform[:bridges][0] == endpoint
endpoint = transform[:bridges][1]
ifaces.push(get_phys_dev_by_endpoint(endpoint, interfaces, transformations))
end
end
return ifaces.flatten.uniq
end
end
end
# vim: set ts=2 sw=2 et :
# vim: set ts=2 sw=2 et :
149 changes: 149 additions & 0 deletions spec/functions/get_network_role_property__phys_dev__spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
require 'spec_helper'
require 'yaml'
require 'puppetx/l23_hash_tools'

describe Puppet::Parser::Functions.function(:get_network_role_property) do
let(:network_scheme) do
<<eof
---
version: 1.1
provider: lnx
interfaces:
eth0:
mtu: 2048
eth1:
mtu: 999
eth2: {}
eth3: {}
eth4: {}
transformations:
- action: add-br
name: br-storage
- action: add-br
name: br-ex
- action: add-br
name: br-mgmt
- action: add-port
name: eth4
mtu: 777
- action: add-port
name: eth1.101
bridge: br-mgmt
- action: add-bond
name: bond0
bridge: br-storage
interfaces:
- eth2
- eth3
mtu: 4000
bond_properties:
mode: balance-rr
interface_properties:
mtu: 9000
vendor_specific:
disable_offloading: true
- action: add-port
name: bond0.102
bridge: br-ex
- action: add-br
name: br-floating
provider: ovs
- action: add-patch
bridges:
- br-floating
- br-ex
provider: ovs
- action: add-br
name: br-prv
provider: ovs
- action: add-patch
bridges:
- br-prv
- br-storage
provider: ovs
endpoints:
eth0:
IP: 'none'
eth4:
IP: 'none'
br-ex:
gateway: 10.1.3.1
IP:
- '10.1.3.11/24'
br-storage:
IP:
- '10.1.2.11/24'
br-mgmt:
IP:
- '10.1.1.11/24'
br-floating:
IP: none
br-prv:
IP: none
roles:
admin: eth0
ex: br-ex
management: br-mgmt
storage: br-storage
neutron/floating: br-floating
neutron/private: br-prv
xxx: eth4
eof
end



let(:scope) { PuppetlabsSpec::PuppetInternals.scope }

subject do
function_name = Puppet::Parser::Functions.function(:get_network_role_property)
scope.method(function_name)
end

context "get_network_role_property('**something_role**', 'phys_dev') usage" do
before(:each) do
scope.stubs(:lookupvar).with('l3_fqdn_hostname').returns('node1.tld')
L23network::Scheme.set_config(
scope.lookupvar('l3_fqdn_hostname'),
L23network.sanitize_keys_in_hash(YAML.load(network_scheme))
)
end

it 'should exist' do
subject == Puppet::Parser::Functions.function(:get_network_role_property)
end

it 'should return physical device name for "management" network role (just subinterface)' do
should run.with_params('management', 'phys_dev').and_return(["eth1"])
end

it 'should return physical device name for "ex" network role (subinterface of bond)' do
should run.with_params('ex', 'phys_dev').and_return(["bond0", "eth2", "eth3"])
end

it 'should return physical device name for "floating" network role (OVS-bridge, connected by patch to LNX bridge)' do
should run.with_params('neutron/floating', 'phys_dev').and_return(["bond0", "eth2", "eth3"])
end

it 'should return physical device name for "private" network role' do
should run.with_params('neutron/private', 'phys_dev').and_return(["bond0", "eth2", "eth3"])
end

it 'should return physical device name for "storage" network role (bond, without tag)' do
should run.with_params('storage', 'phys_dev').and_return(["bond0", "eth2", "eth3"])
end

it 'should return physical device name for "admin" network role (just interface has IP address)' do
should run.with_params('admin', 'phys_dev').and_return(['eth0'])
end

it 'should return physical device name for untagged interface with simple transformation' do
should run.with_params('xxx', 'phys_dev').and_return(['eth4'])
end

it 'should return NIL for "non-existent" network role' do
should run.with_params('non-existent', 'phys_dev').and_return(nil)
end
end

end
18 changes: 17 additions & 1 deletion spec/functions/get_network_role_property__spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
#Puppet::Parser::Scope.any_instance.stubs(:lookupvar).with('l3_fqdn_hostname').returns('node1.tld')
scope.stubs(:lookupvar).with('l3_fqdn_hostname').returns('node1.tld')
L23network::Scheme.set_config(scope.lookupvar('l3_fqdn_hostname'), {
:transformations => [ {
:bridge => 'br-ex',
:name => 'bond0',
:mtu => 1450,
:interfaces => ["eth1", "eth2"],
}, ],
:interfaces => {
:eth0 => {},
},
:endpoints => {
:eth0 => {:IP => 'dhcp'},
:"br-ex" => {
Expand Down Expand Up @@ -61,6 +70,10 @@
should run.with_params('management', 'ipaddr_netmask_pair').and_return(['10.20.1.11','255.255.255.128'])
end

it 'should return physical device name for "ex" network role' do
should run.with_params('ex', 'phys_dev').and_return(["bond0", "eth1", "eth2"])
end

it 'should return NIL for "admin" network role' do
should run.with_params('admin', 'netmask').and_return(nil)
end
Expand All @@ -73,6 +86,9 @@
it 'should return NIL for "admin" network role' do
should run.with_params('admin', 'ipaddr_netmask_pair').and_return(nil)
end
it 'should return NIL for "admin" network role' do
should run.with_params('admin', 'phys_dev').and_return(nil)
end
end

end
end
Loading