-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Implement network plugin #7947
Implement network plugin #7947
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,8 @@ import ( | |
"path/filepath" | ||
"sync" | ||
|
||
cni "github.com/containerd/go-cni" | ||
"github.com/containerd/containerd/pkg/cri/util" | ||
"github.com/containerd/containerd/pkg/net/compat" | ||
"github.com/fsnotify/fsnotify" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
@@ -36,12 +37,12 @@ type cniNetConfSyncer struct { | |
|
||
watcher *fsnotify.Watcher | ||
confDir string | ||
netPlugin cni.CNI | ||
loadOpts []cni.Opt | ||
netPlugin compat.CNI | ||
loadOpts []compat.LoadOpt | ||
} | ||
|
||
// newCNINetConfSyncer creates cni network conf syncer. | ||
func newCNINetConfSyncer(confDir string, netPlugin cni.CNI, loadOpts []cni.Opt) (*cniNetConfSyncer, error) { | ||
func newCNINetConfSyncer(confDir string, netPlugin compat.CNI, loadOpts []compat.LoadOpt) (*cniNetConfSyncer, error) { | ||
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. Would we eventually use
|
||
watcher, err := fsnotify.NewWatcher() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create fsnotify watcher: %w", err) | ||
|
@@ -69,7 +70,7 @@ func newCNINetConfSyncer(confDir string, netPlugin cni.CNI, loadOpts []cni.Opt) | |
loadOpts: loadOpts, | ||
} | ||
|
||
if err := syncer.netPlugin.Load(syncer.loadOpts...); err != nil { | ||
if err := syncer.netPlugin.Load(util.NamespacedContext(), syncer.loadOpts...); err != nil { | ||
logrus.WithError(err).Error("failed to load cni during init, please check CRI plugin status before setting up network for pods") | ||
syncer.updateLastStatus(err) | ||
} | ||
|
@@ -97,7 +98,7 @@ func (syncer *cniNetConfSyncer) syncLoop() error { | |
} | ||
logrus.Debugf("receiving change event from cni conf dir: %s", event) | ||
|
||
lerr := syncer.netPlugin.Load(syncer.loadOpts...) | ||
lerr := syncer.netPlugin.Load(util.NamespacedContext(), syncer.loadOpts...) | ||
if lerr != nil { | ||
logrus.WithError(lerr). | ||
Errorf("failed to reload cni configuration after receiving fs change event(%s)", event) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
/* | ||
Copyright The containerd Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package server | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/containerd/containerd/pkg/net" | ||
"github.com/containerd/containerd/pkg/net/compat" | ||
"github.com/containerd/go-cni" | ||
) | ||
|
||
// cniAdaptor is created to adapt the APIs of network plugin to their | ||
// counterparts in go-cni. | ||
type cniAdaptor struct { | ||
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. The adapter is for both |
||
adapt bool | ||
g cni.CNI | ||
c compat.CNI | ||
} | ||
|
||
var _ compat.CNI = (*cniAdaptor)(nil) | ||
|
||
//nolint:nolintlint,unused | ||
func newCNIAdaptor(netAPI compat.API, name string, opts ...compat.Opt) (*cniAdaptor, error) { | ||
var err error | ||
|
||
c := &cniAdaptor{ | ||
adapt: false, | ||
} | ||
|
||
if netAPI == nil { | ||
c.adapt = true | ||
} | ||
|
||
if c.adapt { | ||
dopts, err := convertOpts(opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if c.g, err = cni.New(dopts...); err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
if c.c, err = netAPI.NewCNI(name, opts...); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
func (c *cniAdaptor) Setup(ctx context.Context, id string, path string, opts ...net.AttachmentOpt) (*compat.Result, error) { | ||
if !c.adapt { | ||
return c.c.Setup(ctx, id, path, opts...) | ||
} | ||
|
||
dopts, err := convertNamespaceOpts(opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
r, err := c.g.Setup(ctx, id, path, dopts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return compat.WrapResult(r), nil | ||
} | ||
|
||
func (c *cniAdaptor) SetupSerially(ctx context.Context, id string, path string, opts ...net.AttachmentOpt) (*compat.Result, error) { | ||
if !c.adapt { | ||
return c.c.SetupSerially(ctx, id, path, opts...) | ||
} | ||
|
||
dopts, err := convertNamespaceOpts(opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
r, err := c.g.Setup(ctx, id, path, dopts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return compat.WrapResult(r), nil | ||
} | ||
|
||
func (c *cniAdaptor) Remove(ctx context.Context, id string, path string, opts ...net.AttachmentOpt) error { | ||
if !c.adapt { | ||
return c.c.Remove(ctx, id, path, opts...) | ||
} | ||
|
||
dopts, err := convertNamespaceOpts(opts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return c.g.Remove(ctx, id, path, dopts...) | ||
} | ||
|
||
func (c *cniAdaptor) Check(ctx context.Context, id string, path string, opts ...net.AttachmentOpt) error { | ||
if !c.adapt { | ||
return c.c.Check(ctx, id, path, opts...) | ||
} | ||
|
||
dopts, err := convertNamespaceOpts(opts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return c.g.Check(ctx, id, path, dopts...) | ||
} | ||
|
||
func (c *cniAdaptor) Load(ctx context.Context, opts ...compat.LoadOpt) error { | ||
if !c.adapt { | ||
return c.c.Load(ctx, opts...) | ||
} | ||
|
||
var dopts []cni.Opt | ||
for _, o := range opts { | ||
name := getFunctionName(o) | ||
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. I'd like to avoid reflection if possible. Why do we need to know the name? |
||
sl := strings.Split(name, "/") | ||
switch sl[len(sl)-1] { | ||
case "compat.WithLoNetwork": | ||
dopts = append(dopts, cni.WithLoNetwork) | ||
case "compat.WithDefaultConf": | ||
dopts = append(dopts, cni.WithDefaultConf) | ||
} | ||
} | ||
|
||
return c.g.Load(dopts...) | ||
} | ||
|
||
func (c *cniAdaptor) Status(ctx context.Context) error { | ||
if !c.adapt { | ||
return c.c.Status(ctx) | ||
} | ||
return c.g.Status() | ||
} | ||
|
||
func (c *cniAdaptor) GetConfig(ctx context.Context) *cni.ConfigResult { | ||
if !c.adapt { | ||
return c.c.GetConfig(ctx) | ||
} | ||
return c.g.GetConfig() | ||
} | ||
|
||
//nolint:nolintlint,unused | ||
func convertOpts(opts []compat.Opt) ([]cni.Opt, error) { | ||
var ( | ||
cfg compat.Config | ||
dopts []cni.Opt | ||
) | ||
|
||
for _, o := range opts { | ||
if err := o(&cfg); err != nil { | ||
return dopts, err | ||
} | ||
} | ||
|
||
if len(cfg.PluginDirs) > 0 { | ||
dopts = append(dopts, cni.WithPluginDir(cfg.PluginDirs)) | ||
} | ||
if len(cfg.PluginConfDir) > 0 { | ||
dopts = append(dopts, cni.WithPluginConfDir(cfg.PluginConfDir)) | ||
} | ||
if cfg.PluginMaxConfNum > 0 { | ||
dopts = append(dopts, cni.WithPluginMaxConfNum(cfg.PluginMaxConfNum)) | ||
} | ||
if len(cfg.Prefix) > 0 { | ||
dopts = append(dopts, cni.WithInterfacePrefix(cfg.Prefix)) | ||
} | ||
if cfg.NetworkCount > 0 { | ||
dopts = append(dopts, cni.WithMinNetworkCount(cfg.NetworkCount)) | ||
} | ||
|
||
return dopts, nil | ||
} | ||
|
||
func convertNamespaceOpts(opts []net.AttachmentOpt) ([]cni.NamespaceOpts, error) { | ||
var dopts []cni.NamespaceOpts | ||
|
||
args := net.AttachmentArgs{ | ||
CapabilityArgs: make(map[string]interface{}), | ||
PluginArgs: make(map[string]string), | ||
} | ||
|
||
for _, o := range opts { | ||
if err := o(&args); err != nil { | ||
return dopts, err | ||
} | ||
} | ||
|
||
for k, v := range args.PluginArgs { | ||
dopts = append(dopts, cni.WithArgs(k, v)) | ||
} | ||
|
||
for k, v := range args.CapabilityArgs { | ||
dopts = append(dopts, cni.WithCapability(k, v)) | ||
} | ||
|
||
return dopts, nil | ||
} | ||
|
||
func getFunctionName(i interface{}) string { | ||
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name() | ||
} |
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 believe that this is inspired by ENABLE_CRI_SANDBOXES. But do you think we cannot make this stable enough before releasing 1.7.0? My worry is that nobody use this if that is opt-in.