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

Add utility functions for new status check #260

Merged
merged 1 commit into from
Oct 15, 2024
Merged

Conversation

lllamnyp
Copy link
Collaborator

@lllamnyp lllamnyp commented Aug 12, 2024

This is part of a series of stacked PRs. The previous PR in the stack is #259. The next PR is #265.

This commit includes some changes for feature #181. It adds skeletons for utility functions that represent various steps in the reconciliation loop, e.g. createClusterFromScratch if it is determined, that no resources for the EtcdCluster exist yet, or scaleUpFromZero, when a cluster was at one point scaled down and temporarily shut down, but the data is still being stored. Care is taken not to alter existing functionality yet.

Summary by CodeRabbit

  • New Features

    • Enhanced management of Etcd clusters with new methods for object lifecycle and state management.
    • Improved handling of Persistent Volume Claims (PVCs) with dynamic labeling and retrieval capabilities.
    • Introduced a new function for generating pod labels, improving code maintainability.
    • Added a new field in the observables struct to hold references to Etcd cluster instances.
  • Bug Fixes

    • Clarified naming conventions in the observables struct for better readability.
  • Documentation

    • Added comments and structured code for future functionalities related to StatefulSets and PVCs.

@lllamnyp lllamnyp mentioned this pull request Aug 13, 2024
6 tasks
@lllamnyp lllamnyp force-pushed the feat/new-status-check-pt2 branch from 6993adf to cb2a88f Compare September 10, 2024 15:56
@lllamnyp lllamnyp force-pushed the feat/new-status-check-pt3 branch from d9036b3 to 4b22a70 Compare September 10, 2024 16:01
Copy link
Contributor

coderabbitai bot commented Sep 10, 2024

Walkthrough

The changes introduce several modifications to the EtcdClusterReconciler class, enhancing state management and lifecycle handling for Etcd clusters. New methods for object patching, cluster creation, and scaling are added, while the observables struct is updated with additional fields. Functions for managing Persistent Volume Claims (PVCs) and a placeholder for StatefulSet functionality are also introduced. These updates reflect a planned expansion of the functionality related to cluster management.

Changes

File Change Summary
internal/controller/etcdcluster_controller.go Added methods for object patching/creation, cluster creation, and scaling; modified existing methods to include new state management.
internal/controller/factory/pvc.go Introduced functions for generating PVC labels and retrieving PVCs based on those labels.
internal/controller/factory/statefulset.go Added a placeholder function for StatefulSets and refactored pod label generation for clarity.
internal/controller/observables.go Updated observables struct with new fields and renamed a method for clarity; marked some fields as unused.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant EtcdClusterReconciler
    participant KubernetesAPI

    User->>EtcdClusterReconciler: Request to manage Etcd cluster
    EtcdClusterReconciler->>KubernetesAPI: Patch or create object
    alt Object not found
        KubernetesAPI-->>EtcdClusterReconciler: Not Found
        EtcdClusterReconciler->>KubernetesAPI: Create new object
    end
    KubernetesAPI-->>EtcdClusterReconciler: Object updated/created
    EtcdClusterReconciler-->>User: Response with status
Loading

Poem

🐇 In the fields where clusters grow,
New methods sprout, a vibrant show.
PVCs dance with labels bright,
StatefulSets wait for future light.
Observables now see far and wide,
A rabbit's cheer for code's new stride! 🐇


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

Base automatically changed from feat/new-status-check-pt2 to main September 10, 2024 16:07
@lllamnyp lllamnyp force-pushed the feat/new-status-check-pt3 branch 4 times, most recently from 672010f to 34f88e6 Compare September 17, 2024 18:50
@lllamnyp lllamnyp changed the title Add remaining changes to PR Add utility functions for new status check Sep 17, 2024
@lllamnyp lllamnyp marked this pull request as ready for review September 17, 2024 19:12
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

Outside diff range and nitpick comments (3)
internal/controller/etcdcluster_controller.go (3)

666-677: Implementation looks good, but remove the TODO and nolint:unused comments when the function is used.

The patchOrCreateObject function follows a common pattern for patching or creating Kubernetes objects in a consistent manner. The implementation looks correct.

However, please remove the TODO comment and the nolint:unused comment when the function is integrated into the codebase to avoid confusion and maintain code cleanliness.


679-692: Complete the implementation and remove the TODO, nolint comments, and panic statement.

The createClusterFromScratch function is intended to create a new EtcdCluster from scratch, but the implementation is currently incomplete. Please consider the following suggestions:

  1. Remove the TODO and nolint:unparam,unused comments when the function is fully implemented and integrated into the codebase to avoid confusion and maintain code cleanliness.

  2. Complete the implementation of the function by adding the necessary logic to create the EtcdCluster from scratch. This may involve creating additional resources, updating the cluster status, etc.

  3. Remove the panic statement at the end of the function and replace it with appropriate error handling and return statements based on the desired behavior of the function.

  4. Ensure that the function returns a meaningful ctrl.Result and error based on the outcome of the cluster creation process.


