Created
May 24, 2018 09:14
-
-
Save brancz/d846b735c67de3df651dfe1f8dc01327 to your computer and use it in GitHub Desktop.
Bin-packing a large object into smaller enumerated objects with max size in jsonnet.
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
local cm = { | |
binPack(data, maxSize, nameTemplate):: | |
self.innerBinPack(data, maxSize, nameTemplate, std.objectFields(data), maxSize, 0), | |
innerBinPack(data, maxSize, nameTemplate, items, remainingSize, count):: | |
if std.length(items) == 0 then | |
{} | |
else | |
local curName = items[0]; | |
local cur = { [curName]: data[curName] }; | |
// Size is the sum of the length of the key + length of the content plus | |
// the yaml divider ": " of `key: value`. This is likely not 100% correct | |
// as objects may be converted to different formats in the process thus | |
// their actual size may differ. | |
local curSize = std.length(curName) + std.length(cur) + 2; | |
if (remainingSize - curSize) < 0 then | |
self.innerBinPack(data, maxSize, nameTemplate, items, maxSize, count + 1) tailstrict | |
else | |
{ [nameTemplate + count]+: cur } + self.innerBinPack(data, maxSize, nameTemplate, items[1:], remainingSize - curSize, count) tailstrict, | |
}; | |
local data = { | |
'1': 'a', | |
'2': 'a', | |
'3': 'a', | |
'4': 'a', | |
'5': 'a', | |
'6': 'a', | |
'7': 'a', | |
'8': 'a', | |
'9': 'a', | |
'10': 'a', | |
'11': 'a', | |
'12': 'a', | |
'13': 'a', | |
}; | |
cm.binPack(data, 10, 'configmap') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment