123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- package cni
- import (
- "errors"
- "github.com/Microsoft/hcsshim/internal/guid"
- "github.com/Microsoft/hcsshim/internal/regstate"
- )
- const (
- cniRoot = "cni"
- cniKey = "cfg"
- )
- // PersistedNamespaceConfig is the registry version of the `NamespaceID` to UVM
- // map.
- type PersistedNamespaceConfig struct {
- namespaceID string
- stored bool
- ContainerID string
- HostUniqueID guid.GUID
- }
- // NewPersistedNamespaceConfig creates an in-memory namespace config that can be
- // persisted to the registry.
- func NewPersistedNamespaceConfig(namespaceID, containerID string, containerHostUniqueID guid.GUID) *PersistedNamespaceConfig {
- return &PersistedNamespaceConfig{
- namespaceID: namespaceID,
- ContainerID: containerID,
- HostUniqueID: containerHostUniqueID,
- }
- }
- // LoadPersistedNamespaceConfig loads a persisted config from the registry that matches
- // `namespaceID`. If not found returns `regstate.NotFoundError`
- func LoadPersistedNamespaceConfig(namespaceID string) (*PersistedNamespaceConfig, error) {
- sk, err := regstate.Open(cniRoot, false)
- if err != nil {
- return nil, err
- }
- defer sk.Close()
- pnc := PersistedNamespaceConfig{
- namespaceID: namespaceID,
- stored: true,
- }
- if err := sk.Get(namespaceID, cniKey, &pnc); err != nil {
- return nil, err
- }
- return &pnc, nil
- }
- // Store stores or updates the in-memory config to its registry state. If the
- // store failes returns the store error.
- func (pnc *PersistedNamespaceConfig) Store() error {
- if pnc.namespaceID == "" {
- return errors.New("invalid namespaceID ''")
- }
- if pnc.ContainerID == "" {
- return errors.New("invalid containerID ''")
- }
- empty := guid.GUID{}
- if pnc.HostUniqueID == empty {
- return errors.New("invalid containerHostUniqueID 'empy'")
- }
- sk, err := regstate.Open(cniRoot, false)
- if err != nil {
- return err
- }
- defer sk.Close()
- if pnc.stored {
- if err := sk.Set(pnc.namespaceID, cniKey, pnc); err != nil {
- return err
- }
- } else {
- if err := sk.Create(pnc.namespaceID, cniKey, pnc); err != nil {
- return err
- }
- }
- pnc.stored = true
- return nil
- }
- // Remove removes any persisted state associated with this config. If the config
- // is not found in the registery `Remove` returns no error.
- func (pnc *PersistedNamespaceConfig) Remove() error {
- if pnc.stored {
- sk, err := regstate.Open(cniRoot, false)
- if err != nil {
- if regstate.IsNotFoundError(err) {
- pnc.stored = false
- return nil
- }
- return err
- }
- defer sk.Close()
- if err := sk.Remove(pnc.namespaceID); err != nil {
- if regstate.IsNotFoundError(err) {
- pnc.stored = false
- return nil
- }
- return err
- }
- }
- pnc.stored = false
- return nil
- }
|