Docs / Reference / Workers and storage
WorkerScript
WorkerScript represents a Cloudflare Worker script and its bindings.
- API version
- kflare.dev/v1alpha1
- Scope
- Namespaced
- Short name
- cfworker
- Token permission
- Account · Workers Scripts · Edit
Example
Every field, with the sample's values. The live lines are the repository's sample, which applies as it is; the commented ones are the other fields, filled from other samples where one sets them. Some exclude each other, so check the rules before you uncomment several.
apiVersion: kflare.dev/v1alpha1
kind: WorkerScript
metadata:
name: example-worker
namespace: default
spec:
name: example-worker
accountRef:
name: my-account
format: module
compatibilityDate: "2024-09-23"
script: |
export default {
async fetch(request, env) {
return new Response(`${env.GREETING} from kflare`);
},
};
bindings:
- name: GREETING
plainText: Hello
- name: API_KEY
secretKeyRef:
name: example-worker-secrets
key: api-key
# - name: GREETING
# plainText: Hello
# secretKeyRef:
# name: example-worker-secrets
# key: api-key
# kvNamespaceID: <kvNamespaceID>
# kvNamespaceRef:
# name: <name>
# r2BucketName: <r2BucketName>
# r2BucketRef:
# name: <name>
# compatibilityFlags:
# - <compatibilityFlags>
# scriptConfigMapRef:
# key: <key>
# name: <name>Spec
What you set, with the limits the API server enforces.
| Field | Description |
|---|---|
accountRefobjectrequiredimmutable | AccountRef references the CloudflareAccount that owns the Worker. Immutable after creation. |
accountRef.namestring | Name of the referent. This field is effectively required, but due to backwards compatibility is allowed to be empty. Instances of this type with an empty value here are almost certainly wrong. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names Default: |
bindings[]object | Bindings expose values and resources to the Worker as environment bindings. Names must be unique. Limits: at most 64 items Rule: exactly one of plainText, secretKeyRef, kvNamespaceRef, kvNamespaceID, r2BucketRef or r2BucketName must be set |
bindings[].kvNamespaceIDstring | KVNamespaceID binds a Workers KV namespace by ID. Prefer kvNamespaceRef for namespaces managed by kflare. Limits: at least 1 character |
bindings[].kvNamespaceRefobject | KVNamespaceRef binds the Workers KV namespace of a KVNamespace in the same namespace. The Worker is uploaded once the KVNamespace is ready, and the KVNamespace is not deleted while this binding exists. |
bindings[].kvNamespaceRef.namestring | Name of the referent. This field is effectively required, but due to backwards compatibility is allowed to be empty. Instances of this type with an empty value here are almost certainly wrong. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names Default: |
bindings[].namestringrequired | Name of the binding, as seen by the Worker (env.NAME). Limits: matches |
bindings[].plainTextstring | PlainText binds a plain text value. Limits: at least 1 character |
bindings[].r2BucketNamestring | R2BucketName binds an R2 bucket by name. Prefer r2BucketRef for buckets managed by kflare. Limits: at least 1 character |
bindings[].r2BucketRefobject | R2BucketRef binds the R2 bucket of an R2Bucket in the same namespace. The Worker is uploaded once the R2Bucket is ready, and the R2Bucket is not deleted while this binding exists. |
bindings[].r2BucketRef.namestring | Name of the referent. This field is effectively required, but due to backwards compatibility is allowed to be empty. Instances of this type with an empty value here are almost certainly wrong. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names Default: |
bindings[].secretKeyRefobject | SecretKeyRef binds a secret text value read from a key of a Secret in the same namespace. The value is sent to Cloudflare and never written to this resource's status. |
bindings[].secretKeyRef.keystringrequired | Key within the ConfigMap or Secret. Limits: at least 1 character |
bindings[].secretKeyRef.namestringrequired | Name of the ConfigMap or Secret. Limits: at least 1 character |
compatibilityDatestring | CompatibilityDate pins the Workers runtime behaviour (YYYY-MM-DD). Limits: matches |
compatibilityFlags[]string | CompatibilityFlags enable or disable individual runtime features. Limits: at most 64 items |
formatstring | Format is the script syntax: "module" (ES modules) or "serviceWorker". Default: One of: |
namestringrequiredimmutable | Name is the Worker's script name in Cloudflare. Immutable after creation. Limits: matches |
scriptstring | Script is the Worker source code. Set either this or ScriptConfigMapRef. Limits: at least 1 character |
scriptConfigMapRefobject | ScriptConfigMapRef reads the Worker source code from a key of a ConfigMap in the same namespace. Set either this or Script. |
scriptConfigMapRef.keystringrequired | Key within the ConfigMap or Secret. Limits: at least 1 character |
scriptConfigMapRef.namestringrequired | Name of the ConfigMap or Secret. Limits: at least 1 character |
Rules
Checked across fields when the resource is applied.
- exactly one of script or scriptConfigMapRef must be set
Status
What kflare reports. conditions holds the Ready condition, whose reason says why a resource isn't ready.
| Field | Description |
|---|---|
appliedHashstring | AppliedHash identifies the desired state last uploaded: the script, format, compatibility settings and bindings. Secret bindings contribute the Secret's UID and resourceVersion, never its value. |
cloudflareMetadataobject | CloudflareMetadata holds identifiers returned by the Cloudflare API. |
cloudflareMetadata.etagstring | Etag is Cloudflare's hash of the uploaded code. It does not change when only bindings or compatibility settings change. |
cloudflareMetadata.modifiedOnstring | ModifiedOn is the modification time Cloudflare reported for kflare's last upload, kept at full precision. A different value on Cloudflare means the Worker was changed outside kflare. |
cloudflareMetadata.scriptNamestring | ScriptName is the name of the Worker kflare last uploaded. |
conditions[]object | Conditions represent the latest available observations of the Worker state. |
kubectl get
The columns kubectl get workerscripts prints.
| Column | Source |
|---|---|
| SCRIPT | .spec.name |
| MODIFIED | .status.cloudflareMetadata.modifiedOn |
| READY | .status.conditions[?(@.type=="Ready")].status |
| AGE | .metadata.creationTimestamp |
Samples
From the repository's config/samples, comments included.
apiVersion: kflare.dev/v1alpha1
kind: WorkerScript
metadata:
name: example-worker
namespace: default
spec:
name: example-worker # script name in Cloudflare (immutable)
accountRef:
name: my-account
format: module
compatibilityDate: "2024-09-23"
# Inline code; use scriptConfigMapRef: {name, key} to read it from a ConfigMap instead.
script: |
export default {
async fetch(request, env) {
return new Response(`${env.GREETING} from kflare`);
},
};
bindings:
- name: GREETING
plainText: Hello
- name: API_KEY # read from Secret example-worker-secrets, key api-key
secretKeyRef:
name: example-worker-secrets
key: api-key
# A KVNamespace or R2Bucket in the same namespace is bound by reference, such as
# the KVNamespace sample:
#
# bindings:
# - name: CACHE
# kvNamespaceRef:
# name: example-kvnamespace