pause / containerd config: take advantage of no kubeadm preflight

also fix zfs
This commit is contained in:
Benjamin Elder
2020-07-22 16:45:48 -07:00
parent 182920665c
commit 17a322c21a
5 changed files with 67 additions and 103 deletions

View File

@@ -114,28 +114,11 @@ func (c *buildContext) buildImage(bits kube.Bits) error {
}()
// pre-pull images that were not part of the build
images, err := c.prePullImages(bits, dir, containerID)
if err != nil {
if _, err = c.prePullImages(bits, dir, containerID); err != nil {
c.logger.Errorf("Image build Failed! Failed to pull Images: %v", err)
return err
}
// find the pause image and inject containerd config
pauseImage := findSandboxImage(images)
if pauseImage == "" {
return errors.New("failed to find imported pause image")
}
containerdConfig, err := getContainerdConfig(containerdConfigTemplateData{
SandboxImage: pauseImage,
})
if err != nil {
return err
}
const containerdConfigPath = "/etc/containerd/config.toml"
if err := createFile(cmder, containerdConfigPath, containerdConfig); err != nil {
return err
}
// Save the image changes to a new image
cmd := exec.Command(
"docker", "commit",
@@ -230,6 +213,24 @@ func (c *buildContext) prePullImages(bits kube.Bits, dir, containerID string) ([
return nil, err
}
// replace pause image with our own
config, err := exec.Output(cmder.Command("cat", "/etc/containerd/config.toml"))
if err != nil {
return nil, err
}
pauseImage, err := findSandboxImage(string(config))
if err != nil {
return nil, err
}
n := 0
for _, image := range requiredImages {
if !strings.Contains(image, "pause") {
requiredImages[n] = image
n++
}
}
requiredImages = append(requiredImages[:n], pauseImage)
// write the default CNI manifest
if err := createFile(cmder, defaultCNIManifestLocation, defaultCNIManifest); err != nil {
c.logger.Errorf("Image build Failed! Failed write default CNI Manifest: %v", err)

View File

@@ -1,65 +0,0 @@
/*
Copyright 2020 The Kubernetes 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 nodeimage
import (
"bytes"
"text/template"
"sigs.k8s.io/kind/pkg/errors"
)
type containerdConfigTemplateData struct {
SandboxImage string
}
const containerdConfigTemplate = `# explicitly use v2 config format
version = 2
# set default runtime handler to v2, which has a per-pod shim
[plugins."io.containerd.grpc.v1.cri".containerd]
default_runtime_name = "runc"
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
runtime_type = "io.containerd.runc.v2"
# Setup a runtime with the magic name ("test-handler") used for Kubernetes
# runtime class tests ...
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.test-handler]
runtime_type = "io.containerd.runc.v2"
[plugins."io.containerd.grpc.v1.cri"]
# ensure the sandbox image matches kubeadm
# TODO: probably we should instead just use the containerd default
# Implementing the pod sandbox is a CRI implementation detail ...
sandbox_image = "{{.SandboxImage}}"
# allow hugepages controller to be missing
# see https://github.com/containerd/cri/pull/1501
tolerate_missing_hugepages_controller = true
`
func getContainerdConfig(data containerdConfigTemplateData) (string, error) {
t, err := template.New("containerd-config").Parse(containerdConfigTemplate)
if err != nil {
return "", errors.Wrap(err, "failed to parse containerd config template")
}
var buff bytes.Buffer
err = t.Execute(&buff, data)
if err != nil {
return "", errors.Wrap(err, "error executing config template")
}
return buff.String(), nil
}

View File

@@ -18,8 +18,10 @@ package nodeimage
import (
"path"
"regexp"
"strings"
"sigs.k8s.io/kind/pkg/errors"
"sigs.k8s.io/kind/pkg/exec"
)
@@ -40,13 +42,10 @@ func createFile(containerCmder exec.Cmder, filePath, contents string) error {
).Run()
}
func findSandboxImage(images []string) string {
for _, image := range images {
// yep this seems legit
// https://github.com/kubernetes-sigs/kind/issues/1471#issuecomment-617579803
if strings.Contains(image, "pause") {
return image
}
func findSandboxImage(config string) (string, error) {
match := regexp.MustCompile(`sandbox_image\s+=\s+"([^\n]+)"`).FindStringSubmatch(config)
if len(match) < 2 {
return "", errors.New("failed to parse sandbox_image from config")
}
return ""
return match[1], nil
}