Skip to content

Commit

Permalink
GitOps- added sync push deleted (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
petruki authored Sep 27, 2024
1 parent 3a8bfde commit 41f95e0
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 28 deletions.
42 changes: 21 additions & 21 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"jsonwebtoken": "^9.0.2",
"moment": "^2.30.1",
"mongodb": "^6.9.0",
"mongoose": "^8.6.3",
"mongoose": "^8.6.4",
"pino": "^9.4.0",
"pino-pretty": "^11.2.2",
"swagger-ui-express": "^5.0.1",
Expand Down
13 changes: 13 additions & 0 deletions src/middleware/gitops.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ const PATH_CONSTRAINTS_CHANGED = {
STRATEGY: 3
};

const PATH_CONSTRAINTS_DELETED = {
GROUP: 1,
CONFIG: 2,
STRATEGY: 3,
COMPONENT: 2,
STRATEGY_VALUE: 3
};

export async function featureFlag(req, res, next) {
try {
await checkGitopsIntegration(req.domain);
Expand Down Expand Up @@ -58,6 +66,11 @@ function validatePathForElement(changes) {
throw new Error('Request has invalid path settings for changed element');
}
break;
case 'DELETED':
if (path.length !== PATH_CONSTRAINTS_DELETED[diff]) {
throw new Error('Request has invalid path settings for deleted element');
}
break;
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/services/gitops/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getDomainById, updateDomainVersion } from '../domain.js';
import { processChanged } from './push-changed.js';
import { processDeleted } from './push-deleted.js';
import { processNew } from './push-new.js';

export const ADMIN_EMAIL = '[email protected]';
Expand All @@ -14,16 +15,15 @@ export async function pushChanges(domainId, environment, changes) {
case 'CHANGED':
await processChanged(domain, change, environment);
break;
case 'DELETED':
await processDeleted(domain, change, environment);
break;
}
};

domain = await updateDomainVersion(domainId);
return successResponse('Changes applied successfully', domain.lastUpdate);
}

function successResponse(message, version) {
return {
message,
version
message: 'Changes applied successfully',
version: domain.lastUpdate
};
}
87 changes: 87 additions & 0 deletions src/services/gitops/push-deleted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { getComponents } from '../component.js';
import { deleteStrategy, getStrategies, removeVal } from '../config-strategy.js';
import { deleteConfig, getConfig, removeComponent } from '../config.js';
import { deleteGroup, getGroupConfig } from '../group-config.js';
import { ADMIN_EMAIL } from './index.js';

export async function processDeleted(domain, change, environment) {
switch (change.diff) {
case 'GROUP':
await processGroupDeleted(domain, change);
break;
case 'CONFIG':
await processConfigDeleted(domain, change);
break;
case 'STRATEGY':
await processStrategyDeleted(domain, change, environment);
break;
case 'STRATEGY_VALUE':
await processStrategyValueDeleted(domain, change, environment);
break;
case 'COMPONENT':
await processComponentDeleted(domain, change);
break;
}
}

async function processGroupDeleted(domain, change) {
const path = change.path;
const admin = { _id: domain.owner, email: ADMIN_EMAIL };
const group = await getGroupConfig({ domain: domain._id, name: path[0] });

await deleteGroup(group._id, admin);
}

async function processConfigDeleted(domain, change) {
const path = change.path;
const admin = { _id: domain.owner, email: ADMIN_EMAIL };
const group = await getGroupConfig({ domain: domain._id, name: path[0] });
const config = await getConfig({ domain: domain._id, group: group._id, key: path[1] });

await deleteConfig(config._id, admin);
}

async function processStrategyDeleted(domain, change, environment) {
const path = change.path;
const admin = { _id: domain.owner, email: ADMIN_EMAIL };
const group = await getGroupConfig({ domain: domain._id, name: path[0] });
const config = await getConfig({ domain: domain._id, group: group._id, key: path[1] });

const strategies = await getStrategies({ config: config._id });
const strategy = strategies.find(strategy => strategy.strategy === path[2] && strategy.activated.get(environment));

await deleteStrategy(strategy._id, admin);
}

async function processStrategyValueDeleted(domain, change, environment) {
const path = change.path;
const admin = { _id: domain.owner, email: ADMIN_EMAIL };
const group = await getGroupConfig({ domain: domain._id, name: path[0] });
const config = await getConfig({ domain: domain._id, group: group._id, key: path[1] });

const strategies = await getStrategies({ config: config._id });
const strategy = strategies.find(strategy => strategy.strategy === path[2] && strategy.activated.get(environment));

for (const value of change.content) {
if (strategy.values.includes(value)) {
await removeVal(strategy._id, { value }, admin);
}
}
}

async function processComponentDeleted(domain, change) {
const path = change.path;
const content = change.content;
const admin = { _id: domain.owner, email: ADMIN_EMAIL };
const group = await getGroupConfig({ domain: domain._id, name: path[0] });
const config = await getConfig({ domain: domain._id, group: group._id, key: path[1] });

const components = await getComponents({ domain: domain._id, name: { $in: content } });
const componentIds = components.map(component => component._id);

for (const id of componentIds) {
if (config.components.includes(id)) {
await removeComponent(config._id, { component: id }, admin);
}
}
}
Loading

0 comments on commit 41f95e0

Please sign in to comment.