pasta: make sure --map-guest-addr is backwards compatible

--map-guest-addr was just added in 20240814, we cannot yet hard require
this option to be present. This means we must deal with the case where
the option is not working. Both a version check or checking --help would
add extra overhead in the good case. To avoid this we try first with the
new option and if this fails check the error message for the right
error. If it didn't know about the new option we remove it and try to
exec pasta again.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger 2024-08-26 15:07:08 +02:00
parent 374514285f
commit 13e70455ad
1 changed files with 31 additions and 16 deletions

View File

@ -72,11 +72,24 @@ func Setup2(opts *SetupOptions) (*SetupResult, error) {
logrus.Debugf("pasta arguments: %s", strings.Join(cmdArgs, " "))
for {
// pasta forks once ready, and quits once we delete the target namespace
out, err := exec.Command(path, cmdArgs...).CombinedOutput()
if err != nil {
exitErr := &exec.ExitError{}
if errors.As(err, &exitErr) {
// special backwards compat check, --map-guest-addr was added in pasta version 20240814 so we
// cannot hard require it yet. Once we are confident that the update is most distros we can remove it.
if exitErr.ExitCode() == 1 &&
strings.Contains(string(out), "unrecognized option '"+mapGuestAddrOpt) &&
len(mapGuestAddrIPs) == 1 && mapGuestAddrIPs[0] == mapGuestAddrIpv4 {
// we did add the default --map-guest-addr option, if users set something different we want
// to get to the error below. We have to unset mapGuestAddrIPs here to avoid a infinite loop.
mapGuestAddrIPs = nil
// Trim off last two args which are --map-guest-addr 169.254.1.2.
cmdArgs = cmdArgs[:len(cmdArgs)-2]
continue
}
return nil, fmt.Errorf("pasta failed with exit code %d:\n%s",
exitErr.ExitCode(), string(out))
}
@ -90,6 +103,8 @@ func Setup2(opts *SetupOptions) (*SetupResult, error) {
// nice to have.
logrus.Infof("pasta logged warnings: %q", string(out))
}
break
}
var ipv4, ipv6 bool
result := &SetupResult{}
@ -244,7 +259,7 @@ func createPastaArgs(opts *SetupOptions) ([]string, []string, []string, error) {
cmdArgs = append(cmdArgs, "--netns", opts.Netns)
// do this as last arg
// do this as last arg so we can easily trim them off in the error case when we have an older version
if len(mapGuestAddrIPs) == 0 {
// the user did not request custom --map-guest-addr so add our own so that we can use this
// for our own host.containers.internal host entry.