namespace.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. Copyright 2016 The Kubernetes 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 sysctl
  14. import (
  15. "strings"
  16. )
  17. // Namespace represents a kernel namespace name.
  18. type Namespace string
  19. const (
  20. // the Linux IPC namespace
  21. IpcNamespace = Namespace("ipc")
  22. // the network namespace
  23. NetNamespace = Namespace("net")
  24. // the zero value if no namespace is known
  25. UnknownNamespace = Namespace("")
  26. )
  27. var namespaces = map[string]Namespace{
  28. "kernel.sem": IpcNamespace,
  29. }
  30. var prefixNamespaces = map[string]Namespace{
  31. "kernel.shm": IpcNamespace,
  32. "kernel.msg": IpcNamespace,
  33. "fs.mqueue.": IpcNamespace,
  34. "net.": NetNamespace,
  35. }
  36. // NamespacedBy returns the namespace of the Linux kernel for a sysctl, or
  37. // UnknownNamespace if the sysctl is not known to be namespaced.
  38. func NamespacedBy(val string) Namespace {
  39. if ns, found := namespaces[val]; found {
  40. return ns
  41. }
  42. for p, ns := range prefixNamespaces {
  43. if strings.HasPrefix(val, p) {
  44. return ns
  45. }
  46. }
  47. return UnknownNamespace
  48. }