Class: Ingress

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/mkit/app/model/ingress.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(yaml) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/mkit/app/model/ingress.rb', line 10

def self.create(yaml)
  validate(yaml)
  ingress = Ingress.new

  yaml["backend"].each do |back|
    ingress.backends << Backend.create(back)
  end

  yaml["frontend"].each do |front|
    ingress.frontends << Frontend.create(front)
  end
  ingress
end

.validate(yaml) ⇒ Object



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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/mkit/app/model/ingress.rb', line 24

def self.validate(yaml)
  frontend_names = []
  frontend_ports = []

  # must have at least one frontend and one backend
  raise_bad_configuration "Ingress section is mandatory" unless yaml
  raise_bad_configuration "At least one frontend is mandatory" unless yaml["frontend"]
  raise_bad_configuration "At least one backend is mandatory" unless yaml["backend"]
  # frontend name is mandatory
  yaml["frontend"].each { |front| raise "Frontend name is mandatory" unless front["name"] }
  # backend name is mandatory
  yaml["backend"].each { |back| raise "Backend name is mandatory" unless back["name"] }
  # frontend must point to a valid backend name - backend names list
  backend_names = yaml["backend"].map { |back| back["name"] }

  # frontend validation
  yaml["frontend"].each do |front|
    # name is mandatory
    raise_bad_configuration "Frontend name is mandatory" unless front["name"]
    # frontend name must be unique
    if frontend_names.include?(front["name"])
      raise_bad_configuration "Frontend name '#{front["name"]}' must be unique"
    end
    frontend_names << front["name"]

    # bind and mode are mandatory, port is not
    raise_bad_configuration "Frontend bind and mode are mandatory" unless front["bind"] && front["bind"]["mode"]

    # port must be unique
    if frontend_ports.include?(front["bind"]["port"])
      raise_bad_configuration "Frontend port '#{front["bind"]["port"]}' must be unique"
    end
    frontend_ports << front["bind"]["port"]

    # default_backend must point to a valid backend name
    unless backend_names.include?(front["default_backend"])
      raise_bad_configuration "Frontend default_backend '#{front["default_backend"]}' must point to a valid backend name"
    end

  end

  # backend validation
  backend_names.each do |name|
    if backend_names.count(name) > 1
      raise_bad_configuration "Backend name '#{name}' must be unique"
    end
  end

  # global validations
  # each backend must point to a valid frontend default_backend
  frontend_default_backends = yaml["frontend"].map { |front| front["default_backend"] }
  yaml["backend"].each do |back|
    unless frontend_default_backends.include?(back["name"])
      raise_bad_configuration "Backend '#{back["name"]}' must be referenced by at least one frontend default_backend"
    end
  end

  # when frontend port is range - e.g. 1200-1220, referred backend port must be empty
  yaml["frontend"].each do |front|
    if front["bind"]["port"] =~ /^\d+-\d+$/
      referred_backend = yaml["backend"].find { |back| back["name"] == front["default_backend"] }
      raise_bad_configuration "Frontend port range '#{front["bind"]["port"]}' must have an empty backend port" unless referred_backend && (referred_backend["bind"]["port"].nil?)
    end
  end

  true
end

Instance Method Details

#to_h(options = {}) ⇒ Object



92
93
94
95
96
97
# File 'lib/mkit/app/model/ingress.rb', line 92

def to_h(options = {})
  {
    frontend: self.frontends.map { |front| front.to_h },
    backend: self.backends.map { |back| back.to_h }
  }.remove_symbols_from_keys
end