Class: NetworkPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/xlsx_to_k8s_network_policy.rb

Overview

The real NetworkPolicy

Defined Under Namespace

Classes: NetworkPolicyPeer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, pod_selector) ⇒ NetworkPolicy

Returns a new instance of NetworkPolicy.



68
69
70
71
72
73
74
# File 'lib/xlsx_to_k8s_network_policy.rb', line 68

def initialize(name, pod_selector)
  raise %(Invalid name "#{name}". Must consist of [a-z_-]+) unless /^[a-z_-]+$/ =~ name
  @name = name
  @pod_selector = pod_selector
  @ingresses = []
  @egresses = []
end

Instance Attribute Details

#egressesObject (readonly)

Returns the value of attribute egresses.



62
63
64
# File 'lib/xlsx_to_k8s_network_policy.rb', line 62

def egresses
  @egresses
end

#ingressesObject (readonly)

Returns the value of attribute ingresses.



61
62
63
# File 'lib/xlsx_to_k8s_network_policy.rb', line 61

def ingresses
  @ingresses
end

#nameObject (readonly)

Returns the value of attribute name.



59
60
61
# File 'lib/xlsx_to_k8s_network_policy.rb', line 59

def name
  @name
end

#pod_selectorObject (readonly)

Returns the value of attribute pod_selector.



60
61
62
# File 'lib/xlsx_to_k8s_network_policy.rb', line 60

def pod_selector
  @pod_selector
end

Class Method Details

.deny_allObject



64
65
66
# File 'lib/xlsx_to_k8s_network_policy.rb', line 64

def self.deny_all
  NetworkPolicy.new('default-deny', PodSelector.new)
end

Instance Method Details

#add_cidr_egress(cidr) ⇒ Object



142
143
144
# File 'lib/xlsx_to_k8s_network_policy.rb', line 142

def add_cidr_egress(cidr)
  add_egress(NetworkPolicyPeer::IPBlock.new(cidr))
end

#add_cidr_ingress(cidr) ⇒ Object



138
139
140
# File 'lib/xlsx_to_k8s_network_policy.rb', line 138

def add_cidr_ingress(cidr)
  add_ingress(NetworkPolicyPeer::IPBlock.new(cidr))
end

#add_egress(egress) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/xlsx_to_k8s_network_policy.rb', line 158

def add_egress(egress)
  npp = case egress
        when PodSelector
          NetworkPolicyPeer::PodSelectorNPP.new(egress)
        when NetworkPolicyPeer
          egress
        else
          raise "Don't know how to handle ingress of type #{egress.class}!"
        end
  @egresses << npp unless @egresses.include?(npp)
end

#add_ingress(ingress) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/xlsx_to_k8s_network_policy.rb', line 146

def add_ingress(ingress)
  npp = case ingress
        when PodSelector
          NetworkPolicyPeer::PodSelectorNPP.new(ingress)
        when NetworkPolicyPeer
          ingress
        else
          raise "Don't know how to handle ingress of type #{ingress.class}!"
        end
  @ingresses << npp unless @ingresses.include?(npp)
end

#add_pod_selector_egress(pod_selector) ⇒ Object



134
135
136
# File 'lib/xlsx_to_k8s_network_policy.rb', line 134

def add_pod_selector_egress(pod_selector)
  add_egress(NetworkPolicyPeer::PodSelectorNPP.new(pod_selector))
end

#add_pod_selector_ingress(pod_selector) ⇒ Object



130
131
132
# File 'lib/xlsx_to_k8s_network_policy.rb', line 130

def add_pod_selector_ingress(pod_selector)
  add_ingress(NetworkPolicyPeer::PodSelectorNPP.new(pod_selector))
end

#as_hashObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/xlsx_to_k8s_network_policy.rb', line 170

def as_hash
  policy_types = []
  policy_types << 'Ingress' if !@ingresses.empty? || @egresses.empty?
  policy_types << 'Egress' if !@egresses.empty? || @ingresses.empty?
  spec = pod_selector.as_hash
  spec[:policyTypes] = policy_types
  hash = {
    apiVersion: 'networking.k8s.io/v1',
    kind: 'NetworkPolicy',
    metadata: {
      name: name
    },
    spec: spec
  }
  add_ingress_and_egress(hash)
  hash.deep_stringify_keys
end