Skip to content

Conversation

@tricktron
Copy link

@tricktron tricktron commented Sep 20, 2025

Description

This change introduces a new configuration setting modify-hosts-file that allows users to disable CRC's automatic modification of the /etc/hosts file. The setting defaults to true to maintain backward compatibility with existing installations.

When set to false, CRC will skip adding OpenShift hostnames to the system's hosts file during cluster startup. This is particularly useful for systems like NixOS where the hosts file is managed declaratively and cannot be modified by applications, or for users who prefer to manage DNS resolution through other means.

The implementation adds the configuration option to all platform-specific DNS modules (Linux, macOS, Windows) and includes the setting in the service configuration passed to DNS setup routines.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change
  • Chore (non-breaking change which doesn't affect codebase;
    test, version modification, documentation, etc.)

Proposed changes

Testing

I tested this e2e on my NixOS machine by manually setting the needed host file entries: api.crc.testing, canary-openshift-ingress-canary.apps-crc.testing, ... and with the new config modify-hosts-file = false.

Contribution Checklist

  • I Keep It Small and Simple: The smaller the PR is, the easier it is to review and have it merged
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Which platform have you tested the code changes on?
    • Linux
    • Windows
    • MacOS

Summary by CodeRabbit

  • New Features
    • Added a user-configurable option to control whether CRC updates the system hosts file during start (default: enabled). When disabled, CRC will skip modifying hosts on Linux, macOS, and Windows and log that it skipped the update.
    • Improved guidance in post-start messages when hosts modification is disabled, advising you to ensure DNS/hosts resolve CRC domains.

@openshift-ci
Copy link

openshift-ci bot commented Sep 20, 2025

Hi @tricktron. Thanks for your PR.

I'm waiting for a crc-org member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@praveenkumar
Copy link
Member

/ok-to-test

@tricktron
Copy link
Author

/retest-required

@tricktron
Copy link
Author

@praveenkumar Thanks for running the tests. I see many timeout failures unrelated to my change. Is this normal and can I rerun the tests or do only maintainers have the right to rerun tests?

@praveenkumar
Copy link
Member

/retest

@tricktron tricktron force-pushed the skip-writing-to-hosts-file branch from 1244205 to 33f1a93 Compare September 23, 2025 12:06
@coderabbitai
Copy link

coderabbitai bot commented Sep 23, 2025

Walkthrough

Adds a new public config key ModifyHostsFile (default true), surfaces it via client.modifyHostsFile into ServicePostStartConfig, conditions DNS post-start hosts-file updates on that flag across Darwin/Linux/Windows, updates some file-permission literals to 0o-prefixed octals, and extends tests for the new setting.

Changes

Cohort / File(s) Summary
Config setting introduction
pkg/crc/config/settings.go, pkg/crc/config/settings_test.go
Adds public key ModifyHostsFile ("modify-hosts-file"), registers it with default true, boolean validation and help text; extends tests to assert default=true and provided=false scenarios.
Machine integration
pkg/crc/machine/client.go, pkg/crc/machine/start.go
Adds client.modifyHostsFile() to read the config and sets ModifyHostsFile on ServicePostStartConfig; augments start path error wrapping for DNS queries to mention (modify-hosts-file=false) when relevant; updates several file permission literals to 0o-prefixed octal notation.
Services API update
pkg/crc/services/services.go
Adds ModifyHostsFile bool field to ServicePostStartConfig.
DNS post-start conditional hosts update
pkg/crc/services/dns/dns_darwin.go, pkg/crc/services/dns/dns_linux.go, pkg/crc/services/dns/dns_windows.go
Changes OS-specific runPostStartForOS to call addOpenShiftHosts only when serviceConfig.ModifyHostsFile is true; otherwise logs "Skipping hosts file modification" and returns nil. Adds/imports logging where needed.

Sequence Diagram(s)

