...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/client.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package crclients
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  
    20  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/containerd"
    21  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/crio"
    22  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/docker"
    23  )
    24  
    25  const (
    26  	ContainerRuntimeDocker     = "docker"
    27  	ContainerRuntimeContainerd = "containerd"
    28  	ContainerRuntimeCrio       = "crio"
    29  
    30  	// TODO(yeya24): make socket and ns configurable
    31  	defaultDockerSocket     = "unix:///var/run/docker.sock"
    32  	defaultContainerdSocket = "/run/containerd/containerd.sock"
    33  	defaultCrioSocket       = "/var/run/crio/crio.sock"
    34  	containerdDefaultNS     = "k8s.io"
    35  )
    36  
    37  // ContainerRuntimeInfoClient represents a struct which can give you information about container runtime
    38  type ContainerRuntimeInfoClient interface {
    39  	GetPidFromContainerID(ctx context.Context, containerID string) (uint32, error)
    40  	ContainerKillByContainerID(ctx context.Context, containerID string) error
    41  	FormatContainerID(ctx context.Context, containerID string) (string, error)
    42  }
    43  
    44  // CreateContainerRuntimeInfoClient creates a container runtime information client.
    45  func CreateContainerRuntimeInfoClient(containerRuntime string) (ContainerRuntimeInfoClient, error) {
    46  	// TODO: support more container runtime
    47  
    48  	var cli ContainerRuntimeInfoClient
    49  	var err error
    50  	switch containerRuntime {
    51  	case ContainerRuntimeDocker:
    52  		cli, err = docker.New(defaultDockerSocket, "", nil, nil)
    53  		if err != nil {
    54  			return nil, err
    55  		}
    56  	case ContainerRuntimeContainerd:
    57  		// TODO(yeya24): add more options?
    58  		cli, err = containerd.New(defaultContainerdSocket, containerd.WithDefaultNamespace(containerdDefaultNS))
    59  		if err != nil {
    60  			return nil, err
    61  		}
    62  	case ContainerRuntimeCrio:
    63  		cli, err = crio.New(defaultCrioSocket)
    64  		if err != nil {
    65  			return nil, err
    66  		}
    67  	default:
    68  		return nil, fmt.Errorf("only docker/containerd/crio is supported, but got %s", containerRuntime)
    69  	}
    70  
    71  	return cli, nil
    72  }
    73