...

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

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

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