net_cls.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. Copyright The containerd Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cgroup1
  14. import (
  15. "os"
  16. "path/filepath"
  17. "strconv"
  18. specs "github.com/opencontainers/runtime-spec/specs-go"
  19. )
  20. func NewNetCls(root string) *netclsController {
  21. return &netclsController{
  22. root: filepath.Join(root, string(NetCLS)),
  23. }
  24. }
  25. type netclsController struct {
  26. root string
  27. }
  28. func (n *netclsController) Name() Name {
  29. return NetCLS
  30. }
  31. func (n *netclsController) Path(path string) string {
  32. return filepath.Join(n.root, path)
  33. }
  34. func (n *netclsController) Create(path string, resources *specs.LinuxResources) error {
  35. if err := os.MkdirAll(n.Path(path), defaultDirPerm); err != nil {
  36. return err
  37. }
  38. if resources.Network != nil && resources.Network.ClassID != nil && *resources.Network.ClassID > 0 {
  39. return os.WriteFile(
  40. filepath.Join(n.Path(path), "net_cls.classid"),
  41. []byte(strconv.FormatUint(uint64(*resources.Network.ClassID), 10)),
  42. defaultFilePerm,
  43. )
  44. }
  45. return nil
  46. }
  47. func (n *netclsController) Update(path string, resources *specs.LinuxResources) error {
  48. return n.Create(path, resources)
  49. }