Update connection on removal

Modify `RemoveConnections` to verify the new default system connection's
rootful state matches the rootful-ness of the podman machine it is associated
with.

Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
This commit is contained in:
Jake Correnti
2024-09-12 15:03:03 -04:00
parent c709be3a29
commit 24deec835c
4 changed files with 45 additions and 17 deletions

View File

@ -30,6 +30,11 @@ func resetMachine() error {
logrus.Errorf("unable to load machines: %q", err)
}
machines, err := p.GetAllMachinesAndRootfulness()
if err != nil {
return err
}
for _, mc := range mcs {
state, err := provider.State(mc, false)
if err != nil {
@ -42,7 +47,7 @@ func resetMachine() error {
}
}
if err := connection.RemoveConnections(mc.Name, mc.Name+"-root"); err != nil {
if err := connection.RemoveConnections(machines, mc.Name, mc.Name+"-root"); err != nil {
logrus.Error(err)
}

View File

@ -75,26 +75,34 @@ func UpdateConnectionPairPort(name string, port, uid int, remoteUsername string,
// Returns true if it modified the default
func UpdateConnectionIfDefault(rootful bool, name, rootfulName string) error {
return config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error {
if name == cfg.Connection.Default && rootful {
cfg.Connection.Default = rootfulName
} else if rootfulName == cfg.Connection.Default && !rootful {
cfg.Connection.Default = name
}
updateConnection(cfg, rootful, name, rootfulName)
return nil
})
}
func RemoveConnections(names ...string) error {
func updateConnection(cfg *config.ConnectionsFile, rootful bool, name, rootfulName string) {
if name == cfg.Connection.Default && rootful {
cfg.Connection.Default = rootfulName
} else if rootfulName == cfg.Connection.Default && !rootful {
cfg.Connection.Default = name
}
}
func RemoveConnections(machines map[string]bool, names ...string) error {
var dest config.Destination
var service string
if err := config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error {
err := setNewDefaultConnection(cfg, &dest, &service, names...)
if err != nil {
return err
}
err := setNewDefaultConnection(cfg, &dest, &service, names...)
if err != nil {
return err
}
return nil
rootful, ok := machines[service]
if dest.IsMachine && ok {
updateConnection(cfg, rootful, service, service+"-root")
}
return nil
}); err != nil {
return err
}

View File

@ -16,6 +16,7 @@ import (
"github.com/containers/podman/v5/pkg/machine/env"
"github.com/containers/podman/v5/pkg/machine/ignition"
"github.com/containers/podman/v5/pkg/machine/lock"
"github.com/containers/podman/v5/pkg/machine/provider"
"github.com/containers/podman/v5/pkg/machine/proxyenv"
"github.com/containers/podman/v5/pkg/machine/vmconfigs"
"github.com/containers/podman/v5/utils"
@ -230,7 +231,11 @@ func Init(opts machineDefine.InitOptions, mp vmconfigs.VMProvider) error {
}
cleanup := func() error {
return connection.RemoveConnections(mc.Name, mc.Name+"-root")
machines, err := provider.GetAllMachinesAndRootfulness()
if err != nil {
return err
}
return connection.RemoveConnections(machines, mc.Name, mc.Name+"-root")
}
callbackFuncs.Add(cleanup)
@ -580,7 +585,12 @@ func Remove(mc *vmconfigs.MachineConfig, mp vmconfigs.VMProvider, dirs *machineD
}
}
rmFiles, genericRm, err := mc.Remove(opts.SaveIgnition, opts.SaveImage)
machines, err := provider.GetAllMachinesAndRootfulness()
if err != nil {
return err
}
rmFiles, genericRm, err := mc.Remove(machines, opts.SaveIgnition, opts.SaveImage)
if err != nil {
return err
}
@ -655,12 +665,17 @@ func Reset(mps []vmconfigs.VMProvider, opts machine.ResetOptions) error {
}
removeDirs = append(removeDirs, d)
machines, err := provider.GetAllMachinesAndRootfulness()
if err != nil {
return err
}
for _, mc := range mcs {
err := Stop(mc, p, d, true)
if err != nil {
resetErrors = multierror.Append(resetErrors, err)
}
_, genericRm, err := mc.Remove(false, false)
_, genericRm, err := mc.Remove(machines, false, false)
if err != nil {
resetErrors = multierror.Append(resetErrors, err)
}

View File

@ -153,7 +153,7 @@ func (mc *MachineConfig) updateLastBoot() error { //nolint:unused
return mc.Write()
}
func (mc *MachineConfig) Remove(saveIgnition, saveImage bool) ([]string, func() error, error) {
func (mc *MachineConfig) Remove(machines map[string]bool, saveIgnition, saveImage bool) ([]string, func() error, error) {
ignitionFile, err := mc.IgnitionFile()
if err != nil {
return nil, nil, err
@ -195,7 +195,7 @@ func (mc *MachineConfig) Remove(saveIgnition, saveImage bool) ([]string, func()
mcRemove := func() error {
var errs []error
if err := connection.RemoveConnections(mc.Name, mc.Name+"-root"); err != nil {
if err := connection.RemoveConnections(machines, mc.Name, mc.Name+"-root"); err != nil {
errs = append(errs, err)
}