Skip to main content
Skip to main content

shell-op

Plugin library for writing shell-operator hooks with clean, typed functions.

Install: argsh add shell-op

shell-op::on

Register a Kubernetes watch binding. Flags -k and -e are repeatable.

# Watch pods
shell-op::on pod::handle -k v1/Pod -e Added -e Modified -e Deleted

# Multiple kinds auto-group
shell-op::on mesh::handle -k Pod -k Service -e Added

# Full options
shell-op::on deploy::handle \
-k apps/v1/Deployment \
-e Added -e Modified \
-l "app=web,tier=frontend" \
-f '.metadata.name' \
-n production \
-g my-group \
-q slow \
-r 1h \
--no-sync
FlagDescription
-k, --kindResource kind with optional apiVersion (v1/Pod)
-e, --eventWatch event: Added, Modified, Deleted
-l, --labelsLabel selector (key=val,...)
-f, --filterjq filter expression
-n, --namespaceNamespace to watch
-g, --groupGroup key for batching
-q, --queueNamed queue for parallel execution
-r, --resyncResynchronization period (1h, 30m)
--no-syncSkip Synchronization on startup

Handler signature

Handlers are argsh functions — declare local for the fields you need:

pod::handle() {
local event kind name namespace
:args "Handle pod events" "${@}"

case "${event}" in
"Synchronization") echo "Sync: ${name}" ;;
*) echo "Pod ${namespace}/${name} was ${event}" ;;
esac
}

Fields (in order): event, kind, name, namespace, type, binding, watchEvent, ctx.

shell-op::cron

Register a schedule binding.

shell-op::cron "*/5 * * * *" cron::tick
shell-op::cron "0 * * * *" hourly::report --queue slow --allow-failure
shell-op::cron "*/5 * * * *" snap::check --include-snapshots pod::handle
FlagDescription
-q, --queueNamed queue for parallel execution
-i, --include-snapshotsInclude snapshots from a named k8s binding
--allow-failureIgnore hook execution errors

Cron handlers receive: event (Schedule), binding, ctx.

shell-op::run

Main entry point — handles --config and dispatches events.

shell-op::run "${@}"
shell-op::run --startup my::init "${@}"
shell-op::run --startup my::init --order 10 "${@}"
FlagDescription
-s, --startupStartup handler function
-o, --orderStartup execution order (default: 1)
--configPrint shell-operator JSON config

Full example

#!/usr/bin/env bash
source argsh
import shell-op

pod::handle() {
local event name namespace
:args "Handle pod events" "${@}"
echo "Pod ${namespace}/${name} was ${event}"
}

cron::tick() {
local event
:args "Cron tick" "${@}"
echo "Tick: ${event}"
}

startup::init() {
echo "Operator started"
}

shell-op::on pod::handle -k v1/Pod -e Added -e Modified -e Deleted
shell-op::cron "*/5 * * * *" cron::tick
shell-op::run --startup startup::init "${@}"

Docker

Pre-built base image with argsh + shell-op:

# Bake hooks into image
FROM ghcr.io/arg-sh/shell-op:latest
COPY hooks/ /hooks/

Or mount hooks via ConfigMap for GitOps workflows:

# kustomization.yaml
configMapGenerator:
- name: my-operator-hooks
files:
- hooks/watch-pods.sh

See the example hook for a complete working script.

Source

  • Repository: github.com/arg-sh/libs
  • Docker: ghcr.io/arg-sh/shell-op:latest
  • Install: argsh add shell-op
  • Import: import shell-op
Was this section helpful?