Skip to content

Conversation

lllamnyp
Copy link
Member

@lllamnyp lllamnyp commented Jul 24, 2025

If running in Hetzner and using Hetzner's cloud load balancers, node ports need to be allocated for the load balancer to function correctly. Therefore if RobotLB is enabled, we probably need to assign node ports.

Release note:
[platform] Autodetect if node ports should be assigned to load balancer services.

What this PR does

Release note

[platform] Autodetect RobotLB

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features
    • Added conditional control to disable load balancer node ports for external services including PostgreSQL, Redis, HAProxy, and others based on configuration.
  • Chores
    • Introduced new configuration options to flexibly manage network-related service settings across multiple applications.

@lllamnyp lllamnyp requested review from kvaps and klinch0 as code owners July 24, 2025 14:02
Copy link
Contributor

coderabbitai bot commented Jul 24, 2025

Walkthrough

A new Helm template helper for network configuration is introduced, allowing conditional disabling of load balancer node ports based on the presence of the "robotlb" component in the configuration. Multiple service templates, including PostgreSQL, FerretDB, HAProxy, Redis, TCP Balancer, Virtual Machine, and VM Instance, are updated to use this helper, setting allocateLoadBalancerNodePorts: false when appropriate.

Changes

File(s) Change Summary
packages/library/cozy-lib/templates/_network.tpl Added new template helper functions for determining load balancer node port allocation based on external config and defaults.
packages/apps/postgres/templates/external-svc.yaml Conditionally set allocateLoadBalancerNodePorts: false in service spec using the new helper.
packages/apps/ferretdb/templates/external-svc.yaml Conditionally set allocateLoadBalancerNodePorts: false in LoadBalancer service spec using the new helper.
packages/apps/http-cache/templates/haproxy/service.yaml Conditionally set allocateLoadBalancerNodePorts: false in LoadBalancer service spec using the new helper.
packages/apps/redis/templates/service.yaml Conditionally set allocateLoadBalancerNodePorts: false in LoadBalancer service spec using the new helper.
packages/apps/tcp-balancer/templates/service.yaml Conditionally set allocateLoadBalancerNodePorts: false in LoadBalancer service spec using the new helper.
packages/apps/virtual-machine/templates/service.yaml Conditionally set allocateLoadBalancerNodePorts: false in LoadBalancer service spec using the new helper.
packages/apps/vm-instance/templates/service.yaml Conditionally set allocateLoadBalancerNodePorts: false in LoadBalancer service spec using the new helper.

Sequence Diagram(s)

sequenceDiagram
    participant Helm Chart
    participant cozy-lib.network.disableLoadBalancerNodePorts
    participant cozyConfig

    Helm Chart->>cozy-lib.network.disableLoadBalancerNodePorts: Evaluate helper
    cozy-lib.network.disableLoadBalancerNodePorts->>cozyConfig: Load cozyConfig
    alt cozyConfig exists
        cozy-lib.network.disableLoadBalancerNodePorts->>cozyConfig: Read "bundle-enable"
        cozy-lib.network.disableLoadBalancerNodePorts-->>Helm Chart: Return true if "robotlb" not enabled
    else cozyConfig missing
        cozy-lib.network.disableLoadBalancerNodePorts-->>Helm Chart: Return default (true)
    end
    Helm Chart->>Helm Chart: Conditionally set allocateLoadBalancerNodePorts: false in service specs
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • kvaps
  • klinch0

Poem

A helper hops in, so clever and neat,
To check if "robotlb" makes our config complete.
If not, node ports are hidden from view—
Services all gleam with a cleaner hue!
With templates and logic, we leap down the lane,
Our charts are now smarter—let’s code once again! 🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/configurable-nodeport-robotlb

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 generate unit tests to generate unit tests for 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.

Copy link

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

Choose a reason for hiding this comment

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

Summary of Changes

Hello @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 implements an automatic detection mechanism to manage the allocation of node ports for Kubernetes LoadBalancer services. The primary goal is to ensure that node ports are correctly assigned or unassigned based on the presence of the RobotLB component, which is crucial for proper load balancer functionality in specific cloud environments like Hetzner.

Highlights

  • Conditional Node Port Allocation: Modified the external-svc.yaml template for PostgreSQL services to conditionally set allocateLoadBalancerNodePorts: false. This ensures node ports are only disabled when appropriate for the environment.
  • New Helm Library Template for Network Logic: Introduced a new Helm template file _network.tpl within the cozy-lib library. This file centralizes the logic for determining whether load balancer node ports should be allocated.
  • RobotLB Autodetection: The new network template includes logic to detect if the robotlb component is enabled via cozyConfig. If robotlb is not enabled, or if cozyConfig is unavailable, node ports for LoadBalancer services will be explicitly disabled. Conversely, if robotlb is enabled, node ports will be allocated (by not applying the allocateLoadBalancerNodePorts: false setting).
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-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-code-assist comments or fill out our survey to provide feedback.

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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

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 (1)
packages/library/cozy-lib/templates/_network.tpl (1)

