...

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  
    21  	"github.com/golang/protobuf/ptypes/empty"
    22  	"github.com/pkg/errors"
    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 := s.getLoggerFromContext(ctx)
    30  
    31  	log.Info("Container Kill", "request", req)
    32  
    33  	action := req.Action.Action
    34  	if action != pb.ContainerAction_KILL {
    35  		err := errors.Errorf("container action is %s , not kill", action)
    36  		log.Error(err, "container action is not expected")
    37  		return nil, err
    38  	}
    39  
    40  	err := s.crClient.ContainerKillByContainerID(ctx, req.ContainerId)
    41  	if err != nil {
    42  		log.Error(err, "error while killing container")
    43  		return nil, err
    44  	}
    45  
    46  	return &empty.Empty{}, nil
    47  }
    48  
    49  func (s *DaemonServer) ContainerGetPid(ctx context.Context, req *pb.ContainerRequest) (*pb.ContainerResponse, error) {
    50  	log := s.getLoggerFromContext(ctx)
    51  
    52  	log.Info("container GetPid", "request", req)
    53  
    54  	action := req.Action.Action
    55  	if action != pb.ContainerAction_GETPID {
    56  		err := errors.Errorf("container action is %s , not getpid", action)
    57  		log.Error(err, "container action is not expected")
    58  		return nil, err
    59  	}
    60  
    61  	pid, err := s.crClient.GetPidFromContainerID(ctx, req.ContainerId)
    62  	if err != nil {
    63  		log.Error(err, "error while getting pid from container")
    64  		return nil, err
    65  	}
    66  
    67  	return &pb.ContainerResponse{Pid: pid}, nil
    68  }
    69