Commit f2c19d80 authored by ap4y's avatar ap4y

Add #autodevops? to the Kubernetes::NetworkPolicy

This commit introduces the way to distinguish between manual and
autodevops installed network policies via #autodevops?. This field is
also exposed in json output.
parent 7df008a5
...@@ -3,9 +3,10 @@ ...@@ -3,9 +3,10 @@
module Gitlab module Gitlab
module Kubernetes module Kubernetes
class NetworkPolicy class NetworkPolicy
def initialize(name:, namespace:, pod_selector:, ingress:, creation_timestamp: nil, policy_types: ["Ingress"], egress: nil) def initialize(name:, namespace:, pod_selector:, ingress:, labels: nil, creation_timestamp: nil, policy_types: ["Ingress"], egress: nil)
@name = name @name = name
@namespace = namespace @namespace = namespace
@labels = labels
@creation_timestamp = creation_timestamp @creation_timestamp = creation_timestamp
@pod_selector = pod_selector @pod_selector = pod_selector
@policy_types = policy_types @policy_types = policy_types
...@@ -24,6 +25,7 @@ module Gitlab ...@@ -24,6 +25,7 @@ module Gitlab
self.new( self.new(
name: metadata[:name], name: metadata[:name],
namespace: metadata[:namespace], namespace: metadata[:namespace],
labels: metadata[:labels],
pod_selector: spec[:podSelector], pod_selector: spec[:podSelector],
policy_types: spec[:policyTypes], policy_types: spec[:policyTypes],
ingress: spec[:ingress], ingress: spec[:ingress],
...@@ -42,6 +44,7 @@ module Gitlab ...@@ -42,6 +44,7 @@ module Gitlab
self.new( self.new(
name: metadata[:name], name: metadata[:name],
namespace: metadata[:namespace], namespace: metadata[:namespace],
labels: metadata[:labels]&.to_h,
creation_timestamp: metadata[:creationTimestamp], creation_timestamp: metadata[:creationTimestamp],
pod_selector: spec[:podSelector], pod_selector: spec[:podSelector],
policy_types: spec[:policyTypes], policy_types: spec[:policyTypes],
...@@ -62,16 +65,25 @@ module Gitlab ...@@ -62,16 +65,25 @@ module Gitlab
name: name, name: name,
namespace: namespace, namespace: namespace,
creation_timestamp: creation_timestamp, creation_timestamp: creation_timestamp,
manifest: manifest manifest: manifest,
is_autodevops: autodevops?
} }
end end
def autodevops?
return false unless labels
!labels[:chart].nil? && labels[:chart].start_with?('auto-deploy-app-')
end
private private
attr_reader :name, :namespace, :creation_timestamp, :pod_selector, :policy_types, :ingress, :egress attr_reader :name, :namespace, :labels, :creation_timestamp, :pod_selector, :policy_types, :ingress, :egress
def metadata def metadata
{ name: name, namespace: namespace } meta = { name: name, namespace: namespace }
meta[:labels] = labels if labels
meta
end end
def spec def spec
......
...@@ -39,28 +39,30 @@ describe Gitlab::Kubernetes::NetworkPolicy do ...@@ -39,28 +39,30 @@ describe Gitlab::Kubernetes::NetworkPolicy do
describe '.from_yaml' do describe '.from_yaml' do
let(:manifest) do let(:manifest) do
<<-POLICY <<~POLICY
apiVersion: networking.k8s.io/v1 apiVersion: networking.k8s.io/v1
kind: NetworkPolicy kind: NetworkPolicy
metadata: metadata:
name: example-name name: example-name
namespace: example-namespace namespace: example-namespace
spec: labels:
podSelector: app: foo
matchLabels: spec:
role: db podSelector:
policyTypes: matchLabels:
- Ingress role: db
ingress: policyTypes:
- from: - Ingress
- namespaceSelector: ingress:
matchLabels: - from:
project: myproject - namespaceSelector:
matchLabels:
project: myproject
POLICY POLICY
end end
let(:resource) do let(:resource) do
::Kubeclient::Resource.new( ::Kubeclient::Resource.new(
metadata: { name: name, namespace: namespace }, metadata: { name: name, namespace: namespace, labels: { app: 'foo' } },
spec: { podSelector: pod_selector, policyTypes: %w(Ingress), ingress: ingress, egress: nil } spec: { podSelector: pod_selector, policyTypes: %w(Ingress), ingress: ingress, egress: nil }
) )
end end
...@@ -83,20 +85,20 @@ spec: ...@@ -83,20 +85,20 @@ spec:
context 'with manifest without metadata' do context 'with manifest without metadata' do
let(:manifest) do let(:manifest) do
<<-POLICY <<~POLICY
apiVersion: networking.k8s.io/v1 apiVersion: networking.k8s.io/v1
kind: NetworkPolicy kind: NetworkPolicy
spec: spec:
podSelector: podSelector:
matchLabels: matchLabels:
role: db role: db
policyTypes: policyTypes:
- Ingress - Ingress
ingress: ingress:
- from: - from:
- namespaceSelector: - namespaceSelector:
matchLabels: matchLabels:
project: myproject project: myproject
POLICY POLICY
end end
...@@ -105,12 +107,12 @@ spec: ...@@ -105,12 +107,12 @@ spec:
context 'with manifest without spec' do context 'with manifest without spec' do
let(:manifest) do let(:manifest) do
<<-POLICY <<~POLICY
apiVersion: networking.k8s.io/v1 apiVersion: networking.k8s.io/v1
kind: NetworkPolicy kind: NetworkPolicy
metadata: metadata:
name: example-name name: example-name
namespace: example-namespace namespace: example-namespace
POLICY POLICY
end end
...@@ -119,24 +121,24 @@ metadata: ...@@ -119,24 +121,24 @@ metadata:
context 'with disallowed class' do context 'with disallowed class' do
let(:manifest) do let(:manifest) do
<<-POLICY <<~POLICY
apiVersion: networking.k8s.io/v1 apiVersion: networking.k8s.io/v1
kind: NetworkPolicy kind: NetworkPolicy
metadata: metadata:
name: example-name name: example-name
namespace: example-namespace namespace: example-namespace
creationTimestamp: 2020-04-14T00:08:30Z creationTimestamp: 2020-04-14T00:08:30Z
spec: spec:
podSelector: podSelector:
matchLabels: matchLabels:
role: db role: db
policyTypes: policyTypes:
- Ingress - Ingress
ingress: ingress:
- from: - from:
- namespaceSelector: - namespaceSelector:
matchLabels: matchLabels:
project: myproject project: myproject
POLICY POLICY
end end
...@@ -147,13 +149,16 @@ spec: ...@@ -147,13 +149,16 @@ spec:
describe '.from_resource' do describe '.from_resource' do
let(:resource) do let(:resource) do
::Kubeclient::Resource.new( ::Kubeclient::Resource.new(
metadata: { name: name, namespace: namespace, creationTimestamp: '2020-04-14T00:08:30Z', resourceVersion: '4990' }, metadata: {
name: name, namespace: namespace, creationTimestamp: '2020-04-14T00:08:30Z',
labels: { app: 'foo' }, resourceVersion: '4990'
},
spec: { podSelector: pod_selector, policyTypes: %w(Ingress), ingress: ingress, egress: nil } spec: { podSelector: pod_selector, policyTypes: %w(Ingress), ingress: ingress, egress: nil }
) )
end end
let(:generated_resource) do let(:generated_resource) do
::Kubeclient::Resource.new( ::Kubeclient::Resource.new(
metadata: { name: name, namespace: namespace }, metadata: { name: name, namespace: namespace, labels: { app: 'foo' } },
spec: { podSelector: pod_selector, policyTypes: %w(Ingress), ingress: ingress, egress: nil } spec: { podSelector: pod_selector, policyTypes: %w(Ingress), ingress: ingress, egress: nil }
) )
end end
...@@ -213,7 +218,8 @@ spec: ...@@ -213,7 +218,8 @@ spec:
metadata: { name: name, namespace: namespace }, metadata: { name: name, namespace: namespace },
spec: { podSelector: pod_selector, policyTypes: %w(Ingress Egress), ingress: ingress, egress: egress } spec: { podSelector: pod_selector, policyTypes: %w(Ingress Egress), ingress: ingress, egress: egress }
}.deep_stringify_keys }.deep_stringify_keys
) ),
is_autodevops: false
} }
end end
...@@ -221,4 +227,33 @@ spec: ...@@ -221,4 +227,33 @@ spec:
it { is_expected.to eq(json_policy) } it { is_expected.to eq(json_policy) }
end end
describe '#autodevops?' do
subject { policy.autodevops? }
let(:chart) { nil }
let(:policy) do
described_class.new(
name: name,
namespace: namespace,
labels: { chart: chart },
pod_selector: pod_selector,
ingress: ingress
)
end
it { is_expected.to be false }
context 'with non-autodevops chart' do
let(:chart) { 'foo' }
it { is_expected.to be false }
end
context 'with autodevops chart' do
let(:chart) { 'auto-deploy-app-0.6.0' }
it { is_expected.to be true }
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