5-14: Verify the logic implementation matches PR objectives.

The function correctly implements the autodetection logic: when robotlb is NOT present in the enabled components, it returns true to disable load balancer node ports. This aligns with the PR objective of allocating node ports only when RobotLB is enabled.

However, consider adding error handling for edge cases:

 {{- define "cozy-lib.network.disableLoadBalancerNodePorts" }}
 {{-   include "cozy-lib.loadCozyConfig" (list "" .) }}
 {{-   $cozyConfig := index . 1 "cozyConfig" }}
 {{-   if not $cozyConfig }}
 {{-     include "cozy-lib.network.defaultDisableLoadBalancerNodePorts" . }}
 {{-   else }}
-{{-     $enabledComponents := splitList "," ((index $cozyConfig.data "bundle-enable") | default "") }}
+{{-     $bundleEnable := index $cozyConfig.data "bundle-enable" | default "" }}
+{{-     $enabledComponents := splitList "," $bundleEnable }}
 {{-     not (has "robotlb" $enabledComponents) }}
 {{-   end }}
 {{- end }}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0c9ab17 and 7e6e1bd.

📒 Files selected for processing (2)
  • packages/apps/postgres/templates/external-svc.yaml (1 hunks)
  • packages/library/cozy-lib/templates/_network.tpl (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: lllamnyp
PR: cozystack/cozystack#1233
File: packages/system/hetzner-robotlb/charts/robotlb/templates/deployment.yaml:33-35
Timestamp: 2025-07-23T09:15:09.624Z
Learning: The `./charts/robotlb/` directory in the hetzner-robotlb package contains vendored code, and the team generally avoids modifying vendored code to maintain clean separation from upstream dependencies.
packages/library/cozy-lib/templates/_network.tpl (1)

Learnt from: NickVolynkin
PR: #1120
File: packages/apps/clickhouse/README.md:60-67
Timestamp: 2025-07-03T05:54:51.264Z
Learning: The cozy-lib.resources.sanitize function in packages/library/cozy-lib/templates/_resources.tpl supports both standard Kubernetes resource format (with limits:/requests: sections) and flat format (direct resource specifications). The flat format takes priority over nested values. CozyStack apps include cozy-lib as a chart dependency through symlinks in packages/apps/*/charts/cozy-lib directories.

🔇 Additional comments (1)
packages/apps/postgres/templates/external-svc.yaml (1)

10-12: Integration is correct; no changes needed.

The helper name cozy-lib.network.disableLoadBalancerNodePorts matches its definition, and fromYaml is required to turn the "false" string into a boolean false for the if check. Indentation and YAML structure are valid.

Copy link

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

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 introduces logic to automatically detect if node ports should be assigned to load balancer services, specifically when running in Hetzner and using Hetzner's cloud load balancers. The changes involve adding a new template to determine whether to disable LoadBalancer node ports based on the presence of "robotlb" in the enabled components, and adjusting the external service template to conditionally disable node ports.

Comment on lines +10 to +12
{{- if (include "cozy-lib.network.disableLoadBalancerNodePorts" $ | fromYaml) }}
allocateLoadBalancerNodePorts: false
{{- end }}

Choose a reason for hiding this comment

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

medium

The indentation here is inconsistent. The {{- end }} should align with the {{- if }} statement for better readability.

    {{- if (include "cozy-lib.network.disableLoadBalancerNodePorts" $ | fromYaml) }}
    allocateLoadBalancerNodePorts: false
    {{- end }}

@@ -0,0 +1,14 @@
{{- define "cozy-lib.resources.defaultDisableLoadBalancerNodePorts" }}
{{- `true` }}

Choose a reason for hiding this comment

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

medium

Consider adding a comment explaining why the default value is true. This will help future maintainers understand the intent behind this default.

{{-   `true` }} {{/* Default to true to disable node ports */}}

{{- include "cozy-lib.network.defaultDisableLoadBalancerNodePorts" . }}
{{- else }}
{{- $enabledComponents := splitList "," ((index $cozyConfig.data "bundle-enable") | default "") }}
{{- not (has "robotlb" $enabledComponents) }}

Choose a reason for hiding this comment

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

medium

This logic determines whether to disable LoadBalancer node ports based on the presence of "robotlb" in the enabled components. It might be beneficial to add a comment explaining the rationale behind this check, especially linking it to the Hetzner RobotLB context mentioned in the PR description.

{{-     not (has "robotlb" $enabledComponents) }} {{/* Disable node ports if robotlb is NOT enabled */}}

Copy link
Member

