registry.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package cni
  2. import (
  3. "errors"
  4. "github.com/Microsoft/hcsshim/internal/guid"
  5. "github.com/Microsoft/hcsshim/internal/regstate"
  6. )
  7. const (
  8. cniRoot = "cni"
  9. cniKey = "cfg"
  10. )
  11. // PersistedNamespaceConfig is the registry version of the `NamespaceID` to UVM
  12. // map.
  13. type PersistedNamespaceConfig struct {
  14. namespaceID string
  15. stored bool
  16. ContainerID string
  17. HostUniqueID guid.GUID
  18. }
  19. // NewPersistedNamespaceConfig creates an in-memory namespace config that can be
  20. // persisted to the registry.
  21. func NewPersistedNamespaceConfig(namespaceID, containerID string, containerHostUniqueID guid.GUID) *PersistedNamespaceConfig {
  22. return &PersistedNamespaceConfig{
  23. namespaceID: namespaceID,
  24. ContainerID: containerID,
  25. HostUniqueID: containerHostUniqueID,
  26. }
  27. }
  28. // LoadPersistedNamespaceConfig loads a persisted config from the registry that matches
  29. // `namespaceID`. If not found returns `regstate.NotFoundError`
  30. func LoadPersistedNamespaceConfig(namespaceID string) (*PersistedNamespaceConfig, error) {
  31. sk, err := regstate.Open(cniRoot, false)
  32. if err != nil {
  33. return nil, err
  34. }
  35. defer sk.Close()
  36. pnc := PersistedNamespaceConfig{
  37. namespaceID: namespaceID,
  38. stored: true,
  39. }
  40. if err := sk.Get(namespaceID, cniKey, &pnc); err != nil {
  41. return nil, err
  42. }
  43. return &pnc, nil
  44. }
  45. // Store stores or updates the in-memory config to its registry state. If the
  46. // store failes returns the store error.
  47. func (pnc *PersistedNamespaceConfig) Store() error {
  48. if pnc.namespaceID == "" {
  49. return errors.New("invalid namespaceID ''")
  50. }
  51. if pnc.ContainerID == "" {
  52. return errors.New("invalid containerID ''")
  53. }
  54. empty := guid.GUID{}
  55. if pnc.HostUniqueID == empty {
  56. return errors.New("invalid containerHostUniqueID 'empy'")
  57. }
  58. sk, err := regstate.Open(cniRoot, false)
  59. if err != nil {
  60. return err
  61. }
  62. defer sk.Close()
  63. if pnc.stored {
  64. if err := sk.Set(pnc.namespaceID, cniKey, pnc); err != nil {
  65. return err
  66. }
  67. } else {
  68. if err := sk.Create(pnc.namespaceID, cniKey, pnc); err != nil {
  69. return err
  70. }
  71. }
  72. pnc.stored = true
  73. return nil
  74. }
  75. // Remove removes any persisted state associated with this config. If the config
  76. // is not found in the registery `Remove` returns no error.
  77. func (pnc *PersistedNamespaceConfig) Remove() error {
  78. if pnc.stored {
  79. sk, err := regstate.Open(cniRoot, false)
  80. if err != nil {
  81. if regstate.IsNotFoundError(err) {
  82. pnc.stored = false
  83. return nil
  84. }
  85. return err
  86. }
  87. defer sk.Close()
  88. if err := sk.Remove(pnc.namespaceID); err != nil {
  89. if regstate.IsNotFoundError(err) {
  90. pnc.stored = false
  91. return nil
  92. }
  93. return err
  94. }
  95. }
  96. pnc.stored = false
  97. return nil
  98. }