-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP Procedures for Kustomize-like overlays
- Loading branch information
Showing
6 changed files
with
150 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
const obj = (name, value) => { | ||
const o = {}; | ||
o[name] = value; | ||
return o; | ||
}; | ||
// dataFromFiles reads the contents of each file given and returns a | ||
// Map of the filenames to file contents. | ||
async function dataFromFiles(readEncoded, files) { | ||
const result = new Map(); | ||
await Promise.all(files.map(f => readEncoded(f).then(c => { | ||
result.set(f, c); | ||
}))); | ||
return result; | ||
} | ||
|
||
// Given `dir` and `read` procedures, construct a map of filename to | ||
// file contents (as strings). | ||
// Given `dir` and `read` procedures, construct a map of file basename | ||
// to file contents (as strings). | ||
const dataFromDir = ({ dir, read, Encoding }) => async function data(path) { | ||
const d = dir(path); | ||
const files = d.files.filter(({ isdir }) => !isdir); | ||
const data0 = files.map( | ||
({ name }) => read(`${path}/${name}`, { encoding: Encoding.String }) | ||
.then(str => obj(name, str)), | ||
); | ||
const data1 = await Promise.all(data0); | ||
return data1.reduce((a, b) => Object.assign(a, b), {}); | ||
const files = d.files.filter(({ isdir }) => !isdir).map(({ name }) => name); | ||
const readFile = f => read(`${path}/${f}`, { encoding: Encoding.String }); | ||
return dataFromFiles(readFile, files); | ||
}; | ||
|
||
export default dataFromDir; | ||
export { dataFromFiles, dataFromDir }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// This module provides procedures for applying Kustomize-like | ||
// [overlays](https://github.com/kubernetes-sigs/kustomize/blob/master/docs/kustomization.yaml). | ||
|
||
// In Kustomize, a configuration is given in a `kustomize.yaml` file; | ||
// here we'll interpret an object (which can of course be loaded from | ||
// a file). In a `kustomize.yaml` you refer to files from which to | ||
// load or generate resource manifests, and transformations to apply | ||
// to all or some resources. | ||
// | ||
// The mechanism for composing configurations is to name `bases` in | ||
// the `kustomize.yaml` file; these are evaluated and included in the | ||
// resources. | ||
// | ||
// Promise [Resource] | ||
// | ||
// There are different kinds of transformations, but they amount to | ||
// | ||
// Resource -> Resource | ||
// | ||
// The approach taken here is | ||
// 0. load the file | ||
// 1. assemble all the transformations mentioned in various ways in the kustomize object; | ||
// 2. assemble all the resources, mentioned in various ways, in the kustomize object; | ||
// 2. run each resource through the transformations. | ||
// | ||
// Easy peasy! | ||
|
||
// overlay constructs an interpreter which takes an overlay object (as | ||
// would be parsed from a `kustomize.yaml`) and constructs a set of | ||
// resources to write out. | ||
const overlay = ({ read, Encoding }) => async function assemble(path, config) { | ||
const readObj = f => read(`${path}/${f}`, { encoding: Encoding.JSON }); | ||
const { | ||
resources: resourceFiles = [], | ||
bases: baseFiles = [], | ||
patches: patchFiles = [], | ||
} = config; | ||
|
||
const transforms = []; | ||
patchFiles.forEach(f => { | ||
transforms.append(readObj(f).then(interpretPatch)); | ||
}); | ||
|
||
// TODO: add the other kinds of transformation: imageTags, | ||
// globalAnnotations, etc. | ||
|
||
let resources = []; | ||
baseFiles.forEach(f => { | ||
const obj = readObj(`${path}/${f}/kustomize.yaml`); | ||
resources = resources.concat(obj.then(o => assemble(`${path}/${f}`, o))); | ||
}); | ||
|
||
resourceFiles.forEach(f => resources.append(readObj(f))); | ||
return await Promise.all(resources); | ||
} | ||
|
||
export default overlay; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const dir = dirs => path => { | ||
if (path in dirs) { | ||
return dirs[path]; | ||
} | ||
throw new Error(`path not found ${path}`); | ||
}; | ||
|
||
const read = files => async function r(path, { encoding }) { | ||
if (path in files) { | ||
const encodings = files[path]; | ||
if (encoding in encodings) { | ||
return encodings[encoding]; | ||
} | ||
throw new Error(`no value for encoding "${encoding}"`); | ||
} | ||
throw new Error(`file not found ${path}`); | ||
}; | ||
|
||
function fs(dirs, files) { | ||
return { | ||
dir: dir(dirs), | ||
read: read(files), | ||
Encoding: Encoding, | ||
}; | ||
} | ||
|
||
// It's not important what the values are, just that we use them | ||
// consistently. | ||
const Encoding = Object.freeze({ | ||
String: 'string', | ||
JSON: 'json', | ||
Bytes: 'bytes', | ||
}); | ||
|
||
export { fs, Encoding }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import overlay from '../src/cons/overlay'; | ||
import { fs, Encoding } from './mock'; | ||
|
||
test('trivial overlay: no bases, resources, patches', () => { | ||
const o = overlay({}); | ||
const { dir, read } = fs({}, {}); | ||
expect.assertions(1); | ||
o('config', {dir, read }).then(v => { | ||
expect(v).toEqual([]); | ||
}); | ||
}); |