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

Automatic cleanup of JuiceShop on uninstall #29

Merged
merged 4 commits into from
Jan 8, 2020
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
34 changes: 32 additions & 2 deletions juice-balancer/src/kubernetes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ const k8sCoreApi = kc.makeApiClient(CoreV1Api);

const { get } = require('./config');

const createDeploymentForTeam = ({ team }) =>
const lodashGet = require('lodash/get');
const once = require('lodash/once');

// Gets the Deployment uid for the JuiceBalancer
// This is required to set the JuiceBalancer as owner of the created JuiceShop Instances
const getJuiceBalancerDeploymentUid = once(async () => {
const deployment = await k8sAppsApi.readNamespacedDeployment('juice-balancer', get('namespace'));
return lodashGet(deployment, ['body', 'metadata', 'uid'], null);
});

const createDeploymentForTeam = async ({ team }) =>
k8sAppsApi
.createNamespacedDeployment(get('namespace'), {
metadata: {
Expand All @@ -17,6 +27,16 @@ const createDeploymentForTeam = ({ team }) =>
team,
'deployment-context': get('deploymentContext'),
},
ownerReferences: [
{
apiVersion: 'apps/v1',
blockOwnerDeletion: true,
controller: true,
kind: 'Deployment',
name: 'juice-balancer',
uid: await getJuiceBalancerDeploymentUid(),
},
],
},
spec: {
selector: {
Expand Down Expand Up @@ -128,7 +148,7 @@ const createDeploymentForTeam = ({ team }) =>

module.exports.createDeploymentForTeam = createDeploymentForTeam;

const createServiceForTeam = teamname =>
const createServiceForTeam = async teamname =>
k8sCoreApi
.createNamespacedService(get('namespace'), {
metadata: {
Expand All @@ -138,6 +158,16 @@ const createServiceForTeam = teamname =>
team: teamname,
'deployment-context': get('deploymentContext'),
},
ownerReferences: [
{
apiVersion: 'apps/v1',
blockOwnerDeletion: true,
controller: true,
kind: 'Deployment',
name: 'juice-balancer',
uid: await getJuiceBalancerDeploymentUid(),
},
],
},
spec: {
selector: {
Expand Down
2 changes: 0 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ helm install -f values.yaml juicy-ctf ./juicy-ctf/helm/juicy-ctf/

```sh
helm delete juicy-ctf
# Also delete all Juice Shop Deployments which still exist
kubectl delete deployment --selector app=juice-shop && kubectl delete service --selector app=juice-shop
```

## FAQ
Expand Down