Skip to content

[cozy-lib] refactor resources - #1127

Merged
Andrei Kvapil (kvaps) merged 1 commit into
mainfrom
resources
Jul 2, 2025
Merged

[cozy-lib] refactor resources#1127
Andrei Kvapil (kvaps) merged 1 commit into
mainfrom
resources

Conversation

@kvaps

@kvaps Andrei Kvapil (kvaps) commented Jun 27, 2025

Copy link
Copy Markdown
Member
  • [cozy-lib, bug] divf by cpu ratio, not mulf
  • [cozy-lib] remove handler for nested resources/requests map
  • [cozy-lib] Introduce memory-allocation-ratio and ephemeral-strorage-allocation-ratio options
  • [system] Recuce resources for some system apps
  • [hack] Add migration script for fixing nested resource maps

Summary by CodeRabbit

  • New Features
    • Introduced a migration process to enhance resource configuration by consolidating CPU and memory settings.
    • System version is automatically updated to reflect the latest changes.

@coderabbitai

coderabbitai Bot commented Jun 27, 2025

Copy link
Copy Markdown
Contributor

"""

Walkthrough

A new shell script has been introduced to automate the migration of Kubernetes custom resources from version 16 to 17. The script patches resource definitions by merging CPU and memory values from requests and limits, updates an "appVersion" field, and records the new version in a configmap.

Changes

File(s) Change Summary
scripts/migrations/16/fix-nested-resources-map.sh Added a migration script to patch CRDs, merge resource values, update fields, and set version.

Suggested labels

enhancement, size:M, lgtm

Poem

A script hops in, so clever and neat,
Merging resources where requests and limits meet.
It patches and updates, version seventeen in sight,
CozyStack’s journey, now lighter and bright.
With a twitch of my nose and a bashful grin,
Here’s to migrations—let the upgrades begin! 🐇✨
"""


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 52f0f98 and 036fa6f.

