Skip to content

[cozy-lib] Improve flatten function - #1647

Merged
Andrei Kvapil (kvaps) merged 1 commit into
mainfrom
fix/cozy-lib-limits-v2
Nov 25, 2025
Merged

[cozy-lib] Improve flatten function#1647
Andrei Kvapil (kvaps) merged 1 commit into
mainfrom
fix/cozy-lib-limits-v2

Conversation

@lllamnyp

@lllamnyp Timofei Larkin (lllamnyp) commented Nov 21, 2025

Copy link
Copy Markdown
Member

This patch breaks introduces a helper function in cozy-lib to correctly handle special case resources when transforming a nested map of limits and requests to a flat map suitable for use in resourceQuotas. As a result, admins can now specify any types of resources as resource quotas for tenants, and they will be correctly transformed to the correct format for the underlying kubernetes ResourceQuota. In addition to the previously supported compute resources, such as CPU, memory, and custom resources, like GPUs, special quota strings such as "services.loadbalancers" are now correctly handled.

[cozy-lib,platform] Support resource quotas for special kubernetes
quotas, such as service.loadbalncer count and others.

What this PR does

Release note

[cozy-lib] Improve flatten function

This patch breaks introduces a helper function in cozy-lib to correctly
handle special case resources when transforming a nested map of limits
and requests to a flat map suitable for use in resourceQuotas. As a
result, admins can now specify any types of resources as resource quotas
for tenants, and they will be correctly transformed to the correct
format for the underlying kubernetes ResourceQuota. In addition to the
previously supported compute resources, such as CPU, memory, and custom
resources, like GPUs, special quota strings such as
"services.loadbalancers" are now correctly handled.

```release-note
[cozy-lib,platform] Support resource quotas for special kubernetes
quotas, such as service.loadbalncer count and others.
```

Signed-off-by: Timofei Larkin <lllamnyp@gmail.com>
@dosubot dosubot Bot added the size/L This PR changes 100-499 lines, ignoring generated files label Nov 21, 2025
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello Timofei Larkin (@lllamnyp), 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 refines the resource flattening logic within cozy-lib to improve how resource quotas are handled for Kubernetes ResourceQuota objects. By introducing a new helper function and restructuring the main flattening logic, it ensures that both standard compute resources and special Kubernetes quota strings are accurately transformed, providing greater flexibility and correctness for resource management.

Highlights

  • Enhanced Resource Quota Handling: The cozy-lib.resources.flatten function has been significantly refactored to correctly process a wider range of resource quota types, including special Kubernetes quota strings like "services.loadbalancers" alongside standard compute resources.
  • New Helper Function Introduced: A new helper, cozy-lib.resources.flattenResource, was introduced to streamline the transformation of resource keys into the format expected by Kubernetes ResourceQuota objects, improving modularity and clarity.
  • Improved Flexibility for Administrators: This change allows administrators to specify any type of resource as a resource quota for tenants, ensuring proper transformation for the underlying Kubernetes ResourceQuota API and expanding configuration possibilities.
  • Expanded Test Coverage: The associated test files (quota_test.yaml and quota_values.yaml) have been updated to validate the new flattening logic, including custom resources (foobar) and storage quotas, and to confirm that incorrect or redundant keys are not generated.
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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

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 pull request 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.

@coderabbitai

coderabbitai Bot commented Nov 21, 2025

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/cozy-lib-limits-v2

Tip

📝 Customizable high-level summaries are now available in beta!

You can now customize how CodeRabbit generates the high-level summary in your pull requests — including its content, structure, tone, and formatting.

  • Provide your own instructions using the high_level_summary_instructions setting.
  • Format the summary however you like (bullet lists, tables, multi-section layouts, contributor stats, etc.).
  • Use high_level_summary_in_walkthrough to move the summary from the description to the walkthrough section.

Example instruction:

"Divide the high-level summary into five sections:

  1. 📝 Description — Summarize the main change in 50–60 words, explaining what was done.
  2. 📓 References — List relevant issues, discussions, documentation, or related PRs.
  3. 📦 Dependencies & Requirements — Mention any new/updated dependencies, environment variable changes, or configuration updates.
  4. 📊 Contributor Summary — Include a Markdown table showing contributions:
    | Contributor | Lines Added | Lines Removed | Files Changed |
  5. ✔️ Additional Notes — Add any extra reviewer context.
    Keep each section concise (under 200 words) and use bullet or numbered lists for clarity."

Note: This feature is currently in beta for Pro-tier users, and pricing will be announced later.


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

Comment @coderabbitai help to get the list of available commands and usage tips.

@dosubot dosubot Bot added the kind/feature Categorizes issue or PR as related to a new feature label Nov 21, 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 significantly improves the cozy-lib.resources.flatten helper by refactoring it to correctly handle special Kubernetes resource quota types alongside standard compute resources. The introduction of the cozy-lib.resources.flattenResource helper centralizes the transformation logic, making the implementation much cleaner and more robust. The accompanying test changes correctly validate the new functionality.

My review includes a few suggestions for the new cozy-lib.resources.flattenResource helper to enhance its correctness and maintainability. I've pointed out a potential typo in a resource key and recommended expanding the documentation and refactoring the conditional logic to be more explicit. Overall, this is a great enhancement.

"services"
"services.loadbalancers"
"services.nodeports"
"services.clusterip"

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.

high

It appears there might be a typo here. According to the Kubernetes documentation, the correct resource quota key for ClusterIP services is services.clusterips (plural), not services.clusterip (singular). Using the singular form will likely result in the quota not being applied correctly.

        "services.clusterips"

Comment on lines +190 to +194
This is a helper function that takes an argument like `list "limits" "services.loadbalancers"`
or `list "limits" "storage"` or `list "requests" "cpu"` and returns "services.loadbalancers",
"", and "requests.cpu", respectively, thus transforming them to an acceptable format for k8s
ResourceQuotas objects.
*/}}

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

The comment provides good examples, but for better long-term maintainability, it would be beneficial to also document the general rules being applied for the transformation. This would make the logic in the template easier to understand without needing to trace the code.

Consider adding a brief explanation of the rules, for example:

  • Standard compute and custom resources (e.g., cpu, memory) are prefixed with requests. or limits..
  • Raw Kubernetes object count quotas (e.g., pods, services.loadbalancers) are treated as limits; the limits. prefix is removed, and any requests. version is dropped.
  • limits.storage is explicitly dropped as it's not a valid resource quota key, while requests.storage is preserved.
  This helper function transforms sanitized resource sections and types into the final key format for a Kubernetes ResourceQuota.
  The transformation rules are as follows:
  - Standard compute/custom resources (e.g., `cpu`, `memory`) are prefixed with `requests.` or `limits.`.
  - Raw object count quotas (e.g., `pods`, `services.loadbalancers`) are treated as limits; the `limits.` prefix is removed, and any `requests.` version is dropped.
  - `limits.storage` is explicitly dropped as it's not a valid key, while `requests.storage` is preserved.

Comment on lines +210 to +218
{{- $out := "" }}
{{- if and (eq $section "limits") (eq $type "storage") }}
{{- $out = "" }}
{{- else if and (eq $section "limits") (has $type $rawQuotaKeys) }}
{{- $out = $type }}
{{- else if not (has $type $rawQuotaKeys) }}
{{- $out = printf "%s.%s" $section $type }}
{{- end }}
{{- $out -}}

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

The current logic for flattening resource keys is correct, but its reliance on fall-through for certain cases (like requests for a raw quota key) makes it less explicit and harder to reason about. To improve clarity and maintainability, consider restructuring the conditional logic to explicitly handle all cases, including those that result in an empty string. This makes the function's behavior more self-documenting.

{{-   $out := "" }}
{{-   if and (eq $section "requests") (has $type $rawQuotaKeys) }}
{{-     /* Raw quota keys are counts and do not have a 'requests' counterpart. */}}
{{-   else if and (eq $section "limits") (eq $type "storage") }}
{{-     /* 'limits.storage' is not a valid resource quota key. */}}
{{-   else if and (eq $section "limits") (has $type $rawQuotaKeys) }}
{{-     $out = $type }}
{{-   else if not (has $type $rawQuotaKeys) }}
{{-     $out = printf "%s.%s" $section $type }}
{{-   end }}
{{-   $out -}}

Base automatically changed from fix/cozy-lib-limits to main November 25, 2025 13:45

@kvaps Andrei Kvapil (kvaps) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Nov 25, 2025
@kvaps
Andrei Kvapil (kvaps) merged commit fb7e39e into main Nov 25, 2025
22 checks passed
@kvaps
Andrei Kvapil (kvaps) deleted the fix/cozy-lib-limits-v2 branch November 25, 2025 13:48
@kvaps Andrei Kvapil (kvaps) added the backport Should change be backported on previous release label Nov 25, 2025
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 kind/feature Categorizes issue or PR as related to a new feature lgtm This PR has been approved by a maintainer size/L This PR changes 100-499 lines, ignoring generated files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants