Commit Graph

22 Commits

Author SHA1 Message Date
Aurel Canciu c3fe4e0a77
Fix resolving image name for marker name attribute
The image name was incorrectly extracted from the given reference,
yielding a trimmed version of the string and breaking the resulting
replacement value.

Signed-off-by: Aurel Canciu <aurelcanciu@gmail.com>
2021-05-06 15:40:29 +03:00
Aurel Canciu 7ad4047319
Switch to kube-openapi/spec flavor
Upgraded kyaml to v0.10.19

Signed-off-by: Aurel Canciu <aurelcanciu@gmail.com>
2021-05-06 14:03:49 +03:00
Stefan Prodan 833b50fdee
Move to ImagePolicy v1alpha2
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
2021-04-22 10:03:22 +03:00
Michael Bridgen 018e9e8a59 Simplify callback bookkeeping
This commit removes an unnecessary indirection through a closure.

Signed-off-by: Michael Bridgen <michael@weave.works>
2021-04-06 10:04:08 +01:00
Michael Bridgen c5cfe9170e Reimplement kyaml filter to record only changes
The update procedure is obliged to return a Result struct with all the
objects that were changed, for filling in the commit template. At
present, the result is collated by running each setter on each object
and seeing if the setter is used. This uses the `Set` from kyaml, with
a small amount of glue.

It doesn't quite work, however, because a setter may be used for a
field without changing the value. The result gets an entry for each
policy _mentioned_, whether or not it had a new value. There is no way
to see whether a setter actually changed a field from the outside,
other than by comparing a copy of the object before using the setter
with the object after (which yaml.v3 does not make easy).

A better approach is to get the setter to record whether it changed
anything, since it is there doing the changing. This means
reimplementing kyaml's `Set`. I have stripped it down to the parts
needed for image updates -- so e.g., only field values are examined.

Signed-off-by: Michael Bridgen <michael@weave.works>
2021-04-06 10:04:08 +01:00
Michael Bridgen 9caa3b42f1 Test that an unchanged image is not in result
With reference to

    https://github.com/fluxcd/image-automation-controller/issues/133

this commit adapts the update->result test so that it checks an
additional case: that a field with an update marker that _does_
correspond to a policy, but _doesn't_ get changed, is not included in
the results.

This test fails at present, because the method for determining the
result is to count which setters are referenced, rather than which
fields were changed.

Signed-off-by: Michael Bridgen <michael@weave.works>
2021-04-06 10:04:08 +01:00
Michael Bridgen 2eebaa46c7 Make the image policy name available to templates
Signed-off-by: Michael Bridgen <michael@weave.works>
2021-03-03 13:01:29 +00:00
Michael Bridgen df7d570ae5 Give details of template data in spec docs
This explains the data available to the commit message template in the
API guide. While writing it, I realised it could be made more
convenient, so:

 - mask external types by embedding them
 - make the most useful parts of an image ref available using a
   wrapper struct and interface

Signed-off-by: Michael Bridgen <michael@weave.works>
2021-03-03 11:08:53 +00:00
Michael Bridgen 908f8b775c Supply values to the commit message template
This commit:

 - passes a value including the update result to the commit message
   template
 - gives the template result a method for enumerating the
   objects regardless of file

This means you can access the images updated either by file
(`.Files`), by object (`.Objects()`), or just as a list
(`.Images()`). The additional test case shows how to use these.

Signed-off-by: Michael Bridgen <michael@weave.works>
2021-03-01 22:25:25 +00:00
Michael Bridgen fbdfa78e87 Adapt pkg/update so it returns a report of updates
It's desirable (see #6) to be able to enumerate the updates that were
made by automation, in the commit message and perhaps in an event
announcing success.

Doing this is counter-intuitively difficult. A `kyaml.setters2.Set`
filter will keep a count of the times its used. Previously, one `Set`
was used with the `SetAll` flag set, which would replace any marker
that corresponded to an image, in one traversal. But to keep track of
images individually, you need to have a setter for _each_ image (and
its tag, and its name, since those can be used separately). This means
`3 x policies` traversals of each node! The saving grace, possibly, is
that only files with a marker in them are considered.

Since you might want to dice the results in different ways, the result
returned is a nested map of file->object->image.

Signed-off-by: Michael Bridgen <michael@weave.works>
2021-03-01 17:38:31 +00:00
Michael Bridgen a6e151d63a Update to kyaml 0.10.5
This contains a number of fixes, in particular for an indentation bug
that seems to have been introduced not long ago, and which would
otherwise cause problems after updating controller-runtime (which
brings a slightly less recent kyaml with it).

