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

adds runmode flag to ECR repo #25

Merged
merged 2 commits into from
Feb 12, 2024
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
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,18 @@ Mocks calls to an ECR repository
- Gets manifest
- Gets minimal layers

An admin endpoint is provided to simulate a new image being pushed `POST /_admin/trigger-ecr-push/{repo}/{tag}`
## Default Data Set

See `~/src/config/mock-data.js` for the starting data set. As the stub is used thse records are updated. Note: these are
NOT currently persistent across restarts.

## Admin Endpoints

`POST /_admin/trigger-ecr-push/{repo}/{tag}?runMode=service` - simulates a docker image being published. runmode can be service or job.

`GET /_admin/oidc/sessions` - dumps the current ODIC sessions

`GET /_admin/data` - dumps the current state of the mock github/ecr/tenant data

## Setup

Expand Down Expand Up @@ -122,6 +133,8 @@ export GITHUB_BASE_URL=http://localhost:3939
export OIDC_WELL_KNOWN_CONFIGURATION_URL=http://localhost:3939/63983fc2-cfff-45bb-8ec2-959e21062b9a/v2.0/.well-known/openid-configuration
```

(Note: the OIDC url can be replaced with any value as long as its the same across local services, in this example we've used a random UUID)

Override using `npm` scripts

```bash
Expand Down Expand Up @@ -168,5 +181,5 @@ AZURE_CLIENT_SECRET=test_value GITHUB_BASE_URL=http://localhost:3939 OIDC_WELL_

## Test Data

The base set of services is held in /config/services.js. Adding or removing services to this list will result in them
The base set of services is held in /config/mock-data.js. Adding or removing services to this list will result in them
being returned in the mock API calls.
6 changes: 4 additions & 2 deletions src/api/admin/controllers/data-controller.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { ecrRepos, githubRepos, tenantServices } from '~/src/config/services'
import { ecrRepos, githubRepos, tenantServices } from '~/src/config/mock-data'
import { sessions } from '~/src/api/oidc/helpers/session-store'

