From 52e04ab29a16fdc40bb3caa031db08fc690c5a30 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Mon, 28 Dec 2020 15:10:10 +0000 Subject: [PATCH] Add latest changes from gitlab-org/gitlab@master --- ..._annotation_to_cilium_network_policies.yml | 5 ++++ .../kubernetes/cilium_network_policy.rb | 8 +++++-- lib/gitlab/uuid.rb | 5 ++++ .../kubernetes/cilium_network_policy_spec.rb | 24 +++++++++++++++---- spec/lib/gitlab/uuid_spec.rb | 19 +++++++++++++++ 5 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 changelogs/unreleased/add_annotation_to_cilium_network_policies.yml diff --git a/changelogs/unreleased/add_annotation_to_cilium_network_policies.yml b/changelogs/unreleased/add_annotation_to_cilium_network_policies.yml new file mode 100644 index 00000000000..6d5a8ba6924 --- /dev/null +++ b/changelogs/unreleased/add_annotation_to_cilium_network_policies.yml @@ -0,0 +1,5 @@ +--- +title: Adds annotations as part of metadata for CiliumNetworkPolicies wrapper +merge_request: 50586 +author: +type: changed diff --git a/lib/gitlab/kubernetes/cilium_network_policy.rb b/lib/gitlab/kubernetes/cilium_network_policy.rb index 9043932bbe5..f77b3e8de99 100644 --- a/lib/gitlab/kubernetes/cilium_network_policy.rb +++ b/lib/gitlab/kubernetes/cilium_network_policy.rb @@ -12,7 +12,7 @@ module Gitlab # We are modeling existing kubernetes resource and don't have # control over amount of parameters. # 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 @description = description @namespace = namespace @@ -22,6 +22,7 @@ module Gitlab @resource_version = resource_version @ingress = ingress @egress = egress + @annotations = annotations end # rubocop:enable Metrics/ParameterLists @@ -37,6 +38,7 @@ module Gitlab name: metadata[:name], description: policy[:description], namespace: metadata[:namespace], + annotations: metadata[:annotations], resource_version: metadata[:resourceVersion], labels: metadata[:labels], selector: spec[:endpointSelector], @@ -57,6 +59,7 @@ module Gitlab name: metadata[:name], description: resource[:description], namespace: metadata[:namespace], + annotations: metadata[:annotations]&.to_h, resource_version: metadata[:resourceVersion], labels: metadata[:labels]&.to_h, creation_timestamp: metadata[:creationTimestamp], @@ -80,7 +83,7 @@ module Gitlab 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 @selector ||= {} @@ -90,6 +93,7 @@ module Gitlab meta = { name: name, namespace: namespace } meta[:labels] = labels if labels meta[:resourceVersion] = resource_version if resource_version + meta[:annotations] = annotations if annotations meta end diff --git a/lib/gitlab/uuid.rb b/lib/gitlab/uuid.rb index 12a4efabc44..80caf2c6788 100644 --- a/lib/gitlab/uuid.rb +++ b/lib/gitlab/uuid.rb @@ -9,6 +9,7 @@ module Gitlab production: "58dc0f06-936c-43b3-93bb-71693f1b6570" }.freeze + UUID_V5_PATTERN = /\h{8}-\h{4}-5\h{3}-\h{4}-\h{4}\h{8}/.freeze NAMESPACE_REGEX = /(\h{8})-(\h{4})-(\h{4})-(\h{4})-(\h{4})(\h{8})/.freeze PACK_PATTERN = "NnnnnN".freeze @@ -17,6 +18,10 @@ module Gitlab Digest::UUID.uuid_v5(namespace_id, name) end + def v5?(string) + string.match(UUID_V5_PATTERN).present? + end + private def default_namespace_id diff --git a/spec/lib/gitlab/kubernetes/cilium_network_policy_spec.rb b/spec/lib/gitlab/kubernetes/cilium_network_policy_spec.rb index 3f5661d4ca6..0092c69d0bb 100644 --- a/spec/lib/gitlab/kubernetes/cilium_network_policy_spec.rb +++ b/spec/lib/gitlab/kubernetes/cilium_network_policy_spec.rb @@ -12,7 +12,8 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do ingress: ingress, egress: egress, labels: labels, - resource_version: resource_version + resource_version: resource_version, + annotations: annotations ) end @@ -20,7 +21,7 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do ::Kubeclient::Resource.new( apiVersion: Gitlab::Kubernetes::CiliumNetworkPolicy::API_VERSION, 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 }, description: description ) @@ -34,6 +35,7 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do let(:description) { 'example-description' } let(:partial_class_name) { described_class.name.split('::').last } let(:resource_version) { 101 } + let(:annotations) { { 'app.gitlab.com/alert': 'true' } } let(:ingress) do [ { @@ -64,6 +66,8 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do name: example-name namespace: example-namespace resourceVersion: 101 + annotations: + app.gitlab.com/alert: "true" spec: endpointSelector: matchLabels: @@ -157,7 +161,7 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do description: description, metadata: { 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 } ) @@ -168,7 +172,7 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do apiVersion: Gitlab::Kubernetes::CiliumNetworkPolicy::API_VERSION, kind: Gitlab::Kubernetes::CiliumNetworkPolicy::KIND, 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 } ) end @@ -211,7 +215,7 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do { apiVersion: Gitlab::Kubernetes::CiliumNetworkPolicy::API_VERSION, 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 }, description: description } @@ -248,5 +252,15 @@ RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do it { is_expected.to eq(resource) } end + + context 'without annotations' do + let(:annotations) { nil } + + before do + resource[:metadata].delete(:annotations) + end + + it { is_expected.to eq(resource) } + end end end diff --git a/spec/lib/gitlab/uuid_spec.rb b/spec/lib/gitlab/uuid_spec.rb index a2e28f5a24d..44c1d30fce0 100644 --- a/spec/lib/gitlab/uuid_spec.rb +++ b/spec/lib/gitlab/uuid_spec.rb @@ -49,4 +49,23 @@ RSpec.describe Gitlab::UUID do it { is_expected.to eq(production_proper_uuid) } end end + + describe 'v5?' do + using RSpec::Parameterized::TableSyntax + + where(:test_string, :is_uuid_v5) do + 'not even a uuid' | false + 'this-seems-like-a-uuid' | false + 'thislook-more-5lik-eava-liduuidbutno' | false + '9f470438-db0f-37b7-9ca9-1d47104c339a' | false + '9f470438-db0f-47b7-9ca9-1d47104c339a' | false + '9f470438-db0f-57b7-9ca9-1d47104c339a' | true + end + + with_them do + subject { described_class.v5?(test_string) } + + it { is_expected.to be(is_uuid_v5) } + end + end end