...

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

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

     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 containerd
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"syscall"
    22  
    23  	"github.com/containerd/containerd"
    24  
    25  	"github.com/chaos-mesh/chaos-mesh/pkg/mock"
    26  )
    27  
    28  const (
    29  	containerdProtocolPrefix = "containerd://"
    30  )
    31  
    32  // ContainerdClientInterface represents the ContainerClient, it's used to simply unit test
    33  type ContainerdClientInterface interface {
    34  	LoadContainer(ctx context.Context, id string) (containerd.Container, error)
    35  }
    36  
    37  // ContainerdClient can get information from containerd
    38  type ContainerdClient struct {
    39  	client ContainerdClientInterface
    40  }
    41  
    42  // FormatContainerID strips protocol prefix from the container ID
    43  func (c ContainerdClient) FormatContainerID(ctx context.Context, containerID string) (string, error) {
    44  	if len(containerID) < len(containerdProtocolPrefix) {
    45  		return "", fmt.Errorf("container id %s is not a containerd container id", containerID)
    46  	}
    47  	if containerID[0:len(containerdProtocolPrefix)] != containerdProtocolPrefix {
    48  		return "", fmt.Errorf("expected %s but got %s", containerdProtocolPrefix, containerID[0:len(containerdProtocolPrefix)])
    49  	}
    50  	return containerID[len(containerdProtocolPrefix):], nil
    51  }
    52  
    53  // GetPidFromContainerID fetches PID according to container id
    54  func (c ContainerdClient) GetPidFromContainerID(ctx context.Context, containerID string) (uint32, error) {
    55  	id, err := c.FormatContainerID(ctx, containerID)
    56  	if err != nil {
    57  		return 0, err
    58  	}
    59  	container, err := c.client.LoadContainer(ctx, id)
    60  	if err != nil {
    61  		return 0, err
    62  	}
    63  	task, err := container.Task(ctx, nil)
    64  	if err != nil {
    65  		return 0, err
    66  	}
    67  	return task.Pid(), nil
    68  }
    69  
    70  // ContainerKillByContainerID kills container according to container id
    71  func (c ContainerdClient) ContainerKillByContainerID(ctx context.Context, containerID string) error {
    72  	containerID, err := c.FormatContainerID(ctx, containerID)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	container, err := c.client.LoadContainer(ctx, containerID)
    78  	if err != nil {
    79  		return err
    80  	}
    81  	task, err := container.Task(ctx, nil)
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	err = task.Kill(ctx, syscall.SIGKILL)
    87  
    88  	return err
    89  }
    90  
    91  func New(address string, opts ...containerd.ClientOpt) (*ContainerdClient, error) {
    92  	// Mock point to return error in unit test
    93  	if err := mock.On("NewContainerdClientError"); err != nil {
    94  		return nil, err.(error)
    95  	}
    96  	if client := mock.On("MockContainerdClient"); client != nil {
    97  		return &ContainerdClient{
    98  			client.(ContainerdClientInterface),
    99  		}, nil
   100  	}
   101  
   102  	c, err := containerd.New(address, opts...)
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  	// The real logic
   107  	return &ContainerdClient{
   108  		client: c,
   109  	}, nil
   110  }
   111  
   112  // WithDefaultNamespace is an alias for the function in containerd with the same name
   113  var WithDefaultNamespace = containerd.WithDefaultNamespace
   114