Skip to content

Commit 046ab0a

Browse files
committed
chore: extract snyk-monitor deployment in tests
Similarly to our platforms module, create a module that handles the deployment of the snyk-monitor. Today there are two ways to deploy the product: with YAMLs and with a Helm chart. The goal of this deployment module is to add support for Helm deployments in the future, and for Operators. This is the first step of organising the code, preserving the original flow, without extra refactoring.
1 parent 2a5e909 commit 046ab0a

File tree

4 files changed

+101
-63
lines changed

4 files changed

+101
-63
lines changed

test/setup/deployers/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as yaml from './yaml';
2+
import { DeploymentType } from './types';
3+
4+
interface IDeployer {
5+
deploy: (
6+
integrationId: string,
7+
imageOpts: {
8+
imageNameAndTag: string;
9+
imagePullPolicy: string;
10+
},
11+
) => Promise<void>;
12+
}
13+
14+
const yamlDeployer: IDeployer = {
15+
deploy: yaml.deployKubernetesMonitor,
16+
};
17+
18+
export default {
19+
[DeploymentType.YAML]: yamlDeployer,
20+
};

test/setup/deployers/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export enum DeploymentType {
2+
YAML,
3+
Helm,
4+
Operator,
5+
}

test/setup/deployers/yaml.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { readFileSync, writeFileSync } from 'fs';
2+
import { parse, stringify } from 'yaml';
3+
4+
import * as kubectl from '../../helpers/kubectl';
5+
6+
export async function deployKubernetesMonitor(
7+
integrationId: string,
8+
imageOpts: {
9+
imageNameAndTag: string;
10+
imagePullPolicy: string;
11+
},
12+
): Promise<void> {
13+
const namespace = 'snyk-monitor';
14+
await kubectl.createNamespace(namespace);
15+
16+
const secretName = 'snyk-monitor';
17+
const gcrDockercfg = process.env['GCR_IO_DOCKERCFG'] || '{}';
18+
await kubectl.createSecret(secretName, namespace, {
19+
'dockercfg.json': gcrDockercfg,
20+
integrationId,
21+
});
22+
23+
const testYaml = 'snyk-monitor-test-deployment.yaml';
24+
createTestYamlDeployment(testYaml, integrationId, imageOpts.imageNameAndTag, imageOpts.imagePullPolicy);
25+
26+
await kubectl.applyK8sYaml('./snyk-monitor-cluster-permissions.yaml');
27+
await kubectl.applyK8sYaml('./snyk-monitor-test-deployment.yaml');
28+
}
29+
30+
function createTestYamlDeployment(
31+
newYamlPath: string,
32+
integrationId: string,
33+
imageNameAndTag: string,
34+
imagePullPolicy: string,
35+
): void {
36+
console.log('Creating YAML snyk-monitor deployment...');
37+
const originalDeploymentYaml = readFileSync('./snyk-monitor-deployment.yaml', 'utf8');
38+
const deployment = parse(originalDeploymentYaml);
39+
40+
deployment.spec.template.spec.containers[0].image = imageNameAndTag;
41+
deployment.spec.template.spec.containers[0].imagePullPolicy = imagePullPolicy;
42+
43+
// This is important due to an odd bug when running on Travis.
44+
// By adding the Google nameserver, the container can start resolving external hosts.
45+
deployment.spec.template.spec.dnsConfig = {
46+
nameservers: ['8.8.8.8'],
47+
};
48+
49+
// Inject the integration ID that will be used throughout the integration tests.
50+
deployment.spec.template.spec.containers[0].env[0] = {
51+
name: 'SNYK_INTEGRATION_ID',
52+
value: integrationId,
53+
};
54+
55+
// Inject the baseUrl of kubernetes-upstream that snyk-monitor container use to send metadata
56+
deployment.spec.template.spec.containers[0].env[2] = {
57+
name: 'SNYK_INTEGRATION_API',
58+
value: 'https://kubernetes-upstream.dev.snyk.io',
59+
};
60+
61+
writeFileSync(newYamlPath, stringify(deployment));
62+
console.log('Created YAML snyk-monitor deployment');
63+
}

test/setup/index.ts

Lines changed: 13 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { readFileSync, writeFileSync } from 'fs';
21
import * as sleep from 'sleep-promise';
32
import * as uuidv4 from 'uuid/v4';
4-
import { parse, stringify } from 'yaml';
3+
54
import platforms from './platforms';
5+
import deployers from './deployers';
66
import * as kubectl from '../helpers/kubectl';
77
import * as waiters from './waiters';
8+
import { DeploymentType } from './deployers/types';
89

910
const testPlatform = process.env['TEST_PLATFORM'] || 'kind';
1011
const createCluster = process.env['CREATE_CLUSTER'] === 'true';
@@ -22,41 +23,6 @@ function getEnvVariableOrDefault(envVarName: string, defaultValue: string): stri
2223
: value;
2324
}
2425

25-
function createTestYamlDeployment(
26-
newYamlPath: string,
27-
integrationId: string,
28-
imageNameAndTag: string,
29-
imagePullPolicy: string,
30-
): void {
31-
console.log('Creating test deployment...');
32-
const originalDeploymentYaml = readFileSync('./snyk-monitor-deployment.yaml', 'utf8');
33-
const deployment = parse(originalDeploymentYaml);
34-
35-
deployment.spec.template.spec.containers[0].image = imageNameAndTag;
36-
deployment.spec.template.spec.containers[0].imagePullPolicy = imagePullPolicy;
37-
38-
// This is important due to an odd bug when running on Travis.
39-
// By adding the Google nameserver, the container can start resolving external hosts.
40-
deployment.spec.template.spec.dnsConfig = {
41-
nameservers: ['8.8.8.8'],
42-
};
43-
44-
// Inject the integration ID that will be used throughout the integration tests.
45-
deployment.spec.template.spec.containers[0].env[0] = {
46-
name: 'SNYK_INTEGRATION_ID',
47-
value: integrationId,
48-
};
49-
50-
// Inject the baseUrl of kubernetes-upstream that snyk-monitor container use to send metadata
51-
deployment.spec.template.spec.containers[0].env[2] = {
52-
name: 'SNYK_INTEGRATION_API',
53-
value: 'https://kubernetes-upstream.dev.snyk.io',
54-
};
55-
56-
writeFileSync(newYamlPath, stringify(deployment));
57-
console.log('Created test deployment');
58-
}
59-
6026
export async function removeMonitor(): Promise<void> {
6127
try {
6228
if (createCluster) {
@@ -96,30 +62,6 @@ async function createSecretForGcrIoAccess(): Promise<void> {
9662
);
9763
}
9864

99-
async function installKubernetesMonitor(
100-
imageNameAndTag: string,
101-
imagePullPolicy: string,
102-
): Promise<string> {
103-
const namespace = 'snyk-monitor';
104-
await kubectl.createNamespace(namespace);
105-
106-
const secretName = 'snyk-monitor';
107-
const integrationId = getIntegrationId();
108-
const gcrDockercfg = getEnvVariableOrDefault('GCR_IO_DOCKERCFG', '{}');
109-
await kubectl.createSecret(secretName, namespace, {
110-
'dockercfg.json': gcrDockercfg,
111-
integrationId,
112-
});
113-
114-
const testYaml = 'snyk-monitor-test-deployment.yaml';
115-
createTestYamlDeployment(testYaml, integrationId, imageNameAndTag, imagePullPolicy);
116-
117-
await kubectl.applyK8sYaml('./snyk-monitor-cluster-permissions.yaml');
118-
await kubectl.applyK8sYaml('./snyk-monitor-test-deployment.yaml');
119-
120-
return integrationId;
121-
}
122-
12365
export async function deployMonitor(): Promise<string> {
12466
console.log('Begin deploying the snyk-monitor...');
12567

@@ -145,10 +87,18 @@ export async function deployMonitor(): Promise<string> {
14587
await createEnvironment();
14688
await createSecretForGcrIoAccess();
14789

90+
const integrationId = getIntegrationId();
91+
14892
// TODO: hack, rewrite this
14993
const imagePullPolicy = testPlatform === 'kind' ? 'Never' : 'Always';
150-
151-
const integrationId = await installKubernetesMonitor(remoteImageName, imagePullPolicy);
94+
const deploymentImageOptions = {
95+
imageNameAndTag: remoteImageName,
96+
imagePullPolicy,
97+
};
98+
await deployers[DeploymentType.YAML].deploy(
99+
integrationId,
100+
deploymentImageOptions,
101+
);
152102
await waiters.waitForMonitorToBeReady();
153103
console.log(`Deployed the snyk-monitor with integration ID ${integrationId}`);
154104
return integrationId;

0 commit comments

Comments
 (0)