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: add node selector to intance #594

Merged
merged 1 commit into from
Dec 13, 2024

Conversation

smuu
Copy link
Member

@smuu smuu commented Dec 11, 2024

Overview

Summary by CodeRabbit

  • New Features

    • Introduced a nodeSelector field for enhanced build configuration.
    • Added a NodeSelector field to the pod configuration, allowing for specific node selection during deployment.
  • Bug Fixes

    • Enhanced error handling with a new error for invalid state during node selector updates.
  • Documentation

    • Updated method signatures to reflect the addition of new fields in configuration structures.
  • Chores

    • Improved signal handling in the cleanup process for better resource management.

Signed-off-by: Smuu <18609909+Smuu@users.noreply.github.com>
Copy link
Contributor

coderabbitai bot commented Dec 11, 2024

Walkthrough

The changes introduce several enhancements across multiple files related to the build struct and its interaction with Kubernetes pod configurations. A new field nodeSelector is added to the build struct, allowing for node selection criteria to be specified. Correspondingly, error handling is improved with a new error variable for state restrictions. Modifications in the execution and k8s packages incorporate this nodeSelector into the pod configuration. Additionally, the HandleStopSignal method in the Knuu struct is updated to reset signal handlers post-cleanup.

Changes

File Change Summary
pkg/instance/build.go Added field nodeSelector map[string]string to build struct. Introduced method SetNodeSelector to update nodeSelector with state checks.
pkg/instance/errors.go Added new error variable ErrSettingNodeSelectorNotAllowed for state restriction on setting node selectors.
pkg/instance/execution.go Updated prepareReplicaSetConfig method to include NodeSelector in PodConfig, populated from build.nodeSelector.
pkg/k8s/pod.go Added field NodeSelector map[string]string to PodConfig. Updated preparePodSpec and preparePod methods to include NodeSelector in pod specs.
pkg/knuu/knuu.go Modified HandleStopSignal method to reset signal handlers for SIGINT, SIGTERM, and os.Interrupt after cleanup.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Build
    participant Execution
    participant K8s

    User->>Build: SetNodeSelector(nodeSelector)
    Build->>Build: Check State
    alt State Valid
        Build->>Build: Update nodeSelector
        Build->>Execution: Prepare ReplicaSet Config
        Execution->>K8s: Include NodeSelector in PodConfig
        K8s->>K8s: Create Pod with NodeSelector
    else State Invalid
        Build->>User: Return ErrSettingNodeSelectorNotAllowed
    end
Loading

🐇 "In the meadow, changes bloom,
A selector for nodes, to dispel the gloom.
With signals reset and states in line,
Our builds now dance, oh how they shine!
Hooray for the code, let’s hop and cheer,
For every new feature brings joy near!" 🌼


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

@sysrex sysrex left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Contributor

@tty47 tty47 left a comment

Choose a reason for hiding this comment

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

neat!

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 (2)
pkg/instance/build.go (1)

163-170: Consider adding validation for nodeSelector contents.

While the state validation is good, consider adding validation for the nodeSelector map contents to ensure:

  1. The map is not empty
  2. Keys follow Kubernetes label key format
  3. Values follow Kubernetes label value format
 func (b *build) SetNodeSelector(nodeSelector map[string]string) error {
 	if !b.instance.IsInState(StatePreparing, StateCommitted) {
 		return ErrSettingNodeSelectorNotAllowed.WithParams(b.instance.state.String())
 	}
+
+	if len(nodeSelector) == 0 {
+		return fmt.Errorf("nodeSelector cannot be empty")
+	}
+
+	// Validate keys and values follow k8s label format
+	for k, v := range nodeSelector {
+		if !isValidLabelKey(k) || !isValidLabelValue(v) {
+			return fmt.Errorf("invalid nodeSelector key/value: %s=%s", k, v)
+		}
+	}

 	b.nodeSelector = nodeSelector
 	return nil
 }
pkg/k8s/pod.go (1)

65-65: Consider adding validation for node selector keys and values.

The NodeSelector field is correctly typed as map[string]string matching Kubernetes requirements. However, consider adding validation to ensure:

  • Keys follow Kubernetes label key format (prefix/name)
  • Values follow Kubernetes label value format (63 characters max, alphanumeric, '-', '_', '.')
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 21d8b67 and a75e129.

📒 Files selected for processing (5)
  • pkg/instance/build.go (2 hunks)
  • pkg/instance/errors.go (1 hunks)
  • pkg/instance/execution.go (1 hunks)
  • pkg/k8s/pod.go (2 hunks)
  • pkg/knuu/knuu.go (1 hunks)
🔇 Additional comments (5)
pkg/knuu/knuu.go (1)

112-113: LGTM: Good practice to reset signal handlers.

The addition of signal handler reset after cleanup is a good practice as it allows other signal handlers to run and prevents potential handler conflicts.

Let's verify there are no other signal handlers that might be affected:

✅ Verification successful

Signal handling implementation is correctly isolated and reset

The verification confirms that there is only one signal handler registration in the codebase using signal.Notify(), which is in the same file (pkg/knuu/knuu.go) where the reset is performed. The reset is correctly placed after the handler has served its purpose, ensuring clean signal handling management.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for other signal handler registrations in the codebase
# Test: Search for signal.Notify calls to ensure we're not interfering with other handlers

rg -A 2 "signal\.Notify\(" 

Length of output: 169

pkg/instance/build.go (1)

28-28: LGTM: Well-typed nodeSelector field.

The nodeSelector field is correctly typed as map[string]string to match Kubernetes node selector requirements.

pkg/instance/execution.go (1)

420-420: LGTM: Proper integration of nodeSelector.

The nodeSelector is correctly passed from the build configuration to the PodConfig, maintaining the proper configuration chain.

pkg/k8s/pod.go (1)

627-627: LGTM: NodeSelector correctly integrated into pod spec.

The NodeSelector is properly assigned from PodConfig to the Kubernetes PodSpec.

pkg/instance/errors.go (1)

117-117: LGTM: Error definition follows established patterns.

The error variable is well-defined and consistent with other state-restricted setting errors in the codebase.

Copy link
Member

@mojtaba-esk mojtaba-esk left a comment

Choose a reason for hiding this comment

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

Great work.

[QQ]: why does the node selector have to be a map?

@smuu smuu merged commit 83a1a24 into main Dec 13, 2024
16 of 18 checks passed
@smuu smuu deleted the smuu/20241210-add-node-selector-to-instance branch December 13, 2024 06:44
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.

4 participants