Skip to content

Commit

Permalink
setup transformer code with openfaas pro deployment changes
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimanyubabbar committed Mar 12, 2024
1 parent d2eba21 commit ece6614
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 26 deletions.
32 changes: 18 additions & 14 deletions src/util/openfaas/faasApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,62 @@ const parseAxiosError = (error) => {
return error;
};

const deleteFunction = async (functionName) =>
const deleteFunction = async (functionName, auth = {}) =>
new Promise((resolve, reject) => {
const url = `${OPENFAAS_GATEWAY_URL}/system/functions`;
axios
.delete(url, { data: { functionName } })
.delete(url, { data: { functionName } }, { auth: auth })
.then(() => resolve())
.catch((err) => reject(parseAxiosError(err)));
});

const getFunction = async (functionName) =>
const getFunction = async (functionName, auth = {}) =>
new Promise((resolve, reject) => {
const url = `${OPENFAAS_GATEWAY_URL}/system/function/${functionName}`;
axios
.get(url)
.get(url, { auth: auth })
.then((resp) => resolve(resp.data))
.catch((err) => reject(parseAxiosError(err)));
});

const getFunctionList = async () =>
const getFunctionList = async (auth = {}) =>
new Promise((resolve, reject) => {
const url = `${OPENFAAS_GATEWAY_URL}/system/functions`;
axios
.get(url)
.get(url, { auth: auth })
.then((resp) => resolve(resp.data))
.catch((err) => reject(parseAxiosError(err)));
});

const invokeFunction = async (functionName, payload) =>
const invokeFunction = async (functionName, payload, auth = {}) =>
new Promise((resolve, reject) => {
const url = `${OPENFAAS_GATEWAY_URL}/function/${functionName}`;
axios
.post(url, payload)
.post(url, payload, { auth: auth })
.then((resp) => resolve(resp.data))
.catch((err) => reject(parseAxiosError(err)));
});

const checkFunctionHealth = async (functionName) =>
const checkFunctionHealth = async (functionName, auth = {}) =>
new Promise((resolve, reject) => {
const url = `${OPENFAAS_GATEWAY_URL}/function/${functionName}`;
axios
.get(url, {
headers: { 'X-REQUEST-TYPE': 'HEALTH-CHECK' },
})
.get(
url,
{
headers: { 'X-REQUEST-TYPE': 'HEALTH-CHECK' },
},
{ auth: auth },
)
.then((resp) => resolve(resp))
.catch((err) => reject(parseAxiosError(err)));
});

const deployFunction = async (payload) =>
const deployFunction = async (payload, auth = {}) =>
new Promise((resolve, reject) => {
const url = `${OPENFAAS_GATEWAY_URL}/system/functions`;
axios
.post(url, payload)
.post(url, payload, { auth: auth })
.then((resp) => resolve(resp.data))
.catch((err) => reject(parseAxiosError(err)));
});
Expand Down
37 changes: 31 additions & 6 deletions src/util/openfaas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ const stats = require('../stats');
const { getMetadata, getTransformationMetadata } = require('../../v0/util');
const { HTTP_STATUS_CODES } = require('../../v0/util/constant');

