Skip to content

Commit 13a2eda

Browse files
rawagnersuomiy
authored andcommitted
Match VM Services against all labels (kubevirt#305)
1 parent f424248 commit 13a2eda

File tree

7 files changed

+56
-12
lines changed

7 files changed

+56
-12
lines changed

src/components/Details/Services/fixtures/Services.fixture.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ import { Services } from '../Services';
55
import { TEMPLATE_VM_NAME_LABEL } from '../../../../constants';
66

77
const vm = {
8-
metadata: {
9-
name: 'my-vm',
10-
namespace: 'my-namespace',
8+
spec: {
9+
template: {
10+
metadata: {
11+
labels: {
12+
[TEMPLATE_VM_NAME_LABEL]: 'my-vm',
13+
fooSelector: 'fooValue',
14+
},
15+
},
16+
},
1117
},
1218
};
1319

@@ -29,6 +35,7 @@ export const services = [
2935
spec: {
3036
selector: {
3137
[TEMPLATE_VM_NAME_LABEL]: 'my-vm',
38+
fooSelector: 'fooValue',
3239
},
3340
},
3441
},
@@ -42,6 +49,18 @@ export const services = [
4249
},
4350
},
4451
},
52+
{
53+
metadata: {
54+
name: 'fooService3',
55+
},
56+
spec: {
57+
selector: {
58+
[TEMPLATE_VM_NAME_LABEL]: 'my-vm',
59+
fooSelector: 'fooValue',
60+
anotherFooSelector: 'fooValue',
61+
},
62+
},
63+
},
4564
];
4665

4766
export const ResourceLinkComponent = ({ name }) => (

src/components/Details/VmDetails/fixtures/VmDetails.fixture.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22

33
import { VmDetails } from '../VmDetails';
4-
import { LABEL_USED_TEMPLATE_NAME, LABEL_USED_TEMPLATE_NAMESPACE } from '../../../../constants';
4+
import { LABEL_USED_TEMPLATE_NAME, LABEL_USED_TEMPLATE_NAMESPACE, TEMPLATE_VM_NAME_LABEL } from '../../../../constants';
55
import { k8sPatch, k8sGet } from '../../../../tests/k8s';
66
import { services, ResourceLinkComponent } from '../../Services/fixtures/Services.fixture';
77

@@ -154,6 +154,21 @@ export const vmFixtures = {
154154
},
155155
spec: { running: false },
156156
},
157+
vmWithVmiLabels: {
158+
metadata: {
159+
...metadata,
160+
},
161+
spec: {
162+
template: {
163+
metadata: {
164+
labels: {
165+
[TEMPLATE_VM_NAME_LABEL]: 'my-vm',
166+
fooSelector: 'fooValue',
167+
},
168+
},
169+
},
170+
},
171+
},
157172
};
158173

159174
const vmiFixture = {
@@ -269,7 +284,7 @@ export default [
269284
component: VmDetails,
270285
name: 'VM with services',
271286
props: {
272-
vm: vmFixtures.downVm,
287+
vm: vmFixtures.vmWithVmiLabels,
273288
k8sPatch,
274289
k8sGet,
275290
NodeLink: () => true,

src/components/Details/VmDetails/tests/VmDetails.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('<VmDetails />', () => {
6464
});
6565

6666
it('renders correctly with services', () => {
67-
const component = render(testVmDetails(vmFixtures.downVm, { services }));
67+
const component = render(testVmDetails(vmFixtures.vmWithVmiLabels, { services }));
6868
expect(component).toMatchSnapshot();
6969
});
7070

src/selectors/service/combined.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import { get } from 'lodash';
1+
import { getVmiTemplateLabels } from '../vm/selectors';
2+
import { getServiceSelectors } from './selectors';
23

3-
import { TEMPLATE_VM_NAME_LABEL } from '../../constants';
4-
import { getName } from '../common';
5-
6-
export const getServicesForVm = (services, vm) =>
7-
services.filter(service => get(service, ['spec', 'selector', TEMPLATE_VM_NAME_LABEL]) === getName(vm));
4+
export const getServicesForVm = (services, vm) => {
5+
const vmLabels = getVmiTemplateLabels(vm);
6+
return services.filter(service => {
7+
const selectors = getServiceSelectors(service);
8+
const selectorKeys = Object.keys(selectors);
9+
return selectorKeys.length > 0 ? selectorKeys.every(key => vmLabels[key] === selectors[key]) : false;
10+
});
11+
};

src/selectors/service/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './combined';
2+
export * from './selectors';

src/selectors/service/selectors.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { get } from 'lodash';
2+
3+
export const getServiceSelectors = service => get(service, 'spec.selector', {});

src/selectors/vm/selectors.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,5 @@ export const getInterfaceBinding = intface => {
8989
}
9090
return null;
9191
};
92+
93+
export const getVmiTemplateLabels = vm => get(vm, 'spec.template.metadata.labels', {});

0 commit comments

Comments
 (0)