Skip to content

Commit

Permalink
Merge pull request #1834 from codyrancher/cortex-merge
Browse files Browse the repository at this point in the history
Change the way that we merge presets with existing configs
  • Loading branch information
codyrancher committed Nov 6, 2023
2 parents e1c691c + 01811c4 commit 92a8f46
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions web/pkg/opni/components/MonitoringBackend/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import LabeledSelect from '@shell/components/form/LabeledSelect';
import Tab from '@shell/components/Tabbed/Tab';
import Tabbed from '@shell/components/Tabbed';
import { cloneDeep, merge } from 'lodash';
import { cloneDeep } from 'lodash';
import { CortexOps, DriverUtil } from '@pkg/opni/api/opni';
import { getClusterStats } from '@pkg/opni/utils/requests';
import { Duration } from '@bufbuild/protobuf';
Expand All @@ -22,6 +22,32 @@ export async function isEnabled() {
}
}
export function mergeConfig(target, ...sources) {
function isObject(item) {
return (item && typeof item === 'object' && !Array.isArray(item));
}
if (!sources.length) {
return target;
}
const source = sources.shift();
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) {
Object.assign(target, { [key]: {} });
}
mergeConfig(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}
return mergeConfig(target, ...sources);
}
export default {
components: {
Backend,
Expand Down Expand Up @@ -265,7 +291,7 @@ export default {
setPresetAsConfig(index) {
const presetConfig = cloneDeep(this.presets[index].spec);
const currentConfig = cloneDeep(this.config);
const mergedConfig = merge(currentConfig, presetConfig);
const mergedConfig = mergeConfig({}, currentConfig, presetConfig);
this.$set(this, 'config', mergedConfig);
this.prepareConfigFieldsForUI();
Expand Down

0 comments on commit 92a8f46

Please sign in to comment.