mirror of
https://github.com/labring/sealos.git
synced 2026-09-24 15:46:19 +08:00
refactor(master): add sealctl cri (#852)
* refactor(master): add sealctl cri * refactor(master): add sealctl cri .github action
This commit is contained in:
@@ -1,272 +0,0 @@
|
||||
/*
|
||||
Copyright 2018 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 cri
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
goruntime "runtime"
|
||||
"strings"
|
||||
|
||||
strings2 "github.com/fanux/sealos/pkg/utils/strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
errorsutil "k8s.io/apimachinery/pkg/util/errors"
|
||||
utilsexec "k8s.io/utils/exec"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultDockerCRISocket = "/var/run/dockershim.sock"
|
||||
DefaultContainerdCRISocket = containerdSocket
|
||||
DefaultCgroupDriver = "cgroupfs"
|
||||
DefaultSystemdCgroupDriver = "systemd"
|
||||
// PullImageRetry specifies how many times ContainerRuntime retries when pulling image failed
|
||||
PullImageRetry = 5
|
||||
)
|
||||
|
||||
// ContainerRuntime is an interface for working with container runtimes
|
||||
type ContainerRuntime interface {
|
||||
IsDocker() bool
|
||||
IsRunning() error
|
||||
ListKubeContainers() ([]string, error)
|
||||
RemoveContainers(containers []string) error
|
||||
PullImage(image string) error
|
||||
ImageExists(image string) (bool, error)
|
||||
CGroupDriver() (string, error)
|
||||
}
|
||||
|
||||
// ContainerdRuntime is a struct that interfaces with the CRI
|
||||
type ContainerdRuntime struct {
|
||||
exec utilsexec.Interface
|
||||
criSocket string
|
||||
}
|
||||
|
||||
// DockerRuntime is a struct that interfaces with the Docker daemon
|
||||
type DockerRuntime struct {
|
||||
exec utilsexec.Interface
|
||||
}
|
||||
|
||||
// NewContainerRuntime sets up and returns a ContainerRuntime struct
|
||||
func NewContainerRuntime(execer utilsexec.Interface, criSocket string) (ContainerRuntime, error) {
|
||||
var toolName string
|
||||
var runtime ContainerRuntime
|
||||
|
||||
if criSocket != DefaultDockerCRISocket {
|
||||
toolName = "crictl"
|
||||
// !!! temporary work around crictl warning:
|
||||
// Using "/var/run/crio/crio.sock" as endpoint is deprecated,
|
||||
// please consider using full url format "unix:///var/run/crio/crio.sock"
|
||||
if filepath.IsAbs(criSocket) && goruntime.GOOS != "windows" {
|
||||
criSocket = "unix://" + criSocket
|
||||
}
|
||||
runtime = &ContainerdRuntime{execer, criSocket}
|
||||
} else {
|
||||
toolName = "docker"
|
||||
runtime = &DockerRuntime{execer}
|
||||
}
|
||||
|
||||
if _, err := execer.LookPath(toolName); err != nil {
|
||||
return nil, errors.Wrapf(err, "%s is required for container runtime", toolName)
|
||||
}
|
||||
|
||||
return runtime, nil
|
||||
}
|
||||
|
||||
// IsDocker returns true if the runtime is docker
|
||||
func (runtime *ContainerdRuntime) IsDocker() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsDocker returns true if the runtime is docker
|
||||
func (runtime *DockerRuntime) IsDocker() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// IsRunning checks if runtime is running
|
||||
func (runtime *ContainerdRuntime) IsRunning() error {
|
||||
if out, err := runtime.exec.Command("crictl", "-r", runtime.criSocket, "info").CombinedOutput(); err != nil {
|
||||
return errors.Wrapf(err, "container runtime is not running: output: %s, error", string(out))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsRunning checks if runtime is running
|
||||
func (runtime *DockerRuntime) IsRunning() error {
|
||||
if out, err := runtime.exec.Command("docker", "info").CombinedOutput(); err != nil {
|
||||
return errors.Wrapf(err, "container runtime is not running: output: %s, error", string(out))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (runtime *DockerRuntime) CGroupDriver() (string, error) {
|
||||
if err := runtime.IsRunning(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
var err error
|
||||
var out []byte
|
||||
if out, err = runtime.exec.Command("docker", "info", "--format", "{{.CgroupDriver}}").CombinedOutput(); err != nil {
|
||||
return "", errors.Wrapf(err, "container runtime is not running: output: %s, error", string(out))
|
||||
}
|
||||
return string(out), nil
|
||||
}
|
||||
|
||||
func (runtime *ContainerdRuntime) CGroupDriver() (string, error) {
|
||||
if err := runtime.IsRunning(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
var err error
|
||||
var out []byte
|
||||
if out, err = runtime.exec.Command("crictl", "-r", runtime.criSocket, "info", "-o", "go-template", "--template", "{{.config.systemdCgroup}}").CombinedOutput(); err != nil {
|
||||
return "", errors.Wrapf(err, "container runtime is not running: output: %s, error", string(out))
|
||||
}
|
||||
if strings2.TrimWS(string(out)) == "false" {
|
||||
return DefaultCgroupDriver, nil
|
||||
}
|
||||
return DefaultSystemdCgroupDriver, nil
|
||||
}
|
||||
|
||||
// ListKubeContainers lists running k8s CRI pods
|
||||
func (runtime *ContainerdRuntime) ListKubeContainers() ([]string, error) {
|
||||
out, err := runtime.exec.Command("crictl", "-r", runtime.criSocket, "pods", "-q").CombinedOutput()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "output: %s, error", string(out))
|
||||
}
|
||||
pods := []string{}
|
||||
pods = append(pods, strings.Fields(string(out))...)
|
||||
return pods, nil
|
||||
}
|
||||
|
||||
// ListKubeContainers lists running k8s containers
|
||||
func (runtime *DockerRuntime) ListKubeContainers() ([]string, error) {
|
||||
output, err := runtime.exec.Command("docker", "ps", "-a", "--filter", "name=k8s_", "-q").CombinedOutput()
|
||||
return strings.Fields(string(output)), err
|
||||
}
|
||||
|
||||
// RemoveContainers removes running k8s pods
|
||||
func (runtime *ContainerdRuntime) RemoveContainers(containers []string) error {
|
||||
errs := []error{}
|
||||
for _, container := range containers {
|
||||
out, err := runtime.exec.Command("crictl", "-r", runtime.criSocket, "stopp", container).CombinedOutput()
|
||||
if err != nil {
|
||||
// don't stop on errors, try to remove as many containers as possible
|
||||
errs = append(errs, errors.Wrapf(err, "failed to stop running pod %s: output: %s, error", container, string(out)))
|
||||
} else {
|
||||
out, err = runtime.exec.Command("crictl", "-r", runtime.criSocket, "rmp", container).CombinedOutput()
|
||||
if err != nil {
|
||||
errs = append(errs, errors.Wrapf(err, "failed to remove running container %s: output: %s, error", container, string(out)))
|
||||
}
|
||||
}
|
||||
}
|
||||
return errorsutil.NewAggregate(errs)
|
||||
}
|
||||
|
||||
// RemoveContainers removes running containers
|
||||
func (runtime *DockerRuntime) RemoveContainers(containers []string) error {
|
||||
errs := []error{}
|
||||
for _, container := range containers {
|
||||
out, err := runtime.exec.Command("docker", "stop", container).CombinedOutput()
|
||||
if err != nil {
|
||||
// don't stop on errors, try to remove as many containers as possible
|
||||
errs = append(errs, errors.Wrapf(err, "failed to stop running container %s: output: %s, error", container, string(out)))
|
||||
} else {
|
||||
out, err = runtime.exec.Command("docker", "rm", "--volumes", container).CombinedOutput()
|
||||
if err != nil {
|
||||
errs = append(errs, errors.Wrapf(err, "failed to remove running container %s: output: %s, error", container, string(out)))
|
||||
}
|
||||
}
|
||||
}
|
||||
return errorsutil.NewAggregate(errs)
|
||||
}
|
||||
|
||||
// PullImage pulls the image
|
||||
func (runtime *ContainerdRuntime) PullImage(image string) error {
|
||||
var err error
|
||||
var out []byte
|
||||
for i := 0; i < PullImageRetry; i++ {
|
||||
out, err = runtime.exec.Command("crictl", "-r", runtime.criSocket, "pull", image).CombinedOutput()
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return errors.Wrapf(err, "output: %s, error", out)
|
||||
}
|
||||
|
||||
// PullImage pulls the image
|
||||
func (runtime *DockerRuntime) PullImage(image string) error {
|
||||
var err error
|
||||
var out []byte
|
||||
for i := 0; i < PullImageRetry; i++ {
|
||||
out, err = runtime.exec.Command("docker", "pull", image).CombinedOutput()
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return errors.Wrapf(err, "output: %s, error", out)
|
||||
}
|
||||
|
||||
// ImageExists checks to see if the image exists on the system
|
||||
func (runtime *ContainerdRuntime) ImageExists(image string) (bool, error) {
|
||||
err := runtime.exec.Command("crictl", "-r", runtime.criSocket, "inspecti", image).Run()
|
||||
return err == nil, nil
|
||||
}
|
||||
|
||||
// ImageExists checks to see if the image exists on the system
|
||||
func (runtime *DockerRuntime) ImageExists(image string) (bool, error) {
|
||||
err := runtime.exec.Command("docker", "inspect", image).Run()
|
||||
return err == nil, nil
|
||||
}
|
||||
|
||||
// detectCRISocketImpl is separated out only for test purposes, DON'T call it directly, use DetectCRISocket instead
|
||||
func detectCRISocketImpl(isSocket func(string) bool) (string, error) {
|
||||
foundCRISockets := []string{}
|
||||
knownCRISockets := []string{
|
||||
// Docker and containerd sockets are special cased below, hence not to be included here
|
||||
"/var/run/crio/crio.sock",
|
||||
}
|
||||
|
||||
if isSocket(dockerSocket) {
|
||||
// the path in dockerSocket is not CRI compatible, hence we should replace it with a CRI compatible socket
|
||||
foundCRISockets = append(foundCRISockets, DefaultDockerCRISocket)
|
||||
} else if isSocket(containerdSocket) {
|
||||
// Docker 18.09 gets bundled together with containerd, thus having both dockerSocket and containerdSocket present.
|
||||
// For compatibility reasons, we use the containerd socket only if Docker is not detected.
|
||||
foundCRISockets = append(foundCRISockets, containerdSocket)
|
||||
}
|
||||
|
||||
for _, socket := range knownCRISockets {
|
||||
if isSocket(socket) {
|
||||
foundCRISockets = append(foundCRISockets, socket)
|
||||
}
|
||||
}
|
||||
|
||||
switch len(foundCRISockets) {
|
||||
case 0:
|
||||
// Fall back to Docker if no CRI is detected, we can error out later on if we need it
|
||||
return DefaultDockerCRISocket, nil
|
||||
case 1:
|
||||
// Precisely one CRI found, use that
|
||||
return foundCRISockets[0], nil
|
||||
default:
|
||||
// Multiple CRIs installed?
|
||||
return "", errors.Errorf("Found multiple CRI sockets, please use --cri-socket to select one: %s", strings.Join(foundCRISockets, ", "))
|
||||
}
|
||||
}
|
||||
|
||||
// DetectCRISocket uses a list of known CRI sockets to detect one. If more than one or none is discovered, an error is returned.
|
||||
func DetectCRISocket() (string, error) {
|
||||
return detectCRISocketImpl(isExistingSocket)
|
||||
}
|
||||
@@ -1,463 +0,0 @@
|
||||
/*
|
||||
Copyright 2018 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 cri
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"k8s.io/utils/exec"
|
||||
fakeexec "k8s.io/utils/exec/testing"
|
||||
)
|
||||
|
||||
const crlctlBin = "/usr/bin/crictl"
|
||||
|
||||
func TestNewContainerRuntime(t *testing.T) {
|
||||
execLookPathOK := fakeexec.FakeExec{
|
||||
LookPathFunc: func(cmd string) (string, error) { return crlctlBin, nil },
|
||||
}
|
||||
execLookPathErr := fakeexec.FakeExec{
|
||||
LookPathFunc: func(cmd string) (string, error) { return "", errors.Errorf("%s not found", cmd) },
|
||||
}
|
||||
cases := []struct {
|
||||
name string
|
||||
execer fakeexec.FakeExec
|
||||
criSocket string
|
||||
isDocker bool
|
||||
isError bool
|
||||
}{
|
||||
{"valid: default cri socket", execLookPathOK, DefaultDockerCRISocket, true, false},
|
||||
{"valid: cri-o socket url", execLookPathOK, "unix:///var/run/crio/crio.sock", false, false},
|
||||
{"valid: cri-o socket path", execLookPathOK, "/var/run/crio/crio.sock", false, false},
|
||||
{"invalid: no crictl", execLookPathErr, "unix:///var/run/crio/crio.sock", false, true},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
runtime, err := NewContainerRuntime(&tc.execer, tc.criSocket)
|
||||
if err != nil {
|
||||
if !tc.isError {
|
||||
t.Fatalf("unexpected NewContainerRuntime error. criSocket: %s, error: %v", tc.criSocket, err)
|
||||
}
|
||||
return // expected error occurs, impossible to test runtime further
|
||||
}
|
||||
if tc.isError && err == nil {
|
||||
t.Fatalf("unexpected NewContainerRuntime success. criSocket: %s", tc.criSocket)
|
||||
}
|
||||
isDocker := runtime.IsDocker()
|
||||
if tc.isDocker != isDocker {
|
||||
t.Fatalf("unexpected isDocker() result %v for the criSocket %s", isDocker, tc.criSocket)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func genFakeActions(fcmd *fakeexec.FakeCmd, num int) []fakeexec.FakeCommandAction {
|
||||
var actions []fakeexec.FakeCommandAction
|
||||
for i := 0; i < num; i++ {
|
||||
actions = append(actions, func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(fcmd, cmd, args...)
|
||||
})
|
||||
}
|
||||
return actions
|
||||
}
|
||||
|
||||
func TestIsRunning(t *testing.T) {
|
||||
fcmd := fakeexec.FakeCmd{
|
||||
CombinedOutputScript: []fakeexec.FakeAction{
|
||||
func() ([]byte, []byte, error) { return nil, nil, nil },
|
||||
func() ([]byte, []byte, error) { return []byte("error"), nil, &fakeexec.FakeExitError{Status: 1} },
|
||||
func() ([]byte, []byte, error) { return nil, nil, nil },
|
||||
func() ([]byte, []byte, error) { return []byte("error"), nil, &fakeexec.FakeExitError{Status: 1} },
|
||||
},
|
||||
}
|
||||
|
||||
criExecer := fakeexec.FakeExec{
|
||||
CommandScript: genFakeActions(&fcmd, len(fcmd.CombinedOutputScript)),
|
||||
LookPathFunc: func(cmd string) (string, error) { return crlctlBin, nil },
|
||||
}
|
||||
|
||||
dockerExecer := fakeexec.FakeExec{
|
||||
CommandScript: genFakeActions(&fcmd, len(fcmd.CombinedOutputScript)),
|
||||
LookPathFunc: func(cmd string) (string, error) { return "/usr/bin/docker", nil },
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
criSocket string
|
||||
execer fakeexec.FakeExec
|
||||
isError bool
|
||||
}{
|
||||
{"valid: CRI-O is running", "unix:///var/run/crio/crio.sock", criExecer, false},
|
||||
{"invalid: CRI-O is not running", "unix:///var/run/crio/crio.sock", criExecer, true},
|
||||
{"valid: docker is running", DefaultDockerCRISocket, dockerExecer, false},
|
||||
{"invalid: docker is not running", DefaultDockerCRISocket, dockerExecer, true},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
runtime, err := NewContainerRuntime(&tc.execer, tc.criSocket)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected NewContainerRuntime error: %v", err)
|
||||
}
|
||||
isRunning := runtime.IsRunning()
|
||||
if tc.isError && isRunning == nil {
|
||||
t.Error("unexpected IsRunning() success")
|
||||
}
|
||||
if !tc.isError && isRunning != nil {
|
||||
t.Error("unexpected IsRunning() error")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestListKubeContainers(t *testing.T) {
|
||||
fcmd := fakeexec.FakeCmd{
|
||||
CombinedOutputScript: []fakeexec.FakeAction{
|
||||
func() ([]byte, []byte, error) { return []byte("k8s_p1\nk8s_p2"), nil, nil },
|
||||
func() ([]byte, []byte, error) { return nil, nil, &fakeexec.FakeExitError{Status: 1} },
|
||||
func() ([]byte, []byte, error) { return []byte("k8s_p1\nk8s_p2"), nil, nil },
|
||||
},
|
||||
}
|
||||
execer := fakeexec.FakeExec{
|
||||
CommandScript: genFakeActions(&fcmd, len(fcmd.CombinedOutputScript)),
|
||||
LookPathFunc: func(cmd string) (string, error) { return crlctlBin, nil },
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
criSocket string
|
||||
isError bool
|
||||
}{
|
||||
{"valid: list containers using CRI socket url", "unix:///var/run/crio/crio.sock", false},
|
||||
{"invalid: list containers using CRI socket url", "unix:///var/run/crio/crio.sock", true},
|
||||
{"valid: list containers using docker", DefaultDockerCRISocket, false},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
runtime, err := NewContainerRuntime(&execer, tc.criSocket)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected NewContainerRuntime error: %v", err)
|
||||
}
|
||||
|
||||
containers, err := runtime.ListKubeContainers()
|
||||
if tc.isError {
|
||||
if err == nil {
|
||||
t.Errorf("unexpected ListKubeContainers success")
|
||||
}
|
||||
return
|
||||
} else if err != nil {
|
||||
t.Errorf("unexpected ListKubeContainers error: %v", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(containers, []string{"k8s_p1", "k8s_p2"}) {
|
||||
t.Errorf("unexpected ListKubeContainers output: %v", containers)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveContainers(t *testing.T) {
|
||||
fakeOK := func() ([]byte, []byte, error) { return nil, nil, nil }
|
||||
fakeErr := func() ([]byte, []byte, error) { return []byte("error"), nil, &fakeexec.FakeExitError{Status: 1} }
|
||||
fcmd := fakeexec.FakeCmd{
|
||||
CombinedOutputScript: []fakeexec.FakeAction{
|
||||
fakeOK, fakeOK, fakeOK, fakeOK, fakeOK, fakeOK, // Test case 1
|
||||
fakeOK, fakeOK, fakeOK, fakeErr, fakeOK, fakeOK,
|
||||
fakeErr, fakeOK, fakeOK, fakeErr, fakeOK,
|
||||
fakeOK, fakeOK, fakeOK, fakeOK, fakeOK, fakeOK,
|
||||
fakeOK, fakeOK, fakeOK, fakeErr, fakeOK, fakeOK,
|
||||
fakeErr, fakeOK, fakeOK, fakeErr, fakeOK,
|
||||
},
|
||||
}
|
||||
execer := fakeexec.FakeExec{
|
||||
CommandScript: genFakeActions(&fcmd, len(fcmd.CombinedOutputScript)),
|
||||
LookPathFunc: func(cmd string) (string, error) { return crlctlBin, nil },
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
criSocket string
|
||||
containers []string
|
||||
isError bool
|
||||
}{
|
||||
{"valid: remove containers using CRI", "unix:///var/run/crio/crio.sock", []string{"k8s_p1", "k8s_p2", "k8s_p3"}, false}, // Test case 1
|
||||
{"invalid: CRI rmp failure", "unix:///var/run/crio/crio.sock", []string{"k8s_p1", "k8s_p2", "k8s_p3"}, true},
|
||||
{"invalid: CRI stopp failure", "unix:///var/run/crio/crio.sock", []string{"k8s_p1", "k8s_p2", "k8s_p3"}, true},
|
||||
{"valid: remove containers using docker", DefaultDockerCRISocket, []string{"k8s_c1", "k8s_c2", "k8s_c3"}, false},
|
||||
{"invalid: docker rm failure", DefaultDockerCRISocket, []string{"k8s_c1", "k8s_c2", "k8s_c3"}, true},
|
||||
{"invalid: docker stop failure", DefaultDockerCRISocket, []string{"k8s_c1", "k8s_c2", "k8s_c3"}, true},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
runtime, err := NewContainerRuntime(&execer, tc.criSocket)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected NewContainerRuntime error: %v, criSocket: %s", err, tc.criSocket)
|
||||
}
|
||||
|
||||
err = runtime.RemoveContainers(tc.containers)
|
||||
if !tc.isError && err != nil {
|
||||
t.Errorf("unexpected RemoveContainers errors: %v, criSocket: %s, containers: %v", err, tc.criSocket, tc.containers)
|
||||
}
|
||||
if tc.isError && err == nil {
|
||||
t.Errorf("unexpected RemoveContainers success, criSocket: %s, containers: %v", tc.criSocket, tc.containers)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPullImage(t *testing.T) {
|
||||
fcmd := fakeexec.FakeCmd{
|
||||
CombinedOutputScript: []fakeexec.FakeAction{
|
||||
func() ([]byte, []byte, error) { return nil, nil, nil },
|
||||
// If the pull fails, it will be retried 5 times (see PullImageRetry in constants/constants.go)
|
||||
func() ([]byte, []byte, error) { return []byte("error"), nil, &fakeexec.FakeExitError{Status: 1} },
|
||||
func() ([]byte, []byte, error) { return []byte("error"), nil, &fakeexec.FakeExitError{Status: 1} },
|
||||
func() ([]byte, []byte, error) { return []byte("error"), nil, &fakeexec.FakeExitError{Status: 1} },
|
||||
func() ([]byte, []byte, error) { return []byte("error"), nil, &fakeexec.FakeExitError{Status: 1} },
|
||||
func() ([]byte, []byte, error) { return []byte("error"), nil, &fakeexec.FakeExitError{Status: 1} },
|
||||
func() ([]byte, []byte, error) { return nil, nil, nil },
|
||||
// If the pull fails, it will be retried 5 times (see PullImageRetry in constants/constants.go)
|
||||
func() ([]byte, []byte, error) { return []byte("error"), nil, &fakeexec.FakeExitError{Status: 1} },
|
||||
func() ([]byte, []byte, error) { return []byte("error"), nil, &fakeexec.FakeExitError{Status: 1} },
|
||||
func() ([]byte, []byte, error) { return []byte("error"), nil, &fakeexec.FakeExitError{Status: 1} },
|
||||
func() ([]byte, []byte, error) { return []byte("error"), nil, &fakeexec.FakeExitError{Status: 1} },
|
||||
func() ([]byte, []byte, error) { return []byte("error"), nil, &fakeexec.FakeExitError{Status: 1} },
|
||||
},
|
||||
}
|
||||
execer := fakeexec.FakeExec{
|
||||
CommandScript: genFakeActions(&fcmd, len(fcmd.CombinedOutputScript)),
|
||||
LookPathFunc: func(cmd string) (string, error) { return crlctlBin, nil },
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
criSocket string
|
||||
image string
|
||||
isError bool
|
||||
}{
|
||||
{"valid: pull image using CRI", "unix:///var/run/crio/crio.sock", "image1", false},
|
||||
{"invalid: CRI pull error", "unix:///var/run/crio/crio.sock", "image2", true},
|
||||
{"valid: pull image using docker", DefaultDockerCRISocket, "image1", false},
|
||||
{"invalid: docker pull error", DefaultDockerCRISocket, "image2", true},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
runtime, err := NewContainerRuntime(&execer, tc.criSocket)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected NewContainerRuntime error: %v, criSocket: %s", err, tc.criSocket)
|
||||
}
|
||||
|
||||
err = runtime.PullImage(tc.image)
|
||||
if !tc.isError && err != nil {
|
||||
t.Errorf("unexpected PullImage error: %v, criSocket: %s, image: %s", err, tc.criSocket, tc.image)
|
||||
}
|
||||
if tc.isError && err == nil {
|
||||
t.Errorf("unexpected PullImage success, criSocket: %s, image: %s", tc.criSocket, tc.image)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageExists(t *testing.T) {
|
||||
fcmd := fakeexec.FakeCmd{
|
||||
RunScript: []fakeexec.FakeAction{
|
||||
func() ([]byte, []byte, error) { return nil, nil, nil },
|
||||
func() ([]byte, []byte, error) { return nil, nil, &fakeexec.FakeExitError{Status: 1} },
|
||||
func() ([]byte, []byte, error) { return nil, nil, nil },
|
||||
func() ([]byte, []byte, error) { return nil, nil, &fakeexec.FakeExitError{Status: 1} },
|
||||
},
|
||||
}
|
||||
execer := fakeexec.FakeExec{
|
||||
CommandScript: genFakeActions(&fcmd, len(fcmd.RunScript)),
|
||||
LookPathFunc: func(cmd string) (string, error) { return crlctlBin, nil },
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
criSocket string
|
||||
image string
|
||||
result bool
|
||||
}{
|
||||
{"valid: test if image exists using CRI", "unix:///var/run/crio/crio.sock", "image1", false},
|
||||
{"invalid: CRI inspecti failure", "unix:///var/run/crio/crio.sock", "image2", true},
|
||||
{"valid: test if image exists using docker", DefaultDockerCRISocket, "image1", false},
|
||||
{"invalid: docker inspect failure", DefaultDockerCRISocket, "image2", true},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
runtime, err := NewContainerRuntime(&execer, tc.criSocket)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected NewContainerRuntime error: %v, criSocket: %s", err, tc.criSocket)
|
||||
}
|
||||
|
||||
result, err := runtime.ImageExists(tc.image)
|
||||
if !tc.result != result {
|
||||
t.Errorf("unexpected ImageExists result: %t, criSocket: %s, image: %s, expected result: %t", err, tc.criSocket, tc.image, tc.result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsExistingSocket(t *testing.T) {
|
||||
// this test is not expected to work on Windows
|
||||
if runtime.GOOS == "windows" {
|
||||
return
|
||||
}
|
||||
|
||||
const tempPrefix = "test.kubeadm.runtime.isExistingSocket."
|
||||
tests := []struct {
|
||||
name string
|
||||
proc func(*testing.T)
|
||||
}{
|
||||
{
|
||||
name: "Valid domain socket is detected as such",
|
||||
proc: func(t *testing.T) {
|
||||
tmpFile, err := ioutil.TempFile("", tempPrefix)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error by TempFile: %v", err)
|
||||
}
|
||||
theSocket := tmpFile.Name()
|
||||
os.Remove(theSocket)
|
||||
tmpFile.Close()
|
||||
|
||||
con, err := net.Listen("unix", theSocket)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error while dialing a socket: %v", err)
|
||||
}
|
||||
defer con.Close()
|
||||
|
||||
if !isExistingSocket(theSocket) {
|
||||
t.Fatalf("isExistingSocket(%q) gave unexpected result. Should have been true, instead of false", theSocket)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Regular file is not a domain socket",
|
||||
proc: func(t *testing.T) {
|
||||
tmpFile, err := ioutil.TempFile("", tempPrefix)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error by TempFile: %v", err)
|
||||
}
|
||||
theSocket := tmpFile.Name()
|
||||
defer os.Remove(theSocket)
|
||||
tmpFile.Close()
|
||||
|
||||
if isExistingSocket(theSocket) {
|
||||
t.Fatalf("isExistingSocket(%q) gave unexpected result. Should have been false, instead of true", theSocket)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Non existent socket is not a domain socket",
|
||||
proc: func(t *testing.T) {
|
||||
const theSocket = "/non/existent/socket"
|
||||
if isExistingSocket(theSocket) {
|
||||
t.Fatalf("isExistingSocket(%q) gave unexpected result. Should have been false, instead of true", theSocket)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, test.proc)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectCRISocketImpl(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
existingSockets []string
|
||||
expectedError bool
|
||||
expectedSocket string
|
||||
}{
|
||||
{
|
||||
name: "No existing sockets, use Docker",
|
||||
existingSockets: []string{},
|
||||
expectedError: false,
|
||||
expectedSocket: DefaultDockerCRISocket,
|
||||
},
|
||||
{
|
||||
name: "One valid CRI socket leads to success",
|
||||
existingSockets: []string{"/var/run/crio/crio.sock"},
|
||||
expectedError: false,
|
||||
expectedSocket: "/var/run/crio/crio.sock",
|
||||
},
|
||||
{
|
||||
name: "Correct Docker CRI socket is returned",
|
||||
existingSockets: []string{"/var/run/docker.sock"},
|
||||
expectedError: false,
|
||||
expectedSocket: DefaultDockerCRISocket,
|
||||
},
|
||||
{
|
||||
name: "CRI and Docker sockets lead to an error",
|
||||
existingSockets: []string{
|
||||
"/var/run/docker.sock",
|
||||
"/var/run/crio/crio.sock",
|
||||
},
|
||||
expectedError: true,
|
||||
},
|
||||
{
|
||||
name: "Docker and containerd lead to Docker being used",
|
||||
existingSockets: []string{
|
||||
"/var/run/docker.sock",
|
||||
"/run/containerd/containerd.sock",
|
||||
},
|
||||
expectedError: false,
|
||||
expectedSocket: DefaultDockerCRISocket,
|
||||
},
|
||||
{
|
||||
name: "A couple of CRI sockets lead to an error",
|
||||
existingSockets: []string{
|
||||
"/var/run/crio/crio.sock",
|
||||
"/run/containerd/containerd.sock",
|
||||
},
|
||||
expectedError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
socket, err := detectCRISocketImpl(func(path string) bool {
|
||||
for _, existing := range test.existingSockets {
|
||||
if path == existing {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
})
|
||||
if (err != nil) != test.expectedError {
|
||||
t.Fatalf("detectCRISocketImpl returned unexpected result\n\tExpected error: %t\n\tGot error: %t", test.expectedError, err != nil)
|
||||
}
|
||||
if !test.expectedError && socket != test.expectedSocket {
|
||||
t.Fatalf("detectCRISocketImpl returned unexpected CRI socket\n\tExpected socket: %s\n\tReturned socket: %s",
|
||||
test.expectedSocket, socket)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
// +build !windows
|
||||
|
||||
/*
|
||||
Copyright 2019 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 cri
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
dockerSocket = "/var/run/docker.sock" // The Docker socket is not CRI compatible
|
||||
containerdSocket = "/run/containerd/containerd.sock"
|
||||
)
|
||||
|
||||
// isExistingSocket checks if path exists and is domain socket
|
||||
func isExistingSocket(path string) bool {
|
||||
fileInfo, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return fileInfo.Mode()&os.ModeSocket != 0
|
||||
}
|
||||
@@ -119,16 +119,6 @@ func MkTmpFile(path string) (*os.File, error) {
|
||||
return ioutil.TempFile(path, ".FTmp-")
|
||||
}
|
||||
|
||||
func IsFileExist(filename string) bool {
|
||||
_, err := os.Stat(filename)
|
||||
if !os.IsNotExist(err) {
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
logger.Warn(err)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func WriteFile(fileName string, content []byte) error {
|
||||
dir := filepath.Dir(fileName)
|
||||
|
||||
@@ -18,6 +18,7 @@ package token
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/fanux/sealos/pkg/utils/exec"
|
||||
"github.com/fanux/sealos/pkg/utils/file"
|
||||
@@ -32,12 +33,11 @@ type Token struct {
|
||||
DiscoveryTokenCaCertHash []string `json:"discovery-token-ca-cert-hash,omitempty"`
|
||||
CertificateKey string `json:"certificate-key,omitempty"`
|
||||
Command string `json:"command,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
const defaultAdminConf = "/etc/kubernetes/admin.conf"
|
||||
|
||||
func Master() *Token {
|
||||
func Master() (*Token, error) {
|
||||
token := &Token{}
|
||||
if _, ok := exec.CheckCmdIsExist("kubeadm"); ok && file.IsExist(defaultAdminConf) {
|
||||
key, _ := CreateCertificateKey()
|
||||
@@ -53,21 +53,18 @@ func Master() *Token {
|
||||
token.JoinToken = diff.List()[0]
|
||||
hashs, err := discoveryTokenCaCertHash()
|
||||
if err != nil {
|
||||
token.Message = err.Error()
|
||||
return token
|
||||
return nil, err
|
||||
}
|
||||
token.DiscoveryTokenCaCertHash = hashs
|
||||
return token
|
||||
return token, nil
|
||||
}
|
||||
token.Message = fmt.Sprintf("token list found more than one")
|
||||
} else {
|
||||
token.Message = fmt.Sprintf("kubeadm command not found or /etc/kubernetes/admin.conf not exist")
|
||||
return nil, fmt.Errorf("token list found more than one")
|
||||
}
|
||||
|
||||
return token
|
||||
return nil, fmt.Errorf("kubeadm command not found or /etc/kubernetes/admin.conf not exist")
|
||||
}
|
||||
|
||||
func Node() *Token {
|
||||
func Node() (*Token, error) {
|
||||
token := &Token{}
|
||||
if _, ok := exec.CheckCmdIsExist("kubeadm"); ok && file.IsExist(defaultAdminConf) {
|
||||
tokens := ListToken()
|
||||
@@ -75,15 +72,13 @@ func Node() *Token {
|
||||
token.JoinToken = tokens[0].Token.String()
|
||||
hashs, err := discoveryTokenCaCertHash()
|
||||
if err != nil {
|
||||
token.Message = err.Error()
|
||||
return token
|
||||
return nil, err
|
||||
}
|
||||
return &Token{JoinToken: tokens[0].Token.String(), DiscoveryTokenCaCertHash: hashs}
|
||||
return &Token{JoinToken: tokens[0].Token.String(), DiscoveryTokenCaCertHash: hashs}, nil
|
||||
}
|
||||
token.Message = fmt.Sprintf("token not found")
|
||||
return nil, errors.New("token not found")
|
||||
}
|
||||
token.Message = fmt.Sprintf("kubeadm command not found or /etc/kubernetes/admin.conf not exist")
|
||||
return token
|
||||
return nil, errors.New("kubeadm command not found or /etc/kubernetes/admin.conf not exist")
|
||||
}
|
||||
|
||||
func ListToken() BootstrapTokens {
|
||||
|
||||
Reference in New Issue
Block a user