sequenceDiagram
  actor User
  participant Client as Machine Client
  participant Config as Config Store
  participant Start as Start Flow
  participant Svc as Services (DNS)
  participant OS as OS-specific DNS

  User->>Client: crc start
  Client->>Config: get("modify-hosts-file")
  Config-->>Client: bool
  Client->>Start: ServicePostStartConfig{ModifyHostsFile=bool, ...}
  Start->>Svc: runPostStart(config)
  Svc->>OS: runPostStartForOS(config)
  alt ModifyHostsFile == true
    OS->>OS: addOpenShiftHosts()
    OS-->>Svc: success/error
  else ModifyHostsFile == false
    OS-->>Svc: log "Skipping hosts file modification"
  end
  Svc-->>Start: return
  Start-->>User: start complete
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

I twitched my whiskers, flipped a tiny switch,
Now hosts stay quiet when the flag says “ditch.”
I hopped through config, then hopped through tests,
Permissions tidy, and DNS rests.
A carrot cheer for toggles done—hop on, everyone! 🥕🐇

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description Check ⚠️ Warning The PR description provides a high-level overview and testing details but omits the required “Fixes:” or “Relates to:” issue references and leaves the “Proposed changes” section empty, failing to document the specific changes introduced. Please add the appropriate “Fixes:” or “Relates to:” line(s) linking relevant issues and populate the “Proposed changes” section with a clear list of the main implementation updates to fully align with the repository’s description template.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Title Check ✅ Passed The title clearly and concisely summarizes the primary change—adding a configuration option to disable hosts file modifications—so it directly reflects the main purpose of the PR.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 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 33f1a93 and 6b59ec8.

📒 Files selected for processing (8)
  • pkg/crc/config/settings.go (2 hunks)
  • pkg/crc/config/settings_test.go (2 hunks)
  • pkg/crc/machine/client.go (1 hunks)
  • pkg/crc/machine/start.go (7 hunks)
  • pkg/crc/services/dns/dns_darwin.go (1 hunks)
  • pkg/crc/services/dns/dns_linux.go (1 hunks)
  • pkg/crc/services/dns/dns_windows.go (2 hunks)
  • pkg/crc/services/services.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • pkg/crc/config/settings.go
  • pkg/crc/services/services.go
  • pkg/crc/machine/client.go
🧰 Additional context used
🧬 Code graph analysis (5)
pkg/crc/services/dns/dns_darwin.go (1)
pkg/crc/config/settings.go (1)
  • ModifyHostsFile (31-31)
pkg/crc/machine/start.go (3)
pkg/crc/config/settings.go (2)
  • NetworkMode (23-23)
  • ModifyHostsFile (31-31)
pkg/crc/constants/constants.go (5)
  • GetDeveloperPasswordPath (202-204)
  • DefaultDeveloperPassword (35-35)
  • PasswdFilePath (122-122)
  • ClusterDomain (50-50)
  • KubeconfigFilePath (121-121)
pkg/os/copy.go (1)
  • CopyFile (47-49)
pkg/crc/services/dns/dns_linux.go (2)
pkg/crc/services/services.go (1)
  • ServicePostStartConfig (9-16)
pkg/crc/config/settings.go (1)
  • ModifyHostsFile (31-31)
pkg/crc/services/dns/dns_windows.go (1)
pkg/crc/config/settings.go (1)
  • ModifyHostsFile (31-31)
pkg/crc/config/settings_test.go (1)
pkg/crc/config/settings.go (1)
  • ModifyHostsFile (31-31)
🔇 Additional comments (15)
pkg/crc/services/dns/dns_darwin.go (1)

36-42: LGTM!

The conditional hosts file modification logic is correctly implemented. The else branch with the informational log message addresses previous reviewer feedback and provides clear visibility when the modification is skipped. This implementation is consistent with the Linux and Windows variants.

pkg/crc/services/dns/dns_linux.go (2)

4-4: LGTM!

The logging import is necessary for the informational message when hosts file modification is skipped.


11-17: LGTM!

The conditional logic correctly gates the hosts file modification based on the ModifyHostsFile flag. The implementation is consistent with the Darwin and Windows variants, and the informational log provides clear feedback to users.

pkg/crc/config/settings_test.go (2)

254-256: LGTM!

Test coverage for the default value of ModifyHostsFile is correctly added, consistent with the test pattern for other configuration settings.


