registry.go 2.6 KB

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