debian-mirror-gitlab/lib/gitlab/kubernetes/network_policy.rb

95 lines
2.4 KiB
Ruby
Raw Normal View History

2020-05-24 23:13:21 +05:30
# frozen_string_literal: true
module Gitlab
module Kubernetes
class NetworkPolicy
2020-10-24 23:57:45 +05:30
include NetworkPolicyCommon
extend ::Gitlab::Utils::Override
2020-06-23 00:09:42 +05:30
2020-11-24 15:15:51 +05:30
KIND = 'NetworkPolicy'
2020-10-24 23:57:45 +05:30
def initialize(name:, namespace:, selector:, ingress:, labels: nil, creation_timestamp: nil, policy_types: ["Ingress"], egress: nil)
2020-05-24 23:13:21 +05:30
@name = name
@namespace = namespace
2020-06-23 00:09:42 +05:30
@labels = labels
2020-05-24 23:13:21 +05:30
@creation_timestamp = creation_timestamp
2020-10-24 23:57:45 +05:30
@selector = selector
2020-05-24 23:13:21 +05:30
@policy_types = policy_types
@ingress = ingress
@egress = egress
end
def self.from_yaml(manifest)
return unless manifest
policy = YAML.safe_load(manifest, symbolize_names: true)
return if !policy[:metadata] || !policy[:spec]
metadata = policy[:metadata]
spec = policy[:spec]
self.new(
name: metadata[:name],
namespace: metadata[:namespace],
2020-06-23 00:09:42 +05:30
labels: metadata[:labels],
2020-10-24 23:57:45 +05:30
selector: spec[:podSelector],
2020-05-24 23:13:21 +05:30
policy_types: spec[:policyTypes],
ingress: spec[:ingress],
egress: spec[:egress]
)
rescue Psych::SyntaxError, Psych::DisallowedClass
nil
end
def self.from_resource(resource)
return unless resource
return if !resource[:metadata] || !resource[:spec]
metadata = resource[:metadata]
spec = resource[:spec].to_h
self.new(
name: metadata[:name],
namespace: metadata[:namespace],
2020-06-23 00:09:42 +05:30
labels: metadata[:labels]&.to_h,
2020-05-24 23:13:21 +05:30
creation_timestamp: metadata[:creationTimestamp],
2020-10-24 23:57:45 +05:30
selector: spec[:podSelector],
2020-05-24 23:13:21 +05:30
policy_types: spec[:policyTypes],
ingress: spec[:ingress],
egress: spec[:egress]
)
end
2020-11-24 15:15:51 +05:30
override :resource
def resource
{
kind: KIND,
metadata: metadata,
spec: spec
}
end
2020-05-24 23:13:21 +05:30
private
2020-10-24 23:57:45 +05:30
attr_reader :name, :namespace, :labels, :creation_timestamp, :policy_types, :ingress, :egress
2020-05-24 23:13:21 +05:30
2020-10-24 23:57:45 +05:30
def selector
@selector ||= {}
2020-05-24 23:13:21 +05:30
end
2020-11-24 15:15:51 +05:30
def metadata
meta = { name: name, namespace: namespace }
meta[:labels] = labels if labels
meta
end
2020-05-24 23:13:21 +05:30
def spec
{
2020-10-24 23:57:45 +05:30
podSelector: selector,
2020-05-24 23:13:21 +05:30
policyTypes: policy_types,
ingress: ingress,
egress: egress
}
end
end
end
end