[cozy-lib] refactor resources - #1127
Conversation
|
""" WalkthroughA 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
Suggested labels
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms (2)
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. 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
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 + donepackages/library/cozy-lib/templates/_resources.tpl (1)
105-106: Consider precision loss with integer conversionUsing
| intfor 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
📒 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/bashShow 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.yamlSearch 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/dashboardIf 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.yamlpackages/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 5scripts/migrations/16 (2)
80-80: Verify the implications of setting appVersion to "*".Setting
appVersionto "*" for all resources might have unintended consequences for version tracking or compatibility checks.Please verify that setting
appVersionto "*" 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 donepackages/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
$globalparameter, 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/bashExtract 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 -->
| memory: 160Mi | ||
| requests: | ||
| cpu: 100m | ||
| memory: 1600Mi | ||
| memory: 160Mi |
There was a problem hiding this comment.
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.
| # requests + limits merged; requests override limits on key collision | ||
| | ($res.requests? // {}) as $req | ||
| | ($res.limits? // {}) as $lim | ||
| | ($req + $lim) as $flat |
There was a problem hiding this comment.
🛠️ 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.
| # 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.
| {{- $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" ) |
There was a problem hiding this comment.
🛠️ 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.
| {{- $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.
b4d7b05 to
52f0f98
Compare
Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
52f0f98 to
036fa6f
Compare
…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 -->
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 -->
Summary by CodeRabbit