...

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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package crclients
    17  
    18  import (
    19  	"context"
    20  
    21  	"github.com/pkg/errors"
    22  
    23  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/containerd"
    24  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/crio"
    25  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/docker"
    26  )
    27  
    28  const (
    29  	ContainerRuntimeDocker     = "docker"
    30  	ContainerRuntimeContainerd = "containerd"
    31  	ContainerRuntimeCrio       = "crio"
    32  
    33  	defaultDockerSocket     = "unix:///var/run/docker.sock"
    34  	defaultContainerdSocket = "/run/containerd/containerd.sock"
    35  	defaultCrioSocket       = "/var/run/crio/crio.sock"
    36  	containerdDefaultNS     = "k8s.io"
    37  )
    38  
    39  // CrClientConfig contains the basic cr client configuration.
    40  type CrClientConfig struct {
    41  	// Support docker, containerd, crio for now
    42  	Runtime      string
    43  	SocketPath   string
    44  	ContainerdNS string
    45  }
    46  
    47  // ContainerRuntimeInfoClient represents a struct which can give you information about container runtime
    48  type ContainerRuntimeInfoClient interface {
    49  	GetPidFromContainerID(ctx context.Context, containerID string) (uint32, error)
    50  	ContainerKillByContainerID(ctx context.Context, containerID string) error
    51  	FormatContainerID(ctx context.Context, containerID string) (string, error)
    52  	ListContainerIDs(ctx context.Context) ([]string, error)
    53  	GetLabelsFromContainerID(ctx context.Context, containerID string) (map[string]string, error)
    54  }
    55  
    56  // CreateContainerRuntimeInfoClient creates a container runtime information client.
    57  func CreateContainerRuntimeInfoClient(clientConfig *CrClientConfig) (ContainerRuntimeInfoClient, error) {
    58  	// TODO: support more container runtime
    59  
    60  	var cli ContainerRuntimeInfoClient
    61  	var err error
    62  	socketPath := clientConfig.SocketPath
    63  	switch clientConfig.Runtime {
    64  	case ContainerRuntimeDocker:
    65  		if socketPath == "" {
    66  			socketPath = defaultDockerSocket
    67  		} else {
    68  			socketPath = "unix://" + socketPath
    69  		}
    70  		cli, err = docker.New(socketPath, "", nil, nil)
    71  		if err != nil {
    72  			return nil, err
    73  		}
    74  	case ContainerRuntimeContainerd:
    75  		// TODO(yeya24): add more options?
    76  		if socketPath == "" {
    77  			socketPath = defaultContainerdSocket
    78  		}
    79  		containerdNS := containerdDefaultNS
    80  		if clientConfig.ContainerdNS != "" {
    81  			containerdNS = clientConfig.ContainerdNS
    82  		}
    83  		cli, err = containerd.New(socketPath, containerd.WithDefaultNamespace(containerdNS))
    84  		if err != nil {
    85  			return nil, err
    86  		}
    87  	case ContainerRuntimeCrio:
    88  		if socketPath == "" {
    89  			socketPath = defaultCrioSocket
    90  		}
    91  		cli, err = crio.New(socketPath)
    92  		if err != nil {
    93  			return nil, err
    94  		}
    95  	default:
    96  		return nil, errors.Errorf("only docker/containerd/crio is supported, but got %s", clientConfig.Runtime)
    97  	}
    98  
    99  	return cli, nil
   100  }
   101