pepr
TypeScript icon, indicating that this package has built-in type declarations

0.38.3 • Public • Published

Pepr

Pepr Documentation Npm package license Known Vulnerabilities Npm package version Npm package total downloads OpenSSF Scorecard codecov Contributor Covenant

Type safe Kubernetes middleware for humans

Pepr is on a mission to save Kubernetes from the tyranny of YAML, intimidating glue code, bash scripts, and other makeshift solutions. As a Kubernetes controller, Pepr empowers you to define Kubernetes transformations using TypeScript, without software development expertise thanks to plain-english configurations. Pepr transforms a patchwork of forks, scripts, overlays, and other chaos into a cohesive, well-structured, and maintainable system. With Pepr, you can seamlessly transition IT ops organizational knowledge into code, simplifying documentation, testing, validation, and coordination of changes for a more predictable outcome.

Features

  • Zero-config K8s webhook mutations and validations
  • Automatic leader-elected K8s resource watching
  • Lightweight async key-value store backed by K8s for stateful operations with the Pepr Store
  • Human-readable fluent API for generating Pepr Capabilities
  • A fluent API for creating/modifying/watching and server-side applying K8s resources via Kubernetes Fluent Client
  • Generate new K8s resources based off of cluster resource changes
  • Perform other exec/API calls based off of cluster resources changes or any other arbitrary schedule
  • Out of the box airgap support with Zarf
  • Entire NPM ecosystem available for advanced operations
  • Realtime K8s debugging system for testing/reacting to cluster changes
  • Controller network isolation and tamper-resistant module execution
  • Least-privilege RBAC generation
  • AMD64 and ARM64 support

Example Pepr Action

This quick sample shows how to react to a ConfigMap being created or updated in the cluster. It adds a label and annotation to the ConfigMap and adds some data to the ConfigMap. It also creates a Validating Webhook to make sure the "pepr" label still exists. Finally, after the ConfigMap is created, it logs a message to the Pepr controller and creates or updates a separate ConfigMap with the kubernetes-fluent-client using server-side apply. For more details see actions section.

When(a.ConfigMap)
  .IsCreatedOrUpdated()
  .InNamespace("pepr-demo")
  .WithLabel("unicorn", "rainbow")
  // Create a Mutate Action for the ConfigMap
  .Mutate(request => {
    // Add a label and annotation to the ConfigMap
    request.SetLabel("pepr", "was-here").SetAnnotation("pepr.dev", "annotations-work-too");

    // Add some data to the ConfigMap
    request.Raw.data["doug-says"] = "Pepr is awesome!";

    // Log a message to the Pepr controller logs
    Log.info("A 🦄 ConfigMap was created or updated:");
  })
  // Create a Validate Action for the ConfigMap
  .Validate(request => {
    // Validate the ConfigMap has a specific label
    if (request.HasLabel("pepr")) {
      return request.Approve();
    }

    // Reject the ConfigMap if it doesn't have the label
    return request.Deny("ConfigMap must have a unicorn label");
  })
  // Watch behaves like controller-runtime's Manager.Watch()
  .Watch(async (cm, phase) => {
    Log.info(cm, `ConfigMap was ${phase}.`);

    // Apply a ConfigMap using K8s server-side apply (will create or update)
    await K8s(kind.ConfigMap).Apply({
      metadata: {
        name: "pepr-ssa-demo",
        namespace: "pepr-demo-2",
      },
      data: {
        uid: cm.metadata.uid,
      },
    });
  });

Prerequisites

  • Node.js v18.0.0+ (even-numbered releases only)

    • To ensure compatability and optimal performance, it is recommended to use even-numbered releases of Node.js as they are stable releases and receive long-term support for three years. Odd-numbered releases are experimental and may not be supported by certain libraries utilized in Pepr.
  • npm v10.1.0+

  • Recommended (optional) tools:

Wow, too many words! tl;dr;

# Create a new Pepr Module
npx pepr init

# If you already have a Kind or K3d cluster you want to use, skip this step
npm run k3d-setup

# Start playing with Pepr now
# If using another local K8s distro instead of k3d, run `npx pepr dev --host host.docker.internal`
npx pepr dev
kubectl apply -f capabilities/hello-pepr.samples.yaml

# Be amazed and ⭐️ this repo

https://user-images.githubusercontent.com/882485/230895880-c5623077-f811-4870-bb9f-9bb8e5edc118.mp4

Concepts

Module

A module is the top-level collection of capabilities. It is a single, complete TypeScript project that includes an entry point to load all the configuration and capabilities, along with their actions. During the Pepr build process, each module produces a unique Kubernetes MutatingWebhookConfiguration and ValidatingWebhookConfiguration, along with a secret containing the transpiled and compressed TypeScript code. The webhooks and secret are deployed into the Kubernetes cluster with their own isolated controller.

See Module for more details.

Capability