const FAAS_GATEWAY_USERNAME = process.env.FAAS_GATEWAY_USERNAME || '';
const FAAS_GATEWAY_PASSWORD = process.env.FAAS_GATEWAY_PASSWORD || '';
const FAAS_SCALE_TYPE = process.env.FAAS_SCALE_TYPE || 'capacity';
const FAAS_SCALE_TARGET = process.env.FAAS_SCALE_TARGET || '4';
const FAAS_SCALE_TARGET_PROPORTION = process.env.FAAS_SCALE_TARGET_PROPORTION || '0.70';
const FAAS_SCALE_ZERO = process.env.FAAS_SCALE_ZERO || 'false';
const FAAS_SCALE_ZERO_DURATION = process.env.FAAS_SCALE_ZERO_DURATION || '15m';
const FAAS_BASE_IMG = process.env.FAAS_BASE_IMG || 'rudderlabs/openfaas-flask:main';
const FAAS_MAX_PODS_IN_TEXT = process.env.FAAS_MAX_PODS_IN_TEXT || '40';
const FAAS_MIN_PODS_IN_TEXT = process.env.FAAS_MIN_PODS_IN_TEXT || '1';
Expand Down Expand Up @@ -58,6 +65,7 @@ const callWithRetry = async (

const awaitFunctionReadiness = async (
functionName,
auth,
maxWaitInMs = 22000,
waitBetweenIntervalsInMs = 250,
) => {
Expand All @@ -69,6 +77,7 @@ const awaitFunctionReadiness = async (
waitBetweenIntervalsInMs,
Math.floor(maxWaitInMs / waitBetweenIntervalsInMs),
functionName,
auth,
);

resolve(true);
Expand Down Expand Up @@ -149,6 +158,11 @@ const deployFaasFunction = async (
'parent-component': 'openfaas',
'com.openfaas.scale.max': FAAS_MAX_PODS_IN_TEXT,
'com.openfaas.scale.min': FAAS_MIN_PODS_IN_TEXT,
'com.openfaas.scale.zero': FAAS_SCALE_ZERO,
'com.openfaas.scale.zero-duration': FAAS_SCALE_ZERO_DURATION,
'com.openfaas.scale.target': FAAS_SCALE_TARGET,
'com.openfaas.scale.target.proportion': FAAS_SCALE_TARGET_PROPORTION,
'com.openfaas.scale.type': FAAS_SCALE_TYPE,
transformationId: trMetadata.transformationId,
workspaceId: trMetadata.workspaceId,
};
Expand Down Expand Up @@ -180,7 +194,10 @@ const deployFaasFunction = async (
},
};

await deployFunction(payload);
await deployFunction(payload, {
username: FAAS_GATEWAY_USERNAME,
password: FAAS_GATEWAY_PASSWORD,
});
logger.debug('[Faas] Deployed a faas function');
} catch (error) {
logger.error(`[Faas] Error while deploying ${functionName}: ${error.message}`);
Expand Down Expand Up @@ -245,8 +262,15 @@ const executeFaasFunction = async (
let errorRaised;

try {
if (testMode) await awaitFunctionReadiness(name);
return await invokeFunction(name, events);
if (testMode)
await awaitFunctionReadiness(name, {
username: FAAS_GATEWAY_USERNAME,
password: FAAS_GATEWAY_PASSWORD,
});
return await invokeFunction(name, events, {
username: FAAS_GATEWAY_USERNAME,
password: FAAS_GATEWAY_PASSWORD,
});
} catch (error) {
logger.error(`Error while invoking ${name}: ${error.message}`);
errorRaised = error;
Expand All @@ -273,9 +297,10 @@ const executeFaasFunction = async (
} finally {
// delete the function created, if it's called as part of testMode
if (testMode) {
deleteFunction(name).catch((err) =>
logger.error(`[Faas] Error while deleting ${name}: ${err.message}`),
);
deleteFunction(name, {
username: FAAS_GATEWAY_USERNAME,
password: FAAS_GATEWAY_PASSWORD,
}).catch((err) => logger.error(`[Faas] Error while deleting ${name}: ${err.message}`));
}

// setup the tags for observability and then fire the stats
Expand Down
22 changes: 16 additions & 6 deletions test/__tests__/user_transformation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ const { parserForImport } = require("../../src/util/parser");
const { RetryRequestError, RespStatusError } = require("../../src/util/utils");

const OPENFAAS_GATEWAY_URL = "http://localhost:8080";
const defaultBasicAuth = {
"username": "",
"password": ""
};

const randomID = () =>
Math.random()
Expand Down Expand Up @@ -1400,12 +1404,14 @@ describe("Python transformations", () => {
expect(axios.post).toHaveBeenCalledTimes(1);
expect(axios.post).toHaveBeenCalledWith(
`${OPENFAAS_GATEWAY_URL}/system/functions`,
expect.objectContaining({ name: funcName, service: funcName })
expect.objectContaining({ name: funcName, service: funcName }),
{ auth: defaultBasicAuth },
);
expect(axios.get).toHaveBeenCalledTimes(1);
expect(axios.get).toHaveBeenCalledWith(
`${OPENFAAS_GATEWAY_URL}/function/${funcName}`,
{"headers": {"X-REQUEST-TYPE": "HEALTH-CHECK"}}
{"headers": {"X-REQUEST-TYPE": "HEALTH-CHECK"}},
{ auth: defaultBasicAuth },
);
});

Expand Down Expand Up @@ -1622,7 +1628,8 @@ describe("Python transformations", () => {
expect(axios.post).toHaveBeenCalledTimes(1);
expect(axios.post).toHaveBeenCalledWith(
`${OPENFAAS_GATEWAY_URL}/function/${funcName}`,
inputData
inputData,
{ auth: defaultBasicAuth },
);
});

Expand Down Expand Up @@ -1655,17 +1662,20 @@ describe("Python transformations", () => {
expect(axios.post).toHaveBeenCalledTimes(2);
expect(axios.post).toHaveBeenCalledWith(
`${OPENFAAS_GATEWAY_URL}/function/${funcName}`,
inputData
inputData,
{ auth: defaultBasicAuth },
);
expect(axios.post).toHaveBeenCalledWith(
`${OPENFAAS_GATEWAY_URL}/system/functions`,
expect.objectContaining({ name: funcName, service: funcName })
expect.objectContaining({ name: funcName, service: funcName }),
{ auth: defaultBasicAuth },
);

expect(axios.get).toHaveBeenCalledTimes(1);
expect(axios.get).toHaveBeenCalledWith(
`${OPENFAAS_GATEWAY_URL}/function/${funcName}`,
{"headers": {"X-REQUEST-TYPE": "HEALTH-CHECK"}}
{"headers": {"X-REQUEST-TYPE": "HEALTH-CHECK"}},
{ auth: defaultBasicAuth },
);
});

Expand Down

0 comments on commit ece6614

Please sign in to comment.