📒 Files selected for processing (1)
  • scripts/migrations/16 (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • scripts/migrations/16
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: pre-commit
  • GitHub Check: Build

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.
    • Explain this complex logic.
    • 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. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

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

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai 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

Documentation and Community

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

@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: 3

🧹 Nitpick comments (2)
scripts/migrations/16 (1)

91-91: Consider adding error handling for kubectl patch operations.

The script executes dynamically generated kubectl commands with sh -ex, but there's limited error handling for patch failures.

Consider adding more robust error handling:

-  ' | sh -ex
+  ' | while read -r cmd; do
+      echo "Executing: $cmd"
+      if ! eval "$cmd"; then
+        echo "Warning: Failed to patch resource, continuing..."
+      fi
+    done
packages/library/cozy-lib/templates/_resources.tpl (1)

105-106: Consider precision loss with integer conversion

Using | int for memory and ephemeral storage calculations may cause precision loss when allocation ratios result in fractional values. This differs from CPU handling which preserves decimal precision with | toString.

Consider preserving precision for memory and ephemeral storage:

-      $_ := set $output.requests $k ($memoryRequestF64 | int)
+      $_ := set $output.requests $k ($memoryRequestF64 | toString)
-      $_ := set $output.requests $k ($ephemeralStorageRequestF64 | int) 
+      $_ := set $output.requests $k ($ephemeralStorageRequestF64 | toString)

However, verify if Kubernetes accepts fractional values for these resource types before making this change.

Also applies to: 110-111

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9dc21c6 and b4d7b05.

📒 Files selected for processing (6)
  • packages/library/cozy-lib/templates/_resourcepresets.tpl (1 hunks)
  • packages/library/cozy-lib/templates/_resources.tpl (3 hunks)
  • packages/system/dashboard/values.yaml (1 hunks)
  • packages/system/fluxcd-operator/values.yaml (1 hunks)
  • packages/system/vertical-pod-autoscaler/values.yaml (2 hunks)
  • scripts/migrations/16 (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Build
🔇 Additional comments (13)
packages/system/dashboard/values.yaml (1)

30-32: ```shell
#!/bin/bash

Show the first 60 lines of the values.yaml under review

echo "=== packages/system/dashboard/values.yaml (lines 1-60) ==="
sed -n '1,60p' packages/system/dashboard/values.yaml

Search for redis.master in this values.yaml

echo "=== Searching for 'redis.master' in the same file ==="
grep -n "redis.master" packages/system/dashboard/values.yaml || echo "No direct redis.master found"

Locate Chart.yaml for this chart to inspect subchart dependencies

echo "=== Locate Chart.yaml under packages/system/dashboard ==="
fd Chart.yaml -t f packages/system/dashboard

If found, print its contents

if [ -f packages/system/dashboard/Chart.yaml ]; then
echo "=== packages/system/dashboard/Chart.yaml (lines 1-200) ==="
sed -n '1,200p' packages/system/dashboard/Chart.yaml
else
echo "Chart.yaml not found at expected path"
fi


</details>
<details>
<summary>packages/system/fluxcd-operator/values.yaml (1)</summary>

`10-14`: I need to confirm the exact resource settings in `packages/system/fluxcd-operator/values.yaml`. Let's extract the top of that file:


```shell
#!/bin/bash
# Show the first 50 lines to locate the resources block
sed -n '1,50p' packages/system/fluxcd-operator/values.yaml

# Search for the “resources:” section
rg -n "resources" packages/system/fluxcd-operator/values.yaml
packages/system/vertical-pod-autoscaler/values.yaml (1)

8-11: Verify VPA updater can operate with 10x memory reduction.

The memory reduction from 1100Mi to 110Mi (10x decrease) is extremely significant for the VPA updater component.

Please verify that the VPA updater can function properly with this dramatically reduced memory allocation, as VPA components typically need substantial memory for cluster analysis.

#!/bin/bash
# Description: Check VPA memory usage patterns and requirements
# Expected: Find VPA-related configurations and memory requirements

# Search for VPA memory-related configurations
rg -i "vpa.*memory|memory.*vpa|vertical.*pod.*autoscaler.*memory" --type yaml -A 3 -B 3

# Look for VPA resource requirements or recommendations
rg -i "vpa.*resource|vertical.*autoscaler.*resource" --type yaml -A 5
scripts/migrations/16 (2)

80-80: Verify the implications of setting appVersion to "*".

Setting appVersion to "*" for all resources might have unintended consequences for version tracking or compatibility checks.

Please verify that setting appVersion to "*" is the intended behavior and won't affect application lifecycle management.


57-60: Potential data loss: only CPU and memory are preserved.

The filter only keeps CPU and memory resources, discarding other important resources like ephemeral-storage, which might be needed by applications.

Please verify that discarding non-CPU/memory resources won't break existing applications:

#!/bin/bash
# Description: Check if any CRDs use resources other than CPU/memory
# Expected: Find usage of ephemeral-storage or other resource types

# Search for ephemeral-storage usage in the target CRDs
for crd in clickhouses etcds ferretdb httpcaches; do
  kubectl get "$crd.apps.cozystack.io" -A -o json 2>/dev/null | jq -r '
    .items[] | 
    select(. | paths | select(.[-1] == "ephemeral-storage") | length > 0) |
    "\(.metadata.namespace // "cluster-scoped")/\(.metadata.name)"
  ' 2>/dev/null || true
done
packages/library/cozy-lib/templates/_resourcepresets.tpl (3)

13-19: CPU values doubled across all presets.

The CPU allocations have been roughly doubled (e.g., nano: 125m → 250m, micro: 250m → 500m). This is a significant increase that should be validated against actual usage patterns.

Please verify that the increased CPU allocations align with observed workload requirements and don't lead to resource over-provisioning.


10-10: Template signature change requires verification.

The template now accepts a second $global parameter, which changes the API contract.

Please verify that all callers of this template have been updated to pass the global context:

#!/bin/bash
# Description: Find all usages of cozy-lib.resources.preset template
# Expected: All calls should now include global parameter

# Search for template calls
rg 'include.*cozy-lib\.resources\.preset' --type yaml -A 2 -B 2

42-43: ```shell
#!/bin/bash

Extract the cozy-lib.resources.sanitize function definition and body

sed -n '87,200p' packages/library/cozy-lib/templates/_resources.tpl


</details>
<details>
<summary>packages/library/cozy-lib/templates/_resources.tpl (5)</summary>

`4-9`: **LGTM: Appropriate default allocation ratios**

The default allocation ratios are well-chosen:
- Memory ratio of `1` means requests equal limits, which is standard for memory resources
- Ephemeral storage ratio of `40` allows for significant overcommitment, which is appropriate for ephemeral storage

---

`21-39`: **LGTM: Consistent pattern for allocation ratio functions**

The new functions follow the same pattern as the existing CPU allocation ratio function, maintaining consistency in the codebase.

---

`58-86`: **LGTM: Clear documentation with helpful examples**

The documentation clearly explains the new flat map input format and provides concrete examples. The breaking change from nested to flat structure is well-documented.

---

`93-95`: **LGTM: Proper validation prevents misuse**

The validation correctly prevents the old nested format usage, ensuring users migrate to the new flat structure. The error message is clear and actionable.

---

`97-115`: **LGTM: Consistent resource handling logic**

The logic correctly handles different resource types:
- CPU: Uses allocation ratio with decimal precision
- Memory: Uses allocation ratio with integer conversion
- Ephemeral storage: Uses allocation ratio with integer conversion  
- Other resources: Requests equal limits

The implementation is consistent and follows the documented behavior.

</details>

</blockquote></details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->

Comment on lines +28 to +31
memory: 160Mi
requests:
cpu: 100m
memory: 1600Mi
memory: 160Mi

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.

⚠️ Potential issue

Verify VPA recommender can operate with 10x memory reduction.

The memory reduction from 1600Mi to 160Mi (10x decrease) is extremely significant for the VPA recommender component, which typically requires substantial memory to analyze cluster-wide resource usage patterns and maintain historical data.

This dramatic memory reduction could lead to OOM kills or severely degraded performance. Please verify this change against actual VPA recommender memory usage in your environment.

🤖 Prompt for AI Agents
In packages/system/vertical-pod-autoscaler/values.yaml around lines 28 to 31,
the memory request for the VPA recommender has been reduced from 1600Mi to
160Mi, which is a 10x decrease. Verify the actual memory usage of the VPA
recommender in your environment to ensure it can operate reliably with this
reduced memory. If the memory usage is close to or exceeds 160Mi, increase the
memory request to prevent OOM kills or performance degradation.

Comment thread scripts/migrations/16
Comment on lines +52 to +55
# requests + limits merged; requests override limits on key collision
| ($res.requests? // {}) as $req
| ($res.limits? // {}) as $lim
| ($req + $lim) as $flat

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.

🛠️ Refactor suggestion

Review the resource merging logic for potential data loss.

The merging logic ($req + $lim) means requests will override limits when the same resource key exists in both. This could lead to unintended behavior if limits were intentionally set higher than requests.

Consider using a more explicit merging strategy:

-        # requests + limits merged; requests override limits on key collision
-        | ($res.requests? // {})       as $req
-        | ($res.limits?   // {})       as $lim
-        | ($req + $lim)                as $flat
+        # Merge limits first, then requests (preserving requests priority)
+        | ($res.limits?   // {})       as $lim
+        | ($res.requests? // {})       as $req
+        | ($lim + $req)                as $flat
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# requests + limits merged; requests override limits on key collision
| ($res.requests? // {}) as $req
| ($res.limits? // {}) as $lim
| ($req + $lim) as $flat
# Merge limits first, then requests (preserving requests priority)
| ($res.limits? // {}) as $lim
| ($res.requests? // {}) as $req
| ($lim + $req) as $flat
🤖 Prompt for AI Agents
In scripts/migrations/16 around lines 52 to 55, the current merging logic uses
($req + $lim), which causes requests to override limits on key collisions,
potentially losing higher limit values. To fix this, implement a merging
strategy that compares each resource key's values in requests and limits, and
retains the higher value for each key instead of blindly overriding. This
ensures that limits are preserved when they are higher than requests.

Comment on lines +30 to +37
{{- $baseEphemeralStorage := dict
"nano" (dict "ephemeral-storage" "2Gi" )
"micro" (dict "ephemeral-storage" "2Gi" )
"small" (dict "ephemeral-storage" "2Gi" )
"medium" (dict "ephemeral-storage" "2Gi" )
"large" (dict "ephemeral-storage" "2Gi" )
"xlarge" (dict "ephemeral-storage" "2Gi" )
"2xlarge" (dict "ephemeral-storage" "2Gi" )

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.

🛠️ Refactor suggestion

Uniform ephemeral storage across all preset sizes may be inadequate.

All presets now have the same ephemeral storage (2Gi), regardless of the workload size. Larger workloads (xlarge, 2xlarge) might require more ephemeral storage for logs, temporary files, etc.

Consider scaling ephemeral storage with workload size:

 {{-   $baseEphemeralStorage := dict
-        "nano"    (dict "ephemeral-storage" "2Gi" )
-        "micro"   (dict "ephemeral-storage" "2Gi" )
-        "small"   (dict "ephemeral-storage" "2Gi" )
-        "medium"  (dict "ephemeral-storage" "2Gi" )
-        "large"   (dict "ephemeral-storage" "2Gi" )
-        "xlarge"  (dict "ephemeral-storage" "2Gi" )
-        "2xlarge" (dict "ephemeral-storage" "2Gi" )
+        "nano"    (dict "ephemeral-storage" "1Gi" )
+        "micro"   (dict "ephemeral-storage" "2Gi" )
+        "small"   (dict "ephemeral-storage" "4Gi" )
+        "medium"  (dict "ephemeral-storage" "8Gi" )
+        "large"   (dict "ephemeral-storage" "16Gi" )
+        "xlarge"  (dict "ephemeral-storage" "32Gi" )
+        "2xlarge" (dict "ephemeral-storage" "64Gi" )
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
{{- $baseEphemeralStorage := dict
"nano" (dict "ephemeral-storage" "2Gi" )
"micro" (dict "ephemeral-storage" "2Gi" )
"small" (dict "ephemeral-storage" "2Gi" )
"medium" (dict "ephemeral-storage" "2Gi" )
"large" (dict "ephemeral-storage" "2Gi" )
"xlarge" (dict "ephemeral-storage" "2Gi" )
"2xlarge" (dict "ephemeral-storage" "2Gi" )
{{- $baseEphemeralStorage := dict
"nano" (dict "ephemeral-storage" "1Gi" )
"micro" (dict "ephemeral-storage" "2Gi" )
"small" (dict "ephemeral-storage" "4Gi" )
"medium" (dict "ephemeral-storage" "8Gi" )
"large" (dict "ephemeral-storage" "16Gi" )
"xlarge" (dict "ephemeral-storage" "32Gi" )
"2xlarge" (dict "ephemeral-storage" "64Gi" )
🤖 Prompt for AI Agents
In packages/library/cozy-lib/templates/_resourcepresets.tpl around lines 30 to
37, the ephemeral storage is uniformly set to 2Gi for all preset sizes, which
may be insufficient for larger workloads. Adjust the ephemeral-storage values to
increase progressively with workload size, assigning larger values for presets
like xlarge and 2xlarge to better accommodate their storage needs.

Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
@kvaps
Andrei Kvapil (kvaps) merged commit 55e11fc into main Jul 2, 2025
@kvaps
Andrei Kvapil (kvaps) deleted the resources branch July 2, 2025 07:23
Andrei Kvapil (kvaps) added a commit that referenced this pull request Jul 2, 2025
…1128)

ref to #1127,
clastix/kamaji#856 and
cozystack/etcd-operator#291

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **New Features**
  * Updated etcd chart to version 2.9.0.
* **Improvements**
* Simplified etcd endpoint configuration to use a single static
endpoint.
* Expanded TLS certificate DNS names to include additional service
addresses.
  * Streamlined resource configuration for etcd deployment.
* **Chores**
  * Updated version mapping for etcd package.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Andrei Kvapil (kvaps) added a commit that referenced this pull request Jul 3, 2025
Add. missing commits from
#1127, which were skipped by
mistake

- [cozy-lib, bug] divf by cpu ratio, not mulf (#1125)
- [cozy-lib] remove handler for nested resources/requests map
- [cozy-lib] Introduce memory-allocation-ratio and
ephemeral-strorage-allocation-ratio options
- [system] Recuce resources for some system apps

<!-- Thank you for making a contribution! Here are some tips for you:
- Start the PR title with the [label] of Cozystack component:
- For system components: [platform], [system], [linstor], [cilium],
[kube-ovn], [dashboard], [cluster-api], etc.
- For managed apps: [apps], [tenant], [kubernetes], [postgres],
[virtual-machine] etc.
- For development and maintenance: [tests], [ci], [docs], [maintenance].
- If it's a work in progress, consider creating this PR as a draft.
- Don't hesistate to ask for opinion and review in the community chats,
even if it's still a draft.
- Add the label `backport` if it's a bugfix that needs to be backported
to a previous version.
-->

## What this PR does


### Release note

<!--  Write a release note:
- Explain what has changed internally and for users.
- Start with the same [label] as in the PR title
- Follow the guidelines at
https://github.com/kubernetes/community/blob/master/contributors/guide/release-notes.md.
-->

```release-note
[cozy-lib] refactor resources
```

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **New Features**
* Introduced support for memory and ephemeral storage allocation ratios,
allowing more flexible resource allocation.

* **Refactor**
* Simplified resource preset structure for easier configuration and
management.
* Updated resource preset logic to use a new sanitization process for
resource values.

* **Bug Fixes**
  * Improved error handling for invalid resource preset keys.

* **Chores**
* Adjusted resource requests and limits for Redis master, FluxCD
operator, and Vertical Pod Autoscaler components to optimize resource
usage.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
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