Skip to content
This repository has been archived by the owner on Sep 6, 2024. It is now read-only.

Commit

Permalink
feat(1167): Remove calling exchangeTokenForBuild method (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumada626 authored and tkyi committed Jul 30, 2018
1 parent 2eb128b commit 0f5ba9b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 90 deletions.
141 changes: 68 additions & 73 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,79 +205,74 @@ class K8sVMExecutor extends Executor {
? Math.min(annotations[ANNOTATION_BUILD_TIMEOUT], this.maxBuildTimeout)
: this.buildTimeout;

// exchange temporal JWT to build JWT
return this.exchangeTokenForBuild(config, buildTimeout).then((buildToken) => {
config.token = buildToken;

const podTemplate = tinytim.renderFile(
path.resolve(__dirname, './config/pod.yaml.tim'), {
cpu: CPU,
memory: MEMORY,
pod_name: `${this.prefix}${config.buildId}-${random}`,
build_id_with_prefix: `${this.prefix}${config.buildId}`,
build_id: config.buildId,
build_timeout: buildTimeout,
container: config.container,
api_uri: this.ecosystem.api,
store_uri: this.ecosystem.store,
pushgateway_url: hoek.reach(this.ecosystem, 'pushgatewayUrl', { default: '' }),
token: config.token,
launcher_version: this.launchVersion,
base_image: this.baseImage
});

const podConfig = yaml.safeLoad(podTemplate);

setNodeSelector(podConfig, this.nodeSelectors);
setPreferredNodeSelector(podConfig, this.preferredNodeSelectors);

const options = {
uri: this.podsUrl,
method: 'POST',
body: podConfig,
headers: { Authorization: `Bearer ${this.token}` },
strictSSL: false,
json: true
};

return this.breaker.runCommand(options)
.then((resp) => {
if (resp.statusCode !== 201) {
throw new Error(`Failed to create pod: ${JSON.stringify(resp.body)}`);
}

return resp.body.metadata.name;
})
.then((podname) => {
const statusOptions = {
uri: `${this.podsUrl}/${podname}/status`,
method: 'GET',
headers: { Authorization: `Bearer ${this.token}` },
strictSSL: false,
maxAttempts: MAXATTEMPTS,
retryDelay: RETRYDELAY,
retryStrategy: this.podRetryStrategy,
json: true
};

return this.breaker.runCommand(statusOptions);
})
.then((resp) => {
if (resp.statusCode !== 200) {
throw new Error('Failed to get pod status:' +
`${JSON.stringify(resp.body, null, 2)}`);
}

const status = resp.body.status.phase.toLowerCase();

if (status === 'failed' || status === 'unknown') {
throw new Error('Failed to create pod. Pod status is:' +
`${JSON.stringify(resp.body.status, null, 2)}`);
}

return null;
});
});
const podTemplate = tinytim.renderFile(
path.resolve(__dirname, './config/pod.yaml.tim'), {
cpu: CPU,
memory: MEMORY,
pod_name: `${this.prefix}${config.buildId}-${random}`,
build_id_with_prefix: `${this.prefix}${config.buildId}`,
build_id: config.buildId,
build_timeout: buildTimeout,
container: config.container,
api_uri: this.ecosystem.api,
store_uri: this.ecosystem.store,
pushgateway_url: hoek.reach(this.ecosystem, 'pushgatewayUrl', { default: '' }),
token: config.token,
launcher_version: this.launchVersion,
base_image: this.baseImage
});

const podConfig = yaml.safeLoad(podTemplate);

setNodeSelector(podConfig, this.nodeSelectors);
setPreferredNodeSelector(podConfig, this.preferredNodeSelectors);

const options = {
uri: this.podsUrl,
method: 'POST',
body: podConfig,
headers: { Authorization: `Bearer ${this.token}` },
strictSSL: false,
json: true
};

return this.breaker.runCommand(options)
.then((resp) => {
if (resp.statusCode !== 201) {
throw new Error(`Failed to create pod: ${JSON.stringify(resp.body)}`);
}

return resp.body.metadata.name;
})
.then((podname) => {
const statusOptions = {
uri: `${this.podsUrl}/${podname}/status`,
method: 'GET',
headers: { Authorization: `Bearer ${this.token}` },
strictSSL: false,
maxAttempts: MAXATTEMPTS,
retryDelay: RETRYDELAY,
retryStrategy: this.podRetryStrategy,
json: true
};

return this.breaker.runCommand(statusOptions);
})
.then((resp) => {
if (resp.statusCode !== 200) {
throw new Error('Failed to get pod status:' +
`${JSON.stringify(resp.body, null, 2)}`);
}

const status = resp.body.status.phase.toLowerCase();

if (status === 'failed' || status === 'unknown') {
throw new Error('Failed to create pod. Pod status is:' +
`${JSON.stringify(resp.body.status, null, 2)}`);
}

return null;
});
}

/**
Expand Down
22 changes: 5 additions & 17 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('index', () => {
let fsMock;
let executor;
const testBuildId = 15;
let testToken = 'abcdefg';
const testToken = 'abcdefg';
const testApiUri = 'http://api:8080';
const testStoreUri = 'http://store:8080';
const testContainer = 'node:4';
Expand Down Expand Up @@ -375,7 +375,7 @@ describe('index', () => {
},
spec: testPodSpec,
command: [
'/opt/sd/launch http://api:8080 http://store:8080 someBuildToken '
'/opt/sd/launch http://api:8080 http://store:8080 abcdefg '
+ `${DEFAULT_BUILD_TIMEOUT} 15`
]
},
Expand Down Expand Up @@ -411,13 +411,6 @@ describe('index', () => {
null, fakeStartResponse, fakeStartResponse.body);
requestRetryMock.withArgs(sinon.match({ method: 'GET' })).yieldsAsync(
null, fakeGetResponse, fakeGetResponse.body);

exchangeTokenStub = sinon.stub(executor, 'exchangeTokenForBuild');
exchangeTokenStub.resolves('someBuildToken');
});

afterEach(() => {
testToken = 'abcdefg';
});

it('successfully calls start', () =>
Expand Down Expand Up @@ -491,8 +484,6 @@ describe('index', () => {
},
prefix: 'beta_'
});
exchangeTokenStub = sinon.stub(executor, 'exchangeTokenForBuild');
exchangeTokenStub.resolves('someBuildToken');

getConfig.retryStrategy = executor.podRetryStrategy;

Expand Down Expand Up @@ -550,9 +541,6 @@ describe('index', () => {
}
});

exchangeTokenStub = sinon.stub(executor, 'exchangeTokenForBuild');
exchangeTokenStub.resolves('someBuildToken');

getConfig.retryStrategy = executor.podRetryStrategy;

return executor.start(fakeStartConfig).then(() => {
Expand Down Expand Up @@ -639,7 +627,7 @@ describe('index', () => {

it('sets the build timeout to default build timeout if not configured by user', () => {
postConfig.body.command = [
'/opt/sd/launch http://api:8080 http://store:8080 someBuildToken '
'/opt/sd/launch http://api:8080 http://store:8080 abcdefg '
+ `${DEFAULT_BUILD_TIMEOUT} 15`
];

Expand All @@ -653,7 +641,7 @@ describe('index', () => {
const userTimeout = 45;

postConfig.body.command = [
`/opt/sd/launch http://api:8080 http://store:8080 someBuildToken ${userTimeout} 15`
`/opt/sd/launch http://api:8080 http://store:8080 abcdefg ${userTimeout} 15`
];
fakeStartConfig.annotations = { 'beta.screwdriver.cd/timeout': userTimeout };

Expand All @@ -666,7 +654,7 @@ describe('index', () => {
it('sets the timeout to maxBuildTimeout if user specified a higher timeout', () => {
fakeStartConfig.annotations = { 'beta.screwdriver.cd/timeout': 220 };
postConfig.body.command = [
'/opt/sd/launch http://api:8080 http://store:8080 someBuildToken '
'/opt/sd/launch http://api:8080 http://store:8080 abcdefg '
+ `${MAX_BUILD_TIMEOUT} 15`
];

Expand Down

0 comments on commit 0f5ba9b

Please sign in to comment.