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

Expose custom funcs to template #638

Merged
merged 1 commit into from
Aug 26, 2022

Conversation

chrisdoherty4
Copy link
Member

@chrisdoherty4 chrisdoherty4 commented Aug 25, 2022

Having exposed a subset of hardware data to templates there's a need to provide some helper functions for manipulating the data. The concrete use case is formatting of disk partitions.

This PR introduces formatPartition to templates accepting the device path. The function currently supports historic block devices (/dev/sd<char>) and NVMe drives. Additionally it adds strings.Contains, strings.HasPrefix and strings.HasSuffix which have proven useful.

Example template snippet

name: {{ .Hardware.Name }}
disk: {{ formatPartition ( index .Hardware.Disks 0 ) 1 }}

Documentation will need adding to the docs website in the on-going documentation effort.

@chrisdoherty4 chrisdoherty4 force-pushed the exp/hardware-ref branch 4 times, most recently from f8f1f8d to 779bda5 Compare August 25, 2022 20:33
@chrisdoherty4 chrisdoherty4 marked this pull request as ready for review August 25, 2022 20:33
@codecov
Copy link

codecov bot commented Aug 25, 2022

Codecov Report

Merging #638 (f8f1f8d) into main (a0e9350) will increase coverage by 0.12%.
The diff coverage is 100.00%.

❗ Current head f8f1f8d differs from pull request most recent head d889e78. Consider uploading reports for the commit d889e78 to get more accurate results

@@            Coverage Diff             @@
##             main     #638      +/-   ##
==========================================
+ Coverage   44.95%   45.07%   +0.12%     
==========================================
  Files          61       62       +1     
  Lines        3546     3554       +8     
==========================================
+ Hits         1594     1602       +8     
  Misses       1870     1870              
  Partials       82       82              
Impacted Files Coverage Δ
pkg/controllers/workflow/controller.go 79.31% <100.00%> (ø)
workflow/funcs.go 100.00% <100.00%> (ø)
workflow/template_validator.go 95.34% <100.00%> (+0.05%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Additional helper functions have proven useful to consumers when
rendering templates with hardware data. The concrete use-case is
formatting of partitions where nvme uses a different format to the
historical block device.

Signed-off-by: Chris Doherty <[email protected]>
@displague
Copy link
Member

Documentation will need adding to the docs website in the on-going documentation effort.

In this PR and in added documentation, specifying that the device partition format is the Linux kernel format may avoid some confusion.

@chrisdoherty4
Copy link
Member Author

chrisdoherty4 commented Aug 26, 2022

@displague Good call out. Are you thinking in the function comment (and the docs site when it goes up)? If there's somewhere else in this repository we need it could you fire me a link, thanks.

// Examples
// formatPartition("/dev/nvme0n1", 0) -> /dev/nvme0n1p1
// formatPartition("/dev/sda", 1) -> /dev/sda1
func formatPartition(dev string, partition int) string {
Copy link
Member Author

@chrisdoherty4 chrisdoherty4 Aug 26, 2022

Choose a reason for hiding this comment

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

Are we OK with this name? I originally had formatDevicePartition, and @displague called out the formatting produced is specific to Linux.

Copy link
Member

@displague displague left a comment

Choose a reason for hiding this comment

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

This will be very helpful in future workflows - no doubt we will want to build on this with other internal helper functions (for other string comparisons or modifications) and also for metadata mining.

I could see use in having a helper that chooses the most appropriate disk based on some heuristics, or one that returns the appropriate NIC or bonding device names.

I found the existing template operators helpful when reviewing this https://pkg.go.dev/text/template#pkg-overview (I wanted to make sure we weren't duplicating functionality with the string comparators).

@chrisdoherty4 chrisdoherty4 added the ready-to-merge Signal to Mergify to merge the PR. label Aug 26, 2022
@mergify mergify bot merged commit ed90557 into tinkerbell:main Aug 26, 2022
mergify bot added a commit that referenced this pull request Aug 29, 2022
I'm a maintainer of several other services often related to the Kuberenetes back-end/Kubernetes controllers and I'm taking ownership for a lot of release synchronization making it both appropriate and necessary for me to maintain aspects of the Tink repository.

Requirements:

- I have reviewed the [community membership guidelines](https://github.com/tinkerbell/proposals/blob/main/proposals/0024/GOVERNANCE.md)
- I have [enabled 2FA on my GitHub account](https://github.com/settings/security)
- I have subscribed to the [tinkerbell-contributors e-mail list](https://groups.google.com/g/tinkerbell-contributors)
- I am actively contributing to 1 or more Tinkerbell subprojects

Here is a list of non-trival PRs I have been the primary reviewer on:

#596
#628
#614

I have also made a number of code contributions to this repository, here are a few of them:

#638
#631
#626
#622
#612

I have also raised various issues and am driving the releasing across Tinkerbell including in this repository.

Requesting consideration of expedited responsibilities: yes

Sponsors:

- @mmlb (Maintainer)
- @micahhausler (Maintainer)
- @jacobweinstock (Core contributor in other Tinkerbell repositories)
@chrisdoherty4 chrisdoherty4 deleted the exp/hardware-ref branch May 15, 2023 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-to-merge Signal to Mergify to merge the PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants