Commit b39801f8 authored by Zamir Martins Filho's avatar Zamir Martins Filho

Adds annotations to CiliumNetworkPolicies

This MR adds annotations as part of metadata
for CiliumNetworkPolicies wrapper. With this
change policy editor can send annotations as
part of the payload when creating/updating
network policies
parent 6e00773e
---
title: Adds annotations as part of metadata for CiliumNetworkPolicies wrapper
merge_request: 50586
author:
type: changed
...@@ -12,7 +12,7 @@ module Gitlab ...@@ -12,7 +12,7 @@ module Gitlab
# We are modeling existing kubernetes resource and don't have # We are modeling existing kubernetes resource and don't have
# control over amount of parameters. # control over amount of parameters.
# rubocop:disable Metrics/ParameterLists # rubocop:disable Metrics/ParameterLists
def initialize(name:, namespace:, selector:, ingress:, resource_version: nil, description: nil, labels: nil, creation_timestamp: nil, egress: nil) def initialize(name:, namespace:, selector:, ingress:, resource_version: nil, description: nil, labels: nil, creation_timestamp: nil, egress: nil, annotations: nil)
@name = name @name = name
@description = description @description = description
@namespace = namespace @namespace = namespace
...@@ -22,6 +22,7 @@ module Gitlab ...@@ -22,6 +22,7 @@ module Gitlab
@resource_version = resource_version @resource_version = resource_version
@ingress = ingress @ingress = ingress
@egress = egress @egress = egress
@annotations = annotations
end end
# rubocop:enable Metrics/ParameterLists # rubocop:enable Metrics/ParameterLists
...@@ -37,6 +38,7 @@ module Gitlab ...@@ -37,6 +38,7 @@ module Gitlab
name: metadata[:name], name: metadata[:name],
description: policy[:description], description: policy[:description],
namespace: metadata[:namespace], namespace: metadata[:namespace],
annotations: metadata[:annotations],
resource_version: metadata[:resourceVersion], resource_version: metadata[:resourceVersion],
labels: metadata[:labels], labels: metadata[:labels],
selector: spec[:endpointSelector], selector: spec[:endpointSelector],
...@@ -57,6 +59,7 @@ module Gitlab ...@@ -57,6 +59,7 @@ module Gitlab
name: metadata[:name], name: metadata[:name],
description: resource[:description], description: resource[:description],
namespace: metadata[:namespace], namespace: metadata[:namespace],
annotations: metadata[:annotations]&.to_h,
resource_version: metadata[:resourceVersion], resource_version: metadata[:resourceVersion],
labels: metadata[:labels]&.to_h, labels: metadata[:labels]&.to_h,
creation_timestamp: metadata[:creationTimestamp], creation_timestamp: metadata[:creationTimestamp],
...@@ -80,7 +83,7 @@ module Gitlab ...@@ -80,7 +83,7 @@ module Gitlab
private private
attr_reader :name, :description, :namespace, :labels, :creation_timestamp, :resource_version, :ingress, :egress attr_reader :name, :description, :namespace, :labels, :creation_timestamp, :resource_version, :ingress, :egress, :annotations
def selector def selector
@selector ||= {} @selector ||= {}
...@@ -90,6 +93,7 @@ module Gitlab ...@@ -90,6 +93,7 @@ module Gitlab
meta = { name: name, namespace: namespace } meta = { name: name, namespace: namespace }
meta[:labels] = labels if labels meta[:labels] = labels if labels
meta[:resourceVersion] = resource_version if resource_version meta[:resourceVersion] = resource_version if resource_version
meta[:annotations] = annotations if annotations
meta meta
end end
......
...@@ -12,7 +12,8 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do ...@@ -12,7 +12,8 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do
ingress: ingress, ingress: ingress,
egress: egress, egress: egress,
labels: labels, labels: labels,
resource_version: resource_version resource_version: resource_version,
annotations: annotations
) )
end end
...@@ -20,7 +21,7 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do ...@@ -20,7 +21,7 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do
::Kubeclient::Resource.new( ::Kubeclient::Resource.new(
apiVersion: Gitlab::Kubernetes::CiliumNetworkPolicy::API_VERSION, apiVersion: Gitlab::Kubernetes::CiliumNetworkPolicy::API_VERSION,
kind: Gitlab::Kubernetes::CiliumNetworkPolicy::KIND, kind: Gitlab::Kubernetes::CiliumNetworkPolicy::KIND,
metadata: { name: name, namespace: namespace, resourceVersion: resource_version }, metadata: { name: name, namespace: namespace, resourceVersion: resource_version, annotations: annotations },
spec: { endpointSelector: endpoint_selector, ingress: ingress, egress: egress }, spec: { endpointSelector: endpoint_selector, ingress: ingress, egress: egress },
description: description description: description
) )
...@@ -34,6 +35,7 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do ...@@ -34,6 +35,7 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do
let(:description) { 'example-description' } let(:description) { 'example-description' }
let(:partial_class_name) { described_class.name.split('::').last } let(:partial_class_name) { described_class.name.split('::').last }
let(:resource_version) { 101 } let(:resource_version) { 101 }
let(:annotations) { { 'app.gitlab.com/alert': 'true' } }
let(:ingress) do let(:ingress) do
[ [
{ {
...@@ -64,6 +66,8 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do ...@@ -64,6 +66,8 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do
name: example-name name: example-name
namespace: example-namespace namespace: example-namespace
resourceVersion: 101 resourceVersion: 101
annotations:
app.gitlab.com/alert: "true"
spec: spec:
endpointSelector: endpointSelector:
matchLabels: matchLabels:
...@@ -157,7 +161,7 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do ...@@ -157,7 +161,7 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do
description: description, description: description,
metadata: { metadata: {
name: name, namespace: namespace, creationTimestamp: '2020-04-14T00:08:30Z', name: name, namespace: namespace, creationTimestamp: '2020-04-14T00:08:30Z',
labels: { app: 'foo' }, resourceVersion: resource_version labels: { app: 'foo' }, resourceVersion: resource_version, annotations: annotations
}, },
spec: { endpointSelector: endpoint_selector, ingress: ingress, egress: nil, labels: nil } spec: { endpointSelector: endpoint_selector, ingress: ingress, egress: nil, labels: nil }
) )
...@@ -168,7 +172,7 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do ...@@ -168,7 +172,7 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do
apiVersion: Gitlab::Kubernetes::CiliumNetworkPolicy::API_VERSION, apiVersion: Gitlab::Kubernetes::CiliumNetworkPolicy::API_VERSION,
kind: Gitlab::Kubernetes::CiliumNetworkPolicy::KIND, kind: Gitlab::Kubernetes::CiliumNetworkPolicy::KIND,
description: description, description: description,
metadata: { name: name, namespace: namespace, resourceVersion: resource_version, labels: { app: 'foo' } }, metadata: { name: name, namespace: namespace, resourceVersion: resource_version, labels: { app: 'foo' }, annotations: annotations },
spec: { endpointSelector: endpoint_selector, ingress: ingress } spec: { endpointSelector: endpoint_selector, ingress: ingress }
) )
end end
...@@ -211,7 +215,7 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do ...@@ -211,7 +215,7 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do
{ {
apiVersion: Gitlab::Kubernetes::CiliumNetworkPolicy::API_VERSION, apiVersion: Gitlab::Kubernetes::CiliumNetworkPolicy::API_VERSION,
kind: Gitlab::Kubernetes::CiliumNetworkPolicy::KIND, kind: Gitlab::Kubernetes::CiliumNetworkPolicy::KIND,
metadata: { name: name, namespace: namespace, resourceVersion: resource_version }, metadata: { name: name, namespace: namespace, resourceVersion: resource_version, annotations: annotations },
spec: { endpointSelector: endpoint_selector, ingress: ingress, egress: egress }, spec: { endpointSelector: endpoint_selector, ingress: ingress, egress: egress },
description: description description: description
} }
...@@ -248,5 +252,15 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do ...@@ -248,5 +252,15 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do
it { is_expected.to eq(resource) } it { is_expected.to eq(resource) }
end end
context 'without annotations' do
let(:annotations) { nil }
before do
resource[:metadata].delete(:annotations)
end
it { is_expected.to eq(resource) }
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment