Fix missing cozy-lib.resources.flatten template - #1372
Conversation
Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
WalkthroughAdds a new public template cozy-lib.resources.flatten that sanitizes the current context’s resources, parses YAML, flattens nested keys into dot-notation, and emits them under resourceQuotas. Existing templates remain unchanged. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Caller
participant Flatten as cozy-lib.resources.flatten
participant Sanitize as cozy-lib.resources.sanitize
participant YAML as YAML Parser
Caller->>Flatten: Invoke with current context
Flatten->>Sanitize: Sanitize resource values
Sanitize-->>Flatten: Nested resource map (YAML string)
Flatten->>YAML: Parse to map
YAML-->>Flatten: Nested map
loop For each section and key
Flatten->>Flatten: Build dot-notation keys (e.g., limits.cpu)
end
Flatten-->>Caller: Emit YAML under resourceQuotas
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
✨ Finishing Touches🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Summary of Changes
Hello Andrei Kvapil (@kvaps), I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request resolves a missing template definition essential for resource management within the cozy-lib library. It integrates a new Go template designed to standardize and flatten resource specifications, thereby ensuring uniform handling and representation of resource quotas throughout the system.
Highlights
- New Template Definition: Introduces the "cozy-lib.resources.flatten" Go template within the
_resources.tplfile. - Resource Flattening Logic: The newly added template processes and flattens resource definitions, converting them into a dot-separated key-value structure (e.g.,
requests.cpu,limits.memory) and encapsulating them under aresourceQuotaskey in YAML format.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | Gemini (@gemini-code-assist) | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on Gemini (@gemini-code-assist) comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Code Review
This pull request adds a new Helm template helper cozy-lib.resources.flatten to flatten sanitized resource definitions. The implementation is correct, but it lacks documentation. I've suggested adding a comment block to explain its functionality, consistent with other helpers in the file, to improve maintainability.
|
|
||
| {{- define "cozy-lib.resources.flatten" -}} | ||
| {{- $out := dict -}} | ||
| {{- $res := include "cozy-lib.resources.sanitize" . | fromYaml -}} | ||
| {{- range $section, $values := $res }} | ||
| {{- range $k, $v := $values }} | ||
| {{- $key := printf "%s.%s" $section $k }} | ||
| {{- $_ := set $out $key $v }} | ||
| {{- end }} | ||
| {{- end }} | ||
| {{- dict "resourceQuotas" $out | toYaml }} | ||
| {{- end }} |
There was a problem hiding this comment.
For consistency with other helpers in this file and to improve maintainability, please add a documentation comment block for this new template. It should explain its purpose, expected input, and provide an example of its output.
{{- /*
The flatten helper takes a resource map, sanitizes it using
`cozy-lib.resources.sanitize`, and then flattens the resulting `requests`
and `limits` sections into a single map with dot-separated keys. The
resulting map is suitable for use in a `ResourceQuota` object's `hard`
field.
Usage example:
{{ include "cozy-lib.resources.flatten" list (.Values.resources $) }}
Example input:
==============
cpu: "2"
memory: 256Mi
Example output (cpuAllocationRatio = 10):
=========================================
resourceQuotas:
limits.cpu: "2"
limits.memory: 256Mi
requests.cpu: 200m
requests.memory: 256Mi
*/}}
{{- define "cozy-lib.resources.flatten" -}}
{{- $out := dict -}}
{{- $res := include "cozy-lib.resources.sanitize" . | fromYaml -}}
{{- range $section, $values := $res }}
{{- range $k, $v := $values }}
{{- $key := printf "%s.%s" $section $k }}
{{- $_ := set $out $key $v }}
{{- end }}
{{- end }}
{{- dict "resourceQuotas" $out | toYaml }}
{{- end }}
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (4)
packages/library/cozy-lib/templates/_resources.tpl (4)
179-184: Deterministic output order to reduce diff churnMap iteration order is nondeterministic; sort sections and keys for stable YAML.
Apply:
-{{- range $section, $values := $res }} - {{- range $k, $v := $values }} - {{- $key := printf "%s.%s" $section $k }} - {{- $_ := set $out $key $v }} - {{- end }} -{{- end }} +{{- $sectionKeys := keys $res | sortAlpha -}} +{{- range $section := $sectionKeys -}} + {{- $values := index $res $section -}} + {{- $keys := keys $values | sortAlpha -}} + {{- range $k := $keys -}} + {{- $v := index $values $k -}} + {{- $key := printf "%s.%s" $section $k -}} + {{- $_ := set $out $key $v -}} + {{- end -}} +{{- end -}}
178-178: Avoid YAML round-trip if possibleWe reparse sanitize’s YAML with fromYaml. If feasible, add a variant that returns a dict so flatten can skip parsing.
Happy to draft a sanitizeDict helper and wire this template to it.
176-186: Document expected input shape and usageFlatten passes its dot to sanitize, which expects a 2-item list: [resourcesMap, $]. Add a short comment with an example include to prevent misuse.
Example:
{{/* Usage: {{ include "cozy-lib.resources.flatten" (list .Values.resources $) | nindent 2 }} */}}
185-185: Alternative: return a bare flattened mapIf consumers prefer embedding under spec.hard, consider returning the map without a wrapper.
-{{- dict "resourceQuotas" $out | toYaml }} +{{- $out | toYaml }}
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
packages/library/cozy-lib/templates/_resources.tpl(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Build
🔇 Additional comments (1)
packages/library/cozy-lib/templates/_resources.tpl (1)
176-186: LGTM: helpful additive templateThe flatten helper is small, readable, and composes correctly with sanitize. No blockers.
| {{- $_ := set $out $key $v }} | ||
| {{- end }} | ||
| {{- end }} | ||
| {{- dict "resourceQuotas" $out | toYaml }} |
There was a problem hiding this comment.
💡 Verification agent
🧩 Analysis chain
Confirm wrapper key matches consumers
This emits under a top-level key named resourceQuotas. Verify call sites actually expect that key (vs. embedding directly under spec.hard for a ResourceQuota).
Run to check expected consumers and key usage:
🏁 Script executed:
#!/bin/bash
# Where is the new template used?
rg -nP -C2 $'include\\s+"cozy-lib\\.resources\\.flatten"'
# Do any templates expect "resourceQuotas:"?
rg -n 'resourceQuotas:' -g 'packages/**'
# Typical K8s usage expects "hard:"; do we embed there?
rg -nP -C2 $'kind:\\s*ResourceQuota|\\bhard:' -g 'packages/**'Length of output: 4830
Remove the resourceQuotas wrapper from the flatten helper
The cozy-lib.resources.flatten template currently emits:
{{- dict "resourceQuotas" $out | toYaml }}
which, when included under spec.hard, produces spec.hard.resourceQuotas.*—but Kubernetes expects resource name keys directly under hard. In packages/library/cozy-lib/templates/_resources.tpl at line 185, replace the wrapper with the raw map:
- {{- dict "resourceQuotas" $out | toYaml }}
+ {{- $out | toYaml }}This ensures include "cozy-lib.resources.flatten" will render quota entries directly under spec.hard.
🤖 Prompt for AI Agents
In packages/library/cozy-lib/templates/_resources.tpl around line 185, the
flatten helper currently wraps the map in a "resourceQuotas" key which causes
output like spec.hard.resourceQuotas.*; remove that wrapper and render the raw
map so keys appear directly under spec.hard by outputting the $out map with
toYaml (no dict wrapper) and preserving existing indentation/whitespace
handling.
|
Successfully created backport PR for |
Signed-off-by: Andrei Kvapil kvapss@gmail.com
What this PR does
Release note
Summary by CodeRabbit