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

Introduce --debug option; fix $patch=delete #31

Merged
merged 1 commit into from
Jan 10, 2025
Merged

Introduce --debug option; fix $patch=delete #31

merged 1 commit into from
Jan 10, 2025

Conversation

kvaps
Copy link
Member

@kvaps kvaps commented Jan 10, 2025

Summary by CodeRabbit

Release Notes

  • New Features

    • Added a --debug flag to apply and template commands
    • Enhanced configuration output with optional detailed debugging information
  • Improvements

    • Improved visibility into configuration generation and patch application processes
    • Added more granular control over command output verbosity
  • Configuration

    • New debug option available in configuration files and command-line arguments

Copy link

coderabbitai bot commented Jan 10, 2025

Walkthrough

The pull request introduces a new debug flag across multiple components of the application, enhancing the ability to control and view detailed output during configuration and patch application processes. The changes span several files, adding a boolean Debug option to configuration structures, command-line flags, and the engine's options. This allows users to get more detailed information about configuration rendering and patch application, with the ability to show only rendered patches when needed.

Changes

File Change Summary
pkg/commands/apply.go - Added debug boolean flag to applyCmdFlags
- Updated flag definitions and PreRunE function
- Integrated debug into engine.Options
pkg/commands/root.go - Added Debug field to TemplateOptions struct with YAML serialization support
pkg/commands/template.go - Added debug boolean flag to templateCmdFlags
- Updated flag definitions and PreRunE function
- Integrated debug into engine.Options
pkg/engine/engine.go - Added Debug field to Options struct
- Introduced debugPhase function
- Modified patch application logic to support debug output

Sequence Diagram

sequenceDiagram
    participant User
    participant CLI
    participant Engine
    participant Cluster

    User->>CLI: Run command with --debug flag
    CLI->>Engine: Configure Options with Debug=true
    Engine->>Cluster: Apply Patches
    Engine-->>User: Output detailed debug information
Loading

Poem

🐰 A Debugging Bunny's Delight

With debug flag raised, secrets unfold,
Patches and configs, their stories untold
Verbose output springs forth with glee
Revealing the magic of what used to be hidden from me!
🔍✨

Finishing Touches

  • 📝 Generate Docstrings (Beta)

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

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 725ecb0 and 486e8d2.

📒 Files selected for processing (4)
  • pkg/commands/apply.go (4 hunks)
  • pkg/commands/root.go (1 hunks)
  • pkg/commands/template.go (4 hunks)
  • pkg/engine/engine.go (10 hunks)
🔇 Additional comments (12)
pkg/commands/root.go (1)

43-43: LGTM!

The addition of the Debug field to TemplateOptions follows the existing pattern and is properly tagged for YAML serialization.

pkg/commands/apply.go (4)

32-32: LGTM!

The addition of the debug flag to applyCmdFlags follows the existing pattern.


58-60: LGTM!

The PreRunE check for the debug flag is properly implemented, ensuring consistency with the configuration.


95-95: LGTM!

The debug option is correctly passed to the engine options.


197-197: LGTM!

The flag initialization is properly implemented with a clear description.

pkg/commands/template.go (4)

35-35: LGTM!

The addition of the debug flag to templateCmdFlags maintains consistency with apply.go.


68-70: LGTM!

The PreRunE check for the debug flag is properly implemented, ensuring consistency with the configuration.


206-206: LGTM!

The debug option is correctly passed to the engine options.


242-242: LGTM!

The flag initialization is properly implemented with a clear description.

pkg/engine/engine.go (3)

50-50: LGTM!

The addition of the Debug field to Options follows the existing pattern.


268-268: Address the TODO comment.

The TODO comment should be replaced with proper error handling or removed if no longer needed.

Let's check if there are similar patterns in the codebase:


467-468: Verify the impact of patch application changes.

The patch application logic has been modified to use machine type-specific flags. Please verify that this change aligns with the PR objective to "fix $patch=delete".

Let's check for related changes and tests:

