Skip to content

Fix missing cozy-lib.resources.flatten template - #1372

Merged
Andrei Kvapil (kvaps) merged 1 commit into
mainfrom
fix-tenant-quotas
Sep 1, 2025
Merged

Fix missing cozy-lib.resources.flatten template#1372
Andrei Kvapil (kvaps) merged 1 commit into
mainfrom
fix-tenant-quotas

Conversation

@kvaps

@kvaps Andrei Kvapil (kvaps) commented Sep 1, 2025

Copy link
Copy Markdown
Member

Signed-off-by: Andrei Kvapil kvapss@gmail.com

What this PR does

Release note

[tenant] Fix missing cozy-lib.resources.flatten template

Summary by CodeRabbit

  • New Features
    • Added support to output resource quotas as a flattened key-value map using dot-notation (e.g., limits.cpu, requests.memory) for easier reading and overrides.
    • Outputs are grouped under a top-level resourceQuotas section, ready for YAML-based configuration and tooling.
    • Backward compatible: this is an additive capability and does not change existing behavior.

Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
@coderabbitai

coderabbitai Bot commented Sep 1, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

Adds 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

Cohort / File(s) Summary of Changes
Resource flattening template
packages/library/cozy-lib/templates/_resources.tpl
Introduces cozy-lib.resources.flatten template that calls cozy-lib.resources.sanitize, parses YAML, flattens nested resource maps into dot-notation keys, and outputs YAML under resourceQuotas. Public/exported template addition only; no modifications to existing logic.

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
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I hop through maps with nimble paws,
Turning nests to dotted laws.
limits.cpu, requests.memory—oh my!
Flattened fields beneath one sky.
In YAML burrows, quotas bloom—
A tidy warren, room by room. 🐇✨

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-tenant-quotas

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.tpl file.
  • 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 a resourceQuotas key 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

  1. 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.

@kvaps Andrei Kvapil (kvaps) added the backport Should change be backported on previous release label Sep 1, 2025

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment on lines +175 to +186

{{- 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 }}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

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 }}

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (4)
packages/library/cozy-lib/templates/_resources.tpl (4)

179-184: Deterministic output order to reduce diff churn

Map 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 possible

We 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 usage

Flatten 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 map

If 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.

📥 Commits

Reviewing files that changed from the base of the PR and between 138e5fb and fe869b9.

📒 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 template

The flatten helper is small, readable, and composes correctly with sanitize. No blockers.

{{- $_ := set $out $key $v }}
{{- end }}
{{- end }}
{{- dict "resourceQuotas" $out | toYaml }}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💡 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.

@kvaps
Andrei Kvapil (kvaps) merged commit 8ca8817 into main Sep 1, 2025
20 checks passed
@kvaps
Andrei Kvapil (kvaps) deleted the fix-tenant-quotas branch September 1, 2025 11:09
@github-actions

github-actions Bot commented Sep 1, 2025

Copy link
Copy Markdown

Successfully created backport PR for release-0.35:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport Should change be backported on previous release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant