Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide ability to 'feature flag' an ISV App #59

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion backend/routes/api/components/componentUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const fs = require('fs');
const path = require('path');
const jsYaml = require('js-yaml');
const constants = require('../../../utils/constants');
const features = require('../../../utils/features');

const getServices = async (fastify) => {
const coreV1Api = fastify.kube.coreV1Api;
Expand Down Expand Up @@ -152,11 +153,14 @@ const getEnabledConfigMaps = (fastify, appDefs) => {
const getApplicationDefs = () => {
const normalizedPath = path.join(__dirname, '../../../../data/applications');
const applicationDefs = [];
const featureFlags = features.getComponentFeatureFlags();
fs.readdirSync(normalizedPath).forEach((file) => {
if (constants.yamlRegExp.test(file)) {
try {
const doc = jsYaml.load(fs.readFileSync(path.join(normalizedPath, file), 'utf8'));
applicationDefs.push(doc);
if (!doc.spec.featureFlag || featureFlags[doc.spec.featureFlag]) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

future: this may also work with just the one check and optional chaining, I'd need to fully confirm. Regardless, what you have works, pre-opt chaining

applicationDefs.push(doc);
}
} catch (e) {
console.error(`Error loading application definition ${file}: ${e}`);
}
Expand Down
15 changes: 14 additions & 1 deletion backend/routes/api/docs/docUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,28 @@ const fs = require('fs');
const path = require('path');
const jsYaml = require('js-yaml');
const constants = require('../../../utils/constants');
const features = require('../../../utils/features');
const componentUtils = require('../components/componentUtils');

const getDocs = () => {
const normalizedPath = path.join(__dirname, '../../../../data/docs');
const docs = [];
const featureFlags = features.getComponentFeatureFlags();
const appDefs = componentUtils.getApplicationDefs();

fs.readdirSync(normalizedPath).forEach((file) => {
if (constants.yamlRegExp.test(file)) {
try {
const doc = jsYaml.load(fs.readFileSync(path.join(normalizedPath, file), 'utf8'));
docs.push(doc);
if (doc.spec.featureFlag) {
if (featureFlags[doc.spec.featureFlag]) {
docs.push(doc);
}
return;
}
if (!doc.spec.appName || appDefs.find((def) => def.metadata.name === doc.spec.appName)) {
docs.push(doc);
}
} catch (e) {
console.error(`Error loading doc ${file}: ${e}`);
}
Expand Down
7 changes: 6 additions & 1 deletion backend/routes/api/quickstarts/quickStartUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs');
const path = require('path');
const jsYaml = require('js-yaml');
const constants = require('../../../utils/constants');
const features = require('../../../utils/features');

// TODO: Retrieve from the correct group for dashboard quick starts
const quickStartsGroup = 'odh.openshift.io';
Expand All @@ -24,13 +25,17 @@ const getInstalledQuickStarts = async (fastify) => {
// fastify.log.error(e, 'failed to get quickstarts');
// }

const featureFlags = features.getComponentFeatureFlags();

// TODO: Remove local quick starts when we get the correct quick starts from OpenShift
const normalizedPath = path.join(__dirname, '../../../../data/quickstarts');
fs.readdirSync(normalizedPath).forEach((file) => {
if (constants.yamlRegExp.test(file)) {
try {
const doc = jsYaml.load(fs.readFileSync(path.join(normalizedPath, file), 'utf8'));
installedQuickStarts.push(doc);
if (!doc.spec.featureFlag || featureFlags[doc.spec.featureFlag]) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

future: this may also work with just the one check and optional chaining, I'd need to fully confirm. Regardless, what you have works, pre-opt chaining

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is that if there is no feature flag it is allowed, if there is a feature flag, check the flags.

installedQuickStarts.push(doc);
}
} catch (e) {
console.error(`Error loading quick start ${file}: ${e}`);
}
Expand Down
13 changes: 13 additions & 0 deletions backend/utils/features.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const fs = require('fs');
const path = require('path');

const getComponentFeatureFlags = () => {
const normalizedPath = path.join(__dirname, '../../data/features.json');
try {
return JSON.parse(fs.readFileSync(normalizedPath, 'utf8'));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

future: since not sure if that may be a file prone to update, then using what you have is good, JSON can also be brought through with a require('[your json file]')

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the idea is that the feature flags can be enabled on the deployed site

} catch {
return {};
}
};

module.exports = { getComponentFeatureFlags };
3 changes: 3 additions & 0 deletions data/features.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"example-feature-app-name": true
}