![dependabot[bot]](/assets/img/avatar_default.png)
Bumps [github.com/Microsoft/hcsshim](https://github.com/Microsoft/hcsshim) from 0.9.9 to 0.11.1. - [Release notes](https://github.com/Microsoft/hcsshim/releases) - [Commits](https://github.com/Microsoft/hcsshim/compare/v0.9.9...v0.11.1) --- updated-dependencies: - dependency-name: github.com/Microsoft/hcsshim dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com>
113 lines
2.6 KiB
Go
113 lines
2.6 KiB
Go
//go:build windows
|
|
|
|
package cni
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/Microsoft/go-winio/pkg/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 registry `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
|
|
}
|