Skip to content

Commit

Permalink
Merge pull request #521 from jordojordo/dashboard-ps-pods
Browse files Browse the repository at this point in the history
Fix dashboard policyServerPod card display
  • Loading branch information
jordojordo authored Oct 20, 2023
2 parents 4709887 + 89395d7 commit 421023f
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 78 deletions.
110 changes: 63 additions & 47 deletions pkg/kubewarden/components/Dashboard/DashboardView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import ConsumptionGauge from '@shell/components/ConsumptionGauge';
import Loading from '@shell/components/Loading';
import { DASHBOARD_HEADERS } from '../../config/table-headers';
import { KUBEWARDEN, KUBEWARDEN_APPS, KUBEWARDEN_CHARTS } from '../../types';
import { KUBEWARDEN, KUBEWARDEN_APPS, KUBEWARDEN_CHARTS, KUBEWARDEN_LABELS } from '../../types';
import DefaultsBanner from '../DefaultsBanner';
import Card from './Card';
Expand All @@ -28,7 +28,9 @@ export default {
const hash = {};
const types = [
KUBEWARDEN.ADMISSION_POLICY,
KUBEWARDEN.CLUSTER_ADMISSION_POLICY
KUBEWARDEN.CLUSTER_ADMISSION_POLICY,
POD,
CATALOG.APP
];
for ( const type of types ) {
Expand All @@ -52,8 +54,8 @@ export default {
DASHBOARD_HEADERS,
colorStops,
apps: null,
controller: null,
deployments: null,
psPods: [],
secondaryResourceData: this.secondaryResourceDataConfig(),
};
Expand All @@ -62,47 +64,74 @@ export default {
computed: {
...mapGetters(['currentProduct']),
allApps() {
return this.$store.getters['cluster/all'](CATALOG.APP);
},
allPods() {
return this.$store.getters['cluster/all'](POD);
},
defaultsApp() {
if ( this.apps ) {
return this.apps?.find((a) => {
if ( this.allApps ) {
return this.allApps?.find((a) => {
return a.spec?.chart?.metadata?.annotations?.[CATALOG_ANNOTATIONS.RELEASE_NAME] === KUBEWARDEN_APPS.RANCHER_DEFAULTS;
});
}
return false;
},
policyServerPods() {
if ( this.$store.getters['cluster/canList'](POD) ) {
const pods = this.allPods?.filter(pod => pod?.metadata?.labels?.[KUBEWARDEN_LABELS.POLICY_SERVER]);
return Object.values(pods).flat();
}
return null;
},
/** Counts the current policy server pods - returns the status and total count */
policyServers() {
const pods = this.psPods || [];
policyServerCounts() {
const pods = this.policyServerPods || [];
return pods.reduce((ps, neu) => {
const neuContainerStatues = neu?.status?.containerStatuses;
let terminated = false;
if ( !isEmpty(pods) ) {
return pods?.reduce((ps, neu) => {
const neuContainerStatues = neu?.status?.containerStatuses;
let terminated = false;
// If the container state is terminated, remove it from the available counts
if ( !isEmpty(neuContainerStatues) ) {
const filtered = neuContainerStatues.filter(status => status.state['terminated']);
// If the container state is terminated, remove it from the available counts
if ( !isEmpty(neuContainerStatues) ) {
const filtered = neuContainerStatues.filter(status => status?.state['terminated']);
if ( !isEmpty(filtered) ) {
terminated = true;
if ( !isEmpty(filtered) ) {
terminated = true;
}
}
}
return {
return {
status: {
running: ps?.status?.running + ( neu?.metadata?.state?.name === 'running' && !terminated ? 1 : 0 ),
stopped: ps?.status?.stopped + ( neu?.metadata?.state?.error ? 1 : 0 ),
pending: ps?.status?.transitioning + ( neu?.metadata?.state?.transitioning ? 1 : 0 )
},
total: terminated ? ps?.total || 0 : ps?.total + 1
};
}, {
status: {
running: ps?.status.running + ( neu.metadata.state.name === 'running' && !terminated ? 1 : 0 ),
stopped: ps?.status.stopped + ( neu.metadata.state.error ? 1 : 0 ),
pending: ps?.status.transitioning + ( neu.metadata.state.transitioning ? 1 : 0 )
running: 0, stopped: 0, pending: 0
},
total: terminated ? ps?.total || 0 : ps?.total + 1
};
}, {
total: 0
});
}
return {
status: {
running: 0, stopped: 0, pending: 0
},
total: 0
});
};
},
globalPolicies() {
Expand Down Expand Up @@ -137,44 +166,31 @@ export default {
data: {
[WORKLOAD_TYPES.DEPLOYMENT]: {
applyTo: [
{ var: 'namespacedDeployments' },
{
var: 'controller',
parsingFunc: (data) => {
return data.find(deploy => deploy?.metadata?.labels?.[KUBERNETES.MANAGED_NAME] === KUBEWARDEN_CHARTS.CONTROLLER);
}
}
]
},
[POD]: {
applyTo: [
{ var: 'namespacedPods' },
{
var: 'psPods',
parsingFunc: (data) => {
return data.filter(pod => pod?.metadata?.labels['kubewarden/policy-server']);
}
}
]
},
[CATALOG.APP]: { applyTo: [{ var: 'apps' }] }
}
}
};
},
getPolicyGauges(type) {
return type.reduce((policy, neu) => {
return type?.reduce((policy, neu) => {
return {
status: {
running: policy.status.running + ( neu.status.policyStatus === 'active' ? 1 : 0 ),
stopped: policy.status.stopped + ( neu.status.error ? 1 : 0 ),
pending: policy.status.pending + ( neu.status.policyStatus === 'pending' ? 1 : 0 ),
running: policy?.status?.running + ( neu?.status?.policyStatus === 'active' ? 1 : 0 ),
stopped: policy?.status?.stopped + ( neu?.status?.error ? 1 : 0 ),
pending: policy?.status?.pending + ( neu?.status?.policyStatus === 'pending' ? 1 : 0 ),
},
mode: {
protect: policy.mode.protect + ( neu.spec.mode === 'protect' ? 1 : 0 ),
monitor: policy.mode.monitor + ( neu.spec.mode === 'monitor' ? 1 : 0 )
protect: policy?.mode?.protect + ( neu?.spec?.mode === 'protect' ? 1 : 0 ),
monitor: policy?.mode?.monitor + ( neu?.spec?.mode === 'monitor' ? 1 : 0 )
},
total: policy.total + 1
total: policy?.total + 1
};
}, {
status: {
Expand Down Expand Up @@ -241,9 +257,9 @@ export default {
data-testid="kw-dashboard-ps-gauge"
resource-name="Active"
:color-stops="colorStops"
:capacity="policyServers.total"
:capacity="policyServerCounts.total"
:used-as-resource-name="true"
:used="policyServers.status.running"
:used="policyServerCounts.status.running"
units="Pods"
/>
</slot>
Expand Down
18 changes: 12 additions & 6 deletions pkg/kubewarden/components/Dashboard/InstallView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export default {
this.installSteps[0].ready = true;
}
await this.refreshCharts(1, true);
if ( !this.kubewardenRepo || !this.controllerChart ) {
await this.$store.dispatch('catalog/refresh');
}
}
},
Expand Down Expand Up @@ -109,7 +111,9 @@ export default {
computed: {
...mapGetters(['currentCluster', 'currentProduct']),
...mapGetters({ charts: 'catalog/charts', t: 'i18n/t' }),
...mapGetters({
charts: 'catalog/charts', repos: 'catalog/repos', t: 'i18n/t'
}),
isAirgap() {
return this.$store.getters['kubewarden/airGapped'];
Expand All @@ -122,8 +126,8 @@ export default {
controllerChart() {
if ( this.kubewardenRepo ) {
return this.$store.getters['catalog/chart']({
repoName: this.kubewardenRepo.repoName,
repoType: this.kubewardenRepo.repoType,
repoName: this.kubewardenRepo.id,
repoType: 'cluster',
chartName: KUBEWARDEN_CHARTS.CONTROLLER
});
}
Expand All @@ -132,7 +136,9 @@ export default {
},
kubewardenRepo() {
return this.charts?.find(chart => chart.chartName === KUBEWARDEN_CHARTS.CONTROLLER);
const chart = this.charts?.find(chart => chart.chartName === KUBEWARDEN_CHARTS.CONTROLLER);
return this.repos?.find(repo => repo.id === chart?.repoName);
},
shellEnabled() {
Expand Down Expand Up @@ -321,7 +327,7 @@ export default {
</template>
<template #install>
<template v-if="!kubewardenRepo && !controllerChart">
<template v-if="!kubewardenRepo">
<h2 class="mt-20 mb-10" data-testid="kw-repo-title">
{{ t("kubewarden.dashboard.prerequisites.repository.title") }}
</h2>
Expand Down
19 changes: 3 additions & 16 deletions pkg/kubewarden/components/DefaultsBanner.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { mapGetters } from 'vuex';
import { CATALOG } from '@shell/config/types';
import { REPO_TYPE, REPO, CHART, VERSION } from '@shell/config/query-params';
import ResourceManager from '@shell/mixins/resource-manager';
import { Banner } from '@components/Banner';
Expand All @@ -14,18 +13,10 @@ import { handleGrowl } from '../utils/handle-growl';
export default {
components: { Banner },
mixins: [ResourceManager],
async fetch() {
this.secondaryResourceData = this.secondaryResourceDataConfig();
await this.resourceManagerFetchSecondaryResources(this.secondaryResourceData);
},
data() {
return {
apps: null,
secondaryResourceData: this.secondaryResourceDataConfig()
};
if ( this.$store.getters[`cluster/canList`](CATALOG.CLUSTER_REPO) ) {
await this.$store.dispatch('catalog/refresh');
}
},
computed: {
Expand All @@ -50,10 +41,6 @@ export default {
},
methods: {
secondaryResourceDataConfig() {
return { data: { [CATALOG.APP]: { applyTo: [{ var: 'apps' }] } } };
},
async closeDefaultsBanner(retry = 0) {
const res = await this.$store.dispatch('kubewarden/updateHideBannerDefaults', true);
Expand Down
25 changes: 16 additions & 9 deletions tests/unit/components/Dashboard/DashboardView.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import DashboardView from '@kubewarden/components/Dashboard/DashboardView.vue';
import DefaultsBanner from '@kubewarden/components/DefaultsBanner';
import ConsumptionGauge from '@shell/components/ConsumptionGauge';

import DEFAULTS_APP from '../../templates/defaultsApp';
import PS_POD from '../../templates/policyServerPod';

describe('component: DashboardView', () => {
it('renders defaults banner when default app is not found', () => {
const wrapper = shallowMount(DashboardView as unknown as ExtendedVue<Vue, {}, {}, {}, DefaultProps>, {
computed: {
defaultsApp: () => null,
defaultsApp: () => DEFAULTS_APP,
defaultsChart: () => null,
hideBannerDefaults: () => false,
policyServerPods: () => [],
policyServerPods: () => [PS_POD],
globalPolicies: () => [],
namespacedPolicies: () => [],
version: () => '1.25'
Expand All @@ -28,7 +31,8 @@ describe('component: DashboardView', () => {
'current_product/all': jest.fn(),
'i18n/t': jest.fn(),
'kubewarden/hideBannerDefaults': jest.fn(),
'catalog/chart': jest.fn()
'catalog/chart': jest.fn(),
'cluster/canList': () => true
},
}
},
Expand Down Expand Up @@ -70,7 +74,8 @@ describe('component: DashboardView', () => {
return { psPods: pods };
},
computed: {
defaultsApp: () => null,
defaultsApp: () => DEFAULTS_APP,
policyServerPods: () => [PS_POD],
globalPolicies: () => [],
namespacedPolicies: () => [],
version: () => '1.25'
Expand All @@ -84,7 +89,8 @@ describe('component: DashboardView', () => {
'current_product/all': jest.fn(),
'i18n/t': jest.fn(),
'kubewarden/hideBannerDefaults': jest.fn(),
'catalog/chart': jest.fn()
'catalog/chart': jest.fn(),
'cluster/canList': () => true
},
}
},
Expand All @@ -93,7 +99,7 @@ describe('component: DashboardView', () => {

const gauges = wrapper.findAllComponents(ConsumptionGauge);

expect(gauges.at(0).props().capacity).toStrictEqual(pods.length as Number);
expect(gauges.at(0).props().capacity).toStrictEqual([PS_POD].length as Number);
expect(gauges.at(0).props().used).toStrictEqual(1 as Number);
});

Expand Down Expand Up @@ -124,8 +130,8 @@ describe('component: DashboardView', () => {

const wrapper = shallowMount(DashboardView as unknown as ExtendedVue<Vue, {}, {}, {}, DefaultProps>, {
computed: {
defaultsApp: () => null,
policyServerPods: () => [],
defaultsApp: () => DEFAULTS_APP,
policyServerPods: () => [PS_POD],
globalPolicies: () => [],
namespacedPolicies: () => policies,
version: () => '1.25'
Expand All @@ -139,7 +145,8 @@ describe('component: DashboardView', () => {
'current_product/all': jest.fn(),
'i18n/t': jest.fn(),
'kubewarden/hideBannerDefaults': jest.fn(),
'catalog/chart': jest.fn()
'catalog/chart': jest.fn(),
'cluster/canList': () => true
},
}
},
Expand Down
Loading

0 comments on commit 421023f

Please sign in to comment.