@kvaps kvaps left a comment

Choose a reason for hiding this comment

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

LGTM

If running in Hetzner and using Hetzner's cloud load balancers, node
ports need to be allocated for the load balancer to function correctly.
Therefore if RobotLB is enabled, we probably need to assign node ports.

Release note:
[platform] Autodetect if node ports should be assigned to load balancer
services.

Signed-off-by: Timofei Larkin <lllamnyp@gmail.com>
@lllamnyp lllamnyp force-pushed the feat/configurable-nodeport-robotlb branch from 7e6e1bd to 917a6f3 Compare July 24, 2025 15:55
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (3)
packages/apps/virtual-machine/templates/service.yaml (1)

16-18: Same whitespace-trim caveat as noted in tcp-balancer template – consider dropping the - after {{ for safer, explicit indentation.

packages/apps/redis/templates/service.yaml (1)

14-16: Whitespace-trim note duplicated from earlier comment – consider {{ if …}} instead of {{- if …}}.

packages/apps/vm-instance/templates/service.yaml (1)

16-18: Whitespace-trim note duplicated from earlier comment – consider {{ if …}} instead of {{- if …}}.

🧹 Nitpick comments (3)
packages/apps/tcp-balancer/templates/service.yaml (1)

13-15: Whitespace-trim in {{- if …}} may accidentally drop YAML indentation
Because the opening tag uses {{-, Helm trims the four leading spaces, so the rendered block becomes:

  externalTrafficPolicy: Local
allocateLoadBalancerNodePorts: false

Indentation is still valid in this template (the allocate… line keeps the required 2-space indent), but a future copy-paste with a different indent level could silently break the YAML. Switching to a non-trimming opening tag keeps the guard‐indent explicit and safer:

-    {{- if (include "cozy-lib.network.disableLoadBalancerNodePorts" $ | fromYaml) }}
+    {{ if (include "cozy-lib.network.disableLoadBalancerNodePorts" $ | fromYaml) }}
packages/apps/ferretdb/templates/external-svc.yaml (1)

11-13: Repeated snippet suggests extracting a shared partial
The three-line conditional block disabling node-ports now appears in 7 service templates. A small helper partial (e.g. cozy-lib.service.allocateNodePorts) would avoid duplication and guarantee future fixes land everywhere.

No immediate action required for this PR, just a maintainability hint.

packages/apps/http-cache/templates/haproxy/service.yaml (1)

13-15: Align indentation and switch to toBool for a cleaner, safer boolean check

  1. Indentation: the if/end directives are indented by four spaces while their sibling keys (type, externalTrafficPolicy) use two. After the {{- trim, Helm will emit a blank line that is purely cosmetic but keeps the template visually uneven across files. Using two-space indentation here maintains visual consistency and avoids the blank line.

  2. Boolean parsing: fromYaml works, but its intent is to convert documents to structured data. For single booleans, toBool is more idiomatic and avoids edge-cases when the included helper ever returns an empty string.

Suggested change:

-    {{- if (include "cozy-lib.network.disableLoadBalancerNodePorts" $ | fromYaml) }}
-  allocateLoadBalancerNodePorts: false
-    {{- end }}
+  {{- if (include "cozy-lib.network.disableLoadBalancerNodePorts" $ | toBool) }}
+  allocateLoadBalancerNodePorts: false
+  {{- end }}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 7e6e1bd and 917a6f3.

📒 Files selected for processing (8)
  • packages/apps/ferretdb/templates/external-svc.yaml (1 hunks)
  • packages/apps/http-cache/templates/haproxy/service.yaml (1 hunks)
  • packages/apps/postgres/templates/external-svc.yaml (1 hunks)
  • packages/apps/redis/templates/service.yaml (1 hunks)
  • packages/apps/tcp-balancer/templates/service.yaml (1 hunks)
  • packages/apps/virtual-machine/templates/service.yaml (1 hunks)
  • packages/apps/vm-instance/templates/service.yaml (1 hunks)
  • packages/library/cozy-lib/templates/_network.tpl (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/apps/postgres/templates/external-svc.yaml
  • packages/library/cozy-lib/templates/_network.tpl
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: lllamnyp
PR: cozystack/cozystack#1233
File: packages/system/hetzner-robotlb/charts/robotlb/templates/deployment.yaml:33-35
Timestamp: 2025-07-23T09:15:09.658Z
Learning: The `./charts/robotlb/` directory in the hetzner-robotlb package contains vendored code, and the team generally avoids modifying vendored code to maintain clean separation from upstream dependencies.
⏰ 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

@lllamnyp lllamnyp merged commit 2f61798 into main Jul 24, 2025
19 checks passed
@lllamnyp lllamnyp deleted the feat/configurable-nodeport-robotlb branch July 24, 2025 18:41
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