From e3c9e064a930f5c8572c94ae8c782de01f206bbe Mon Sep 17 00:00:00 2001 From: Alex Suraci Date: Thu, 18 Aug 2022 23:05:58 -0400 Subject: [PATCH] cniprovider: pass hostname as K8S_POD_NAME arg Signed-off-by: Alex Suraci --- executor/containerdexecutor/executor.go | 2 +- executor/runcexecutor/executor.go | 2 +- util/network/cniprovider/cni.go | 20 +++++++++++++++----- util/network/host.go | 2 +- util/network/network.go | 2 +- util/network/none.go | 2 +- 6 files changed, 20 insertions(+), 10 deletions(-) diff --git a/executor/containerdexecutor/executor.go b/executor/containerdexecutor/executor.go index 9336a6489..4801a8488 100644 --- a/executor/containerdexecutor/executor.go +++ b/executor/containerdexecutor/executor.go @@ -146,7 +146,7 @@ func (w *containerdExecutor) Run(ctx context.Context, id string, root executor.M if !ok { return errors.Errorf("unknown network mode %s", meta.NetMode) } - namespace, err := provider.New() + namespace, err := provider.New(meta.Hostname) if err != nil { return err } diff --git a/executor/runcexecutor/executor.go b/executor/runcexecutor/executor.go index 702d51310..1755b5ed4 100644 --- a/executor/runcexecutor/executor.go +++ b/executor/runcexecutor/executor.go @@ -161,7 +161,7 @@ func (w *runcExecutor) Run(ctx context.Context, id string, root executor.Mount, if !ok { return errors.Errorf("unknown network mode %s", meta.NetMode) } - namespace, err := provider.New() + namespace, err := provider.New(meta.Hostname) if err != nil { return err } diff --git a/util/network/cniprovider/cni.go b/util/network/cniprovider/cni.go index edecd6c87..01ad0dad8 100644 --- a/util/network/cniprovider/cni.go +++ b/util/network/cniprovider/cni.go @@ -67,32 +67,42 @@ func (c *cniProvider) initNetwork() error { } defer l.Unlock() } - ns, err := c.New() + ns, err := c.New("test") if err != nil { return err } return ns.Close() } -func (c *cniProvider) New() (network.Namespace, error) { +func (c *cniProvider) New(hostname string) (network.Namespace, error) { id := identity.NewID() nativeID, err := createNetNS(c, id) if err != nil { return nil, err } - if _, err := c.CNI.Setup(context.TODO(), id, nativeID); err != nil { + nsOpts := []cni.NamespaceOpts{ + // NB: K8S_POD_NAME is a semi-well-known arg set by k8s and podman and + // leveraged by the dnsname CNI plugin. a more generic name would be nice. + cni.WithArgs("K8S_POD_NAME", hostname), + + // must be set for plugins that don't understand K8S_POD_NAME + cni.WithArgs("IgnoreUnknown", "1"), + } + + if _, err := c.CNI.Setup(context.TODO(), id, nativeID, nsOpts...); err != nil { deleteNetNS(nativeID) return nil, errors.Wrap(err, "CNI setup error") } - return &cniNS{nativeID: nativeID, id: id, handle: c.CNI}, nil + return &cniNS{nativeID: nativeID, id: id, handle: c.CNI, opts: nsOpts}, nil } type cniNS struct { handle cni.CNI id string nativeID string + opts []cni.NamespaceOpts } func (ns *cniNS) Set(s *specs.Spec) error { @@ -100,7 +110,7 @@ func (ns *cniNS) Set(s *specs.Spec) error { } func (ns *cniNS) Close() error { - err := ns.handle.Remove(context.TODO(), ns.id, ns.nativeID) + err := ns.handle.Remove(context.TODO(), ns.id, ns.nativeID, ns.opts...) if err1 := unmountNetNS(ns.nativeID); err1 != nil && err == nil { err = err1 } diff --git a/util/network/host.go b/util/network/host.go index c50268d45..b390a54cd 100644 --- a/util/network/host.go +++ b/util/network/host.go @@ -15,7 +15,7 @@ func NewHostProvider() Provider { type host struct { } -func (h *host) New() (Namespace, error) { +func (h *host) New(hostname string) (Namespace, error) { return &hostNS{}, nil } diff --git a/util/network/network.go b/util/network/network.go index befeef0c7..e0f5658eb 100644 --- a/util/network/network.go +++ b/util/network/network.go @@ -8,7 +8,7 @@ import ( // Provider interface for Network type Provider interface { - New() (Namespace, error) + New(hostname string) (Namespace, error) } // Namespace of network for workers diff --git a/util/network/none.go b/util/network/none.go index 336ff68b9..81af7b17a 100644 --- a/util/network/none.go +++ b/util/network/none.go @@ -11,7 +11,7 @@ func NewNoneProvider() Provider { type none struct { } -func (h *none) New() (Namespace, error) { +func (h *none) New(hostname string) (Namespace, error) { return &noneNS{}, nil }