694-698: Implement the logic to scale up the EtcdCluster from zero replicas.

The scaleUpFromZero function is intended to scale up an EtcdCluster from zero replicas, but the implementation is currently missing. Please consider the following suggestions:

  1. Remove the TODO and nolint:unused comments when the function is fully implemented and integrated into the codebase to avoid confusion and maintain code cleanliness.

  2. Implement the necessary logic to scale up the EtcdCluster from zero replicas. This may involve updating the desired replicas, creating or updating resources, and handling any necessary reconciliation tasks.

  3. Remove the panic statement and replace it with appropriate error handling and return statements based on the desired behavior of the function.

  4. Ensure that the function returns a meaningful ctrl.Result and error based on the outcome of the scale-up process.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e00e6cb and 34f88e6.

Files selected for processing (4)
  • internal/controller/etcdcluster_controller.go (2 hunks)
  • internal/controller/factory/pvc.go (2 hunks)
  • internal/controller/factory/statefulset.go (1 hunks)
  • internal/controller/observables.go (3 hunks)
Additional comments not posted (11)
internal/controller/observables.go (6)

5-6: LGTM!

Commenting out unused imports is a good practice to keep the code clean.


9-10: LGTM!

The new import statement for "github.com/aenix-io/etcd-operator/api/v1alpha1" is added correctly. Commenting out the unused import statement for "github.com/aenix-io/etcd-operator/pkg/set" is a good practice to keep the code clean.


30-30: LGTM!

The new field instance of type *v1alpha1.EtcdCluster is added correctly to the observables struct. As per the AI-generated summary, this field likely enhances the operator's ability to manage and observe the state of Etcd clusters.


33-33: LGTM!

The new field endpoints of type []string is added correctly to the observables struct. As per the AI-generated summary, this field is likely a placeholder for storing endpoint information related to the Etcd cluster. The field is marked as unused, indicating potential future use.


37-37: LGTM!

The new field pvcs of type []corev1.PersistentVolumeClaim is added correctly to the observables struct. As per the AI-generated summary, this change clarifies the purpose of this field within the struct. The field is marked as unused, indicating potential future use.


77-78: LGTM!

The method _() has been renamed to desiredReplicas() correctly. As per the AI-generated summary, the new name improves code readability and maintainability. The method's logic remains unchanged.

internal/controller/factory/pvc.go (2)

33-39: LGTM!

The PVCLabels function correctly combines the default pod labels with the custom labels specified in the cluster's volume claim template. This allows for flexible labeling of PVCs based on the cluster configuration.


49-57: LGTM!

The PVCs function correctly retrieves a list of PVCs matching the labels generated by PVCLabels. It uses the Kubernetes client to list the PVCs in the specified context and returns the items or an error if the operation fails.

internal/controller/factory/statefulset.go (2)

44-47: Placeholder function for future implementation.

The TemplateStatefulSet function is currently a placeholder that raises a panic. Please remember to implement this function when the corresponding functionality is ready.


49-59: LGTM!

The new PodLabels helper function is a great addition that centralizes the label creation logic for pods. This enhances code readability and maintainability by reducing duplication and making it easier to maintain consistency across the codebase.

internal/controller/etcdcluster_controller.go (1)

94-94: LGTM!

Initializing state.instance with the current instance of the EtcdCluster resource is a good change that aligns with the goal of updating the observables struct for better state observation. This allows the reconciliation logic to access the desired state of the EtcdCluster resource.

@lllamnyp lllamnyp force-pushed the feat/new-status-check-pt3 branch from 34f88e6 to 5e22c32 Compare October 15, 2024 09:32
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: 2

🧹 Outside diff range and nitpick comments (6)
internal/controller/observables.go (4)

5-6: Consider removing commented-out imports if they are no longer needed.

There are several commented-out import statements in the file. If these imports are no longer needed, it's best to remove them entirely rather than leaving them as comments. This helps keep the codebase clean and easier to maintain.

If you anticipate needing these imports in the near future, consider adding a TODO comment explaining why they're kept and when they might be needed.

Also applies to: 9-10


26-37: Approved: New fields added to observables struct. Consider adding explanatory comments.

The new fields instance, endpoints, and pvcs have been added to the observables struct, which aligns with the PR objectives of introducing utility functions for new status checks.

To improve code readability and maintainability:

  1. Consider adding comments explaining the purpose of each new field, especially for instance and endpoints.
  2. For the fields marked with nolint:unused, consider adding a TODO comment explaining when these fields are expected to be used in the future.

Example:

type observables struct {
    // instance holds the reference to the EtcdCluster being reconciled
    instance       *v1alpha1.EtcdCluster
    // ... other fields ...
    // endpoints represents the list of etcd endpoints
    // TODO: This field will be used in future implementations for endpoint management
    endpoints      []string //nolint:unused
    // ... other fields ...
}

This will help other developers understand the intended use of these fields in the context of the ongoing feature development.


Line range hint 77-87: Approved: New desiredReplicas() method added. Consider clarifying the TODO comment.

The new desiredReplicas() method has been added to the observables struct. The logic looks correct for determining the desired number of replicas based on the first non-nil member list in etcdStatuses.

To improve clarity and maintainability:

  1. Consider expanding the TODO comment to explain the intended future use of this method and any pending implementation details.
  2. You might want to add a comment explaining the logic of the method, especially why it returns the length of the first non-nil member list.

Example:

// TODO: Implement full functionality for determining desired replicas.
// This method will be used in the reconciliation loop to determine the
// desired number of replicas for the EtcdCluster.
// nolint:unused
func (o *observables) desiredReplicas() int {
    // Return the number of members in the first non-nil member list
    // This assumes that all member lists should be consistent in a healthy cluster
    if o.etcdStatuses != nil {
        for i := range o.etcdStatuses {
            if o.etcdStatuses[i].memberList != nil {
                return len(o.etcdStatuses[i].memberList.Members)
            }
        }
    }
    return 0
}

This will help other developers understand the purpose and current limitations of this method.


Line range hint 1-87: Overall structure is consistent, but consider adding more context for future development.

The file structure is consistent and aligns well with the PR objectives of laying groundwork for future features. The mix of fully implemented methods (like setClusterID() and inSplitbrain()) and placeholder functionality (like desiredReplicas()) is appropriate for this stage of development.

To improve the file's maintainability and guide future development:

  1. Consider adding a file-level comment explaining the current state of development and the overall purpose of this file in the context of the EtcdCluster operator.
  2. For each TODO comment or nolint directive, try to provide more context about what needs to be done and why. This will help developers (including your future self) understand the intended direction of the code.
  3. If possible, create issues or link to existing issues for each TODO item to track the remaining work.

Example file-level comment:

/*
Package controller implements reconciliation logic for EtcdCluster resources.

This file contains the observables struct and associated methods, which are
used to gather and process information about the current state of an EtcdCluster.
Some functionality is still in development, as indicated by TODO comments.

Related to feature #181: [Brief description of the feature]
*/

These additions will help maintain a clear roadmap for the ongoing development of this package.

internal/controller/factory/pvc.go (1)

49-57: LGTM! Well-implemented function for retrieving PVCs with a minor suggestion.

The PVCs function is implemented correctly and efficiently. It uses the PVCLabels function to generate labels for filtering and the Kubernetes client to list matching PVCs. The error handling and return values are appropriate.

Consider enhancing the error handling to provide more context:

 func PVCs(ctx context.Context, cluster *etcdaenixiov1alpha1.EtcdCluster, cli client.Client) ([]corev1.PersistentVolumeClaim, error) {
 	labels := PVCLabels(cluster)
 	pvcs := corev1.PersistentVolumeClaimList{}
 	err := cli.List(ctx, &pvcs, client.MatchingLabels(labels))
 	if err != nil {
-		return nil, err
+		return nil, fmt.Errorf("failed to list PVCs for cluster %s/%s: %w", cluster.Namespace, cluster.Name, err)
 	}
 	return pvcs.Items, nil
 }

This change would make debugging easier by providing more information about which cluster was involved when an error occurs.

internal/controller/factory/statefulset.go (1)

Line range hint 1-605: Summary of changes in statefulset.go

  1. A new TemplateStatefulSet function has been added, but it's currently unimplemented.
  2. The CreateOrUpdateStatefulSet function has been improved to use a centralized PodLabels function for label management.

These changes align with the PR objectives of introducing utility functions for new status checks. The TemplateStatefulSet function, once implemented, will likely contribute to the groundwork for future functionality.

Consider the following recommendations:

  1. Implement the TemplateStatefulSet function in this PR if it's crucial for the current objectives.
  2. If TemplateStatefulSet is for future use, add a comment explaining its intended purpose and when it will be implemented.
  3. Update the PR description or add a comment in the code to explain how these changes contribute to the new status check functionality mentioned in the PR objectives.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 34f88e6 and 5e22c32.

📒 Files selected for processing (4)
  • internal/controller/etcdcluster_controller.go (2 hunks)
  • internal/controller/factory/pvc.go (2 hunks)
  • internal/controller/factory/statefulset.go (1 hunks)
  • internal/controller/observables.go (3 hunks)
