Editing with Commands
Outcome: a kustomization changed by
kustomize edit instead of by hand — the
file before and after on the page, the
build it produces, and edit fix
converting the lab's deprecated field
while the warning disappears.
Everything so far has edited YAML by
hand, which is the right way to learn
the format. For the tenth image bump of
the week it is also the slow way, and
error-prone in the specific way of YAML:
a tag edited at the wrong indentation
still parses. kustomize edit is a set
of subcommands that make the common
changes to a kustomization file
mechanically, in the current directory:
$ kustomize edit set image webhello=webhello:1.3.0
$ kustomize edit set replicas webhello=3
$ kustomize edit set nameprefix dev-
$ kustomize edit add label team:web
The family is wider than these four —
set namespace, add resource,
add configmap, add patch,
remove ... counterparts, and more;
kustomize edit --help lists them, and
the pattern is uniform: name the thing,
give the new value.
The session, on a copy
The lab ran those four commands on a throwaway copy of the staging overlay (transcript t07, which also shows the copy being made and deleted). The kustomization before:
namePrefix: staging-
commonLabels:
env: staging
images:
- name: webhello
newTag: 1.1.0
replicas:
- name: webhello
count: 1
and the same fields after:
namePrefix: dev-
commonLabels:
env: staging
team: web
images:
- name: webhello
newTag: 1.3.0
replicas:
- name: webhello
count: 3
Each command landed exactly where a
careful hand would have put it: the tag
replaced, the count replaced, the prefix
replaced, the new label added beside the
existing one. The copy then built as
dev-... with image 1.3.0 and three
replicas (t07) — the commands edit the
file, the build obeys the file, and the
diff between this build and staging's
audits the whole session at once.
Two honest observations from the same
transcript. First, edit rewrites the
file in its own formatting: comments and
your careful ordering may not survive a
rewrite, which is a fair trade for a
mechanical edit and a poor one for a
file whose comments carry meaning.
Second, these commands are at their best
in scripts — a release pipeline that runs
edit set image and commits the result
is the pattern GitOps shops actually use
(chapter 11) — and at their most
dangerous typed from memory against the
wrong directory. Check pwd the way you
would check any other target.
edit fix: the converter
Chapter 5's warning offered a remedy, and the lab took it on another copy of staging (t07):
$ kustomize edit fix
# Warning: 'commonLabels' is deprecated. ...
Fixed fields:
patchesJson6902 -> patches
patchesStrategicMerge -> patches
commonLabels -> labels
The file afterwards holds the modern spelling:
labels:
- includeSelectors: true
pairs:
env: staging
and the next build prints no warning.
Two things worth noticing in that small
result. The converter chose
includeSelectors: true — the setting
under which the label is also written
into selectors — because that is what
commonLabels did to some fields, and
preserving behaviour was its job; the
fixdemo build (t07) shows env: staging
in the Service's selector and the
Deployment's matchLabels as well as in
plain label positions, consistently, so
everything still matches. And it renamed
the patch fields pre-emptively, though
the lab's files already used patches:.
A converter that touches more than the
line you pointed at is a converter whose
diff you read before committing — the
same discipline as chapter 10, applied
to a file instead of a build.
There is also kustomize create, which
writes a fresh kustomization in the
current folder (with --resources to
seed it from the YAML files present).
The lab's files were written by hand on
purpose; create is documented here
rather than exercised, and the project's
documentation covers it.