From da8f8a783c6a5d728efbfb5b615e314ea4d8fb15 Mon Sep 17 00:00:00 2001 From: Guy Abramovich Date: Thu, 28 Sep 2023 15:55:37 +0300 Subject: [PATCH 1/8] change namespace to apps --- .../models/kubernetes/chaosExperimentConnector.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chaos-experiments/models/kubernetes/chaosExperimentConnector.js b/src/chaos-experiments/models/kubernetes/chaosExperimentConnector.js index 5a2d77b8..05e403bc 100644 --- a/src/chaos-experiments/models/kubernetes/chaosExperimentConnector.js +++ b/src/chaos-experiments/models/kubernetes/chaosExperimentConnector.js @@ -4,7 +4,7 @@ const kubernetesConfig = require('../../../config/kubernetesConfig'); const logger = require('../../../common/logger'); const requestSender = require('../../../common/requestSender'); const kubernetesUrl = kubernetesConfig.kubernetesUrl; -const kubernetesNamespace = kubernetesConfig.kubernetesNamespace; +const kubernetesNamespace = 'apps'; // kubernetesConfig.kubernetesNamespace; const TOKEN_PATH = '/var/run/secrets/kubernetes.io/serviceaccount/token'; const headers = {}; From bad9870523dca92a2261bd4a87a6c78db4c2c68a Mon Sep 17 00:00:00 2001 From: Guy Abramovich Date: Thu, 28 Sep 2023 17:33:21 +0300 Subject: [PATCH 2/8] v1alpha1 --- .../models/kubernetes/chaosExperimentConnector.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chaos-experiments/models/kubernetes/chaosExperimentConnector.js b/src/chaos-experiments/models/kubernetes/chaosExperimentConnector.js index 05e403bc..1f65c9f1 100644 --- a/src/chaos-experiments/models/kubernetes/chaosExperimentConnector.js +++ b/src/chaos-experiments/models/kubernetes/chaosExperimentConnector.js @@ -24,7 +24,7 @@ if (kubernetesConfig.kubernetesToken) { module.exports.runChaosExperiment = async (kubernetesExperimentConfig) => { const resourceKindName = kubernetesExperimentConfig.kind.toLowerCase(); - const url = util.format('%s/apis/chaos-mesh.org/v1/namespaces/%s/%s', kubernetesUrl, kubernetesNamespace, resourceKindName); + const url = util.format('%s/apis/chaos-mesh.org/v1alpha1/namespaces/%s/%s', kubernetesUrl, kubernetesNamespace, resourceKindName); const options = { url, method: 'POST', From aabefa5523a1d85ee80fe29da9de15b535fe598b Mon Sep 17 00:00:00 2001 From: Guy Abramovich Date: Thu, 28 Sep 2023 17:40:21 +0300 Subject: [PATCH 3/8] v1alpha1 #2 --- .../models/kubernetes/chaosExperimentConnector-test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit-tests/chaos-experiments/models/kubernetes/chaosExperimentConnector-test.js b/tests/unit-tests/chaos-experiments/models/kubernetes/chaosExperimentConnector-test.js index 5f854a50..17505277 100644 --- a/tests/unit-tests/chaos-experiments/models/kubernetes/chaosExperimentConnector-test.js +++ b/tests/unit-tests/chaos-experiments/models/kubernetes/chaosExperimentConnector-test.js @@ -45,7 +45,7 @@ describe('Chaos experiments manager tests', function () { await chaosExperimentConnector.runChaosExperiment(kubernetesJobConfig); requestSenderSendStub.callCount.should.eql(1); requestSenderSendStub.args[0][0].should.eql({ - url: 'localhost:80/apis/chaos-mesh.org/v1/namespaces/default/podchaos', + url: 'localhost:80/apis/chaos-mesh.org/v1alpha1/namespaces/default/podchaos', method: 'POST', body: kubernetesJobConfig, headers: {} @@ -77,7 +77,7 @@ describe('Chaos experiments manager tests', function () { await chaosExperimentConnector.runChaosExperiment(kubernetesJobConfig); requestSenderSendStub.callCount.should.eql(1); requestSenderSendStub.args[0][0].should.eql({ - url: 'localhost:80/apis/chaos-mesh.org/v1/namespaces/default/httpchaos', + url: 'localhost:80/apis/chaos-mesh.org/v1alpha1/namespaces/default/httpchaos', method: 'POST', body: kubernetesJobConfig, headers: {} From acbcb98983927aa56b4600bc37a01edec1e731a4 Mon Sep 17 00:00:00 2001 From: Guy Abramovich Date: Thu, 28 Sep 2023 18:18:05 +0300 Subject: [PATCH 4/8] fix name --- src/jobs/models/jobExperimentsHandler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jobs/models/jobExperimentsHandler.js b/src/jobs/models/jobExperimentsHandler.js index 56df9159..8f754c02 100644 --- a/src/jobs/models/jobExperimentsHandler.js +++ b/src/jobs/models/jobExperimentsHandler.js @@ -33,7 +33,7 @@ async function setSingleJobExperiment(experimentRequest, experimentsFromDb, base const jobExperimentId = uuid(); await chaosExperimentManager.insertChaosJobExperiment(jobExperimentId, jobId, experiment.id, startTime, endTime); const kubeObject = experiment.kubeObject; - kubeObject.name = kubeObject.metadata.name.concat(`_${jobExperimentId}`); + kubeObject.metadata.name = kubeObject.metadata.name.concat(`_${jobExperimentId}`); const timeout = setTimeout(() => chaosExperimentManager.runChaosExperiment(kubeObject, jobExperimentId), experimentRequest.start_after); jobExperimentsIdToTimeout.set(jobExperimentId, timeout); } catch (error){ From 2dfbba3d0b234a31d752de64a893f6cf41002c3f Mon Sep 17 00:00:00 2001 From: Guy Abramovich Date: Sun, 1 Oct 2023 10:28:34 +0300 Subject: [PATCH 5/8] change k8s name --- src/jobs/models/jobExperimentsHandler.js | 2 +- tests/unit-tests/jobs/models/jobExperimentsHandler-test.js | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/jobs/models/jobExperimentsHandler.js b/src/jobs/models/jobExperimentsHandler.js index 8f754c02..c05d973c 100644 --- a/src/jobs/models/jobExperimentsHandler.js +++ b/src/jobs/models/jobExperimentsHandler.js @@ -33,7 +33,7 @@ async function setSingleJobExperiment(experimentRequest, experimentsFromDb, base const jobExperimentId = uuid(); await chaosExperimentManager.insertChaosJobExperiment(jobExperimentId, jobId, experiment.id, startTime, endTime); const kubeObject = experiment.kubeObject; - kubeObject.metadata.name = kubeObject.metadata.name.concat(`_${jobExperimentId}`); + kubeObject.metadata.name = kubeObject.metadata.name.concat(`-${jobExperimentId}`); const timeout = setTimeout(() => chaosExperimentManager.runChaosExperiment(kubeObject, jobExperimentId), experimentRequest.start_after); jobExperimentsIdToTimeout.set(jobExperimentId, timeout); } catch (error){ diff --git a/tests/unit-tests/jobs/models/jobExperimentsHandler-test.js b/tests/unit-tests/jobs/models/jobExperimentsHandler-test.js index 0b241c83..d7ed5daa 100644 --- a/tests/unit-tests/jobs/models/jobExperimentsHandler-test.js +++ b/tests/unit-tests/jobs/models/jobExperimentsHandler-test.js @@ -45,6 +45,7 @@ describe('Job experiments handler tests', function () { it('set chaos experiments with 2 experiments', async () => { const firstExperiment = generateExperiment(); const secondExperiment = generateExperiment(); + const firstExperimentName = firstExperiment.kubeObject.metadata.name; experimentsManagerInsertStub.resolves(); experimentsManagerGetStub.resolves([firstExperiment, secondExperiment]); experimentsManagerRunJobStub.resolves(); @@ -64,6 +65,7 @@ describe('Job experiments handler tests', function () { await jobExperimentHandler.setChaosExperimentsIfExist(jobId, jobExperiments); clock.tick(3000); experimentsManagerRunJobStub.callCount.should.eql(2); + experimentsManagerRunJobStub.args[0][0].metadata.name.should.eql(`${firstExperimentName}-${experimentsManagerRunJobStub.args[0][1]}`); experimentsManagerGetStub.callCount.should.eql(1); experimentsManagerInsertStub.callCount.should.eql(2); experimentsManagerInsertStub.args[0][1].should.eql(jobId); From e7ad07278f90a894dcf9fb729964a6dd0e531d2b Mon Sep 17 00:00:00 2001 From: Guy Abramovich Date: Sun, 1 Oct 2023 10:54:45 +0300 Subject: [PATCH 6/8] add try catch --- src/chaos-experiments/models/chaosExperimentsManager.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/chaos-experiments/models/chaosExperimentsManager.js b/src/chaos-experiments/models/chaosExperimentsManager.js index ebce7d95..730fe4fc 100644 --- a/src/chaos-experiments/models/chaosExperimentsManager.js +++ b/src/chaos-experiments/models/chaosExperimentsManager.js @@ -83,6 +83,10 @@ module.exports.insertChaosJobExperiment = async (jobExperimentId, jobId, experim }; module.exports.runChaosExperiment = async (kubernetesChaosConfig, jobExperimentId) => { - await kubernetesConnector.runChaosExperiment(kubernetesChaosConfig); - await databaseConnector.setChaosJobExperimentTriggered(jobExperimentId, true); + try { + await kubernetesConnector.runChaosExperiment(kubernetesChaosConfig); + await databaseConnector.setChaosJobExperimentTriggered(jobExperimentId, true); + } catch (error){ + logger.error(error, `Error while running chaos job experiment ${jobExperimentId}`); + } }; From d99afff1bba443916547f12344bc7a30304396bb Mon Sep 17 00:00:00 2001 From: Guy Abramovich Date: Sun, 1 Oct 2023 11:13:03 +0300 Subject: [PATCH 7/8] fix namespace --- .../models/kubernetes/chaosExperimentConnector.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chaos-experiments/models/kubernetes/chaosExperimentConnector.js b/src/chaos-experiments/models/kubernetes/chaosExperimentConnector.js index 1f65c9f1..c4896362 100644 --- a/src/chaos-experiments/models/kubernetes/chaosExperimentConnector.js +++ b/src/chaos-experiments/models/kubernetes/chaosExperimentConnector.js @@ -4,7 +4,6 @@ const kubernetesConfig = require('../../../config/kubernetesConfig'); const logger = require('../../../common/logger'); const requestSender = require('../../../common/requestSender'); const kubernetesUrl = kubernetesConfig.kubernetesUrl; -const kubernetesNamespace = 'apps'; // kubernetesConfig.kubernetesNamespace; const TOKEN_PATH = '/var/run/secrets/kubernetes.io/serviceaccount/token'; const headers = {}; @@ -24,6 +23,7 @@ if (kubernetesConfig.kubernetesToken) { module.exports.runChaosExperiment = async (kubernetesExperimentConfig) => { const resourceKindName = kubernetesExperimentConfig.kind.toLowerCase(); + const kubernetesNamespace = kubernetesExperimentConfig.metadata.namespace; const url = util.format('%s/apis/chaos-mesh.org/v1alpha1/namespaces/%s/%s', kubernetesUrl, kubernetesNamespace, resourceKindName); const options = { url, From b4ce0d3a1341f01ff9172ab884e48129bde012f2 Mon Sep 17 00:00:00 2001 From: Guy Abramovich Date: Sun, 1 Oct 2023 11:31:21 +0300 Subject: [PATCH 8/8] unit tests --- .../kubernetes/chaosExperimentConnector-test.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/unit-tests/chaos-experiments/models/kubernetes/chaosExperimentConnector-test.js b/tests/unit-tests/chaos-experiments/models/kubernetes/chaosExperimentConnector-test.js index 17505277..467aa798 100644 --- a/tests/unit-tests/chaos-experiments/models/kubernetes/chaosExperimentConnector-test.js +++ b/tests/unit-tests/chaos-experiments/models/kubernetes/chaosExperimentConnector-test.js @@ -26,7 +26,7 @@ describe('Chaos experiments manager tests', function () { apiVersion: 'chaos-mesh.org/v1alpha1', metadata: { namespace: 'apps', - name: 'first pod fault', + name: 'firstPodChaos', annotations: {} }, spec: { @@ -41,11 +41,11 @@ describe('Chaos experiments manager tests', function () { } } }; - requestSenderSendStub.resolves({ metadata: { name: 'firstPodChaos', uid: 'some_uuid' }, namespace: 'default' }); + requestSenderSendStub.resolves({ metadata: { name: 'firstPodChaos', uid: 'some_uuid' }, namespace: 'apps' }); await chaosExperimentConnector.runChaosExperiment(kubernetesJobConfig); requestSenderSendStub.callCount.should.eql(1); requestSenderSendStub.args[0][0].should.eql({ - url: 'localhost:80/apis/chaos-mesh.org/v1alpha1/namespaces/default/podchaos', + url: 'localhost:80/apis/chaos-mesh.org/v1alpha1/namespaces/apps/podchaos', method: 'POST', body: kubernetesJobConfig, headers: {} @@ -58,7 +58,7 @@ describe('Chaos experiments manager tests', function () { apiVersion: 'chaos-mesh.org/v1alpha1', metadata: { namespace: 'apps', - name: 'first http fault', + name: 'firstHttpChaos', annotations: {} }, spec: { @@ -73,11 +73,11 @@ describe('Chaos experiments manager tests', function () { } } }; - requestSenderSendStub.resolves({ metadata: { name: 'firstHttpChaos', uid: 'some_uuid' }, namespace: 'default' }); + requestSenderSendStub.resolves({ metadata: { name: 'firstHttpChaos', uid: 'some_uuid' }, namespace: 'apps' }); await chaosExperimentConnector.runChaosExperiment(kubernetesJobConfig); requestSenderSendStub.callCount.should.eql(1); requestSenderSendStub.args[0][0].should.eql({ - url: 'localhost:80/apis/chaos-mesh.org/v1alpha1/namespaces/default/httpchaos', + url: 'localhost:80/apis/chaos-mesh.org/v1alpha1/namespaces/apps/httpchaos', method: 'POST', body: kubernetesJobConfig, headers: {}