Commit 4e0d770c authored by Savas Vedova's avatar Savas Vedova

Merge branch 'add_project_id_as_cnp_labels' into 'master'

Add alert related labels to CiliumNetworkPolicies

See merge request gitlab-org/gitlab!53401
parents 5eb34b2f 5f971862
...@@ -33,3 +33,5 @@ export const PortMatchModePortProtocol = 'port/protocol'; ...@@ -33,3 +33,5 @@ export const PortMatchModePortProtocol = 'port/protocol';
export const DisabledByLabel = 'network-policy.gitlab.com/disabled_by'; export const DisabledByLabel = 'network-policy.gitlab.com/disabled_by';
export const CiliumNetworkPolicyKind = 'CiliumNetworkPolicy'; export const CiliumNetworkPolicyKind = 'CiliumNetworkPolicy';
export const ProjectIdLabel = 'app.gitlab.com/proj';
...@@ -113,7 +113,7 @@ function parseRule(item, direction) { ...@@ -113,7 +113,7 @@ function parseRule(item, direction) {
*/ */
export default function fromYaml(manifest) { export default function fromYaml(manifest) {
const { description, metadata, spec } = safeLoad(manifest, { json: true }); const { description, metadata, spec } = safeLoad(manifest, { json: true });
const { name, resourceVersion, annotations } = metadata; const { name, resourceVersion, annotations, labels } = metadata;
const { endpointSelector = {}, ingress = [], egress = [] } = spec; const { endpointSelector = {}, ingress = [], egress = [] } = spec;
const matchLabels = endpointSelector.matchLabels || {}; const matchLabels = endpointSelector.matchLabels || {};
...@@ -135,6 +135,7 @@ export default function fromYaml(manifest) { ...@@ -135,6 +135,7 @@ export default function fromYaml(manifest) {
resourceVersion, resourceVersion,
description, description,
annotations, annotations,
labels,
isEnabled: !Object.keys(matchLabels).includes(DisabledByLabel), isEnabled: !Object.keys(matchLabels).includes(DisabledByLabel),
endpointMatchMode: endpointLabels.length > 0 ? EndpointMatchModeLabel : EndpointMatchModeAny, endpointMatchMode: endpointLabels.length > 0 ? EndpointMatchModeLabel : EndpointMatchModeAny,
endpointLabels: endpointLabels.join(' '), endpointLabels: endpointLabels.join(' '),
......
...@@ -33,11 +33,14 @@ function spec({ rules, isEnabled, endpointMatchMode, endpointLabels }) { ...@@ -33,11 +33,14 @@ function spec({ rules, isEnabled, endpointMatchMode, endpointLabels }) {
Return yaml representation of a policy. Return yaml representation of a policy.
*/ */
export default function toYaml(policy) { export default function toYaml(policy) {
const { annotations, name, resourceVersion, description } = policy; const { annotations, name, resourceVersion, description, labels } = policy;
const metadata = { name }; const metadata = { name };
if (annotations) { if (annotations) {
metadata.annotations = annotations; metadata.annotations = annotations;
} }
if (labels) {
metadata.labels = labels;
}
if (resourceVersion) { if (resourceVersion) {
metadata.resourceVersion = resourceVersion; metadata.resourceVersion = resourceVersion;
} }
......
...@@ -27,6 +27,7 @@ import { ...@@ -27,6 +27,7 @@ import {
EditorModeYAML, EditorModeYAML,
EndpointMatchModeAny, EndpointMatchModeAny,
RuleTypeEndpoint, RuleTypeEndpoint,
ProjectIdLabel,
} from './constants'; } from './constants';
import toYaml from './lib/to_yaml'; import toYaml from './lib/to_yaml';
import fromYaml from './lib/from_yaml'; import fromYaml from './lib/from_yaml';
...@@ -64,6 +65,10 @@ export default { ...@@ -64,6 +65,10 @@ export default {
required: false, required: false,
default: null, default: null,
}, },
projectId: {
type: String,
required: true,
},
}, },
data() { data() {
const policy = this.existingPolicy const policy = this.existingPolicy
...@@ -76,8 +81,9 @@ export default { ...@@ -76,8 +81,9 @@ export default {
endpointLabels: '', endpointLabels: '',
rules: [], rules: [],
annotations: '', annotations: '',
labels: '',
}; };
policy.labels = { [ProjectIdLabel]: this.projectId };
return { return {
editorMode: EditorModeRule, editorMode: EditorModeRule,
yamlEditorValue: '', yamlEditorValue: '',
......
...@@ -20,6 +20,7 @@ export default () => { ...@@ -20,6 +20,7 @@ export default () => {
threatMonitoringPath, threatMonitoringPath,
policy, policy,
projectPath, projectPath,
projectId,
environmentId, environmentId,
} = el.dataset; } = el.dataset;
...@@ -35,7 +36,7 @@ export default () => { ...@@ -35,7 +36,7 @@ export default () => {
store.dispatch('threatMonitoring/setCurrentEnvironmentId', parseInt(environmentId, 10)); store.dispatch('threatMonitoring/setCurrentEnvironmentId', parseInt(environmentId, 10));
} }
const props = { threatMonitoringPath }; const props = { threatMonitoringPath, projectId };
if (policy) { if (policy) {
props.existingPolicy = JSON.parse(policy); props.existingPolicy = JSON.parse(policy);
} }
......
...@@ -23,6 +23,7 @@ module PolicyHelper ...@@ -23,6 +23,7 @@ module PolicyHelper
create_agent_help_path: help_page_url('user/clusters/agent/index.md', anchor: 'create-an-agent-record-in-gitlab'), create_agent_help_path: help_page_url('user/clusters/agent/index.md', anchor: 'create-an-agent-record-in-gitlab'),
environments_endpoint: project_environments_path(project), environments_endpoint: project_environments_path(project),
project_path: project.full_path, project_path: project.full_path,
project_id: project.id,
threat_monitoring_path: project_threat_monitoring_path(project) threat_monitoring_path: project_threat_monitoring_path(project)
} }
end end
......
---
title: Add alert related labels to CiliumNetworkPolicies. The label will contain the
project id and will be applicable to both new and existing policies
merge_request: 53401
author:
type: changed
...@@ -197,6 +197,8 @@ exports[`PolicyEditorApp component renders the policy editor layout 1`] = ` ...@@ -197,6 +197,8 @@ exports[`PolicyEditorApp component renders the policy editor layout 1`] = `
kind: CiliumNetworkPolicy kind: CiliumNetworkPolicy
metadata: metadata:
name: '' name: ''
labels:
app.gitlab.com/proj: '21'
spec: spec:
endpointSelector: endpointSelector:
matchLabels: matchLabels:
......
...@@ -20,13 +20,14 @@ describe('fromYaml', () => { ...@@ -20,13 +20,14 @@ describe('fromYaml', () => {
const cidrExample = '20.1.1.1/32 20.1.1.2/32'; const cidrExample = '20.1.1.1/32 20.1.1.2/32';
const portExample = '80 81/udp 82/tcp'; const portExample = '80 81/udp 82/tcp';
const labels = { 'app.gitlab.com/proj': '21' };
beforeEach(() => { beforeEach(() => {
policy = { policy = {
name: 'test-policy', name: 'test-policy',
endpointLabels: '', endpointLabels: '',
rules: [], rules: [],
isEnabled: true, isEnabled: true,
labels,
}; };
}); });
...@@ -37,6 +38,7 @@ describe('fromYaml', () => { ...@@ -37,6 +38,7 @@ describe('fromYaml', () => {
endpointMatchMode: EndpointMatchModeAny, endpointMatchMode: EndpointMatchModeAny,
endpointLabels: '', endpointLabels: '',
rules: [], rules: [],
labels,
}); });
}); });
......
...@@ -139,6 +139,26 @@ spec: ...@@ -139,6 +139,26 @@ spec:
- fromEndpoints: - fromEndpoints:
- matchLabels: - matchLabels:
foo: bar foo: bar
`);
});
});
describe('when labels are not empty', () => {
beforeEach(() => {
policy.labels = { 'app.gitlab.com/proj': '21' };
});
it('returns yaml representation', () => {
expect(toYaml(policy)).toEqual(`apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: test-policy
labels:
app.gitlab.com/proj: '21'
spec:
endpointSelector:
matchLabels:
network-policy.gitlab.com/disabled_by: gitlab
`); `);
}); });
}); });
......
...@@ -38,6 +38,7 @@ describe('PolicyEditorApp component', () => { ...@@ -38,6 +38,7 @@ describe('PolicyEditorApp component', () => {
wrapper = shallowMount(PolicyEditorApp, { wrapper = shallowMount(PolicyEditorApp, {
propsData: { propsData: {
threatMonitoringPath: '/threat-monitoring', threatMonitoringPath: '/threat-monitoring',
projectId: '21',
...propsData, ...propsData,
}, },
provide: { provide: {
...@@ -124,6 +125,8 @@ kind: CiliumNetworkPolicy ...@@ -124,6 +125,8 @@ kind: CiliumNetworkPolicy
description: test description description: test description
metadata: metadata:
name: test-policy name: test-policy
labels:
app.gitlab.com/proj: '21'
spec: spec:
endpointSelector: endpointSelector:
matchLabels: matchLabels:
...@@ -147,6 +150,7 @@ spec: ...@@ -147,6 +150,7 @@ spec:
matchLabels: 'foo:bar', matchLabels: 'foo:bar',
}, },
], ],
labels: { 'app.gitlab.com/proj': '21' },
}); });
}); });
}); });
......
...@@ -22,6 +22,7 @@ RSpec.describe PolicyHelper do ...@@ -22,6 +22,7 @@ RSpec.describe PolicyHelper do
create_agent_help_path: kind_of(String), create_agent_help_path: kind_of(String),
environments_endpoint: kind_of(String), environments_endpoint: kind_of(String),
project_path: project.full_path, project_path: project.full_path,
project_id: project.id,
threat_monitoring_path: kind_of(String) threat_monitoring_path: kind_of(String)
} }
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