...

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

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

     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 cgroups
    17  
    18  import (
    19  	"os"
    20  
    21  	"github.com/containerd/cgroups"
    22  )
    23  
    24  // defaults returns all known groups
    25  func defaults(root string) ([]cgroups.Subsystem, error) {
    26  	h, err := cgroups.NewHugetlb(root)
    27  	if err != nil && !os.IsNotExist(err) {
    28  		return nil, err
    29  	}
    30  	s := []cgroups.Subsystem{
    31  		cgroups.NewNamed(root, "systemd"),
    32  		cgroups.NewFreezer(root),
    33  		cgroups.NewPids(root),
    34  		cgroups.NewNetCls(root),
    35  		cgroups.NewNetPrio(root),
    36  		cgroups.NewPerfEvent(root),
    37  		cgroups.NewCpuset(root),
    38  		cgroups.NewCpu(root),
    39  		cgroups.NewCpuacct(root),
    40  		cgroups.NewMemory(root),
    41  		cgroups.NewBlkio(root),
    42  		cgroups.NewRdma(root),
    43  	}
    44  	// only add the devices cgroup if we are not in a user namespace
    45  	// because modifications are not allowed
    46  	if !cgroups.RunningInUserNS() {
    47  		s = append(s, cgroups.NewDevices(root))
    48  	}
    49  	// add the hugetlb cgroup if error wasn't due to missing hugetlb
    50  	// cgroup support on the host
    51  	if err == nil {
    52  		s = append(s, h)
    53  	}
    54  	return s, nil
    55  }
    56  
    57  type pather interface {
    58  	cgroups.Subsystem
    59  	Path(path string) string
    60  }
    61  
    62  func pathers(subystems []cgroups.Subsystem) []pather {
    63  	var out []pather
    64  	for _, s := range subystems {
    65  		if p, ok := s.(pather); ok {
    66  			out = append(out, p)
    67  		}
    68  	}
    69  	return out
    70  }
    71