Skip to content

Commit

Permalink
Check the namespace and macvlan before creating them
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelrahmanElawady committed Oct 22, 2023
1 parent 2502823 commit d7ad1df
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions pkg/network/public/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,8 @@ func EnsurePublicSetup(nodeID pkg.Identifier, inf *pkg.PublicConfig) (*netlink.B
return nil, errors.Wrap(err, "failed to get current public bridge uplink")
}

err = ensureTestNamespace(br)
if err != nil {
return nil, fmt.Errorf("failed to create test namespace: %w", err)
if err := ensureTestNamespace(br); err != nil {
return nil, errors.Wrap(err, "failed to create test namespace")
}

if inf == nil || inf.IsEmpty() {
Expand Down Expand Up @@ -357,11 +356,20 @@ func EnsurePublicSetup(nodeID pkg.Identifier, inf *pkg.PublicConfig) (*netlink.B
}

func ensureTestNamespace(publicBrdige *netlink.Bridge) error {
ns, err := namespace.Create(testNamespace)
netNS, err := namespace.GetByName(testNamespace)
if err != nil {
netNS, err = namespace.Create(testNamespace)
if err != nil {
return fmt.Errorf("failed to create namespace %s: %w", testNamespace, err)
}
}
err = netNS.Do(func(_ ns.NetNS) error {
_, err := macvlan.GetByName(testMacvlan)
return err
})
if err != nil {
return fmt.Errorf("failed to create namespace %s: %w", ns, err)
_, err = macvlan.Create(testMacvlan, publicBrdige.Name, netNS)
}
_, err = macvlan.Create(testMacvlan, publicBrdige.Name, ns)
return err
}

Expand Down

0 comments on commit d7ad1df

Please sign in to comment.