patch-operator.libsonnet API reference

This page documents the fields and functions provided by the patch-operator.libsonnet component library.

Usage

The library is intended to provide an easy interface to patch single resources to other components.

For example, to add a custom label to an existing namespace on the cluster, you can use the following Jsonnet snippet.

local kube = import 'lib/kube.libjsonnet';
local p = import 'lib/patch-operator.libsonnet';

// Create a strategic merge patch for the namespace
local ns_patch = p.Patch(
  kube.Namespace('the-namespace'),
  {
    metadata: {
      labels: {
        foo: 'bar'
      },
    },
  }
);

{
  // output the patch
  ns_patch: ns_patch,
}

Function reference

apiVersion

Field apiVersion provides the API version of the Patch resources created by the library.

Return value

redhatcop.redhat.io/v1alpha1

Patch(targetobj, patchtemplate, patchstrategy='application/strategic-merge-patch+json')

Arguments

targetobj

The target object to patch. The library expects this to be a valid Kubernetes object definition. The object doesn’t need to be complete, but fields apiVersion, kind, metadata.name must be present. For namespaced resources, field metadata.namespace also must be present for accurate patch targeting.

patchtemplate

An object describing the patch to apply. The exact format of this object is defined by parameter patchstrategy. The operator supports the same patch formats as kubectl patch.

See the Kubernetes docs on patching objects for more information on supported formats.

See the patch-operator README for more details on additional features provided by patch-operator.

Please note that this function doesn’t support selecting multiple target objects for a patch, but users are free to modify the returned patch object to do so.

patchstrategy

The strategy to use to apply the patch provided in parameter patchtemplate. The parameter defaults to Kubernetes' strategic merge patch (application/strategic-merge-patch+json).

Supported strategies are

Return value

A list containing the rendered Patch custom resource.

We return a list, so that we could return additional resources if required. Users should always add the complete list to the component’s output to ensure that patches can be applied correctly.

render_patch(patch, rl_version, patch_id='patch1')

This function can be used to render individual patches. There’s no real need for this function anymore, and it may be removed in a future release.

Arguments

patch

The patch to format. See the documentation for the Patch() function for details.

rl_version

This parameter is deprecated, and no longer has any effect. It will be removed in a future release.

patch_id

The patch ID to inject into the patch provided in argument patch. Defaults to patch1.

Return value

An object with key patch_id and value patch.