Skip to content
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

OS Script templates improvements #1664

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pkg/apis/kubeone/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
110 changes: 110 additions & 0 deletions pkg/containerruntime/containerd_config.go
Original file line number Diff line number Diff line change
@@ -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
}
55 changes: 55 additions & 0 deletions pkg/containerruntime/docker_config.go
Original file line number Diff line number Diff line change
@@ -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
}
43 changes: 43 additions & 0 deletions pkg/containerruntime/map.go
Original file line number Diff line number Diff line change
@@ -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
}
124 changes: 0 additions & 124 deletions pkg/scripts/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ limitations under the License.
package scripts

import (
"encoding/json"
"fmt"
"strings"

"github.com/BurntSushi/toml"
"github.com/MakeNowJust/heredoc/v2"
)

Expand All @@ -43,13 +38,6 @@ var (
esac
{{ end }}

{{ define "docker-daemon-config" }}
sudo mkdir -p /etc/docker
cat <<EOF | sudo tee /etc/docker/daemon.json
{{ dockerCfg .INSECURE_REGISTRY }}
EOF
{{ end }}

{{ define "sysctl-k8s" }}
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
Expand Down Expand Up @@ -92,115 +80,3 @@ const (
defaultAmazonContainerdVersion = "1.4.*"
defaultAmazonCrictlVersion = "1.13.0"
)

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"`
}

func dockerCfg(insecureRegistry string) (string, error) {
cfg := dockerConfig{
ExecOpts: []string{"native.cgroupdriver=systemd"},
StorageDriver: "overlay2",
LogDriver: "json-file",
LogOpts: map[string]string{
"max-size": "100m",
},
}
if insecureRegistry != "" {
cfg.InsecureRegistries = []string{insecureRegistry}
}

b, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return "", err
}

return string(b), nil
}

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 containerdCfg(insecureRegistry string) (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"},
},
},
},
}

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
}
Loading