Skip to content

Commit

Permalink
nameservers: Use nmcli to update the nameserver and search info
Browse files Browse the repository at this point in the history
From 4.15, because of ovn-kubernetes the info around nameserver and
search option in the `/etc/resolv.conf` not match with user expectation.
`ovs-configuration` service is run on this node which is a dependency
for kubelet and this make changes in the networking by creating bridge
network and restarting the NetworkManager. As soon as NM restart happen,
changes which are done as part of crc vm post start are vanished.

This PR is going to make sure that changes are done using `nmcli` and
shouldn't removed when NM restart happen. Following steps are done in
this PR
- Start the `ovs-configuration` service, even it is enabled it doesn't
  autostart because it only required by `kubelet-dependencies.target`.
- `ovs-configuration` service always create the network named as
  `ovs-if-br-ex` so we run the nmcli command to update this connection
  and add the nameserver and search option
- Restart the NM to update the /etc/resolv.conf
  • Loading branch information
praveenkumar committed May 7, 2024
1 parent 9d616d6 commit 60040cd
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions pkg/crc/network/nameservers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"

"github.com/crc-org/crc/v2/pkg/crc/ssh"
"github.com/crc-org/crc/v2/pkg/crc/systemd"
"github.com/crc-org/crc/v2/pkg/crc/systemd/states"
)

// HasGivenNameserversConfigured returns true if the instance uses a provided nameserver.
Expand All @@ -32,15 +34,47 @@ func GetResolvValuesFromInstance(sshRunner *ssh.Runner) (*ResolvFileValues, erro
}

func CreateResolvFileOnInstance(sshRunner *ssh.Runner, resolvFileValues ResolvFileValues) error {
resolvFile, _ := CreateResolvFile(resolvFileValues)
sd := systemd.NewInstanceSystemdCommander(sshRunner)
// Check if ovs-configuration.service exist and if not then it is old bundle and use the same way to
// update resolve.conf file
if state, err := sd.Status("ovs-configuration.service"); err != nil || state == states.NotFound {
if err := replaceResolvConfFile(sshRunner, resolvFileValues); err != nil {
return fmt.Errorf("error updating resolv.conf file: %s", err)
}
return nil
}

if err := sd.Start("ovs-configuration.service"); err != nil {
return err
}

return updateNetworkManagerConfig(sd, sshRunner, resolvFileValues)
}

err := sshRunner.CopyDataPrivileged([]byte(resolvFile), "/etc/resolv.conf", 0644)
func replaceResolvConfFile(sshRunner *ssh.Runner, resolvFileValues ResolvFileValues) error {
resolvFile, err := CreateResolvFile(resolvFileValues)
if err != nil {
return fmt.Errorf("error to create resolv conf file: %v", err)
}
err = sshRunner.CopyDataPrivileged([]byte(resolvFile), "/etc/resolv.conf", 0644)
if err != nil {
return fmt.Errorf("Error creating /etc/resolv on instance: %s", err.Error())
}
return nil
}

func updateNetworkManagerConfig(sd *systemd.Commander, sshRunner *ssh.Runner, resolvFileValues ResolvFileValues) error {
nameservers := strings.Join(resolvFileValues.GetNameServer(), ",")
searchDomains := strings.Join(resolvFileValues.GetSearchDomains(), ",")
// When ovs-configuration service is running, name of the connection should be ovs-if-br-ex
_, stderr, err := sshRunner.RunPrivileged("Update resolv.conf file", "nmcli", "con", "modify", "ovs-if-br-ex",
"ipv4.dns", nameservers, "ipv4.dns-search", searchDomains)
if err != nil {
return fmt.Errorf("failed to update resolv.conf file %s: %v", stderr, err)
}
return sd.Restart("NetworkManager.service")
}

// AddNameserversToInstance will add additional nameservers to the end of the
// /etc/resolv.conf file inside the instance.
func AddNameserversToInstance(sshRunner *ssh.Runner, nameservers []NameServer) error {
Expand Down

0 comments on commit 60040cd

Please sign in to comment.