Skip to content

Commit

Permalink
Don't fetch all workload types to check for prom operator
Browse files Browse the repository at this point in the history
- Previously ALL workloads were fetched and then filtered for those containing certain images
- We now do this upfront via API
- depends on rancher#10024
  • Loading branch information
richard-cox committed Nov 17, 2023
1 parent fd9285e commit 0c7e711
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 54 deletions.
51 changes: 41 additions & 10 deletions shell/chart/monitoring/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,41 @@ export default {
async fetch() {
const { $store } = this;
const hash = await allHash({
// Fetch all the resources required for all the tabs asyncronously up front
const hashPromises = {
namespaces: $store.getters['namespaces'](),
pvcs: $store.dispatch('cluster/findAll', { type: PVC }),
// Used in Alerting tab
monitoringSecrets: $store.dispatch('cluster/findAll', {
type: SECRET,
opt: { namespaced: CATTLE_MONITORING_NAMESPACE }
}),
storageClasses: $store.dispatch('cluster/findAll', { type: STORAGE_CLASS }),
};
// Are we editing an existing chart?
// (ported from shell/chart/monitoring/prometheus/index.vue)
// const { existing = false } = this.$attrs; // TODO: RC
const existing = false;
// If needed, fetch all the workloads that have prometheus operator like containers
this.workloadTypes = !existing ? Object.values(WORKLOAD_TYPES) : [];
this.workloadTypes.forEach((type) => {
// We'll use a filter to fetch the results. Atm there's no neat way to differentiate between ALL results and JUST filtered
// So to avoid calls to all getting these filtered (and vice-versa) forget type before and after
$store.dispatch('cluster/forgetType', type);
hashPromises[type] = $store.dispatch('cluster/findAll', {
type,
opt: {
watch: false,
// We're only interested in images with operator like names (note: these will match partial strings)
filter: { 'spec.template.spec.containers.image': ['quay.io/coreos/prometheus-operator', 'rancher/coreos-prometheus-operator'] }
}
});
});
await Promise.all(
Object.values(WORKLOAD_TYPES).map((type) => this.$store.dispatch('cluster/findAll', { type })
)
);
const hash = await allHash(hashPromises);
this.targetNamespace = hash.namespaces[this.chart.targetNamespace] || false;
Expand All @@ -81,6 +102,18 @@ export default {
if (!isEmpty(hash.monitoringSecrets)) {
this.monitoringSecrets = hash.monitoringSecrets;
}
this.workloadTypes.forEach((type) => {
if (hash[type]) {
this.filteredWorkloads.push(...hash[type]);
}
});
},
beforeDestroy() {
this.workloadTypes.forEach((type) => {
this.$store.dispatch('cluster/forgetType', type);
});
},
data() {
Expand All @@ -106,6 +139,8 @@ export default {
monitoringSecrets: [],
storageClasses: [],
targetNamespace: null,
filteredWorkloads: [],
workloadTypes: []
};
},
Expand All @@ -114,10 +149,6 @@ export default {
provider() {
return this.currentCluster.status.provider.toLowerCase();
},
workloads() {
return Object.values(WORKLOAD_TYPES).flatMap((type) => this.$store.getters['cluster/all'](type)
);
},
},
watch: {
Expand Down Expand Up @@ -282,7 +313,7 @@ export default {
:mode="mode"
:storage-classes="storageClasses"
:prometheus-pods="prometheusResources"
:workloads="workloads"
:filteredWorkloads="filteredWorkloads"
/>
</div>
</Tab>
Expand Down
63 changes: 20 additions & 43 deletions shell/chart/monitoring/prometheus/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script>
import isEmpty from 'lodash/isEmpty';
import { mapGetters } from 'vuex';
import { Banner } from '@components/Banner';
Expand Down Expand Up @@ -51,21 +50,19 @@ export default {
default: () => ({}),
},
workloads: {
filteredWorkloads: {
type: Array,
default: () => ([]),
},
},
data() {
return {
enablePersistentStorage: !!this.value?.prometheus?.prometheusSpec?.storageSpec?.volumeClaimTemplate?.spec,
warnUser: false,
};
return { enablePersistentStorage: !!this.value?.prometheus?.prometheusSpec?.storageSpec?.volumeClaimTemplate?.spec };
},
computed: {
...mapGetters(['currentCluster']),
matchExpressions: {
get() {
const selector = this.value?.prometheus?.prometheusSpec?.storageSpec?.volumeClaimTemplate?.spec?.selector;
Expand All @@ -80,42 +77,21 @@ export default {
}
}
},
filteredWorkloads() {
let { workloads } = this;
const { existing = false } = this.$attrs;
if (!existing) {
workloads = workloads.filter((workload) => {
if (
!isEmpty(workload?.spec?.template?.spec?.containers) &&
(workload.spec.template.spec.containers.find((c) => c.image.includes('quay.io/coreos/prometheus-operator') ||
c.image.includes('rancher/coreos-prometheus-operator'))
)
) {
if (!this.warnUser) {
this.warnUser = true;
}
return workload;
}
});
}
return workloads.map((wl) => {
return {
label: wl.id,
link: {
name: 'c-cluster-product-resource-namespace-id',
params: {
cluster: this.currentCluster.id,
product: 'explorer',
resource: wl.type,
namespace: wl.metadata.namespace,
id: wl.metadata.name
},
}
};
});
mappedFilteredWorkloads() {
return this.filteredWorkloads.map((wl) => ({
label: wl.id,
link: {
name: 'c-cluster-product-resource-namespace-id',
params: {
cluster: this.currentCluster.id,
product: 'explorer',
resource: wl.type,
namespace: wl.metadata.namespace,
id: wl.metadata.name
},
}
}));
},
podsAndNamespaces() {
Expand Down Expand Up @@ -189,8 +165,9 @@ export default {
<div class="title">
<h3>{{ t('monitoring.prometheus.title') }}</h3>
</div>
<!-- https://github.com/rancher/dashboard/issues/1167 -->
<Banner
v-if="filteredWorkloads && warnUser"
v-if="mappedFilteredWorkloads.length"
color="warning"
>
<template #default>
Expand All @@ -199,7 +176,7 @@ export default {
:raw="true"
/>
<div
v-for="wl in filteredWorkloads"
v-for="wl in mappedFilteredWorkloads"
:key="wl.id"
class="mt-10"
>
Expand Down
5 changes: 4 additions & 1 deletion shell/plugins/steve/getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ const GC_IGNORE_TYPES = {
[UI.NAV_LINK]: true,
};

// Include calls to /v1 AND /k8s/clusters/<cluster id>/v1
const steveRegEx = new RegExp('(/v1)|(\/k8s\/clusters\/[a-z0-9-]+\/v1)');

export default {
urlOptions: () => (url, opt) => {
opt = opt || {};
const parsedUrl = parse(url);
const isSteve = parsedUrl.path.startsWith('/v1');
const isSteve = steveRegEx.test(parsedUrl.path);

// Filter
if ( opt.filter ) {
Expand Down

0 comments on commit 0c7e711

Please sign in to comment.