Skip to content

Commit

Permalink
feat(k8s): add more ksonnet-util functions (jsonnet-libs#181)
Browse files Browse the repository at this point in the history
* feat(k8s): add container.withResources{Limits,Requests} functions

* feat(k8s): add secretVolumeMountAnnotated function
  • Loading branch information
Duologic authored Jun 13, 2022
1 parent e907f62 commit fbfc37f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
22 changes: 21 additions & 1 deletion libs/k8s/custom/core/core.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ local d = import 'doc-util/main.libsonnet';
$.core.v1.envVar.new(k, env[k])
for k in std.objectFields(env)
]),

withResourcesRequests(cpu, memory)::
self.resources.withRequests(
(if cpu != null
then { cpu: cpu }
else {}) +
(if memory != null
then { memory: memory }
else {})
),

withResourcesLimits(cpu, memory)::
self.resources.withLimits(
(if cpu != null
then { cpu: cpu }
else {}) +
(if memory != null
then { memory: memory }
else {})
),
},

containerPort+: {
Expand Down Expand Up @@ -131,7 +151,7 @@ local d = import 'doc-util/main.libsonnet';
d.arg('name', d.T.string),
]),
newWithoutSelector(name)::
super.new(name)
super.new(name),
},

servicePort+:: {
Expand Down
15 changes: 15 additions & 0 deletions libs/k8s/custom/core/volumeMounts.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,21 @@ local d = import 'doc-util/main.libsonnet';
volume.secret.withDefaultMode(defaultMode),
]),

'#secretVolumeMountAnnotated': d.fn(
'same as `secretVolumeMount`, adding an annotation to force redeploy on change.'
+ volumeMountDescription,
[
d.arg('name', d.T.string),
d.arg('path', d.T.string),
d.arg('defaultMode', d.T.string),
d.arg('volumeMountMixin', d.T.object),
]
),
secretVolumeMountAnnotated(name, path, defaultMode=256, volumeMountMixin={})::
local annotations = { ['%s-secret-hash' % name]: std.md5(std.toString(name)) };

self.secretVolumeMount(name, path, defaultMode, volumeMountMixin)
+ super.spec.template.metadata.withAnnotationsMixin(annotations),

'#emptyVolumeMount': d.fn(
'`emptyVolumeMount` mounts empty volume by `name` into all container on `path`.'
Expand Down

0 comments on commit fbfc37f

Please sign in to comment.