337-339: LGTM!

Test coverage for the provided value of ModifyHostsFile is correctly added, ensuring that the setting can be overridden as expected.

pkg/crc/services/dns/dns_windows.go (2)

6-6: LGTM!

The logging import is necessary for the informational message when hosts file modification is skipped.


16-22: LGTM!

The conditional logic correctly implements the hosts file modification gating. The network mode validation is appropriately performed first, ensuring that the function returns an error for unsupported network modes before attempting any hosts file operations.

pkg/crc/machine/start.go (8)

484-487: LGTM!

The ModifyHostsFile flag is correctly wired into the ServicePostStartConfig using the client method modifyHostsFile(). This properly propagates the configuration setting to the DNS post-start handlers.


510-514: LGTM!

The enhanced error message provides valuable guidance when DNS queries fail and modify-hosts-file is disabled. This helps users understand that they need to manage DNS/hosts entries themselves in this configuration.


702-702: LGTM!

The octal prefix (0o600) is clearer and more explicit than the traditional 0600 notation, aligning with modern Go style guidelines.


756-756: LGTM!

The octal prefix (0o600) improves code clarity and consistency.


783-783: LGTM!

The octal prefix (0o644) improves code clarity and consistency.


882-882: LGTM!

The octal prefix (0o600) improves code clarity and consistency.


885-885: LGTM!

The octal prefix (0o644) improves code clarity and consistency.


903-903: LGTM!

The octal prefix (0o600) improves code clarity and consistency.


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

Comment @coderabbitai help to get the list of available commands and usage tips.

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)
pkg/crc/config/settings.go (1)

133-135: Help text: make OS-agnostic to avoid misleading Windows users.

Current text mentions “/etc/hosts”, which is Unix-specific. Prefer “system hosts file”.

Apply this diff:

-	cfg.AddSetting(ModifyHostsFile, true, ValidateBool, SuccessfullyApplied,
-		"Allow CRC to modify the /etc/hosts file (true/false, default: true)")
+	cfg.AddSetting(ModifyHostsFile, true, ValidateBool, SuccessfullyApplied,
+		"Allow CRC to modify the system hosts file (true/false, default: true)")
pkg/crc/services/dns/dns_darwin.go (1)

36-39: Optional: log when skipping hosts modification for visibility.

A brief info log helps users correlate behavior with config.

Apply this diff:

-	if serviceConfig.ModifyHostsFile {
-		if err := addOpenShiftHosts(serviceConfig); err != nil {
-			return err
-		}
-	}
+	if serviceConfig.ModifyHostsFile {
+		if err := addOpenShiftHosts(serviceConfig); err != nil {
+			return err
+		}
+	} else {
+		logging.Infof("Skipping hosts file modification on macOS (modify-hosts-file=false)")
+	}
pkg/crc/machine/start.go (1)

506-513: Improve error message when DNS-from-host check fails with modify-hosts-file=false.

Make the failure actionable by hinting at manual DNS/hosts setup when the flag is disabled.

You can adjust the message like this (snippet for context):