const dataController = {
handler: async (request, h) => {
const data = {
github: githubRepos,
ecr: ecrRepos,
tenants: tenantServices
tenants: tenantServices,
sessions
}

return h.response(data).code(200)
Expand Down
10 changes: 7 additions & 3 deletions src/api/admin/controllers/trigger-ecr-push.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
// Simulates a new image being pushed to the ECR registry
import { config } from '~/src/config'
import { SendMessageCommand } from '@aws-sdk/client-sqs'
import { ecrRepos } from '~/src/config/services'
import { ecrRepos } from '~/src/config/mock-data'

const triggerEcrPush = {
handler: async (request, h) => {
const repo = request.params.repo
const tag = request.params.tag
const runMode = request.query.runMode ? request.query.runMode : 'service'

if (ecrRepos[repo] === undefined) {
ecrRepos[repo] = []
ecrRepos[repo] = {
runMode,
tags: []
}
}

ecrRepos[repo].push(tag)
ecrRepos[repo].tags.push(tag)

const payload = JSON.stringify(generateMessage(repo, tag))
const msg = {
Expand Down
21 changes: 13 additions & 8 deletions src/api/ecr/controllers/blobs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ecrRepos } from '~/src/config/services'
import { ecrRepos } from '~/src/config/mock-data'
import * as crypto from 'crypto'
import { config } from '~/src/config'

Expand Down Expand Up @@ -41,12 +41,16 @@ const configBlobs = () => {
const org = config.get('githubOrg')

const sha256 = crypto.createHash('sha256').update(s).digest('hex')
configBlobs[`sha256:${sha256}`] = generateConfig(org, s)
configBlobs[`sha256:${sha256}`] = generateConfig(
org,
s,
ecrRepos[s].runMode
)
})
return configBlobs
}

const generateConfig = (org, service) => {
const generateConfig = (org, service, runMode) => {
return {
architecture: 'amd64',
config: {
Expand All @@ -73,7 +77,8 @@ const generateConfig = (org, service) => {
OnBuild: null,
Labels: {
'defra.cdp.git.repo.url': `https://github.com/${org}/${service}`,
'defra.cdp.service.name': service
'defra.cdp.service.name': service,
'defra.cdp.run_mode': runMode
}
},
container:
Expand Down Expand Up @@ -106,13 +111,13 @@ const generateConfig = (org, service) => {
Entrypoint: ['dotnet', 'Defra.Cdp.Deployments.dll'],
OnBuild: null,
Labels: {
'defra.cdp.git.repo.url':
'https://github.com/defra-cdp-sandpit/cdp-deployments',
'defra.cdp.service.name': 'cdp-deployments'
'defra.cdp.git.repo.url': `https://github.com/${org}/${service}`,
'defra.cdp.service.name': service,
'defra.cdp.run_mode': runMode
}
},
created: '2023-04-11T13:32:11.321678253Z',
docker_version: '20.10.23+azure-2',
docker_version: '20.10.23',
history: [],
os: 'linux',
rootfs: {
Expand Down
6 changes: 3 additions & 3 deletions src/api/ecr/controllers/tags.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { ecrRepos } from '~/src/config/services'
import { ecrRepos } from '~/src/config/mock-data'

const tagsController = {
handler: async (request, h) => {
const repo = request.params.repo

if (ecrRepos[repo] === undefined) {
if (ecrRepos[repo]?.tags === undefined) {
return h.response({ message: 'unknown repo' }).code(404)
}

const payload = {
name: repo,
tags: ecrRepos[repo]
tags: ecrRepos[repo]?.tags
}

return h.response(JSON.stringify(payload)).code(200)
Expand Down
2 changes: 1 addition & 1 deletion src/api/github/content/teams-and-repos.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { githubRepos } from '~/src/config/services'
import { githubRepos } from '~/src/config/mock-data'

const teams = [
{
Expand Down
2 changes: 1 addition & 1 deletion src/api/github/content/tfsvcinfra.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { tenantServices } from '~/src/config/services'
import { tenantServices } from '~/src/config/mock-data'

const getTfSvcInfraFile = (path) => {
if (path.endsWith('tenant_services.json')) {
Expand Down
2 changes: 1 addition & 1 deletion src/api/github/controllers/gitTree.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { tenantServices } from '~/src/config/services'
import { tenantServices } from '~/src/config/mock-data'

const gitTreeController = {
handler: async (request, h) => {
Expand Down
13 changes: 12 additions & 1 deletion src/api/github/controllers/trigger-workflow.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { triggerCreateRepoWorkflow } from '~/src/api/github/events/trigger-create-repo-workflow'
import {
ecrRepos,
githubRepos,
topicsService,
topicsTestSuite
} from '~/src/config/services'
} from '~/src/config/mock-data'

const triggerWorkflow = {
handler: async (request, h) => {
Expand All @@ -24,11 +25,21 @@ const triggerWorkflow = {
if (workflowFile === 'create_microservice.yml') {
request.logger.info(`Adding service ${repositoryName} to github repos`)
githubRepos.push({ name: repositoryName, topics: topicsService })

// technically this should happen on the TF-svc stage but its easier to
// detect the service type+name here
if (ecrRepos[repositoryName] === undefined) {
ecrRepos[repositoryName] = { tags: [], runMode: 'service' }
}
}

if (workflowFile === 'create_env_test_suite.yml') {
request.logger.info(`Adding test suite ${repositoryName} to github repos`)
githubRepos.push({ name: repositoryName, topics: topicsTestSuite })

if (ecrRepos[repositoryName] === undefined) {
ecrRepos[repositoryName] = { tags: [], runMode: 'job' }
}
}

return h.response({}).code(200)
Expand Down
20 changes: 16 additions & 4 deletions src/config/services.js → src/config/mock-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,22 @@ const githubRepos = [
]

const ecrRepos = {
'cdp-portal-frontend': ['0.1.0', '0.2.0', '0.3.0'],
'cdp-portal-backend': ['0.1.0', '0.2.0', '0.3.0'],
'cdp-self-service-ops': ['0.1.0', '0.2.0', '0.3.0'],
'cdp-user-service': ['0.1.0', '0.2.0', '0.3.0']
'cdp-portal-frontend': {
runMode: 'service',
tags: ['0.1.0', '0.2.0', '0.3.0']
},
'cdp-portal-backend': {
runMode: 'service',
tags: ['0.1.0', '0.2.0', '0.3.0']
},
'cdp-self-service-ops': {
runMode: 'service',
tags: ['0.1.0', '0.2.0', '0.3.0']
},
'cdp-user-service': {
runMode: 'service',
tags: ['0.1.0', '0.2.0', '0.3.0']
}
}

export { tenantServices, githubRepos, ecrRepos, topicsTestSuite, topicsService }
Loading