-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial work to support dynamic multiple CNI configurations #113
Open
MikeZappa87
wants to merge
1
commit into
containerd:main
Choose a base branch
from
MikeZappa87:issue/multi-network-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,8 +35,12 @@ type CNI interface { | |
Setup(ctx context.Context, id string, path string, opts ...NamespaceOpts) (*Result, error) | ||
// SetupSerially sets up each of the network interfaces for the namespace in serial | ||
SetupSerially(ctx context.Context, id string, path string, opts ...NamespaceOpts) (*Result, error) | ||
//SetupNetworks sets up a list of networks | ||
SetupNetworks(ctx context.Context, id string, path string, networks []*Network, opts ...NamespaceOpts) (*Result, error) | ||
// Remove tears down the network of the namespace. | ||
Remove(ctx context.Context, id string, path string, opts ...NamespaceOpts) error | ||
// RemoveNetworks tears down a list of provided networks | ||
RemoveNetworks(ctx context.Context, id string, path string, networks []*Network, opts ...NamespaceOpts) error | ||
// Check checks if the network is still in desired state | ||
Check(ctx context.Context, id string, path string, opts ...NamespaceOpts) error | ||
// Load loads the cni network config | ||
|
@@ -45,6 +49,8 @@ type CNI interface { | |
Status() error | ||
// GetConfig returns a copy of the CNI plugin configurations as parsed by CNI | ||
GetConfig() *ConfigResult | ||
//BuildMultiNetwork returns a list of networks that match the network name in the cni cache | ||
BuildMultiNetwork(networkNames []*NetworkInterface) ([]*Network, error) | ||
} | ||
|
||
type ConfigResult struct { | ||
|
@@ -83,6 +89,11 @@ type libcni struct { | |
sync.RWMutex | ||
} | ||
|
||
type NetworkInterface struct { | ||
NetworkName string | ||
InterfaceName string | ||
} | ||
|
||
func defaultCNIConfig() *libcni { | ||
return &libcni{ | ||
config: config{ | ||
|
@@ -183,15 +194,77 @@ func (c *libcni) SetupSerially(ctx context.Context, id string, path string, opts | |
return c.createResult(result) | ||
} | ||
|
||
// BuildMultiNetwork build dynamic list of Networks. | ||
func (c *libcni) BuildMultiNetwork(networkNames []*NetworkInterface) ([]*Network, error) { | ||
var network []*Network | ||
ifaceindex := 0 | ||
|
||
config := make(map[string]*Network) | ||
ifs := make(map[string]*Network) | ||
|
||
for _, v := range c.Networks() { | ||
config[v.config.Name] = v | ||
} | ||
|
||
name := "" | ||
for _, v := range networkNames { | ||
if net, ok := config[v.NetworkName]; ok { | ||
if v.InterfaceName == "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if a network has two or more interfaces? |
||
name = getIfName(c.prefix, ifaceindex) | ||
ifaceindex++ | ||
} else { | ||
name = v.InterfaceName | ||
} | ||
|
||
network = append(network, &Network{ | ||
cni: net.cni, | ||
config: net.config, | ||
ifName: name, | ||
}) | ||
|
||
if _, ok := ifs[name]; ok { | ||
return nil, fmt.Errorf("the interface: %v already exists and must be unique", name) | ||
} | ||
|
||
ifs[name] = net | ||
} else { | ||
return nil, fmt.Errorf("the network config: %v does not exist", v.NetworkName) | ||
} | ||
} | ||
|
||
return network, nil | ||
} | ||
|
||
// SetupSerially setups specific networks in the namespace and returns a Result | ||
func (c *libcni) SetupNetworks(ctx context.Context, id string, path string, networks []*Network, opts ...NamespaceOpts) (*Result, error) { | ||
if err := c.Status(); err != nil { | ||
return nil, err | ||
} | ||
ns, err := newNamespace(id, path, opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result, err := c.attachWithMultipleNetworksSerially(ctx, ns, networks) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return c.createResult(result) | ||
} | ||
|
||
func (c *libcni) attachNetworksSerially(ctx context.Context, ns *Namespace) ([]*types100.Result, error) { | ||
return c.attachWithMultipleNetworksSerially(ctx, ns, c.Networks()) | ||
} | ||
|
||
func (c *libcni) attachWithMultipleNetworksSerially(ctx context.Context, ns *Namespace, networks []*Network) ([]*types100.Result, error) { | ||
var results []*types100.Result | ||
for _, network := range c.Networks() { | ||
for _, network := range networks { | ||
r, err := network.Attach(ctx, ns) | ||
if err != nil { | ||
return nil, err | ||
} | ||
results = append(results, r) | ||
} | ||
|
||
return results, nil | ||
} | ||
|
||
|
@@ -258,6 +331,33 @@ func (c *libcni) Remove(ctx context.Context, id string, path string, opts ...Nam | |
return nil | ||
} | ||
|
||
func (c *libcni) RemoveNetworks(ctx context.Context, id string, path string, networks []*Network, opts ...NamespaceOpts) error { | ||
if err := c.Status(); err != nil { | ||
return err | ||
} | ||
ns, err := newNamespace(id, path, opts...) | ||
if err != nil { | ||
return err | ||
} | ||
for i := len(networks) - 1; i >= 0; i-- { | ||
if err := networks[i].Remove(ctx, ns); err != nil { | ||
// Based on CNI spec v0.7.0, empty network namespace is allowed to | ||
// do best effort cleanup. However, it is not handled consistently | ||
// right now: | ||
// https://github.com/containernetworking/plugins/issues/210 | ||
// TODO(random-liu): Remove the error handling when the issue is | ||
// fixed and the CNI spec v0.6.0 support is deprecated. | ||
// NOTE(claudiub): Some CNIs could return a "not found" error, which could mean that | ||
// it was already deleted. | ||
if (path == "" && strings.Contains(err.Error(), "no such file or directory")) || strings.Contains(err.Error(), "not found") { | ||
continue | ||
} | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Check checks if the network is still in desired state | ||
func (c *libcni) Check(ctx context.Context, id string, path string, opts ...NamespaceOpts) error { | ||
if err := c.Status(); err != nil { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am thinking through this type. It works in the current however give me some time to debate. If anyone likes the current state lmk