Comment on lines +59 to +95
// debugPhase is a unified debug function that prints debug information based on the given stage and context,
// then exits the program.
func debugPhase(opts Options, patches []string, clusterName string, clusterEndpoint string, mType machine.Type) {
phase := 2
if clusterName == "" {
clusterName = "dummy"
phase = 1
}
if clusterEndpoint == "" {
clusterEndpoint = "clusterEndpoint"
phase = 1
}

fmt.Printf(
"# DEBUG(phase %d): talosctl gen config %s %s -t %s --with-secrets=%s --talos-version=%s --kubernetes-version=%s -o -",
phase, clusterName, clusterEndpoint, mType,
opts.WithSecrets, opts.TalosVersion, opts.KubernetesVersion,
)

patchOption := "--config-patch-control-plane"
if mType == machine.TypeWorker {
patchOption = "--config-patch-worker"
}

// Print patches
for _, patch := range patches {
if string(patch[0]) == "@" {
// Apply patch is always one
fmt.Printf(" %s=%s\n", patchOption, patch)
} else {
fmt.Printf("\n---")
fmt.Printf("\n# DEBUG(phase %d): %s=\n%s", phase, patchOption, patch)
}
}

os.Exit(0)
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider making the debug output less disruptive.

The debugPhase function currently exits the program using os.Exit(0), which could be disruptive to the application flow. Consider:

  1. Making the exit behavior configurable
  2. Returning the debug information instead of printing directly
  3. Using a logger interface for better control over output

This would make the debug functionality more flexible and less intrusive.

-func debugPhase(opts Options, patches []string, clusterName string, clusterEndpoint string, mType machine.Type) {
+func debugPhase(opts Options, patches []string, clusterName string, clusterEndpoint string, mType machine.Type) string {
     phase := 2
     if clusterName == "" {
         clusterName = "dummy"
         phase = 1
     }
     if clusterEndpoint == "" {
         clusterEndpoint = "clusterEndpoint"
         phase = 1
     }

-    fmt.Printf(
+    output := fmt.Sprintf(
         "# DEBUG(phase %d): talosctl gen config %s %s -t %s --with-secrets=%s --talos-version=%s --kubernetes-version=%s -o -",
         phase, clusterName, clusterEndpoint, mType,
         opts.WithSecrets, opts.TalosVersion, opts.KubernetesVersion,
     )

     patchOption := "--config-patch-control-plane"
     if mType == machine.TypeWorker {
         patchOption = "--config-patch-worker"
     }

     // Print patches
     for _, patch := range patches {
         if string(patch[0]) == "@" {
-            fmt.Printf(" %s=%s\n", patchOption, patch)
+            output += fmt.Sprintf(" %s=%s\n", patchOption, patch)
         } else {
-            fmt.Printf("\n---")
-            fmt.Printf("\n# DEBUG(phase %d): %s=\n%s", phase, patchOption, patch)
+            output += fmt.Sprintf("\n---\n# DEBUG(phase %d): %s=\n%s", phase, patchOption, patch)
         }
     }

-    os.Exit(0)
+    return output
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// debugPhase is a unified debug function that prints debug information based on the given stage and context,
// then exits the program.
func debugPhase(opts Options, patches []string, clusterName string, clusterEndpoint string, mType machine.Type) {
phase := 2
if clusterName == "" {
clusterName = "dummy"
phase = 1
}
if clusterEndpoint == "" {
clusterEndpoint = "clusterEndpoint"
phase = 1
}
fmt.Printf(
"# DEBUG(phase %d): talosctl gen config %s %s -t %s --with-secrets=%s --talos-version=%s --kubernetes-version=%s -o -",
phase, clusterName, clusterEndpoint, mType,
opts.WithSecrets, opts.TalosVersion, opts.KubernetesVersion,
)
patchOption := "--config-patch-control-plane"
if mType == machine.TypeWorker {
patchOption = "--config-patch-worker"
}
// Print patches
for _, patch := range patches {
if string(patch[0]) == "@" {
// Apply patch is always one
fmt.Printf(" %s=%s\n", patchOption, patch)
} else {
fmt.Printf("\n---")
fmt.Printf("\n# DEBUG(phase %d): %s=\n%s", phase, patchOption, patch)
}
}
os.Exit(0)
}
// debugPhase is a unified debug function that prints debug information based on the given stage and context,
// then exits the program.
func debugPhase(opts Options, patches []string, clusterName string, clusterEndpoint string, mType machine.Type) string {
phase := 2
if clusterName == "" {
clusterName = "dummy"
phase = 1
}
if clusterEndpoint == "" {
clusterEndpoint = "clusterEndpoint"
phase = 1
}
output := fmt.Sprintf(
"# DEBUG(phase %d): talosctl gen config %s %s -t %s --with-secrets=%s --talos-version=%s --kubernetes-version=%s -o -",
phase, clusterName, clusterEndpoint, mType,
opts.WithSecrets, opts.TalosVersion, opts.KubernetesVersion,
)
patchOption := "--config-patch-control-plane"
if mType == machine.TypeWorker {
patchOption = "--config-patch-worker"
}
// Print patches
for _, patch := range patches {
if string(patch[0]) == "@" {
output += fmt.Sprintf(" %s=%s\n", patchOption, patch)
} else {
output += fmt.Sprintf("\n---\n# DEBUG(phase %d): %s=\n%s", phase, patchOption, patch)
}
}
return output
}

@kvaps kvaps merged commit 75d2a67 into main Jan 10, 2025
2 of 4 checks passed
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