Skip to content

Commit f98cad4

Browse files
authored
feat(ingress): create an Ingress if there is one in the .nodeshift directory (#244)
* feat: create an Ingress if there is one in the .nodeshift directory There is no enrichment happening here, for the moment you need to create the full Ingress yaml. fixes #238
1 parent e952c44 commit f98cad4

7 files changed

Lines changed: 134 additions & 2 deletions

File tree

lib/apply-resources.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const services = require('./services');
2222
const routes = require('./routes');
2323
const deploymentConfig = require('./deployment-config').deploy;
2424
const secrets = require('./secrets');
25+
const ingress = require('./ingress');
2526

2627
module.exports = (config, resourceList) => {
2728
const mappedResources = resourceList.map((r) => {
@@ -34,6 +35,8 @@ module.exports = (config, resourceList) => {
3435
return deploymentConfig(config, r);
3536
case 'Secret':
3637
return secrets(config, r);
38+
case 'Ingress':
39+
return ingress(config, r);
3740
default:
3841
return Promise.resolve(r);
3942
}

lib/goals/undeploy.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ module.exports = async function (config) {
6666
case 'Secret':
6767
response.secret = await config.openshiftRestClient.secrets.remove(item.metadata.name, removeOptions);
6868
break;
69+
case 'Ingress':
70+
response.ingress = await config.openshiftRestClient.ingress.remove(item.metadata.name, removeOptions);
71+
break;
6972
default:
7073
logger.error(`${item.kind} is not recognized`);
7174
}

lib/ingress.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
*
3+
* Copyright 2016-2017 Red Hat, Inc, and individual contributors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
'use strict';
20+
21+
const log = require('./common-log')();
22+
23+
module.exports = async function getIngress (config, ingressResource) {
24+
// First check to see if we already have a route created
25+
let ingress = await config.openshiftRestClient.ingress.find(ingressResource.metadata.name);
26+
if (ingress.code === 404) {
27+
// There isn't a ingress yet, so we need to create one
28+
log.info(`creating new ingress ${ingressResource.metadata.name}`);
29+
ingress = await config.openshiftRestClient.ingress.create(ingressResource);
30+
} else {
31+
log.info(`using existing ingress ${ingress.metadata.name}`);
32+
}
33+
return ingress;
34+
};

lib/resource-loader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ const kindMappings = {
3434
svc: 'Service',
3535
service: 'Service',
3636
secret: 'Secret',
37-
deployment: 'Deployment'
37+
deployment: 'Deployment',
38+
ingress: 'Ingress'
3839
};
3940

4041
function loadYaml (fileLocation) {

test/apply-resources-test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ test('apply resource test', (t) => {
2020
{ kind: 'Route' },
2121
{ kind: 'DeploymentConfig' },
2222
{ kind: 'Secret' },
23+
{ kind: 'Ingress' },
2324
{ kind: 'other' }
2425
];
2526

@@ -31,7 +32,8 @@ test('apply resource test', (t) => {
3132
'./deployment-config': {
3233
deploy: mockedPromiseResolve
3334
},
34-
'./secrets': mockedPromiseResolve
35+
'./secrets': mockedPromiseResolve,
36+
'./ingress': mockedPromiseResolve
3537
});
3638
const ar = applyResources({}, resourceList).then(() => {
3739
t.end();

test/goals/undeploy-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ test('return list items', (t) => {
6161
{
6262
kind: 'DeploymentConfig',
6363
metadata: metadata
64+
},
65+
{
66+
kind: 'Ingress',
67+
metadata: metadata
6468
}
6569
]
6670
};
@@ -80,6 +84,12 @@ test('return list items', (t) => {
8084
return Promise.resolve();
8185
}
8286
},
87+
ingress: {
88+
remove: (name) => {
89+
t.equal(name, metadata.name, 'name should be equal');
90+
return Promise.resolve();
91+
}
92+
},
8393
secrets: {
8494
remove: (name) => {
8595
t.equal(name, metadata.name, 'name should be equal');

test/ingress-test.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'use strict';
2+
3+
const test = require('tape');
4+
5+
test('test ingress, already created', (t) => {
6+
const ingress = require('../lib/ingress');
7+
const resource = {
8+
apiVersion: 'v1',
9+
kind: 'Service',
10+
metadata: {
11+
name: 'my route'
12+
},
13+
spec: {
14+
host: '192.168.1.1'
15+
}
16+
};
17+
18+
const config = {
19+
projectName: 'test-project',
20+
context: {
21+
namespace: 'namespace'
22+
},
23+
projectVersion: '1.0.0',
24+
openshiftRestClient: {
25+
ingress:
26+
{
27+
find: (name) => {
28+
if (name !== resource.metadata.name) {
29+
t.fail('name argument does not match the resource.metadata.name');
30+
}
31+
return { code: 200, metadata: { name: 'ingress' } };
32+
}
33+
}
34+
}
35+
};
36+
37+
const p = ingress(config, resource).then((service) => {
38+
t.equal(service.code, 200, 'ingress response code should be 200');
39+
t.end();
40+
});
41+
42+
t.equal(p instanceof Promise, true, 'should return a Promise');
43+
});
44+
45+
test('test ingress, not created', (t) => {
46+
const ingress = require('../lib/ingress');
47+
const resource = {
48+
apiVersion: 'extensions/v1beta1',
49+
kind: 'Ingress',
50+
metadata: {
51+
name: 'nodejs-istio-circuit-breaker-gateway'
52+
}
53+
};
54+
55+
const config = {
56+
projectName: 'test-project',
57+
context: {
58+
namespace: 'namespace'
59+
},
60+
projectVersion: '1.0.0',
61+
openshiftRestClient: {
62+
ingress:
63+
{
64+
find: (name) => {
65+
return { code: 404 };
66+
},
67+
create: ingress => ingress
68+
}
69+
}
70+
};
71+
72+
const p = ingress(config, resource).then((ingress) => {
73+
t.equal(ingress.kind, 'Ingress', 'is a Ingress Kind');
74+
t.equal(ingress.metadata.name, 'nodejs-istio-circuit-breaker-gateway', 'metadata.name should be nodejs-istio-circuit-breaker-gateway');
75+
t.end();
76+
});
77+
78+
t.equal(p instanceof Promise, true, 'should return a Promise');
79+
});

0 commit comments

Comments
 (0)