Class: Gitlab::Kubernetes::CiliumNetworkPolicy

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

Constant Summary collapse

API_VERSION =
"cilium.io/v2"
KIND =
'CiliumNetworkPolicy'
PREDEFINED_POLICIES =
{
  'allow-inbound-http' => <<~YAML.rstrip,
  'drop-outbound' => <<~YAML.rstrip
    apiVersion: cilium.io/v2
    kind: CiliumNetworkPolicy
    metadata:
      name: drop-outbound
    spec:
      endpointSelector:
        matchLabels:
          network-policy.gitlab.com/disabled_by: gitlab
      egress:
      - {}
  YAML
}.freeze

Constants included from NetworkPolicyCommon

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:, resource_version: nil, description: nil, labels: nil, creation_timestamp: nil, egress: nil, annotations: nil, environment_ids: []) ⇒ CiliumNetworkPolicy

We are modeling existing kubernetes resource and don't have control over amount of parameters. rubocop:disable Metrics/ParameterLists


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gitlab/kubernetes/cilium_network_policy.rb', line 45

def initialize(name:, namespace:, selector:, ingress:, resource_version: nil, description: nil, labels: nil, creation_timestamp: nil, egress: nil, annotations: nil, environment_ids: [])
  @name = name
  @description = description
  @namespace = namespace
  @labels = labels
  @creation_timestamp = creation_timestamp
  @selector = selector
  @resource_version = resource_version
  @ingress = ingress
  @egress = egress
  @annotations = annotations
  @environment_ids = environment_ids
end

Class Method Details

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


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/gitlab/kubernetes/cilium_network_policy.rb', line 83

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],
    description: resource[:description],
    namespace: [:namespace],
    annotations: [:annotations]&.to_h,
    resource_version: [:resourceVersion],
    labels: [:labels]&.to_h,
    creation_timestamp: [:creationTimestamp],
    selector: spec[:endpointSelector],
    ingress: spec[:ingress],
    egress: spec[:egress],
    environment_ids: environment_ids
  )
end

.from_yaml(manifest) ⇒ Object

rubocop:enable Metrics/ParameterLists


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gitlab/kubernetes/cilium_network_policy.rb', line 60

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],
    description: policy[:description],
    namespace: [:namespace],
    annotations: [:annotations],
    resource_version: [:resourceVersion],
    labels: [:labels],
    selector: spec[:endpointSelector],
    ingress: spec[:ingress],
    egress: spec[:egress]
  )
rescue Psych::SyntaxError, Psych::DisallowedClass
  nil
end

Instance Method Details

#resourceObject


105
106
107
108
109
110
111
112
113
114
# File 'lib/gitlab/kubernetes/cilium_network_policy.rb', line 105

def resource
  resource = {
    apiVersion: API_VERSION,
    kind: KIND,
    metadata: ,
    spec: spec
  }
  resource[:description] = description if description
  resource
end