-
Notifications
You must be signed in to change notification settings - Fork 21
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
added kubernetes.v2 endpoint #350
Changes from 1 commit
7b51886
7b3c660
aa7657c
448d1ea
43c4ad2
030d119
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const AWSXRay = require('aws-xray-sdk'); | ||
const k8s = require('@kubernetes/client-node'); | ||
const getLogger = require('../../utils/getLogger'); | ||
|
||
const kc = new k8s.KubeConfig(); | ||
kc.loadFromDefault(); | ||
|
||
const logger = getLogger(); | ||
|
||
module.exports = { | ||
'kubernetes#event': async (req, res, next) => { | ||
logger.log('received kubernetes event'); | ||
try { | ||
const { | ||
reason, message, type, involvedObject: { name, namespace }, | ||
} = req.body; | ||
logger.log(`[${reason}] received ${type} kubernetes event: ${message} ${name} in ${namespace}`); | ||
|
||
// remove only pods in your namespace and due to backoff errors | ||
if ((namespace.match('^pipeline-.*') || namespace.match('^worker-.*')) | ||
&& reason === 'BackOff' && type !== 'Normal' && message.includes('restarting')) { | ||
kafkasl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const k8sApi = kc.makeApiClient(k8s.CoreV1Api); | ||
logger.log(`removing pod ${name} in ${namespace}`); | ||
await k8sApi.deleteNamespacedPod(name, namespace); | ||
} | ||
res.status(200).send('ok'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer not to, they are not equivalent and the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I modified it in the end, we'll need to verify it works correctly with the |
||
} catch (e) { | ||
logger.error('error processing k8s event', e); | ||
AWSXRay.getSegment().addError(e); | ||
next(e); | ||
} | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
const k8s = require('@kubernetes/client-node'); | ||
|
||
|
||
jest.mock('@kubernetes/client-node'); | ||
|
||
|
||
const deleteNamespacedPod = jest.fn(); | ||
const patchNamespacedPod = jest.fn(); | ||
const listNamespacedPod = jest.fn(); | ||
const mockApi = { | ||
deleteNamespacedPod, | ||
patchNamespacedPod, | ||
listNamespacedPod, | ||
}; | ||
|
||
k8s.KubeConfig.mockImplementation(() => ({ | ||
loadFromDefault: jest.fn(), | ||
makeApiClient: (() => mockApi), | ||
})); | ||
|
||
|
||
const removeRequest = { | ||
metadata: { | ||
name: 'pipeline-6f87dbcb55-wzvnk.16b31901b952f1ec', | ||
namespace: 'pipeline-default', | ||
uid: 'cf831e5f-29bf-4750-ae9a-8bbe4d1f93fd', | ||
resourceVersion: '226149137', | ||
creationTimestamp: '2021-10-31T11:09:44Z', | ||
managedFields: [[Object]], | ||
}, | ||
reason: 'BackOff', | ||
message: 'Back-off restarting failed container', | ||
source: { | ||
component: 'kubelet', | ||
host: 'fargate-ip-192-168-180-35.eu-west-1.compute.internal', | ||
}, | ||
firstTimestamp: '2021-10-31T11:09:44Z', | ||
lastTimestamp: '2021-10-31T11:13:28Z', | ||
count: 16, | ||
type: 'Warning', | ||
eventTime: null, | ||
reportingComponent: '', | ||
reportingInstance: '', | ||
involvedObject: { | ||
kind: 'Pod', | ||
namespace: 'pipeline-default', | ||
name: 'pipeline-6f87dbcb55-wzvnk', | ||
uid: 'd204be1b-6626-4320-8bae-3b203aff9562', | ||
apiVersion: 'v1', | ||
resourceVersion: '226145375', | ||
fieldPath: 'spec.containers{pipeline}', | ||
labels: { | ||
activityId: 'wrong', | ||
'eks.amazonaws.com/fargate-profile': 'pipeline-default', | ||
'pod-template-hash': '6f87dbcb55', | ||
sandboxId: 'default', | ||
type: 'pipeline', | ||
}, | ||
annotations: { | ||
CapacityProvisioned: '1vCPU 5GB', | ||
Logging: 'LoggingDisabled: LOGGING_CONFIGMAP_NOT_FOUND', | ||
}, | ||
}, | ||
}; | ||
|
||
const express = require('express'); | ||
const request = require('supertest'); | ||
const expressLoader = require('../../../src/loaders/express'); | ||
|
||
describe('tests for experiment route', () => { | ||
kafkasl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let app = null; | ||
|
||
beforeEach(async () => { | ||
const mockApp = await expressLoader(express()); | ||
app = mockApp.app; | ||
}); | ||
|
||
afterEach(() => { | ||
/** | ||
* Most important since b'coz of caching, the mocked implementations sometimes does not reset | ||
*/ | ||
jest.resetModules(); | ||
jest.restoreAllMocks(); | ||
kafkasl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
it('sending a remove request works', async (done) => { | ||
request(app) | ||
.post('/v2/kubernetesEvents') | ||
.send(removeRequest) | ||
.expect(200) | ||
.end((err) => { | ||
if (err) { | ||
return done(err); | ||
} | ||
|
||
return done(); | ||
}); | ||
}); | ||
|
||
it('sending an empty request works', async (done) => { | ||
request(app) | ||
.post('/v2/kubernetesEvents') | ||
.send(undefined) | ||
.expect(500) | ||
.end((err) => done(err)); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This string includes regex characters
^
and.*
. Instead of a string, would it be clearer to change this string as a Regex? e.g./^pipeline-.*/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the difference? I think they do the same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it just makes it clearer that you're matching against a regex, not a string. It wouldn't affect the functionality, but it's fine if it's kept this way.