Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
feat: allow setting additional markup on generated secret resource us…
Browse files Browse the repository at this point in the history
…ing template (#192)

The template will be deep merged without mutating any existing fields. For example: you cannot override metadata.name.

```yaml
apiVersion: 'kubernetes-client.io/v1'
kind: ExternalSecret
metadata:
  name: hello-service
secretDescriptor:
  template:
    metadata:
      annotations:
        cat: cheese
      labels:
        dog: farfel
  data:
    ...
```
  • Loading branch information
iAnomaly authored and Flydiverny committed Nov 5, 2019
1 parent 90f01c5 commit 25e2f74
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 7 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ secretDescriptor:
data:
- key: hello-service/password
name: password
# optional: specify a template with any additional markup you would like added to the downstream Secret resource.
# This template will be deep merged without mutating any existing fields. For example: you cannot override metadata.name.
template:
metadata:
annotations:
cat: cheese
labels:
dog: farfel
```
or
```yml
Expand Down Expand Up @@ -198,6 +206,10 @@ apiVersion: v1
kind: Secret
metadata:
name: hello-service
annotations:
cat: cheese
labels:
dog: farfel
type: Opaque
data:
password: MTIzNA==
Expand Down
13 changes: 11 additions & 2 deletions lib/poller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict'

const clonedeep = require('lodash.clonedeep')
const merge = require('lodash.merge')

/**
* Kubernetes secret descriptor.
* @typedef {Object} SecretDescriptor
Expand Down Expand Up @@ -72,10 +75,10 @@ class Poller {
*/
async _createSecretManifest () {
const secretDescriptor = this._secretDescriptor
const template = secretDescriptor.template
const data = await this._backends[secretDescriptor.backendType]
.getSecretManifestData({ secretDescriptor })

return {
let secretManifest = {
apiVersion: 'v1',
kind: 'Secret',
metadata: {
Expand All @@ -87,6 +90,12 @@ class Poller {
type: secretDescriptor.type || 'Opaque',
data
}

if (template) {
secretManifest = merge(clonedeep(template), secretManifest)
}

return secretManifest
}

/**
Expand Down
73 changes: 73 additions & 0 deletions lib/poller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,79 @@ describe('Poller', () => {
}
})
})

it('creates secret manifest - with template', async () => {
const poller = pollerFactory({
type: 'dummy-test-type',
backendType: 'fakeBackendType',
name: 'fakeSecretName',
properties: [
'fakePropertyName1',
'fakePropertyName2'
],
template: {
metadata: {
annotations: {
cat: 'cheese'
},
labels: {
dog: 'farfel'
},
name: 'fakerSecretName'
}
}
})

backendMock.getSecretManifestData.resolves({
fakePropertyName1: 'ZmFrZVByb3BlcnR5VmFsdWUx', // base 64 value
fakePropertyName2: 'ZmFrZVByb3BlcnR5VmFsdWUy' // base 64 value
})

const secretManifest = await poller._createSecretManifest()

expect(backendMock.getSecretManifestData.calledWith({
secretDescriptor: {
type: 'dummy-test-type',
backendType: 'fakeBackendType',
name: 'fakeSecretName',
properties: [
'fakePropertyName1',
'fakePropertyName2'
],
template: {
metadata: {
annotations: {
cat: 'cheese'
},
labels: {
dog: 'farfel'
},
name: 'fakerSecretName'
}
}
}
})).to.equal(true)

expect(secretManifest).deep.equals({
apiVersion: 'v1',
kind: 'Secret',
metadata: {
name: 'fakeSecretName',
ownerReferences: [getOwnerReference()],
annotations: {
cat: 'cheese'
},
labels: {
dog: 'farfel'
}
},
type: 'dummy-test-type',
data: {
fakePropertyName1: 'ZmFrZVByb3BlcnR5VmFsdWUx', // base 64 value
fakePropertyName2: 'ZmFrZVByb3BlcnR5VmFsdWUy' // base 64 value
}
})
})
})

describe('_poll', () => {
Expand Down
24 changes: 19 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"json-stream": "^1.0.0",
"kubernetes-client": "^8.3.0",
"lodash.clonedeep": "^4.5.0",
"lodash.merge": "^4.6.2",
"make-promises-safe": "^5.0.0",
"pino": "^5.12.0",
"prom-client": "^11.5.3"
Expand Down

0 comments on commit 25e2f74

Please sign in to comment.