This also comes with a nice enhancement: it's no longer necessary to
use the global schema for kio setters, you can just build a schema and
use that. No need to serialise access to the global schema. Yay!

Signed-off-by: Michael Bridgen <michael@weave.works>
2021-01-13 16:46:14 +00:00
nguyenductoan 0506b974e8 fix comment typo
Signed-off-by: nguyenductoan <ductoan593@gmail.com>
2021-01-07 16:11:27 +07:00
Michael Bridgen 05832c5fd4 Give new and lacking files a copyright notice
In general a copyright notice takes the form:

    Copyright year name

.. where the year is the year of first publication, to let people know
from when the copyright applies. It's fairly common in software to
affix additional years in which the software was modified and
released. I have chosen here to use `2020, 2021` for the new and
modified files; it is OK that not _all_ files are updated, since the
important bit is the _first_ year, which they already have.

Signed-off-by: Michael Bridgen <michael@weave.works>
2021-01-04 15:19:59 +00:00
Michael Bridgen 8eec8c095c Screen files, and output only those updated
This is intended to address two problems:

 - LocalPackage{Reader,Writer} like to reformat the YAML that passes
   through them; mostly this is harmless, but occasionally it will end
   up fighting format tooling, e.g., prettier.

 - It's possible that things like Helm chart templates are lying
   around in the git repository to which automation is applied. Those
   templates have extensions of ".yaml" but are not usually parseable
   as YAML, so would result in errors from the file reader.

This commit changes how updates are run -- firstly, it screens files
by checking for a token (`"$imagepolicy"`) that will be present in
files that might need updating. This cheaply removes some nodes --
likely including Helm chart templates -- from consideration.

Secondly, it now only writes files that were actually updated by an
imagepolicy setter, rather than writing everything that was an
input. This means it's less likely to reformat something that doesn't
need to be touched at all.

Signed-off-by: Michael Bridgen <michael@weave.works>
2021-01-04 14:44:42 +00:00
Michael Bridgen da2f23caf1 Remove proof-of-concept update mode
The initial implementation of image updates required a single image
policy, and updated every image field that used the image, in the git
repo. This mode has limited practical value, and rather than
elaborating on it, it would be better to concentrate on making the
more carefully thought-through "setters" mode.
2020-10-22 14:53:41 +01:00
Michael Bridgen 76da2079e6 Supply :tag and :name setters for each image
It will be useful, for kustomizations e.g., to be able to set just the
tag or just the name (repository). This commit adds setters for those
to the schema -- they have the name of the image setter plus a suffix
of `:tag` or `:name`. For example:

    newName: ubuntu # {"$imagepolicy": "ns:policy:name"}
    newTag: 18.10   # {"$imagepolicy": "ns:policy:tag"}
2020-10-13 21:00:13 +01:00
Michael Bridgen 7e58a5bf47 Implement and test func for update using setters
This adds another means of updating files to the package pkg/update/,
in setters.go (and gives the existing file a better name).

In passing, I changed the test util for comparing before/after
updates, in pkg/files/, to give a little more context when comparing
file contents; and, since the comparison between actual and expected
is not symmetrical, I corrected the order of the args in the tests.
2020-10-13 20:59:35 +01:00
Michael Bridgen 963ee35c23 Preserve line-end comments when replacing image
Previously: replace the YNode (yaml.Node) with a new one which is just
the replacement string.
Now: change the value of the YNode, and set it back in place.
2020-09-29 12:19:12 +01:00
Kevin McDermott aad1df6af6 Apply image updates to CronJob objects
This updates the image replacement to switch on the kind of the node,
and change the way the replacements are done for CronJobs.

CronJobs have PodTemplateSpecs embedded deeper in the structure.
2020-09-09 10:30:48 +01:00
Michael Bridgen ab4a963d50 Change package name and fixup imports and paths 2020-08-10 18:09:57 +01:00
Michael Bridgen 7c6e87d06a Test the result of the automated commit
This factors out the function that checks directories for equivalence,
and uses it to check that the upstream repo has the expected update
when the controller has pushed its commit.
2020-07-23 18:20:35 +01:00
Michael Bridgen 739f3c1770 Add update strategy that uses image policy
Just to get points on the board, this gives ImageUpdateAutomation an
`update` field into which you can plug the name of an ImagePolicy
resource (from the image reflector controller). The idea is that the
automation will then replace the image in the policy, anywhere it's
used, with its latest version.
2020-07-23 18:20:35 +01:00