A capability is set of related actions that work together to achieve a specific transformation or operation on Kubernetes resources. Capabilities are user-defined and can include one or more actions. They are defined within a Pepr module and can be used in both MutatingWebhookConfigurations and ValidatingWebhookConfigurations. A Capability can have a specific scope, such as mutating or validating, and can be reused in multiple Pepr modules.

See Capabilities for more details.

Action

Action is a discrete set of behaviors defined in a single function that acts on a given Kubernetes GroupVersionKind (GVK) passed in from Kubernetes. Actions are the atomic operations that are performed on Kubernetes resources by Pepr.

For example, an action could be responsible for adding a specific label to a Kubernetes resource, or for modifying a specific field in a resource's metadata. Actions can be grouped together within a Capability to provide a more comprehensive set of operations that can be performed on Kubernetes resources.

There are both Mutate() and Validate() Actions that can be used to modify or validate Kubernetes resources within the admission controller lifecycle. There is also a Watch() Action that can be used to watch for changes to Kubernetes resources that already exist.

See actions for more details.

Logical Pepr Flow

Arch Diagram Source Diagram

TypeScript

TypeScript is a strongly typed, object-oriented programming language built on top of JavaScript. It provides optional static typing and a rich type system, allowing developers to write more robust code. TypeScript is transpiled to JavaScript, enabling it to run in any environment that supports JavaScript. Pepr allows you to use JavaScript or TypeScript to write capabilities, but TypeScript is recommended for its type safety and rich type system. You can learn more about TypeScript here.

Community

To join our channel go to Kubernetes Slack and join the #pepr channel.

Made with contrib.rocks.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.38.3
3,337latest

Version History

VersionDownloads (Last 7 Days)Published
0.38.3
3,337
0.37.349
0.38.2
492
0.38.1
2
0.38.0
1,098
0.38.0-rc
0
0.37.2
2,128
0.37.1
45
0.37.0
8
0.36.0
2
0.35.0
1
0.34.1
1
0.34.0
0
0.33.0
1
0.32.7
2
0.32.6
0
0.32.5
0
0.32.4
0
0.32.3
0
0.32.2
0
0.32.1
0
0.32.0
0
0.31.1
1
0.31.0
0
0.30.2
1
0.30.1
0
0.30.0
0
0.29.2
1
0.29.1
0
0.29.0
19
0.28.8
1
0.28.7
0
0.28.6
0
0.28.5
0
0.28.4
0
0.28.3
0
0.28.2
0
0.28.1
0
0.28.0
0
0.27.0
1
0.26.2
1
0.26.1
0
0.26.0
0
0.25.0
1
0.24.1
1
0.24.0
0
0.23.2
1
0.23.1
0
0.23.0
0
0.22.4
1
0.22.3-alpha
0
0.22.2
22
0.22.1
0
0.22.0
0
0.21.1
1
0.21.0
0
0.21.0-rc00
0.20.3
1
0.20.2
0
0.20.1
0
20.0.0
0
0.19.0
0
0.18.1
6
0.18.0
0
0.17.1
0
0.17.0
0
0.16.0
0
0.15.0
0
0.14.2
0
0.14.1
0
0.14.0
1
0.13.4
0
0.13.3
0
0.13.2
8
0.13.1
0
0.13.0
0
0.12.2
0
0.12.1
0
0.12.0
0
0.11.0
0
0.10.2
0
0.10.1
0
0.10.0
0
0.9.0
0
0.8.0
0
0.7.1
0
0.7.0
0
0.6.1
0
0.6.0
0
0.5.0
0
0.4.2
0
0.4.1
0
0.4.0
0
0.3.2
0
0.3.1
0
0.3.0
0
0.3.0-rc0
0
0.2.10
0
0.2.9
0
0.2.8
0
0.2.7
0
0.2.6
0
0.2.5
0
0.2.4
0
0.2.3
0
0.2.2
0
0.2.10
0.1.450
0.1.441
0.1.430
0.1.420
0.1.410
0.1.400
0.1.390
0.1.380
0.1.370
0.1.360
0.1.352
0.1.340
0.1.330
0.1.320
0.1.311
0.1.301
0.1.291
0.1.281
0.1.270
0.1.260
0.1.250
0.1.241
0.1.220
0.1.211
0.1.200
0.1.190
0.1.181
0.1.170
0.1.161
0.1.151
0.1.141
0.1.121
0.1.111
0.1.100
0.1.90
0.1.80
0.1.70
0.1.60
0.1.50
0.1.40
0.1.30
0.1.20
0.1.10
0.1.00
0.0.90
0.0.80
0.0.70
0.0.60
0.0.50
0.0.40
0.0.30
0.0.20
0.0.10

Package Sidebar

Install

npm i pepr

Weekly Downloads

7,249

Version

0.38.3

License

Apache-2.0

Unpacked Size

1.12 MB

Total Files

188

Last publish

Collaborators

  • pepr-dev