Skip to content

Commit

Permalink
Add support for Kubenet with containerd
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciprian Hacman committed May 12, 2020
1 parent 8768178 commit abbf60a
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 4 deletions.
42 changes: 41 additions & 1 deletion nodeup/pkg/model/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,13 @@ func (b *ContainerdBuilder) Build(c *fi.ModelBuilderContext) error {
return err
}

// Using containerd with Kubenet requires special configuration. This is a temporary backwards-compatible solution
// and will be deprecated when Kubenet is deprecated:
// https://github.com/containerd/cri/blob/master/docs/config.md#cni-config-template
if b.Cluster.Spec.ContainerRuntime == "containerd" && b.Cluster.Spec.Networking != nil && b.Cluster.Spec.Networking.Kubenet != nil {
b.buildKubenetConflistTemplate(c)
}

return nil
}

Expand Down Expand Up @@ -389,7 +396,6 @@ func (b *ContainerdBuilder) buildContainerOSConfigurationDropIn(c *fi.ModelBuild
"EnvironmentFile=/etc/environment",
"TasksMax=infinity",
}

contents := strings.Join(lines, "\n")

c.AddTask(&nodetasks.File{
Expand Down Expand Up @@ -441,6 +447,40 @@ func (b *ContainerdBuilder) buildSysconfig(c *fi.ModelBuilderContext) error {
return nil
}

// buildKubenetConflistTemplate is responsible for creating a special template for setups using Kubenet
func (b *ContainerdBuilder) buildKubenetConflistTemplate(c *fi.ModelBuilderContext) {
lines := []string{
"{",
" \"cniVersion\": \"0.3.1\",",
" \"name\": \"kubenet\",",
" \"plugins\": [",
" {",
" \"type\": \"bridge\",",
" \"bridge\": \"cbr0\",",
" \"mtu\": 1460,",
" \"addIf\": \"eth0\",",
" \"isGateway\": true,",
" \"ipMasq\": true,",
" \"promiscMode\": true,",
" \"ipam\": {",
" \"type\": \"host-local\",",
" \"subnet\": \"{{.PodCIDR}}\",",
" \"routes\": [{ \"dst\": \"0.0.0.0/0\" }]",
" }",
" }",
" ]",
"}",
}
contents := strings.Join(lines, "\n")
klog.V(8).Infof("Built kubenet CNI config file\n%s", contents)

c.AddTask(&nodetasks.File{
Path: "/etc/containerd/kubenet.conflist.template",
Contents: fi.NewStringResource(contents),
Type: nodetasks.FileType_File,
})
}

// skipInstall determines if kops should skip the installation and configuration of containerd
func (b *ContainerdBuilder) skipInstall() bool {
d := b.Cluster.Spec.Containerd
Expand Down
24 changes: 24 additions & 0 deletions nodeup/pkg/model/tests/containerdbuilder/simple/tasks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@ contents: ""
path: /etc/containerd/config-kops.toml
type: file
---
contents: |-
{
"cniVersion": "0.3.1",
"name": "kubenet",
"plugins": [
{
"type": "bridge",
"bridge": "cbr0",
"mtu": 1460,
"addIf": "eth0",
"isGateway": true,
"ipMasq": true,
"promiscMode": true,
"ipam": {
"type": "host-local",
"subnet": "{{.PodCIDR}}",
"routes": [{ "dst": "0.0.0.0/0" }]
}
}
]
}
path: /etc/containerd/kubenet.conflist.template
type: file
---
contents: CONTAINERD_OPTS=
path: /etc/sysconfig/containerd
type: file
Expand Down
18 changes: 17 additions & 1 deletion pkg/model/components/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package components

import (
"fmt"
"strings"

"k8s.io/klog"
"k8s.io/kops/pkg/apis/kops"
Expand Down Expand Up @@ -62,7 +63,22 @@ func (b *ContainerdOptionsBuilder) BuildOptions(o interface{}) error {

// Apply defaults for containerd running in container runtime mode
containerd.LogLevel = fi.String("info")
containerd.ConfigOverride = fi.String("")
if clusterSpec.Networking != nil && clusterSpec.Networking.Kubenet != nil {
// Using containerd with Kubenet requires special configuration. This is a temporary backwards-compatible solution
// and will be deprecated when Kubenet is deprecated:
// https://github.com/containerd/cri/blob/master/docs/config.md#cni-config-template
lines := []string{
"version = 2",
"[plugins]",
" [plugins.\"io.containerd.grpc.v1.cri\"]",
" [plugins.\"io.containerd.grpc.v1.cri\".cni]",
" conf_template = \"/etc/containerd/kubenet.conflist.template\"",
}
contents := strings.Join(lines, "\n")
containerd.ConfigOverride = fi.String(contents)
} else {
containerd.ConfigOverride = fi.String("")
}

} else if clusterSpec.ContainerRuntime == "docker" {
if fi.StringValue(containerd.Version) == "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ Resources.AWSAutoScalingLaunchConfigurationmasterustest1amasterscontainerdexampl
cloudConfig: null
containerRuntime: containerd
containerd:
configOverride: ""
configOverride: |-
version = 2
[plugins]
[plugins."io.containerd.grpc.v1.cri"]
[plugins."io.containerd.grpc.v1.cri".cni]
conf_template = "/etc/containerd/kubenet.conflist.template"
logLevel: info
version: 1.2.10
docker:
Expand Down Expand Up @@ -421,7 +426,12 @@ Resources.AWSAutoScalingLaunchConfigurationnodescontainerdexamplecom.Properties.
cloudConfig: null
containerRuntime: containerd
containerd:
configOverride: ""
configOverride: |-
version = 2
[plugins]
[plugins."io.containerd.grpc.v1.cri"]
[plugins."io.containerd.grpc.v1.cri".cni]
conf_template = "/etc/containerd/kubenet.conflist.template"
logLevel: info
version: 1.2.10
docker:
Expand Down

0 comments on commit abbf60a

Please sign in to comment.