mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-30 06:54:01 +00:00
kubeadm: reuse found crictl path for exec
We now re-use the crictl tool path within the `ContainerRuntime` when exec'ing into it. This allows introducing a convenience function to create the crictl command and re-use it where necessary. Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
This commit is contained in:
parent
0b8a665d50
commit
6b63231b7e
@ -48,16 +48,17 @@ type ContainerRuntime interface {
|
|||||||
type CRIRuntime struct {
|
type CRIRuntime struct {
|
||||||
exec utilsexec.Interface
|
exec utilsexec.Interface
|
||||||
criSocket string
|
criSocket string
|
||||||
|
crictlPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewContainerRuntime sets up and returns a ContainerRuntime struct
|
// NewContainerRuntime sets up and returns a ContainerRuntime struct
|
||||||
func NewContainerRuntime(execer utilsexec.Interface, criSocket string) (ContainerRuntime, error) {
|
func NewContainerRuntime(execer utilsexec.Interface, criSocket string) (ContainerRuntime, error) {
|
||||||
toolName := "crictl"
|
const toolName = "crictl"
|
||||||
runtime := &CRIRuntime{execer, criSocket}
|
crictlPath, err := execer.LookPath(toolName)
|
||||||
if _, err := execer.LookPath(toolName); err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "%s is required by the container runtime", toolName)
|
return nil, errors.Wrapf(err, "%s is required by the container runtime", toolName)
|
||||||
}
|
}
|
||||||
return runtime, nil
|
return &CRIRuntime{execer, criSocket, crictlPath}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Socket returns the CRI socket endpoint
|
// Socket returns the CRI socket endpoint
|
||||||
@ -65,9 +66,14 @@ func (runtime *CRIRuntime) Socket() string {
|
|||||||
return runtime.criSocket
|
return runtime.criSocket
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// crictl creates a crictl command for the provided args.
|
||||||
|
func (runtime *CRIRuntime) crictl(args ...string) utilsexec.Cmd {
|
||||||
|
return runtime.exec.Command(runtime.crictlPath, append([]string{"-r", runtime.Socket()}, args...)...)
|
||||||
|
}
|
||||||
|
|
||||||
// IsRunning checks if runtime is running
|
// IsRunning checks if runtime is running
|
||||||
func (runtime *CRIRuntime) IsRunning() error {
|
func (runtime *CRIRuntime) IsRunning() error {
|
||||||
if out, err := runtime.exec.Command("crictl", "-r", runtime.criSocket, "info").CombinedOutput(); err != nil {
|
if out, err := runtime.crictl("info").CombinedOutput(); err != nil {
|
||||||
return errors.Wrapf(err, "container runtime is not running: output: %s, error", string(out))
|
return errors.Wrapf(err, "container runtime is not running: output: %s, error", string(out))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -75,7 +81,7 @@ func (runtime *CRIRuntime) IsRunning() error {
|
|||||||
|
|
||||||
// ListKubeContainers lists running k8s CRI pods
|
// ListKubeContainers lists running k8s CRI pods
|
||||||
func (runtime *CRIRuntime) ListKubeContainers() ([]string, error) {
|
func (runtime *CRIRuntime) ListKubeContainers() ([]string, error) {
|
||||||
out, err := runtime.exec.Command("crictl", "-r", runtime.criSocket, "pods", "-q").CombinedOutput()
|
out, err := runtime.crictl("pods", "-q").CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "output: %s, error", string(out))
|
return nil, errors.Wrapf(err, "output: %s, error", string(out))
|
||||||
}
|
}
|
||||||
@ -88,12 +94,12 @@ func (runtime *CRIRuntime) ListKubeContainers() ([]string, error) {
|
|||||||
func (runtime *CRIRuntime) RemoveContainers(containers []string) error {
|
func (runtime *CRIRuntime) RemoveContainers(containers []string) error {
|
||||||
errs := []error{}
|
errs := []error{}
|
||||||
for _, container := range containers {
|
for _, container := range containers {
|
||||||
out, err := runtime.exec.Command("crictl", "-r", runtime.criSocket, "stopp", container).CombinedOutput()
|
out, err := runtime.crictl("stopp", container).CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// don't stop on errors, try to remove as many containers as possible
|
// 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)))
|
errs = append(errs, errors.Wrapf(err, "failed to stop running pod %s: output: %s, error", container, string(out)))
|
||||||
} else {
|
} else {
|
||||||
out, err = runtime.exec.Command("crictl", "-r", runtime.criSocket, "rmp", container).CombinedOutput()
|
out, err = runtime.crictl("rmp", container).CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errs = append(errs, errors.Wrapf(err, "failed to remove running container %s: output: %s, error", container, string(out)))
|
errs = append(errs, errors.Wrapf(err, "failed to remove running container %s: output: %s, error", container, string(out)))
|
||||||
}
|
}
|
||||||
@ -107,7 +113,7 @@ func (runtime *CRIRuntime) PullImage(image string) error {
|
|||||||
var err error
|
var err error
|
||||||
var out []byte
|
var out []byte
|
||||||
for i := 0; i < constants.PullImageRetry; i++ {
|
for i := 0; i < constants.PullImageRetry; i++ {
|
||||||
out, err = runtime.exec.Command("crictl", "-r", runtime.criSocket, "pull", image).CombinedOutput()
|
out, err = runtime.crictl("pull", image).CombinedOutput()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -117,7 +123,7 @@ func (runtime *CRIRuntime) PullImage(image string) error {
|
|||||||
|
|
||||||
// ImageExists checks to see if the image exists on the system
|
// ImageExists checks to see if the image exists on the system
|
||||||
func (runtime *CRIRuntime) ImageExists(image string) (bool, error) {
|
func (runtime *CRIRuntime) ImageExists(image string) (bool, error) {
|
||||||
err := runtime.exec.Command("crictl", "-r", runtime.criSocket, "inspecti", image).Run()
|
err := runtime.crictl("inspecti", image).Run()
|
||||||
return err == nil, nil
|
return err == nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user