-
Notifications
You must be signed in to change notification settings - Fork 7
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
Use 'crc' section for hosts file modification #42
Conversation
44d227c
to
22266ba
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have a 'migration plan' in mind to go from /etc/hosts without comments to /etc/hosts with comments? I think after this change the Clean
methods will no longer be able to remove crc entries which are outside of the comment block?
I also noticed libhosty's code uses Lock()
/Unlock()
when modifying the content of the host file (but not in the GetHostsFile* methods??), it would not hurt to do it in our code as well.
Last but not least, this removes the "9 entries per line" code on Windows, which will cause regressions on Windows. libhosty's corresponding issue is areYouLazy/libhosty#12
pkg/hosts/hosts.go
Outdated
return err | ||
} | ||
return h.File.Flush() | ||
|
||
line, err := h.findIP(start, end, ip) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe you could reuse the getHostsFileLineByIP
name? Just a suggestion, both names are fine with me.
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this needs to call hfl.Raw = h.File.RenderHostsFileLine(newLineNum)
like what Add
is doing? This is not terribly important as SaveHostsFile()
is not using it for rendering the file, but would not hurt to be consistent.
@@ -154,7 +204,17 @@ func (h *Hosts) Contains(ip, host string) bool { | |||
return false | |||
} | |||
|
|||
return h.File.Has(ip, host) | |||
lines := h.File.GetHostsFileLinesByAddress(ip) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reasons this is not restricted to the range owned by crc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I keep original implementation, just update code with new lib usage, because I can’t find how Contains
func used.
Maybe we can delete Contains
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's used here https://github.com/crc-org/crc/blob/8957ea9f15e953e67b115feed2fb88b1fbae2b57/pkg/crc/adminhelper/hosts.go#L30 to avoid adding multiple times the same hostname. Given the way it's used, I'd restrict it to the comment block.
However, you added support for not adding duplicate entries directly in admin-helper, so this code could be removed from crc, and Contains
is no l onger needed.
bbad7f0
to
29df4d1
Compare
@gbraad @praveenkumar @cfergeau @anjannath Migrating plan could be: During the add/clean/remove, we can check that hosts file contains crc related records outside of crc section, Basically we need to do that only once, and rest of the time we should modify only crc section, allowing users to have own custom settings in hosts file for crc. WDYT? |
I'd only do this if the |
@cfergeau I think we can check records outside CRC section during So, if user already has crc related hosts changes, and upgrades to new version of CRC, we force him to recreate cluster(with
|
Ah good suggestion, this should work! We'll be missing people doing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still need to take a look at the last commit (the "9 entries per line" one)
h.File.HostsFileLines = newHosts | ||
|
||
// generate raw version of the line | ||
hfl.Raw = h.File.RenderHostsFileLine(newLineNum) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find these line number occurrences in libhosty API a bit awkward, but nothing we can do here :-/
Now it is the test case below which fails :-/ I've tried to simplify the code to make it easier to follow, but this comes at the cost of less in place modifications, and is more in the form of "let's rewrite everything in the crc section which corresponds to this IP", which may be something you want to avoid? With my changes the test case passes, but I've broken in the process the handling of different IPs in the CRC section :( need to try a bit more work on it.
|
I spent more time on this code, https://github.com/cfergeau/admin-helper/tree/change-hosts-modification2 keeps the same behaviour as what you did, in place modification of the file as much as possible. I've tried to make |
@cfergeau I've cherry-picked your commits |
For this one, if we wanted to go with a nice history with smaller commit, this could look like
This would gradually add each feature that are important to us, and should make each commit smaller, and easier to understand. |
@cfergeau I also prefer to squash commit in to single one |
@cfergeau I have, finally, time to work on this. So what you suggest to do with this PR, squash all commits in single one? |
@crc-org/crc-team Any thoughts on this? |
Better to squash the commits, we need to finally merge this, but this will require careful testing imo. |
ddbda9c
to
c853707
Compare
replace goodhosts with libhosty re-add 9 hosts per line limit perform clean only if crc section present add migration code for old hosts file Co-Authored-By: Christophe Fergeau <[email protected]>
c853707
to
6b9e4fa
Compare
Build has lint error, it should be fixed with #44 |
Is there something we can do automated. Eventually this needs a test set. |
Took another look at this, and it's still looking fine. Tested it with microshift on a mac and it behaved as expected (created multiple routes until a new line is created, removed some of these routes, readded them, then ran Preexisting .crc.testing entries in the /etc/hosts file are not removed, but it seems normal (tried with Preexisting issue, |
This PR rewrites the way of modifications of hosts file, by adding 'crc' section and perform all modification inside it.
Also it changes library which we use to parse/edit hosts file to https://github.com/areYouLazy/libhosty
Fixes crc-org/crc#2992
Sample: