Skip to content

Commit

Permalink
fix: set k8s annotations if configured
Browse files Browse the repository at this point in the history
  • Loading branch information
minzcmu committed Aug 14, 2018
1 parent a1b775a commit 0f14500
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ artifacts/
npm-debug.log
.DS_STORE
.*.swp
.nyc_output
17 changes: 17 additions & 0 deletions index.js
Expand Up @@ -22,6 +22,21 @@ const AFFINITY_NODE_SELECTOR_PATH = 'spec.affinity.nodeAffinity.' +
const AFFINITY_PREFERRED_NODE_SELECTOR_PATH = 'spec.affinity.nodeAffinity.' +
'preferredDuringSchedulingIgnoredDuringExecution';
const PREFERRED_WEIGHT = 100;
const ANNOTATIONS_PATH = 'metadata.annotations';

/**
* Parses annotations config and update intended annotations
* @param {Object} podConfig k8s pod config
* @param {Object} annotations key-value pairs of annotations
*/
function setAnnotations(podConfig, annotations) {
if (!annotations || typeof annotations !== 'object' ||
Object.keys(annotations).length === 0) {
return;
}

_.set(podConfig, ANNOTATIONS_PATH, annotations);
}

/**
* Parses nodeSelector config and update intended nodeSelector in tolerations
Expand Down Expand Up @@ -153,6 +168,7 @@ class K8sExecutor extends Executor {
this.microMemory = hoek.reach(options, 'kubernetes.resources.memory.micro', { default: 1 });
this.nodeSelectors = hoek.reach(options, 'kubernetes.nodeSelectors');
this.preferredNodeSelectors = hoek.reach(options, 'kubernetes.preferredNodeSelectors');
this.annotations = hoek.reach(options, 'kubernetes.annotations');
}

/**
Expand Down Expand Up @@ -206,6 +222,7 @@ class K8sExecutor extends Executor {

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

const options = {
uri: this.podsUrl,
Expand Down
27 changes: 27 additions & 0 deletions test/index.test.js
Expand Up @@ -88,6 +88,12 @@ describe('index', function () {
}
}
};
const testAnnotations = {
annotations: {
key: 'value',
key2: 'value2'
}
};

before(() => {
mockery.enable({
Expand Down Expand Up @@ -432,6 +438,27 @@ describe('index', function () {
});
});

it('sets annotations with appropriate annotations config', () => {
postConfig.json.metadata.annotations = testAnnotations.annotations;

executor = new Executor({
ecosystem: {
api: testApiUri,
store: testStoreUri
},
fusebox: { retry: { minTimeout: 1 } },
prefix: 'beta_',
kubernetes: {
annotations: { key: 'value', key2: 'value2' }
}
});

return executor.start(fakeStartConfig).then(() => {
assert.calledOnce(requestMock);
assert.calledWith(requestMock, postConfig);
});
});

it('sets tolerations and node affinity with appropriate node config', () => {
postConfig.json.spec = testSpec;

Expand Down

0 comments on commit 0f14500

Please sign in to comment.