-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: collect deprecated images metric (CR-26713) (#138)
- Loading branch information
Showing
23 changed files
with
786 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ lib/state.json | |
.eslintrc.json | ||
test | ||
.eslintignore | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ state.json | |
.idea | ||
.vscode | ||
lib/manual-test.js | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
declare module 'cf-logs'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
while ($true) { | ||
Start-Sleep -s 1 | ||
& node lib/index.js | ||
& node dist/index.js | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import fastify from 'fastify'; | ||
import cfLogs from 'cf-logs'; | ||
|
||
import { saveServerAddress } from '../helpers'; | ||
|
||
// eslint-disable-next-line import/no-unresolved | ||
import deprecatedImagesCollector from '../metric/deprecated-images/deprecated-images.collector'; | ||
|
||
const logger = cfLogs.Logger('codefresh:containerLogger'); | ||
|
||
export class HttpServer { | ||
|
||
private readonly host; | ||
private readonly port; | ||
private readonly server; | ||
|
||
constructor(private taskLogger: any) { | ||
try { | ||
this.host = process.env.HOST || '0.0.0.0'; | ||
this.port = +(process.env.PORT || 8080); | ||
this.server = fastify(); | ||
|
||
this.initSecrets(); | ||
this.initDeprecatedImages(); | ||
} catch (error) { | ||
logger.error(`could not initialize server for engine's requests due to error: ${error}`); | ||
throw error; | ||
} | ||
} | ||
|
||
private initSecrets() { | ||
const secretsOptions = { | ||
schema: { | ||
body: { | ||
type: 'object', | ||
required: ['key', 'value'], | ||
properties: { | ||
key: { type: 'string' }, | ||
value: { type: 'string' }, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
this.server.post('/secrets', secretsOptions, async (request, reply) => { | ||
try { | ||
const { body }: { body: any } = request; | ||
const { secret } = body; | ||
logger.info(`got request to add new mask: ${secret.key}`); | ||
this.taskLogger.addNewMask(secret); | ||
reply.code(201); | ||
return 'secret added'; | ||
} catch (err) { | ||
logger.info(`could not create new mask for due to error: ${err}`); | ||
reply.code(500); | ||
throw err; | ||
} | ||
}); | ||
} | ||
|
||
private initDeprecatedImages() { | ||
this.server.get('/deprecated-images', async () => { | ||
logger.info(`got request to retrieve deprecated images`); | ||
return deprecatedImagesCollector.consumeAll(); | ||
}); | ||
|
||
this.server.post<{ Body: { count: number } }>('/deprecated-images/ack', async (request, reply) => { | ||
const { count } = request.body; | ||
|
||
if (typeof count !== 'number' || count < 1) { | ||
return reply.status(400).send({ error: 'You must provide a valid count (positive integer).' }); | ||
} | ||
|
||
deprecatedImagesCollector.destroyConsumed(count); | ||
|
||
return reply.status(200).send({ count }); | ||
}); | ||
} | ||
|
||
async start() { | ||
let address: string; | ||
try { | ||
address = await this.server.listen({ | ||
host: this.host, | ||
port: this.port, | ||
}); | ||
|
||
logger.info(`listening for engine's requests on ${address}`); | ||
} catch (error) { | ||
logger.error(`could not start server for engine updates due to error: ${error}`); | ||
throw error; | ||
} | ||
|
||
try { | ||
await saveServerAddress(address); | ||
} catch (error) { | ||
logger.error(`could not save server address due to error: ${error}`); | ||
throw error; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.