Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: user can set PDB by himself #1093

Merged
merged 1 commit into from
Dec 24, 2024
Merged

feat: user can set PDB by himself #1093

merged 1 commit into from
Dec 24, 2024

Conversation

Rory-Z
Copy link
Member

@Rory-Z Rory-Z commented Dec 24, 2024

Fix #1092

Summary by CodeRabbit

  • New Features

    • Introduced minAvailable and maxUnavailable fields for enhanced pod availability management during updates in EMQX resources.
    • Updated deployment strategy capabilities for better control over replica availability.
  • Documentation

    • Added references to the new fields minAvailable and maxUnavailable in the API documentation for EMQXCoreTemplateSpec and EMQXReplicantTemplateSpec.

@Rory-Z Rory-Z requested a review from a team as a code owner December 24, 2024 01:44
Copy link

coderabbitai bot commented Dec 24, 2024

Warning

Rate limit exceeded

@Rory-Z has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 20 minutes and 54 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 53b8947 and aff9217.

📒 Files selected for processing (9)
  • README.md (1 hunks)
  • apis/apps/v2beta1/emqx_types.go (3 hunks)
  • apis/apps/v2beta1/zz_generated.deepcopy.go (2 hunks)
  • config/crd/bases/apps.emqx.io_emqxes.yaml (4 hunks)
  • controllers/apps/v2beta1/add_pdb.go (1 hunks)
  • deploy/charts/emqx-operator/Chart.yaml (1 hunks)
  • deploy/charts/emqx-operator/templates/crd.emqxes.apps.emqx.io.yaml (4 hunks)
  • docs/en_US/reference/v2beta1-reference.md (3 hunks)
  • docs/zh_CN/reference/v2beta1-reference.md (3 hunks)

Walkthrough

The pull request introduces new fields MinAvailable and MaxUnavailable to the EMQX Operator's custom resource definition, enhancing pod eviction and availability management. These fields allow flexible specification of minimum and maximum pod availability during updates using integer or string values. The changes span multiple files including type definitions, CRD configurations, deep copy generation, controller logic, and documentation in both English and Chinese. The modifications provide more granular control over pod disruption budgets and deployment strategies.

Changes

File Change Summary
apis/apps/v2beta1/emqx_types.go Added MinAvailable and MaxUnavailable fields to EMQXReplicantTemplateSpec, annotated Replicas with default value of 2
apis/apps/v2beta1/zz_generated.deepcopy.go Added intstr import, updated deep copy method for new fields
config/crd/bases/apps.emqx.io_emqxes.yaml Added minAvailable and maxUnavailable fields to CRD specification
controllers/apps/v2beta1/add_pdb.go Modified PDB generation to use dynamic MinAvailable and MaxUnavailable values
deploy/charts/emqx-operator/templates/crd.emqxes.apps.emqx.io.yaml Added minAvailable and maxUnavailable properties to OpenAPI schema
docs/en_US/reference/v2beta1-reference.md Updated documentation with new minAvailable and maxUnavailable fields
docs/zh_CN/reference/v2beta1-reference.md Updated Chinese documentation with new fields

Sequence Diagram

sequenceDiagram
    participant Operator
    participant CRD
    participant PDB
    participant Pods

    Operator->>CRD: Read MinAvailable/MaxUnavailable
    CRD-->>Operator: Return configuration
    Operator->>PDB: Generate PodDisruptionBudget
    PDB-->>Pods: Apply availability constraints
    Pods-->>Operator: Maintain desired availability
Loading

Poem

🐰 Pods dancing, a resilient ballet,
Min and max, they find their way,
Unavailable, yet carefully planned,
EMQX clusters, gracefully manned!
Rabbit's wisdom in each deploy, hooray! 🎉


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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

@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 (2)
docs/en_US/reference/v2beta1-reference.md (1)

111-112: Documentation could be enhanced with examples

The documentation for minAvailable and maxUnavailable fields is accurate but would benefit from practical examples showing how to use these fields in YAML configurations.

Consider adding examples like:

# Example 1: Using minAvailable
spec:
  coreTemplate:
    spec:
      minAvailable: 1  # Ensure at least 1 pod is available

# Example 2: Using maxUnavailable
spec:
  coreTemplate:
    spec:
      maxUnavailable: "50%"  # Allow up to 50% pods to be unavailable
deploy/charts/emqx-operator/templates/crd.emqxes.apps.emqx.io.yaml (1)

9813-9823: Add descriptions and validation for PDB fields

The implementation of maxUnavailable and minAvailable fields looks good, matching Kubernetes PodDisruptionBudget spec. Consider these enhancements:

  1. Add field descriptions to improve API documentation:
 maxUnavailable:
+  description: "An eviction is allowed if at most \"maxUnavailable\" pods selected by \"selector\" are unavailable after the eviction. Can be absolute number or percentage."
   anyOf:
     - type: integer
     - type: string
   x-kubernetes-int-or-string: true
 minAvailable:
+  description: "An eviction is allowed if at least \"minAvailable\" pods selected by \"selector\" remain available after the eviction. Can be absolute number or percentage."
   anyOf:
     - type: integer
     - type: string
   default: 1
   x-kubernetes-int-or-string: true
  1. Consider adding OpenAPI v3 validation rules to prevent setting both fields simultaneously and validate against replicas:
x-kubernetes-validations:
  - rule: "self.maxUnavailable == null || self.minAvailable == null"
    message: "maxUnavailable and minAvailable are mutually exclusive"
  - rule: "self.maxUnavailable == null || self.maxUnavailable <= self.replicas"
    message: "maxUnavailable must not exceed replicas"

Also applies to: 13378-13388

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between cb430ee and 199bf09.

📒 Files selected for processing (7)
  • apis/apps/v2beta1/emqx_types.go (2 hunks)
  • apis/apps/v2beta1/zz_generated.deepcopy.go (2 hunks)
  • config/crd/bases/apps.emqx.io_emqxes.yaml (2 hunks)
  • controllers/apps/v2beta1/add_pdb.go (1 hunks)
  • deploy/charts/emqx-operator/templates/crd.emqxes.apps.emqx.io.yaml (2 hunks)
  • docs/en_US/reference/v2beta1-reference.md (2 hunks)
  • docs/zh_CN/reference/v2beta1-reference.md (2 hunks)
🔇 Additional comments (12)
controllers/apps/v2beta1/add_pdb.go (2)

79-83: Double-check mutual exclusivity for MinAvailable and MaxUnavailable.
This block correctly sets one field to nil if the other is specified. Just verify that your controller logic and CR validations also prevent both from being set simultaneously in an ambiguous way.


92-96: Replicant PDB mirrors the core logic.
The code block for the replicant node’s PDB is consistent with the core node’s approach. It also correctly toggles MinAvailable and MaxUnavailable. This seems correct.

apis/apps/v2beta1/emqx_types.go (3)

22-22: No concerns with intstr import.
The addition of "k8s.io/apimachinery/pkg/util/intstr" is necessary to handle the new IntOrString fields for MinAvailable and MaxUnavailable.


202-202: Replicas default assignment appears correct.
Defining a default of 2 is sensible and consistent with the existing design; no issues found.


209-219: Ensure MaxUnavailable and MinAvailable are truly exclusive.
Declaring both fields as pointers to intstr.IntOrString is fine, but verify that your validation logic enforces mutual exclusivity. Otherwise, unexpected PDB behavior might occur if both fields are non-nil.

apis/apps/v2beta1/zz_generated.deepcopy.go (2)

27-27: Import for intstr recognized.
Including "k8s.io/apimachinery/pkg/util/intstr" aligns with the usage of IntOrString.


251-260: DeepCopy logic for new fields.
The additional code correctly ensures that MinAvailable and MaxUnavailable pointers are deeply copied. This is crucial for CRD operations; well done.

docs/zh_CN/reference/v2beta1-reference.md (2)

111-111: Documentation of minAvailable is consistent with the code changes.
Thank you for adding a clear explanation in the reference documentation.


112-112: Documentation of maxUnavailable is accurately described.
This aligns with the new field in the CRD and helps users understand how to configure PDB.

docs/en_US/reference/v2beta1-reference.md (1)

235-236: Verify consistent implementation across templates

The documentation for PDB fields in EMQXReplicantTemplateSpec matches the CoreTemplateSpec. Please ensure that the implementation handles these fields consistently for both core and replicant nodes.

