Skip to content

Commit

Permalink
move network alias validation to container create
Browse files Browse the repository at this point in the history
Podman 4.0 currently errors when you use network aliases for a network which
has dns disabled. Because the error happens on network setup this can
cause regression for old working containers. The network backend should not
validate this. Instead podman should check this at container create time
and also for network connect.

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Sep 28, 2021
1 parent d0950f3 commit 1c89262
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 31 deletions.
3 changes: 0 additions & 3 deletions libpod/network/cni/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,6 @@ outer:
}
return errors.Errorf("requested static ip %s not in any subnet on network %s", ip.String(), network.libpodNet.Name)
}
if len(netOpts.Aliases) > 0 && !network.libpodNet.DNSEnabled {
return errors.New("cannot set aliases on a network without dns enabled")
}
return nil
}

Expand Down
41 changes: 20 additions & 21 deletions libpod/network/cni/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,26 @@ var _ = Describe("run CNI", func() {
})
})

It("setup with aliases but dns disabled should work", func() {
runTest(func() {
defNet := types.DefaultNetworkName
intName := "eth0"
setupOpts := types.SetupOptions{
NetworkOptions: types.NetworkOptions{
ContainerID: stringid.GenerateNonCryptoID(),
Networks: map[string]types.PerNetworkOptions{
defNet: {
InterfaceName: intName,
Aliases: []string{"somealias"},
},
},
},
}
_, err := libpodNet.Setup(netNSContainer.Path(), setupOpts)
Expect(err).ToNot(HaveOccurred())
})
})

})

Context("invalid network setup test", func() {
Expand Down Expand Up @@ -1052,27 +1072,6 @@ var _ = Describe("run CNI", func() {
})
})

It("setup with aliases but dns disabled", func() {
runTest(func() {
defNet := types.DefaultNetworkName
intName := "eth0"
setupOpts := types.SetupOptions{
NetworkOptions: types.NetworkOptions{
ContainerID: stringid.GenerateNonCryptoID(),
Networks: map[string]types.PerNetworkOptions{
defNet: {
InterfaceName: intName,
Aliases: []string{"somealias"},
},
},
},
}
_, err := libpodNet.Setup(netNSContainer.Path(), setupOpts)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("cannot set aliases on a network without dns enabled"))
})
})

It("setup without networks", func() {
runTest(func() {
setupOpts := types.SetupOptions{
Expand Down
8 changes: 8 additions & 0 deletions libpod/networking_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,14 @@ func (c *Container) NetworkConnect(nameOrID, netName string, aliases []string) e
// get network status before we connect
networkStatus := c.getNetworkStatus()

network, err := c.runtime.network.NetworkInspect(netName)
if err != nil {
return err
}
if !network.DNSEnabled && len(aliases) > 0 {
return errors.Wrapf(define.ErrInvalidArg, "cannot set network aliases for network %q because dns is disabled", netName)
}

if err := c.runtime.state.NetworkConnect(c, netName, aliases); err != nil {
return err
}
Expand Down
22 changes: 15 additions & 7 deletions libpod/runtime_ctr.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,6 @@ func (r *Runtime) newContainer(ctx context.Context, rSpec *spec.Spec, options ..
}

func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Container, retErr error) {
// Validate the container
if err := ctr.validate(); err != nil {
return nil, err
}
if ctr.config.IsInfra {
ctr.config.StopTimeout = 10
}
// normalize the networks to names
// ocicni only knows about cni names so we have to make
// sure we do not use ids internally
Expand All @@ -265,11 +258,26 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Contai
if err != nil {
return nil, err
}
network, err := r.network.NetworkInspect(netName)
if err != nil {
return nil, err
}
if !network.DNSEnabled {
return nil, errors.Wrapf(define.ErrInvalidArg, "cannot set network aliases for network %q because dns is disabled", netName)
}
netAliases[netName] = aliases
}
ctr.config.NetworkAliases = netAliases
}

// Validate the container
if err := ctr.validate(); err != nil {
return nil, err
}
if ctr.config.IsInfra {
ctr.config.StopTimeout = 10
}

// Inhibit shutdown until creation succeeds
shutdown.Inhibit()
defer shutdown.Uninhibit()
Expand Down

0 comments on commit 1c89262

Please sign in to comment.