Type templates
Write down how one type of note should look, as a type template, and okfctl
starts new notes from it and tells you which old ones have drifted. The
OKF spec
won’t do that for you. It wants one thing from each note, its type. Open its
sample playbook, playbooks/freshness-alert.md, and you’ll see a # Trigger
heading over a # Steps heading. You may want every playbook laid out that
way, with an owner named at the top and a one-line description for the index.
OKF won’t hold anyone to it. The spec registers no types and wants no key
but type.
Templates are okfctl’s addition. The spec never mentions them, and you don’t give up anything by using them. A template is an ordinary note in the bundle. Another OKF tool just sees one more concept with a type and keys it doesn’t know, and the spec’s “Frontmatter” section says readers have to accept unknown types and unknown keys. A bundle with templates in it is still plain OKF.
okfctl reads templates in three places. node new starts a note from one,
validate --templates reports notes that stray from theirs, and
template list and template show print what the bundle has. Run everything
below in an empty directory, and if bundles and nodes are new to you, read
Concepts first.
Write a template
Section titled “Write a template”Make a bundle for an on-call team:
$ okfctl bundle init oncallInitialized OKF bundle in oncallSave this as oncall/templates/playbook.md:
---type: Type Templatetitle: Playbook templatetarget_type: Playbookrequired_fields: [owner, description]recommended_fields: [tags]body_sections: [Trigger, Steps]---
# Playbook template
Every playbook names the person on call for it.$ okfctl template list oncallPlaybook 2 required field(s), 2 body section(s) (source: templates/playbook.md)$ okfctl template show Playbook oncalltarget_type: Playbooksource: templates/playbook.mdrequired_fields: owner, descriptionrecommended_fields: tagsbody_sections: Trigger, Stepstemplate list prints a line for each type that has a template, and
template show prints every list in one template. Neither one writes a file.
A note is a template when its type is Type Template, spelled just like
that. Past that, okfctl reads four keys and ignores the rest. The title and
the body are there for people:
| Key | What okfctl does with it |
|---|---|
target_type |
Names the type the template governs, matched exactly, capitals included. A template without it is skipped. |
required_fields |
Header keys each note of that type must fill in. node new writes each one as TODO; validate --templates warns when one is missing or blank. |
recommended_fields |
Header keys node new adds with no value. validate never reports them. |
body_sections |
Headings node new writes as empty ## sections; validate --templates warns when one is missing. |
The three lists have to be YAML lists. [owner] works, and so does a bulleted
list. A bare owner is read as no fields at all (see the limits at the end).
Any heading level from # down to ###### satisfies a body section, and
capitals don’t matter (### steps satisfies Steps). A field passes when its
value isn’t blank. Three names get read the way the spec defines them.
sources passes with a non-empty sources list in any of its shapes, and
also with a body # Citations list from an older bundle. generated.at
passes with the nested generated: {at: ...} the spec defines and also with
the timestamp key older bundles used. A template that names timestamp
accepts either form too. Any other name with a dot in it, like generated.by,
is read as a path through nested keys.
Start a note from a template
Section titled “Start a note from a template”node new --type looks for a template whose target_type matches and, when
it finds one, builds the new note from it:
$ okfctl node new playbooks/disk-full --type Playbook --title "Disk full" --bundle oncallCreated oncall/playbooks/disk-full.md (from Playbook template)$ sed -n '/^owner:/,$p' oncall/playbooks/disk-full.mdowner: TODOdescription: TODOtags:---
# Disk full
## Trigger
## StepsThe header also gets type, title, and the created and modified
times that node new always writes. owner and description read TODO
because the template insists on them, and tags sits empty because it’s only
recommended. node new found the template in templates/, but the folder
name means nothing to okfctl. It looks at every note in the bundle.
TODO counts as a value. Even a note fresh from node new passes
validate --templates while every field still says TODO. The check below
catches a field someone deletes. To find the ones no one has filled in, grep
for the placeholder:
$ grep -rl 'TODO' oncall/playbooksoncall/playbooks/disk-full.mdCheck notes against their templates
Section titled “Check notes against their templates”Now write a playbook by hand that skips the template, and run validate
three ways:
$ printf -- '---\ntype: Playbook\ntitle: Cert expiry\n---\n\n# Cert expiry\n\n## Steps\n' > oncall/playbooks/cert-expiry.md$ okfctl validate oncallOK: bundle conforms to the OKF spec floor$ okfctl validate --templates oncallwarning playbooks/cert-expiry.md: missing required field: owner (template Playbook)warning playbooks/cert-expiry.md: missing required field: description (template Playbook)warning playbooks/cert-expiry.md: missing body section: Trigger (template Playbook)3 drift warning(s)$ okfctl validate --templates --strict oncallwarning playbooks/cert-expiry.md: missing required field: owner (template Playbook)warning playbooks/cert-expiry.md: missing required field: description (template Playbook)warning playbooks/cert-expiry.md: missing body section: Trigger (template Playbook)3 drift warning(s)okfctl: 3 drift warning(s)Plain validate checks the spec floor and never opens a template. The cert
playbook passes it. With --templates you get one line for each thing the
note lacks, and the command still exits 0. okfctl calls a note that has
wandered from its template drift. Drift is advice until you add --strict
(then the same warnings exit 1).
Put the strict form in CI:
okfctl validate --templates --strict ./oncall--strict counts every kind of drift validate reports. A modified date
that disagrees with the note’s last git commit fails it too, and so does a
version in an old .okf file that disagrees with index.md. The
index and freshness guide explains those two. A
spec-floor failure exits 1 even when you leave --strict off.
Fill in the gaps and the check comes back clean:
$ printf -- '---\ntype: Playbook\ntitle: Cert expiry\nowner: dana\ndescription: Renew a TLS certificate before it lapses.\n---\n\n# Cert expiry\n\n## Trigger\n\n## Steps\n' > oncall/playbooks/cert-expiry.md$ okfctl validate --templates --strict oncallOK: bundle conforms to the OKF spec floor and team templatesShare one set of templates across bundles
Section titled “Share one set of templates across bundles”Any number of bundles can use one set of templates. Put the templates in a
bundle of their own, then point the others at it with a templates: line in a
file named .okf at the top of each bundle. The path starts from the bundle
holding the .okf:
$ okfctl bundle init vendorkbInitialized OKF bundle in vendorkb$ printf 'templates: ../oncall\n' > vendorkb/.okf$ okfctl template list vendorkbPlaybook 2 required field(s), 2 body section(s) (source: ../oncall/templates/playbook.md)node new and validate --templates find templates the same way.
--templates-from <dir> on any of the four commands points at a different
bundle for one run and overrides the .okf line.
A bundle can still have a template of its own. When the two define one type,
the local template wins and template list marks the line:
$ mkdir vendorkb/templates$ printf -- '---\ntype: Type Template\ntitle: Vendor playbook\ntarget_type: Playbook\nrequired_fields: [owner, vendor]\n---\n\n# Vendor playbook\n' > vendorkb/templates/playbook.md$ okfctl template list vendorkbPlaybook 2 required field(s), 0 body section(s) (source: templates/playbook.md) [shadows referenced template]The local template replaces the shared one whole. Nothing is merged field by
field, and vendorkb playbooks no longer need a Trigger section.
The reference goes one level deep. If oncall pointed at a third bundle,
vendorkb wouldn’t see that bundle’s templates. A reference that doesn’t lead
to a directory stops every command that reads templates. Plain validate
never looks:
$ printf 'templates: ../shared\n' > vendorkb/.okf$ okfctl template list vendorkbokfctl: templates reference "../shared" is not a readable bundle directory$ okfctl validate --templates vendorkbokfctl: templates reference "../shared" is not a readable bundle directory$ okfctl validate vendorkbOK: bundle conforms to the OKF spec floorLimits and edge cases
Section titled “Limits and edge cases”Each case below comes from how the code behaves today. All of them use the
oncall bundle from above.
A type with no template is fine. node new writes a plain note, template show exits 1, and validate --templates has nothing to compare:
$ okfctl node new reviews/march-outage --type Postmortem --bundle oncallCreated oncall/reviews/march-outage.md$ okfctl template show Postmortem oncallokfctl: no template governs type "Postmortem"$ okfctl validate --templates oncallOK: bundle conforms to the OKF spec floor and team templatesThe type has to match target_type letter for letter. Nothing warns you that
a note typed playbook isn’t governed by the Playbook template:
$ printf -- '---\ntype: playbook\ntitle: DNS\n---\n\n# DNS\n' > oncall/playbooks/dns.md$ okfctl validate --templates oncallOK: bundle conforms to the OKF spec floor and team templates$ rm oncall/playbooks/dns.mdTwo templates for one type don’t raise an error either. okfctl sorts the
note paths and the last one wins. Put a copy in zz-drafts/ and it replaces
the template you meant to use, silently:
$ mkdir oncall/zz-drafts$ printf -- '---\ntype: Type Template\ntitle: Draft playbook\ntarget_type: Playbook\nrequired_fields: [owner]\n---\n\n# Draft playbook\n' > oncall/zz-drafts/playbook.md$ okfctl template list oncallPlaybook 1 required field(s), 0 body section(s) (source: zz-drafts/playbook.md)$ rm -r oncall/zz-draftsAfter adding a template, run template list to see which file the source
column names.
A list written as a bare word is read as empty. The template still shows up, with zero in its counts:
$ printf -- '---\ntype: Type Template\ntitle: Memo template\ntarget_type: Memo\nrequired_fields: owner\n---\n\n# Memo template\n' > oncall/templates/memo.md$ okfctl template list oncallMemo 0 required field(s), 0 body section(s) (source: templates/memo.md)Playbook 2 required field(s), 2 body section(s) (source: templates/playbook.md)Templates live anywhere a note can. Folders whose names start with a dot
aren’t part of the bundle (okfctl never reads a template in .drafts/).
node new can’t write a dotted field today. Name generated.by in a
template and the new note gets a flat key called generated.by. Then
validate --templates reads that key as missing:
$ printf -- '---\ntype: Type Template\ntitle: Memo template\ntarget_type: Memo\nrequired_fields: [generated.by]\n---\n\n# Memo template\n' > oncall/templates/memo.md$ okfctl node new memos/q3 --type Memo --bundle oncallCreated oncall/memos/q3.md (from Memo template)$ grep generated oncall/memos/q3.mdgenerated.by: TODO$ okfctl validate --templates oncallwarning memos/q3.md: missing required field: generated.by (template Memo)1 drift warning(s)Until that’s fixed, replace the stub with the nested form by hand:
generated: on one line and an indented by: under it.
- Starting and authoring a bundle uses a template as one step in building a bundle from scratch.
- The command reference lists every flag on
template list,template show,node new, andvalidate.