kubekey/pkg/container-engine/docker/docker.go
pixiake 42f442d6f6 update project layout
Signed-off-by: pixiake <guofeng@yunify.com>
2020-09-25 11:41:57 +08:00

90 lines
2.9 KiB
Go

/*
Copyright 2020 The KubeSphere 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 docker
import (
"encoding/base64"
"fmt"
kubekeyapiv1alpha1 "github.com/kubesphere/kubekey/api/v1alpha1"
"github.com/kubesphere/kubekey/pkg/util"
"github.com/kubesphere/kubekey/pkg/util/manager"
"github.com/lithammer/dedent"
"github.com/pkg/errors"
"strings"
"text/template"
)
var (
DockerConfigTempl = template.Must(template.New("DockerConfig").Parse(
dedent.Dedent(`{
"log-opts": {
"max-size": "5m",
"max-file":"3"
},
{{- if .Mirrors }}
"registry-mirrors": [{{ .Mirrors }}],
{{- end}}
{{- if .InsecureRegistries }}
"insecure-registries": [{{ .InsecureRegistries }}],
{{- end}}
"exec-opts": ["native.cgroupdriver=systemd"]
}
`)))
)
func GenerateDockerConfig(mgr *manager.Manager) (string, error) {
var Mirrors, InsecureRegistries string
if mgr.Cluster.Registry.RegistryMirrors != nil {
mirrors := []string{}
for _, mirror := range mgr.Cluster.Registry.RegistryMirrors {
mirrors = append(mirrors, fmt.Sprintf("\"%s\"", mirror))
}
Mirrors = strings.Join(mirrors, ", ")
}
if mgr.Cluster.Registry.InsecureRegistries != nil {
registries := []string{}
for _, repostry := range mgr.Cluster.Registry.InsecureRegistries {
registries = append(registries, fmt.Sprintf("\"%s\"", repostry))
}
InsecureRegistries = strings.Join(registries, ", ")
}
return util.Render(DockerConfigTempl, util.Data{
"Mirrors": Mirrors,
"InsecureRegistries": InsecureRegistries,
})
}
func InstallerDocker(mgr *manager.Manager) error {
mgr.Logger.Infoln("Installing docker ...")
return mgr.RunTaskOnAllNodes(installDockerOnNode, true)
}
func installDockerOnNode(mgr *manager.Manager, _ *kubekeyapiv1alpha1.HostCfg) error {
dockerConfig, err := GenerateDockerConfig(mgr)
if err != nil {
return err
}
dockerConfigBase64 := base64.StdEncoding.EncodeToString([]byte(dockerConfig))
output, err1 := mgr.Runner.ExecuteCmd(fmt.Sprintf("sudo -E /bin/sh -c \"if [ -z $(which docker) ] || [ ! -e /var/run/docker.sock ]; then curl https://kubernetes.pek3b.qingstor.com/tools/kubekey/docker-install.sh | sh && systemctl enable docker && echo %s | base64 -d > /etc/docker/daemon.json && systemctl reload docker && systemctl restart docker; fi\"", dockerConfigBase64), 0, false)
if err1 != nil {
return errors.Wrap(errors.WithStack(err1), fmt.Sprintf("Failed to install docker:\n%s", output))
}
return nil
}