✅ Verification successful

Let me gather more information to verify the implementation consistency across templates.


Let me search for the core template spec to compare the implementations.


Let me check how the PDB fields are used in the controller implementation.


Based on the gathered information, I can now provide the final response:

PDB fields are properly implemented across templates

The implementation in add_pdb.go correctly handles both minAvailable and maxUnavailable fields for both core and replicant templates:

  • Core template: Uses the fields from CoreTemplate.Spec
  • Replicant template: Uses the fields from ReplicantTemplate.Spec
  • Maintains mutual exclusivity by clearing minAvailable when maxUnavailable is set
  • Both templates inherit from EMQXReplicantTemplateSpec, ensuring consistent field definitions
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify consistent implementation of PDB fields

# Check for PDB field handling in controller code
rg -A 5 "minAvailable|maxUnavailable" --type go

Length of output: 1605


Script:

#!/bin/bash
# Search for PDB-related struct definitions and fields in both core and replicant templates
ast-grep --pattern 'type $_ struct {
  $$$
  MinAvailable $_
  $$$
}'

ast-grep --pattern 'type $_ struct {
  $$$
  MaxUnavailable $_
  $$$
}'

# Search for PDB creation/handling logic
rg -A 10 "NewPodDisruptionBudget|CreatePodDisruptionBudget" --type go

Length of output: 28389


Script:

#!/bin/bash
# Search for CoreTemplateSpec definition
ast-grep --pattern 'type EMQXCoreTemplateSpec struct {
  $$$
}'

# Search for PDB-related code in controllers
rg -A 5 "PodDisruptionBudget" --type go

Length of output: 5618


Script:

#!/bin/bash
# Search for the actual usage of minAvailable and maxUnavailable in PDB generation
rg -B 5 -A 10 "MinAvailable|MaxUnavailable" controllers/apps/v2beta1/add_pdb.go

Length of output: 1753

config/crd/bases/apps.emqx.io_emqxes.yaml (1)

9800-9810: LGTM: Schema changes for PDB fields are well-defined

The CRD schema correctly defines:

  • Both integer and string formats for flexible specification
  • Default value of 1 for minAvailable
  • Proper Kubernetes int-or-string type annotation
deploy/charts/emqx-operator/templates/crd.emqxes.apps.emqx.io.yaml (1)

9813-9823: LGTM on PDB field implementation

The implementation correctly:

  • Uses IntOrString type to support both absolute numbers and percentages
  • Provides a safe default of 1 for minAvailable
  • Maintains consistency with Kubernetes PDB spec

Also applies to: 13378-13388

config/crd/bases/apps.emqx.io_emqxes.yaml Show resolved Hide resolved
Copy link

codecov bot commented Dec 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 76.17%. Comparing base (cb430ee) to head (aff9217).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1093      +/-   ##
==========================================
- Coverage   76.33%   76.17%   -0.16%     
==========================================
  Files          69       69              
  Lines        6250     6251       +1     
==========================================
- Hits         4771     4762       -9     
- Misses       1242     1249       +7     
- Partials      237      240       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Rory-Z Rory-Z force-pushed the feat/user-can-set-pdb branch 2 times, most recently from 57ec4d5 to 53b8947 Compare December 24, 2024 02:26
Copy link

@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

🧹 Nitpick comments (3)
config/crd/bases/apps.emqx.io_emqxes.yaml (1)

Line range hint 209-223: Consider adding documentation for PDB configuration best practices

While the implementation is solid, consider adding documentation that covers:

  • Recommended values for different deployment sizes
  • Impact of different settings on cluster availability
  • Examples of common configurations
  • Migration guide for existing deployments

This would help users make informed decisions when configuring these values.

deploy/charts/emqx-operator/templates/crd.emqxes.apps.emqx.io.yaml (2)

9813-9829: Consider extracting common PDB field definitions to reduce duplication.

The maxUnavailable and minAvailable fields are duplicated in both coreTemplate.spec and replicantTemplate.spec. Consider extracting these field definitions into a shared schema to improve maintainability.

# Define a shared schema
x-pdb-fields: &pdbFields
  maxUnavailable:
    anyOf:
      - type: integer
      - type: string
    x-kubernetes-int-or-string: true
    x-kubernetes-validations:
      - message: Cannot be set when minAvailable is specified
        rule: 'has(self.minAvailable) ?false : true'
  minAvailable:
    anyOf:
      - type: integer
      - type: string
    default: 1
    x-kubernetes-int-or-string: true
    x-kubernetes-validations:
      - message: Cannot be set when maxUnavailable is specified
        rule: 'has(self.maxUnavailable) ?false : true'

# Use the shared schema
spec:
  coreTemplate:
    spec:
      <<: *pdbFields
  replicantTemplate:
    spec:
      <<: *pdbFields

Also applies to: 13384-13400


9819-9820: Enhance validation messages for better clarity.

The current validation messages could be more descriptive to help users understand the constraint better.

- message: This is a mutually exclusive setting with 'minAvailable'
+ message: maxUnavailable cannot be set when minAvailable is specified. These fields are mutually exclusive in PodDisruptionBudget.
- message: This is a mutually exclusive setting with 'maxUnavailable'
+ message: minAvailable cannot be set when maxUnavailable is specified. These fields are mutually exclusive in PodDisruptionBudget.

Also applies to: 9828-9829, 13390-13391, 13399-13400

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 57ec4d5 and 53b8947.

📒 Files selected for processing (7)
  • apis/apps/v2beta1/emqx_types.go (2 hunks)
  • apis/apps/v2beta1/zz_generated.deepcopy.go (2 hunks)
  • config/crd/bases/apps.emqx.io_emqxes.yaml (2 hunks)
  • controllers/apps/v2beta1/add_pdb.go (1 hunks)
  • deploy/charts/emqx-operator/templates/crd.emqxes.apps.emqx.io.yaml (2 hunks)
  • docs/en_US/reference/v2beta1-reference.md (2 hunks)
  • docs/zh_CN/reference/v2beta1-reference.md (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • apis/apps/v2beta1/zz_generated.deepcopy.go
  • docs/zh_CN/reference/v2beta1-reference.md
  • controllers/apps/v2beta1/add_pdb.go
  • docs/en_US/reference/v2beta1-reference.md
🔇 Additional comments (4)
apis/apps/v2beta1/emqx_types.go (2)

209-216: LGTM: Well-implemented MinAvailable field with proper validation

The implementation follows Kubernetes PodDisruptionBudget spec pattern and includes:

  • Clear documentation of the field's purpose
  • Proper type using IntOrString for flexibility
  • Default value of 1 for safe defaults
  • Validation rule to ensure mutual exclusivity with MaxUnavailable

217-223: LGTM: Well-implemented MaxUnavailable field with proper validation

The implementation follows Kubernetes PodDisruptionBudget spec pattern and includes:

  • Clear documentation of the field's purpose
  • Proper type using IntOrString for flexibility
  • Validation rule to ensure mutual exclusivity with MinAvailable
config/crd/bases/apps.emqx.io_emqxes.yaml (1)

13371-13387: LGTM: CRD schema properly reflects the PDB configuration

The CRD schema correctly implements:

  • Both MinAvailable and MaxUnavailable fields with proper types
  • Mutual exclusivity validation rules
  • Default value for MinAvailable
  • Consistent documentation with the Go types
deploy/charts/emqx-operator/templates/crd.emqxes.apps.emqx.io.yaml (1)

9813-9829: LGTM on the PDB field implementation.

The implementation of maxUnavailable and minAvailable fields follows Kubernetes PodDisruptionBudget specification correctly:

  • Proper use of x-kubernetes-int-or-string for supporting both integer and percentage values
  • Correct mutual exclusivity validation
  • Sensible default of 1 for minAvailable

Also applies to: 13384-13400

@Rory-Z Rory-Z force-pushed the feat/user-can-set-pdb branch 4 times, most recently from 028f59d to ea84b0d Compare December 24, 2024 03:59
@Rory-Z Rory-Z force-pushed the feat/user-can-set-pdb branch from ea84b0d to aff9217 Compare December 24, 2024 04:04
@Rory-Z Rory-Z merged commit 4f28c00 into main Dec 24, 2024
44 of 45 checks passed
@Rory-Z Rory-Z deleted the feat/user-can-set-pdb branch December 24, 2024 04:38
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.

1 participant