...

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

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

     1  // Copyright 2022 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 cgroups
    17  
    18  import (
    19  	"fmt"
    20  	"os/exec"
    21  
    22  	"github.com/containerd/cgroups"
    23  	"github.com/pkg/errors"
    24  )
    25  
    26  type CGroupInfo struct {
    27  	CGMode       cgroups.CGMode
    28  	V1Path       cgroups.Path
    29  	V2CGroupPath string
    30  }
    31  
    32  type AttachCGroup interface {
    33  	TargetCGroup() CGroupInfo
    34  	AttachProcess(pid int) error
    35  }
    36  
    37  var _ AttachCGroup = (*AttachCGroupV1)(nil)
    38  
    39  type AttachCGroupV1 struct {
    40  	mode cgroups.CGMode
    41  	path cgroups.Path
    42  }
    43  
    44  func (a *AttachCGroupV1) TargetCGroup() CGroupInfo {
    45  	return CGroupInfo{
    46  		CGMode:       a.mode,
    47  		V1Path:       a.path,
    48  		V2CGroupPath: "",
    49  	}
    50  }
    51  
    52  func (a *AttachCGroupV1) AttachProcess(pid int) error {
    53  	cgroupv1, err := cgroups.Load(V1, a.path)
    54  	if err != nil {
    55  		cpuCGroupPath, _ := a.path("cpu")
    56  		memoryCGroupPath, _ := a.path("memory")
    57  		return errors.Wrapf(err, "load cgroup v1 manager, pid %d, cpu path %s, memory path %s", pid, cpuCGroupPath, memoryCGroupPath)
    58  	}
    59  	err = cgroupv1.Add(cgroups.Process{Pid: pid})
    60  	if err != nil {
    61  		cpuCGroupPath, _ := a.path("cpu")
    62  		memoryCGroupPath, _ := a.path("memory")
    63  		return errors.Wrapf(err, "add process to cgroup, pid %d, cpu path %s, memory path %s", pid, cpuCGroupPath, memoryCGroupPath)
    64  	}
    65  	return nil
    66  }
    67  
    68  var _ AttachCGroup = (*AttachCGroupV2)(nil)
    69  
    70  type AttachCGroupV2 struct {
    71  	mode cgroups.CGMode
    72  	path string
    73  }
    74  
    75  func (a *AttachCGroupV2) TargetCGroup() CGroupInfo {
    76  	return CGroupInfo{
    77  		CGMode:       a.mode,
    78  		V1Path:       nil,
    79  		V2CGroupPath: a.path,
    80  	}
    81  }
    82  
    83  func (a *AttachCGroupV2) AttachProcess(pid int) error {
    84  	// escape the CGroup Namespace, we could not modify cgroups across different cgroups namespace,
    85  	// resolve https://github.com/chaos-mesh/chaos-mesh/pull/2928#issuecomment-1049465242
    86  	targetFile := fmt.Sprintf("/host-sys/fs/cgroup%s/cgroup.procs", a.path)
    87  	command := exec.Command("nsenter", "-C", "-t", "1", "--", "sh", "-c", fmt.Sprintf("echo %d >> %s", pid, targetFile))
    88  	output, err := command.CombinedOutput()
    89  	if err != nil {
    90  		return errors.Wrapf(err, "attach process to cgroup, pid %d, target cgourp file %s, output %s", pid, targetFile, string(output))
    91  	}
    92  	return nil
    93  }
    94  
    95  // GetAttacherForPID return a AttachCGroup, which could attach a process to the same cgroup of the target pid
    96  func GetAttacherForPID(targetPID int) (AttachCGroup, error) {
    97  	if cgroups.Mode() == cgroups.Unified {
    98  		groupPath, err := V2PidGroupPath(targetPID)
    99  		if err != nil {
   100  			return nil, err
   101  		}
   102  		return &AttachCGroupV2{
   103  			mode: cgroups.Unified,
   104  			path: groupPath,
   105  		}, nil
   106  	}
   107  
   108  	// By default it's cgroup v1
   109  	return &AttachCGroupV1{
   110  		mode: cgroups.Mode(),
   111  		path: PidPath(targetPID),
   112  	}, nil
   113  }
   114