Skip to content

feat(tanka-utils): add isKubernetesObject test and findKubernetesObject utilities #1442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions tanka-util/k8s.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,51 @@ local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';
{
local this = self,

'#isKubernetesObject':: d.fn(
|||
`isKubernetesObject` determines whether `object` is a Kubernetes object.

Note: this is characterized by being an object with `apiVersion` and `kind` fields.
|||,
[d.arg('object', d.T.any)]
),
isKubernetesObject(object)::
std.isObject(object)
&& std.objectHas(object, 'apiVersion')
&& std.objectHas(object, 'kind'),

'#findKubernetesObjects':: d.fn(
'`findKubernetesObjects` returns an array of all Kubernetes objects it finds in `object`',
[d.arg('object', d.T.any)]
),
findKubernetesObjects(object, kind=null, name=null)::
// Find Kubernetes objects by descending recursively.
if self.isKubernetesObject(object) then
// Stop descending immediately, and check whether this object meets the
// kind and name filters, if provided.
if
(kind == null || object.kind == kind)
&& (name == null || object.metadata.name == name)
then
[object]
else []

else if std.isObject(object) then
// Recurse into the object's values, flattening the returned arrays into one.
std.flatMap(
function(v) self.findKubernetesObjects(v, kind, name),
std.objectValues(object)
)

else if std.isArray(object) then
// Recurse into the array's elements, flattening the returned arrays into one.
std.flatMap(
function(v) self.findKubernetesObjects(v, kind, name),
object
)

else [],

'#patchKubernetesObjects':: d.fn(
'`patchKubernetesObjects` applies `patch` to all Kubernetes objects it finds in `object`.',
[
Expand All @@ -12,9 +57,9 @@ local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';
patchKubernetesObjects(object, patch, kind=null, name=null)::
if std.isObject(object)
then
// a Kubernetes object is characterized by having an apiVersion and Kind
if std.objectHas(object, 'apiVersion') && std.objectHas(object, 'kind')
&& (kind == null || object.kind == kind) && (name == null || object.metadata.name == name)
if self.isKubernetesObject(object)
&& (kind == null || object.kind == kind)
&& (name == null || object.metadata.name == name)
then object + patch
else
std.mapWithKey(
Expand Down
Loading