Skip to content

Commit 87bab6c

Browse files
authored
Merge pull request containerd#10238 from MikeZappa87/feature/provideinternalloup
Add support to set loopback to up
2 parents 28b77e3 + 332caf1 commit 87bab6c

File tree

9 files changed

+107
-8
lines changed

9 files changed

+107
-8
lines changed

docs/cri/config.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,8 @@ version = 2
443443
# * ipv6 - select the first ipv6 address
444444
# * cni - use the order returned by the CNI plugins, returning the first IP address from the results
445445
ip_pref = "ipv4"
446+
# use_internal_loopback specifies if we use the CNI loopback plugin or internal mechanism to set lo to up
447+
use_internal_loopback = false
446448

447449
# 'plugins."io.containerd.grpc.v1.cri".image_decryption' contains config related
448450
# to handling decryption of encrypted container images.

internal/cri/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ type CniConfig struct {
185185
// * ipv6 - select the first ipv6 address
186186
// * cni - use the order returned by the CNI plugins, returning the first IP address from the results
187187
IPPreference string `toml:"ip_pref" json:"ipPref"`
188+
// UseInternalLoopback specifies if we use the CNI loopback plugin or internal mechanism to set lo to up
189+
UseInternalLoopback bool `toml:"use_internal_loopback" json:"useInternalLoopback"`
188190
}
189191

190192
// Mirror contains the config related to the registry mirror

internal/cri/config/config_unix.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func DefaultRuntimeConfig() RuntimeConfig {
7777
NetworkPluginMaxConfNum: 1, // only one CNI plugin config file will be loaded
7878
NetworkPluginSetupSerially: false,
7979
NetworkPluginConfTemplate: "",
80+
UseInternalLoopback: false,
8081
},
8182
ContainerdConfig: ContainerdConfig{
8283
DefaultRuntimeName: "runc",

internal/cri/config/config_windows.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func DefaultRuntimeConfig() RuntimeConfig {
4747
NetworkPluginMaxConfNum: 1,
4848
NetworkPluginSetupSerially: false,
4949
NetworkPluginConfTemplate: "",
50+
UseInternalLoopback: false,
5051
},
5152
ContainerdConfig: ContainerdConfig{
5253
DefaultRuntimeName: "runhcs-wcow-process",

internal/cri/server/sandbox_run.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,12 @@ func (c *criService) setupPodNetwork(ctx context.Context, sandbox *sandboxstore.
455455
if netPlugin == nil {
456456
return errors.New("cni config not initialized")
457457
}
458-
458+
if c.config.UseInternalLoopback {
459+
err := c.bringUpLoopback(path)
460+
if err != nil {
461+
return fmt.Errorf("unable to set lo to up: %w", err)
462+
}
463+
}
459464
opts, err := cniNamespaceOpts(id, config)
460465
if err != nil {
461466
return fmt.Errorf("get cni namespace options: %w", err)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package server
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/containernetworking/plugins/pkg/ns"
23+
"github.com/vishvananda/netlink"
24+
)
25+
26+
func (c *criService) bringUpLoopback(netns string) error {
27+
if err := ns.WithNetNSPath(netns, func(_ ns.NetNS) error {
28+
link, err := netlink.LinkByName("lo")
29+
if err != nil {
30+
return err
31+
}
32+
return netlink.LinkSetUp(link)
33+
}); err != nil {
34+
return fmt.Errorf("error setting loopback interface up: %w", err)
35+
}
36+
return nil
37+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//go:build !windows && !linux
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package server
20+
21+
func (c *criService) bringUpLoopback(string) error {
22+
return nil
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package server
18+
19+
func (c *criService) bringUpLoopback(string) error {
20+
return nil
21+
}

internal/cri/server/service_linux.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ func init() {
3737
}
3838
}
3939

40-
// networkAttachCount is the minimum number of networks the PodSandbox
41-
// attaches to
42-
const networkAttachCount = 2
43-
4440
// initPlatform handles linux specific initialization for the CRI service.
4541
func (c *criService) initPlatform() (err error) {
4642
if userns.RunningInUserNS() {
@@ -69,6 +65,12 @@ func (c *criService) initPlatform() (err error) {
6965
}
7066
}
7167

68+
networkAttachCount := 2
69+
70+
if c.Config().UseInternalLoopback {
71+
networkAttachCount = 1
72+
}
73+
7274
c.netPlugin = make(map[string]cni.CNI)
7375
for name, dir := range pluginDirs {
7476
max := c.config.NetworkPluginMaxConfNum
@@ -78,9 +80,10 @@ func (c *criService) initPlatform() (err error) {
7880
}
7981
}
8082
// Pod needs to attach to at least loopback network and a non host network,
81-
// hence networkAttachCount is 2. If there are more network configs the
82-
// pod will be attached to all the networks but we will only use the ip
83-
// of the default network interface as the pod IP.
83+
// hence networkAttachCount is 2 if the CNI plugin is used and
84+
// 1 if the internal mechanism for setting lo to up is used.
85+
// If there are more network configs the pod will be attached to all the networks
86+
// but we will only use the ip of the default network interface as the pod IP.
8487
i, err := cni.New(cni.WithMinNetworkCount(networkAttachCount),
8588
cni.WithPluginConfDir(dir),
8689
cni.WithPluginMaxConfNum(max),
@@ -110,5 +113,9 @@ func (c *criService) initPlatform() (err error) {
110113

111114
// cniLoadOptions returns cni load options for the linux.
112115
func (c *criService) cniLoadOptions() []cni.Opt {
116+
if c.config.UseInternalLoopback {
117+
return []cni.Opt{cni.WithDefaultConf}
118+
}
119+
113120
return []cni.Opt{cni.WithLoNetwork, cni.WithDefaultConf}
114121
}

0 commit comments

Comments
 (0)