Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supply update result value to the commit message template #119

Merged
merged 7 commits into from
Mar 4, 2021

Conversation

squaremo
Copy link
Member

@squaremo squaremo commented Mar 2, 2021

The effect of this PR is to give the commit message template some data to work with. Specifically: a data structure (definition given below) which holds the images updated for each object, for each file; and the name of the image automation object that created the commit.

This is the update result:

type Result struct {
	Files map[string]FileResult
}

type FileResult struct {
	Objects map[yaml.ResourceIdentifier][]name.Reference
}

.. where the map keys are file paths and {APIVersion, Kind, Name, Namespace}, and name.Reference is an image name.

Here's a template that uses the result structure to print out what was updated, in different ways:

template := `Commit summary

Image update automation object: {{ .AutomationObject }}

Files:
{{ range $filename, $_ := .Updated.Files -}}
- {{ $filename }}
{{ end -}}

Objects:
{{ range $resource, $_ := .Updated.Objects -}}
- {{ $resource.Kind }} {{ $resource.Name }}
{{ end -}}

Images:
{{ range .Updated.Images -}}
- {{.}}
{{ end -}}
`

(There are methods on the Result type to make enumerating the objects and image names possible without descending into the structure.)

EDIT While writing the guide I made the template data more convenient to use (see the commit):

  • Write a reference for the template data
  • Give the name of the policy for each update (A: yes it's useful)

EDIT Not sure whether these are worth it:

  • [?] Wrap ResourceIdentifier in something that has convenient string-returning methods

squaremo added 5 commits March 1, 2021 17:38
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 <[email protected]>
There is a core chuck of testing that is repeated for {SSH,HTTP} x
{go-git,libgit2}, which is done by repeating a func value in different
contexts. Instead of mutating variables in the func's closure, it's a
bit clearer (and shorter) to pass them to a higher-order func.

Signed-off-by: Michael Bridgen <[email protected]>
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 <[email protected]>
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 <[email protected]>
@squaremo squaremo force-pushed the data-for-commit-msg branch from 148a624 to df7d570 Compare March 3, 2021 11:09
@stefanprodan
Copy link
Member

After reading the API docs I don't get how to use this feature, is this intended to place a go template inside messageTemplate? I think the api doc needs a YAML example.

The message template is used in YAML files, not in go code. I've also
explained up-front that it's a text template -- something not
mentioned to this point.

Signed-off-by: Michael Bridgen <[email protected]>
@squaremo squaremo force-pushed the data-for-commit-msg branch from 0694585 to cfa5a9c Compare March 4, 2021 13:29
Copy link
Member

@stefanprodan stefanprodan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Thanks @squaremo 🥇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants