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

Commit

Permalink
feat: add terminationGracePeriodSeconds annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
pritamstyz4ever committed Oct 19, 2020
1 parent e17e362 commit 8c62d73
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/pod.cache2disk.yaml.tim
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ spec:
values:
- screwdriver-vm
restartPolicy: Never
terminationGracePeriodSeconds: {{termination_grace_period_seconds}}
containers:
- name: vm-launcher
image: {{base_image}}
Expand Down
1 change: 1 addition & 0 deletions config/pod.yaml.tim
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ spec:
values:
- screwdriver-vm
restartPolicy: Never
terminationGracePeriodSeconds: {{termination_grace_period_seconds}}
containers:
- name: vm-launcher
image: {{base_image}}
Expand Down
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const AFFINITY_PREFERRED_NODE_SELECTOR_PATH = 'spec.affinity.nodeAffinity.' +
'preferredDuringSchedulingIgnoredDuringExecution';
const PREFERRED_WEIGHT = 100;
const DISK_CACHE_STRATEGY = 'disk';
const TERMINATION_GRACE_PERIOD_SECONDS = 'terminationGracePeriodSeconds';

/**
* Parses nodeSelector config and update intended nodeSelector in tolerations
Expand Down Expand Up @@ -133,6 +134,7 @@ class K8sVMExecutor extends Executor {
* @param {String} [options.kubernetes.resources.disk.speed] Value for disk speed label (e.g.: screwdriver.cd/diskSpeed)
* @param {Number} [options.kubernetes.jobsNamespace=default] Pods namespace for Screwdriver Jobs
* @param {Object} [options.kubernetes.nodeSelectors] Object representing node label-value pairs
* @param {String} [options.kubernetes.terminationGracePeriodSeconds] TerminationGracePeriodSeconds setting for k8s pods
* @param {String} [options.launchVersion=stable] Launcher container version to use
* @param {String} [options.prefix=''] Prefix for job name
* @param {String} [options.fusebox] Options for the circuit breaker (https://github.com/screwdriver-cd/circuit-fuses)
Expand Down Expand Up @@ -168,6 +170,7 @@ class K8sVMExecutor extends Executor {
this.baseImage = this.kubernetes.baseImage;
this.buildTimeout = this.kubernetes.buildTimeout || DEFAULT_BUILD_TIMEOUT;
this.maxBuildTimeout = this.kubernetes.maxBuildTimeout || MAX_BUILD_TIMEOUT;
this.terminationGracePeriodSeconds = this.kubernetes.terminationGracePeriodSeconds || 60;
this.podsUrl = `https://${this.host}/api/v1/namespaces/${this.jobsNamespace}/pods`;
this.breaker = new Fusebox(requestretry, options.fusebox);
this.retryDelay = this.requestretryOptions.retryDelay || DEFAULT_RETRYDELAY;
Expand Down Expand Up @@ -320,6 +323,12 @@ class K8sVMExecutor extends Executor {
? Math.min(annotations[ANNOTATION_BUILD_TIMEOUT], this.maxBuildTimeout)
: this.buildTimeout;

const terminationGracePeriod = annotations[TERMINATION_GRACE_PERIOD_SECONDS]
? Math.max(
annotations[TERMINATION_GRACE_PERIOD_SECONDS], this.terminationGracePeriodSeconds
)
: this.terminationGracePeriodSeconds;

const podSpec = {
cpu,
memory,
Expand All @@ -337,6 +346,7 @@ class K8sVMExecutor extends Executor {
pushgateway_url: hoek.reach(this.ecosystem, 'pushgatewayUrl', { default: '' }),
token,
launcher_image: `${this.launchImage}:${this.launchVersion}`,
termination_grace_period_seconds: terminationGracePeriod,
launcher_version: this.launchVersion,
base_image: this.baseImage,
cache_strategy: this.cacheStrategy,
Expand Down
21 changes: 21 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ metadata:
command:
- "/opt/sd/launch {{api_uri}} {{store_uri}} {{token}} {{build_timeout}} {{build_id}}"
spec:
terminationGracePeriodSeconds: {{termination_grace_period_seconds}}
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
Expand Down Expand Up @@ -53,6 +54,7 @@ describe('index', () => {
const testLaunchVersion = 'stable';
const podsUrl = 'https://kubernetes.default/api/v1/namespaces/default/pods';
const testSpec = {
terminationGracePeriodSeconds: 60,
tolerations: [{
key: 'key',
value: 'value',
Expand Down Expand Up @@ -85,6 +87,7 @@ describe('index', () => {
effect: 'NoSchedule',
operator: 'Equal'
}],
terminationGracePeriodSeconds: 60,
affinity: {
nodeAffinity: {
requiredDuringSchedulingIgnoredDuringExecution: {
Expand All @@ -106,6 +109,7 @@ describe('index', () => {
}
};
const testSpecWithDiskSpeed = {
terminationGracePeriodSeconds: 60,
tolerations: [{
key: 'screwdriver.cd/diskspeed',
value: 'high',
Expand Down Expand Up @@ -138,6 +142,7 @@ describe('index', () => {
}
};
const testPreferredSpec = {
terminationGracePeriodSeconds: 60,
affinity: {
nodeAffinity: {
preferredDuringSchedulingIgnoredDuringExecution: [
Expand All @@ -163,6 +168,7 @@ describe('index', () => {
}
};
const testPodSpec = {
terminationGracePeriodSeconds: 60,
affinity: {
podAntiAffinity: {
preferredDuringSchedulingIgnoredDuringExecution: [
Expand Down Expand Up @@ -251,6 +257,7 @@ describe('index', () => {
host: 'kubernetes2',
jobsNamespace: 'baz',
baseImage: 'hyperctl',
terminationGracePeriodSeconds: 30,
resources: {
cpu: {
turbo: 10,
Expand Down Expand Up @@ -300,13 +307,15 @@ describe('index', () => {
assert.equal(executor.cacheMd5Check, 'true');
assert.equal(executor.cacheMaxSizeInMB, '2048');
assert.equal(executor.cacheMaxGoThreads, '10000');
assert.equal(executor.terminationGracePeriodSeconds, 30);
});

it('allow empty options', () => {
fsMock.existsSync.returns(false);
executor = new Executor();
assert.equal(executor.buildTimeout, DEFAULT_BUILD_TIMEOUT);
assert.equal(executor.maxBuildTimeout, MAX_BUILD_TIMEOUT);
assert.equal(executor.terminationGracePeriodSeconds, 60);
assert.equal(executor.launchVersion, 'stable');
assert.equal(executor.host, 'kubernetes.default');
assert.equal(executor.launchVersion, 'stable');
Expand Down Expand Up @@ -666,6 +675,16 @@ describe('index', () => {
});
});

it('sets the terminationGracePeriodSeconds appropriately when annotation is set', () => {
postConfig.body.spec.terminationGracePeriodSeconds = 60;
fakeStartConfig.annotations = { 'screwdriver.cd/terminationGracePeriodSeconds': 60 };

return executor.start(fakeStartConfig).then(() => {
assert.calledWith(requestRetryMock.firstCall, postConfig);
assert.calledWith(requestRetryMock.secondCall, sinon.match(getConfig));
});
});

it('sets tolerations and node affinity with appropriate node config', () => {
const spec = _.merge({}, testSpec, testPodSpec);
const options = _.assign({}, executorOptions, {
Expand Down Expand Up @@ -1029,6 +1048,7 @@ describe('index', () => {
beforeEach(() => {
nodeSelectors = null;
fakeConfig = yaml.safeLoad(TEST_TIM_YAML);
fakeConfig.spec.terminationGracePeriodSeconds = 60;
});

it('does nothing if nodeSelector is not set', () => {
Expand Down Expand Up @@ -1060,6 +1080,7 @@ describe('index', () => {
beforeEach(() => {
nodeSelectors = null;
fakeConfig = yaml.safeLoad(TEST_TIM_YAML);
fakeConfig.spec.terminationGracePeriodSeconds = 60;
});

it('does nothing if preferredNodeSelector is not set', () => {
Expand Down

0 comments on commit 8c62d73

Please sign in to comment.