Skip to content

simplifies k8s client creation with singleton pattern #84

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

harisudarsan1
Copy link

@harisudarsan1 harisudarsan1 commented Apr 28, 2025

K8s client was created multiple time within a single function and created directly from config each time. This PR simplifies the client creation and creates one client per command. It also introduces distinct error regarding client creation which is useful for debugging.

Summary by CodeRabbit

  • Refactor
    • Improved efficiency by reusing a single Kubernetes client across multiple operations, reducing redundant client creation.
    • Enhanced error handling for Kubernetes client creation failures.
  • Chores
    • Streamlined internal logic for interacting with Kubernetes resources.
  • Chores
    • Updated linting configuration to specify golangci-lint version.

Copy link

coderabbitai bot commented Apr 28, 2025

Walkthrough

The changes refactor how Kubernetes clients are managed and used within the codebase. Functions that previously created their own Kubernetes clients internally are now updated to accept an existing client as a parameter. A new function is introduced to handle Kubernetes client creation, allowing a single client instance to be reused across multiple operations. Function signatures are updated accordingly, and internal logic is adjusted to use the passed-in client. Error handling is updated to address client creation failures.

Changes

File(s) Change Summary
cmd/cluster.go Updated commands to explicitly create and pass a Kubernetes client (k8sClient) to functions; updated function signatures and error handling; simplified internal logic for secret deletion to use the passed client.
pkg/common/common.go Modified ReadInstallerConfigMap and RemoveInstallerEntry to accept a Kubernetes client as a parameter; added CreateK8sClient function for centralized client creation; removed internal client creation from affected functions.
.golangci.yml Added top-level version: 1 field to specify the golangci-lint configuration format version.

Poem

In the warren of code, a client now hops,
Passed from function to function, it never stops.
No more spawning anew in each secret or map,
Just one clever bunny with kubeconfig in its lap.
Centralized, streamlined, it’s easy to see—
This rabbit’s refactor brings harmony! 🐇✨


📜 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 670f508 and d302670.

📒 Files selected for processing (1)
  • .golangci.yml (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • .golangci.yml

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

Copy link

github-actions bot commented Apr 28, 2025

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

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

🔭 Outside diff range comments (2)
pkg/common/common.go (1)

225-271: ⚠️ Potential issue

Fix undefined variable in RemoveInstallerEntry

There's a reference to an undefined variable clientset on line 265. It should be using the k8sclient parameter that was passed to the function.

-	_, err = clientset.CoreV1().ConfigMaps(namespace).Update(context.TODO(), configMap, metav1.UpdateOptions{})
+	_, err = k8sclient.CoreV1().ConfigMaps(namespace).Update(context.TODO(), configMap, metav1.UpdateOptions{})
cmd/cluster.go (1)

235-243: 🛠️ Refactor suggestion

Use secretName parameter instead of hardcoded value

The function accepts a secretName parameter but then ignores it and uses the hardcoded string "parseable-env-secret" in the deletion call. This makes the parameter redundant and potentially confusing.

 func deleteSecret(namespace, secretName string, k8sClient *kubernetes.Clientset) error {
 	// Delete the secrets
-	err := k8sClient.CoreV1().Secrets(namespace).Delete(context.TODO(), "parseable-env-secret", metav1.DeleteOptions{})
+	err := k8sClient.CoreV1().Secrets(namespace).Delete(context.TODO(), secretName, metav1.DeleteOptions{})
 	if err != nil {
 		return fmt.Errorf("failed to delete secret '%s': %v", secretName, err)
 	}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b60e374 and 1183832.

📒 Files selected for processing (2)
  • cmd/cluster.go (5 hunks)
  • pkg/common/common.go (3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
cmd/cluster.go (1)
pkg/common/common.go (3)
  • CreateK8sClient (93-105)
  • ReadInstallerConfigMap (59-85)
  • RemoveInstallerEntry (225-271)
🔇 Additional comments (4)
pkg/common/common.go (2)

93-105: Good implementation of the K8s client creation function

The CreateK8sClient function effectively centralizes the Kubernetes client creation logic, making it reusable across the codebase. The error handling is comprehensive with clear error messages that will help with debugging.


58-85: Well-refactored function to accept client as parameter

Good job refactoring ReadInstallerConfigMap to accept a K8s client as a parameter instead of creating it internally. This promotes the singleton pattern and improves resource utilization.

cmd/cluster.go (2)

194-196: Good use of the singleton pattern

Well done passing the Kubernetes client to RemoveInstallerEntry. This is consistent with the singleton pattern being implemented throughout the codebase.


158-161: Good reuse of the Kubernetes client

Good job reusing the created Kubernetes client across multiple operations, which was the main goal of this refactoring.

@harisudarsan1
Copy link
Author

@nitisht Please review

@nitisht nitisht self-requested a review April 28, 2025 08:43
@nitisht
Copy link
Member

nitisht commented Apr 28, 2025

Can you sign the CLA? Also the build seems to fail - can you check

Signed-off-by: Harisudarsan <[email protected]>
@harisudarsan1
Copy link
Author

harisudarsan1 commented Apr 28, 2025

Can you sign the CLA? Also the build seems to fail - can you check'

Sure

@harisudarsan1
Copy link
Author

recheck

@harisudarsan1
Copy link
Author

I have read the CLA Document and I hereby sign the CLA

@harisudarsan1
Copy link
Author

recheck

nitisht added a commit to parseablehq/.github that referenced this pull request Apr 28, 2025
@harisudarsan1
Copy link
Author

@nitisht I'm facing lint errors for already existing code? are you facing it as well?

@harisudarsan1
Copy link
Author

@nitisht I have created a seperate PR for fixing lint errors please check it out

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