🧰 Additional context used
🔇 Additional comments (7)
internal/controller/observables.go (1)

Line range hint 1-87: Summary: Changes align well with PR objectives and lay groundwork for future functionality.

The modifications in this file successfully introduce the groundwork for new utility functions related to EtcdCluster status checks and management, as outlined in the PR objectives. Key points:

  1. The new instance field in the observables struct allows for holding a reference to an EtcdCluster, which is crucial for the planned functionality.
  2. The placeholder desiredReplicas() method aligns with the goal of supporting various stages in the reconciliation loop, particularly for determining cluster size.
  3. The changes are preparatory and don't alter existing functionality, maintaining the current behavior of the operator.

These additions set the stage for implementing the functions mentioned in the PR objectives, such as createClusterFromScratch and scaleUpFromZero, in future development iterations.

The approach of introducing these changes incrementally is commendable, as it allows for gradual implementation and testing of new features without disrupting existing functionality.

internal/controller/factory/pvc.go (2)

33-39: LGTM! Well-implemented function for PVC label generation.

The PVCLabels function is implemented correctly and efficiently. It combines default pod labels with any additional labels specified in the EtcdCluster's volume claim template. The use of the existing PodLabels function and the idiomatic approach to merging maps in Go are particularly noteworthy.


Line range hint 33-57: New PVC utility functions align well with PR objectives.

The addition of PVCLabels and PVCs functions enhances the capability to manage PersistentVolumeClaims for EtcdClusters. These changes align well with the PR objectives of introducing utility functions for new status checks.

To ensure these new functions are properly integrated, let's verify their usage across the codebase:

This will help confirm that the new functions are being utilized as intended in other parts of the codebase.

✅ Verification successful

PVC utility functions are correctly added.

The newly introduced PVCLabels and PVCs functions are properly integrated within internal/controller/factory/pvc.go. No usages of these functions are found elsewhere in the codebase, which is expected for new additions.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for usage of new PVC utility functions

# Test: Search for usage of PVCLabels function
echo "Searching for PVCLabels usage:"
rg --type go "PVCLabels\(" --glob '!internal/controller/factory/pvc.go'

# Test: Search for usage of PVCs function
echo "Searching for PVCs usage:"
rg --type go "PVCs\(" --glob '!internal/controller/factory/pvc.go'

Length of output: 274


Script:

#!/bin/bash
# Description: Search for invocations of PVCLabels and PVCs functions using ast-grep

# Search for PVCLabels function calls
echo "Searching for PVCLabels function calls:"
ast-grep --lang go --pattern 'CallExpr:func: "PVCLabels"($_)' .

# Search for PVCs function calls
echo "Searching for PVCs function calls:"
ast-grep --lang go --pattern 'CallExpr:func: "PVCs"($_)' .

Length of output: 294

internal/controller/factory/statefulset.go (2)

67-67: LGTM: Improved label management using PodLabels function.

The change to use the PodLabels function for setting the Labels field in podMetadata is a good improvement. It centralizes the label creation logic, enhancing maintainability and adhering to the DRY principle.


44-47: ⚠️ Potential issue

Implement or remove the TemplateStatefulSet function.

The TemplateStatefulSet function is currently a placeholder with a panic statement. This could lead to runtime errors if called elsewhere in the codebase.

Consider one of the following actions:

  1. Implement the function if it's needed for the current PR objectives.
  2. Remove the function if it's not immediately necessary.
  3. If the implementation is planned for a future PR, replace the panic with a more informative error message.

Example of a more informative error:

func TemplateStatefulSet() *appsv1.StatefulSet {
    return nil, errors.New("TemplateStatefulSet: not yet implemented")
}

To ensure this function is not called elsewhere, run:

✅ Verification successful

Remove the TemplateStatefulSet function as it's not used.

The TemplateStatefulSet function is currently defined but not called anywhere in the codebase. Removing this unimplemented placeholder will help avoid potential confusion and reduce dead code.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for calls to TemplateStatefulSet
rg "TemplateStatefulSet\(" --type go

Length of output: 130

internal/controller/etcdcluster_controller.go (2)

94-94: Assignment of instance to state.instance is appropriate

The line correctly initializes state.instance with the current instance, which is necessary for subsequent state management.


668-676: Function patchOrCreateObject handles object patching and creation effectively

The patchOrCreateObject method correctly attempts to patch the object and falls back to creation if the object does not exist. The use of client.Apply and client.ForceOwnership ensures that the desired state is applied properly.

@lllamnyp lllamnyp self-assigned this Oct 15, 2024
@lllamnyp lllamnyp merged commit a8076da into main Oct 15, 2024
9 checks passed
@lllamnyp lllamnyp deleted the feat/new-status-check-pt3 branch October 15, 2024 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants