diff --git a/pkg/apis/kubeone/helpers.go b/pkg/apis/kubeone/helpers.go index c03d60158..b838c671d 100644 --- a/pkg/apis/kubeone/helpers.go +++ b/pkg/apis/kubeone/helpers.go @@ -108,6 +108,17 @@ func (crc *ContainerRuntimeConfig) UnmarshalText(text []byte) error { return nil } +func (crc ContainerRuntimeConfig) ConfigPath() string { + switch { + case crc.Containerd != nil: + return "/etc/containerd/config.toml" + case crc.Docker != nil: + return "/etc/docker/daemon.json" + } + + return "" +} + func (crc ContainerRuntimeConfig) CRISocket() string { switch { case crc.Containerd != nil: diff --git a/pkg/containerruntime/containerd_config.go b/pkg/containerruntime/containerd_config.go new file mode 100644 index 000000000..cf6ba7f63 --- /dev/null +++ b/pkg/containerruntime/containerd_config.go @@ -0,0 +1,110 @@ +/* +Copyright 2021 The KubeOne 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 containerruntime + +import ( + "fmt" + "strings" + + "github.com/BurntSushi/toml" + + "k8c.io/kubeone/pkg/apis/kubeone" +) + +type containerdConfig struct { + Version int `toml:"version"` + Metrics *containerdMetrics `toml:"metrics"` + Plugins map[string]interface{} `toml:"plugins"` +} + +type containerdMetrics struct { + Address string `toml:"address"` +} + +type containerdCRIPlugin struct { + Containerd *containerdCRISettings `toml:"containerd"` + Registry *containerdCRIRegistry `toml:"registry"` +} + +type containerdCRISettings struct { + Runtimes map[string]containerdCRIRuntime `toml:"runtimes"` +} + +type containerdCRIRuntime struct { + RuntimeType string `toml:"runtime_type"` + Options interface{} `toml:"options"` +} + +type containerdCRIRuncOptions struct { + SystemdCgroup bool +} + +type containerdCRIRegistry struct { + Mirrors map[string]containerdMirror `toml:"mirrors"` +} + +type containerdMirror struct { + Endpoint []string `toml:"endpoint"` +} + +func marshalContainerdConfig(cluster *kubeone.KubeOneCluster) (string, error) { + criPlugin := containerdCRIPlugin{ + Containerd: &containerdCRISettings{ + Runtimes: map[string]containerdCRIRuntime{ + "runc": { + RuntimeType: "io.containerd.runc.v2", + Options: containerdCRIRuncOptions{ + SystemdCgroup: true, + }, + }, + }, + }, + Registry: &containerdCRIRegistry{ + Mirrors: map[string]containerdMirror{ + "docker.io": { + Endpoint: []string{"https://registry-1.docker.io"}, + }, + }, + }, + } + + insecureRegistry := cluster.RegistryConfiguration.InsecureRegistryAddress() + if insecureRegistry != "" { + criPlugin.Registry.Mirrors[insecureRegistry] = containerdMirror{ + Endpoint: []string{fmt.Sprintf("http://%s", insecureRegistry)}, + } + } + + cfg := containerdConfig{ + Version: 2, + Metrics: &containerdMetrics{ + // metrics available at http://127.0.0.1:1338/v1/metrics + Address: "127.0.0.1:1338", + }, + + Plugins: map[string]interface{}{ + "io.containerd.grpc.v1.cri": criPlugin, + }, + } + + var buf strings.Builder + enc := toml.NewEncoder(&buf) + enc.Indent = "" + err := enc.Encode(cfg) + + return buf.String(), err +} diff --git a/pkg/containerruntime/docker_config.go b/pkg/containerruntime/docker_config.go new file mode 100644 index 000000000..539f60d22 --- /dev/null +++ b/pkg/containerruntime/docker_config.go @@ -0,0 +1,55 @@ +/* +Copyright 2021 The KubeOne 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 containerruntime + +import ( + "encoding/json" + + "k8c.io/kubeone/pkg/apis/kubeone" +) + +type dockerConfig struct { + ExecOpts []string `json:"exec-opts,omitempty"` + StorageDriver string `json:"storage-driver,omitempty"` + LogDriver string `json:"log-driver,omitempty"` + LogOpts map[string]string `json:"log-opts,omitempty"` + InsecureRegistries []string `json:"insecure-registries,omitempty"` + RegistryMirrors []string `json:"registry-mirrors,omitempty"` +} + +func marshalDockerConfig(cluster *kubeone.KubeOneCluster) (string, error) { + cfg := dockerConfig{ + ExecOpts: []string{"native.cgroupdriver=systemd"}, + StorageDriver: "overlay2", + LogDriver: "json-file", + LogOpts: map[string]string{ + "max-size": "100m", + }, + } + + insecureRegistry := cluster.RegistryConfiguration.InsecureRegistryAddress() + if insecureRegistry != "" { + cfg.InsecureRegistries = []string{insecureRegistry} + } + + b, err := json.MarshalIndent(cfg, "", " ") + if err != nil { + return "", err + } + + return string(b), nil +} diff --git a/pkg/containerruntime/map.go b/pkg/containerruntime/map.go new file mode 100644 index 000000000..8d1c85958 --- /dev/null +++ b/pkg/containerruntime/map.go @@ -0,0 +1,43 @@ +/* +Copyright 2021 The KubeOne 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 containerruntime + +import "k8c.io/kubeone/pkg/apis/kubeone" + +func UpdateDataMap(cluster *kubeone.KubeOneCluster, inputMap map[string]interface{}) error { + var ( + crConfig string + err error + ) + + switch { + case cluster.ContainerRuntime.Containerd != nil: + crConfig, err = marshalContainerdConfig(cluster) + case cluster.ContainerRuntime.Docker != nil: + crConfig, err = marshalDockerConfig(cluster) + } + + if err != nil { + return err + } + + inputMap["CONTAINER_RUNTIME_CONFIG_PATH"] = cluster.ContainerRuntime.ConfigPath() + inputMap["CONTAINER_RUNTIME_CONFIG"] = crConfig + inputMap["CONTAINER_RUNTIME_SOCKET"] = cluster.ContainerRuntime.CRISocket() + + return nil +} diff --git a/pkg/scripts/funcs.go b/pkg/scripts/funcs.go index be8738944..9a9275ed1 100644 --- a/pkg/scripts/funcs.go +++ b/pkg/scripts/funcs.go @@ -17,11 +17,6 @@ limitations under the License. package scripts import ( - "encoding/json" - "fmt" - "strings" - - "github.com/BurntSushi/toml" "github.com/MakeNowJust/heredoc/v2" ) @@ -43,13 +38,6 @@ var ( esac {{ end }} - {{ define "docker-daemon-config" }} - sudo mkdir -p /etc/docker - cat <