-
Notifications
You must be signed in to change notification settings - Fork 31
/
containerd_linux.go
183 lines (155 loc) · 5.43 KB
/
containerd_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright © 2019 Banzai Cloud
//
// 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 container
import (
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"text/template"
"github.com/banzaicloud/pke/cmd/pke/app/constants"
"github.com/banzaicloud/pke/cmd/pke/app/util/file"
"github.com/banzaicloud/pke/cmd/pke/app/util/linux"
"github.com/pkg/errors"
)
const (
containerDVersion = "1.2.5"
containerDSHA256 = "40300c61ab44bd39a2440c5eedb8ac573d8b8d848acbad5efa0bc214cffb422b"
containerDURL = "https://storage.googleapis.com/cri-containerd-release/cri-containerd-%s.linux-amd64.tar.gz"
containerDVersionPath = "/opt/containerd/cluster/version"
containerDConf = "/etc/containerd/config.toml"
criConfFile = "/etc/sysctl.d/99-kubernetes-cri.conf"
criConf = `net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
`
)
func (r *Runtime) installRuntime(out io.Writer) error {
if ver, err := linux.CentOSVersion(out); err == nil {
if ver == "7" {
return installCentOS7(out, r.imageRepository)
}
return constants.ErrUnsupportedOS
}
return constants.ErrUnsupportedOS
}
func installCentOS7(out io.Writer, imageRepository string) error {
// modprobe overlay
if err := linux.ModprobeOverlay(out); err != nil {
return errors.Wrap(err, "missing overlay Linux Kernel module")
}
// modprobe br_netfilter
if err := linux.ModprobeBFNetFilter(out); err != nil {
return errors.Wrap(err, "missing br_netfilter Linux Kernel module")
}
// Ensure network settings
// cat > /etc/sysctl.d/99-kubernetes-cri.conf <<EOF
// net.bridge.bridge-nf-call-iptables = 1
// net.bridge.bridge-nf-call-ip6tables = 1
// net.ipv4.ip_forward = 1
// EOF
if err := file.Overwrite(criConfFile, criConf); err != nil {
return err
}
if err := linux.SysctlLoadAllFiles(out); err != nil {
return errors.Wrapf(err, "unable to load all sysctl rules from files")
}
// yum install -y libseccomp
if err := linux.YumInstall(out, []string{"libseccomp"}); err != nil {
return errors.Wrap(err, "unable to install libseccomp package")
}
_ = linux.SystemctlDisableAndStop(out, "containerd")
// Check ContainerD installed or not
if err := installContainerD(out, imageRepository); err != nil {
return err
}
// # Start containerd.
if err := linux.SystemctlEnableAndStart(out, "containerd"); err != nil {
return err
}
_ = linux.SystemctlDisableAndStop(out, "kubelet")
// systemctl daemon-reload
return linux.SystemctlReload(out)
}
func installContainerD(out io.Writer, imageRepository string) error {
// Check ContainerD installed or not
if _, err := os.Stat(containerDVersionPath); !os.IsNotExist(err) {
// TODO: check ContainerD version
_, _ = fmt.Fprintln(out, "ContainerD already installed, skipping download")
return nil
}
// Download ContainerD tar.
f, err := ioutil.TempFile("", "download_test")
if err != nil {
return errors.Wrapf(err, "unable to create temporary file: %q", f.Name())
}
// export CONTAINERD_VERSION="1.2.0"
// export CONTAINERD_SHA256="ee076c6260de140f9aa6dee30b0e360abfb80af252d271e697982d1209ca5dee"
// wget https://storage.googleapis.com/cri-containerd-release/cri-containerd-${CONTAINERD_VERSION}.linux-amd64.tar.gz
dl := fmt.Sprintf(containerDURL, containerDVersion)
u, err := url.Parse(dl)
if err != nil {
return errors.Wrapf(err, "failed to parse url: %q", dl)
}
_, _ = fmt.Fprintf(out, "wget %q -O %s\n", u.String(), f.Name())
if err = file.Download(u, f.Name()); err != nil {
return errors.Wrapf(err, "unable to download containerd. url: %q", u.String())
}
// echo "${CONTAINERD_SHA256} cri-containerd-${CONTAINERD_VERSION}.linux-amd64.tar.gz" | sha256sum --check -
_, _ = fmt.Fprintf(out, "echo \"%s %s\" | sha256sum --check -\n", containerDSHA256, f.Name())
err = file.SHA256File(f.Name(), containerDSHA256)
if err != nil {
return errors.Wrapf(err, "hash mismatch. hash: %q", containerDSHA256)
}
// # Unpack.
// tar --no-overwrite-dir -C / -xzf cri-containerd-${CONTAINERD_VERSION}.linux-amd64.tar.gz
fh, err := os.Open(f.Name())
if err != nil {
return err
}
err = file.Untar(out, fh)
if err != nil {
return err
}
return writeContainerDConfig(out, containerDConf, imageRepository)
}
func writeContainerDConfig(out io.Writer, filename, imageRepository string) error {
dir := filepath.Dir(filename)
_, _ = fmt.Fprintf(out, "[%s] creating directory: %q\n", use, dir)
err := os.MkdirAll(dir, 0750)
if err != nil {
return err
}
conf := `[plugins.cri]
sandbox_image = "{{ .ImageRepository }}/pause:3.1"`
tmpl, err := template.New("containerd-config").Parse(conf)
if err != nil {
return err
}
// create and truncate write only file
w, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0640)
if err != nil {
return err
}
defer func() { _ = w.Close() }()
type data struct {
ImageRepository string
}
d := data{
ImageRepository: imageRepository,
}
return tmpl.Execute(w, d)
}