Class: Gitlab::Kubernetes::NetworkPolicy

Inherits:
Object
  • Object
show all
Extended by:
Utils::Override
Includes:
NetworkPolicyCommon
Defined in:
lib/gitlab/kubernetes/network_policy.rb

Constant Summary collapse

KIND =
'NetworkPolicy'

Constants included from NetworkPolicyCommon

Gitlab::Kubernetes::NetworkPolicyCommon::DISABLED_BY_LABEL

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Override

extended, extensions, included, method_added, override, prepended, queue_verification, verify!

Methods included from NetworkPolicyCommon

#as_json, #autodevops?, #disable, #enable, #enabled?, #generate

Constructor Details

#initialize(name:, namespace:, selector:, ingress:, labels: nil, creation_timestamp: nil, policy_types: ["Ingress"], egress: nil, environment_ids: []) ⇒ NetworkPolicy

rubocop:disable Metrics/ParameterLists


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gitlab/kubernetes/network_policy.rb', line 12

def initialize(name:, namespace:, selector:, ingress:, labels: nil, creation_timestamp: nil, policy_types: ["Ingress"], egress: nil, environment_ids: [])
  @name = name
  @namespace = namespace
  @labels = labels
  @creation_timestamp = creation_timestamp
  @selector = selector
  @policy_types = policy_types
  @ingress = ingress
  @egress = egress
  @environment_ids = environment_ids
end

Class Method Details

.from_resource(resource, environment_ids = []) ⇒ Object


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gitlab/kubernetes/network_policy.rb', line 46

def self.from_resource(resource, environment_ids = [])
  return unless resource
  return if !resource[:metadata] || !resource[:spec]

   = resource[:metadata]
  spec = resource[:spec].to_h
  self.new(
    name: [:name],
    namespace: [:namespace],
    labels: [:labels]&.to_h,
    creation_timestamp: [:creationTimestamp],
    selector: spec[:podSelector],
    policy_types: spec[:policyTypes],
    ingress: spec[:ingress],
    egress: spec[:egress],
    environment_ids: environment_ids
  )
end

.from_yaml(manifest) ⇒ Object

rubocop:enable Metrics/ParameterLists


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gitlab/kubernetes/network_policy.rb', line 25

def self.from_yaml(manifest)
  return unless manifest

  policy = YAML.safe_load(manifest, symbolize_names: true)
  return if !policy[:metadata] || !policy[:spec]

   = policy[:metadata]
  spec = policy[:spec]
  self.new(
    name: [:name],
    namespace: [:namespace],
    labels: [:labels],
    selector: spec[:podSelector],
    policy_types: spec[:policyTypes],
    ingress: spec[:ingress],
    egress: spec[:egress]
  )
rescue Psych::SyntaxError, Psych::DisallowedClass
  nil
end

Instance Method Details

#resourceObject


66
67
68
69
70
71
72
# File 'lib/gitlab/kubernetes/network_policy.rb', line 66

def resource
  {
    kind: KIND,
    metadata: ,
    spec: spec
  }
end