...

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

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

     1  // Copyright 2020 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 chaosdaemon
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  
    20  	"github.com/golang/protobuf/ptypes/empty"
    21  
    22  	pb "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
    23  )
    24  
    25  // ContainerKill kills container according to container id in the req
    26  func (s *DaemonServer) ContainerKill(ctx context.Context, req *pb.ContainerRequest) (*empty.Empty, error) {
    27  	log.Info("Container Kill", "request", req)
    28  
    29  	action := req.Action.Action
    30  	if action != pb.ContainerAction_KILL {
    31  		err := fmt.Errorf("container action is %s , not kill", action)
    32  		log.Error(err, "container action is not expected")
    33  		return nil, err
    34  	}
    35  
    36  	err := s.crClient.ContainerKillByContainerID(ctx, req.ContainerId)
    37  	if err != nil {
    38  		log.Error(err, "error while killing container")
    39  		return nil, err
    40  	}
    41  
    42  	return &empty.Empty{}, nil
    43  }
    44  
    45  func (s *DaemonServer) ContainerGetPid(ctx context.Context, req *pb.ContainerRequest) (*pb.ContainerResponse, error) {
    46  	log.Info("container GetPid", "request", req)
    47  
    48  	action := req.Action.Action
    49  	if action != pb.ContainerAction_GETPID {
    50  		err := fmt.Errorf("container action is %s , not getpid", action)
    51  		log.Error(err, "container action is not expected")
    52  		return nil, err
    53  	}
    54  
    55  	pid, err := s.crClient.GetPidFromContainerID(ctx, req.ContainerId)
    56  	if err != nil {
    57  		log.Error(err, "error while getting pid from container")
    58  		return nil, err
    59  	}
    60  
    61  	return &pb.ContainerResponse{Pid: pid}, nil
    62  }
    63