if err := dns.CheckCRCLocalDNSReachableFromHost(servicePostStartConfig); err != nil {
    if !client.useVSock() {
        msg := "Failed to query DNS from host"
        if !servicePostStartConfig.ModifyHostsFile {
            msg += " (modify-hosts-file=false). Ensure your system DNS/hosts entries resolve the CRC domains."
        }
        return nil, errors.Wrap(err, msg)
    }
    logging.Warn(fmt.Sprintf("Failed to query DNS from host: %v", 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 4b73b8b and 33f1a93.

📒 Files selected for processing (8)
  • pkg/crc/config/settings.go (2 hunks)
  • pkg/crc/config/settings_test.go (2 hunks)
  • pkg/crc/machine/client.go (1 hunks)
  • pkg/crc/machine/start.go (1 hunks)
  • pkg/crc/services/dns/dns_darwin.go (1 hunks)
  • pkg/crc/services/dns/dns_linux.go (1 hunks)
  • pkg/crc/services/dns/dns_windows.go (1 hunks)
  • pkg/crc/services/services.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (8)
pkg/crc/services/services.go (4)
pkg/crc/ssh/ssh.go (1)
  • Runner (16-18)
pkg/crc/machine/bundle/metadata.go (1)
  • CrcBundleInfo (27-38)
pkg/crc/config/settings.go (2)
  • NetworkMode (23-23)
  • ModifyHostsFile (31-31)
pkg/crc/network/types.go (1)
  • Mode (39-39)
pkg/crc/services/dns/dns_darwin.go (1)
pkg/crc/config/settings.go (1)
  • ModifyHostsFile (31-31)
pkg/crc/services/dns/dns_windows.go (1)
pkg/crc/config/settings.go (1)
  • ModifyHostsFile (31-31)
pkg/crc/config/settings_test.go (1)
pkg/crc/config/settings.go (1)
  • ModifyHostsFile (31-31)
pkg/crc/services/dns/dns_linux.go (1)
pkg/crc/config/settings.go (1)
  • ModifyHostsFile (31-31)
pkg/crc/machine/client.go (1)
pkg/crc/config/settings.go (1)
  • ModifyHostsFile (31-31)
pkg/crc/config/settings.go (2)
pkg/crc/config/validations.go (1)
  • ValidateBool (19-25)
pkg/crc/config/callbacks.go (1)
  • SuccessfullyApplied (36-38)
pkg/crc/machine/start.go (2)
pkg/crc/config/settings.go (2)
  • NetworkMode (23-23)
  • ModifyHostsFile (31-31)
pkg/crc/preflight/labels.go (1)
  • NetworkMode (13-13)
🔇 Additional comments (8)
pkg/crc/config/settings.go (1)

31-31: LGTM: new setting key introduced cleanly.

pkg/crc/services/dns/dns_linux.go (1)

10-14: LGTM: gating host file changes behind the new flag.

pkg/crc/machine/client.go (1)

67-69: LGTM: simple accessor for the new setting.

pkg/crc/services/dns/dns_windows.go (1)

15-19: LGTM: respects modify-hosts-file on Windows.

pkg/crc/config/settings_test.go (2)

254-256: LGTM: default value covered in matrix.


337-339: LGTM: provided (false) value covered in matrix.

pkg/crc/machine/start.go (1)

484-487: LGTM: value threaded into ServicePostStartConfig.

pkg/crc/services/services.go (1)

10-15: LGTM: config plumbed via ServicePostStartConfig — no unkeyed struct literals found. Call sites in pkg/crc/machine/start.go and pkg/crc/services/dns/dns_test.go use keyed initializers.

Copy link
Contributor

@gbraad gbraad left a comment

Choose a reason for hiding this comment

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

Suggest minor changes to ensure this is recorded as part of log

if serviceConfig.ModifyHostsFile {
if err := addOpenShiftHosts(serviceConfig); err != nil {
return err
}
Copy link
Contributor

Choose a reason for hiding this comment

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

- }
+ } else {
+ 	logging.Infof("Skipping hosts file modification")
+ }

Copy link
Contributor

Choose a reason for hiding this comment

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

... this of course applies to all the platforms (also dns_linux.go and dns_windows.go).

Copy link
Author

Choose a reason for hiding this comment

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

@gbraad Thank you for your time and the review. I applied your suggestions and also the other two nitpicks by coderabbitai.

@tricktron tricktron force-pushed the skip-writing-to-hosts-file branch from 33f1a93 to 4bac765 Compare October 15, 2025 20:33
@openshift-ci
Copy link

openshift-ci bot commented Oct 15, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from gbraad. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

This change introduces a new configuration setting 'modify-hosts-file'
that allows users to disable CRC's automatic modification of the
/etc/hosts file. The setting defaults to true to maintain backward
compatibility with existing installations.
@tricktron tricktron force-pushed the skip-writing-to-hosts-file branch from 4bac765 to 6b59ec8 Compare October 15, 2025 20:34
@openshift-ci
Copy link

openshift-ci bot commented Oct 15, 2025

@tricktron: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
ci/prow/security 6b59ec8 link false /test security
ci/prow/e2e-crc 6b59ec8 link true /test e2e-crc

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

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