...

Source file src/github.com/Microsoft/hcsshim/internal/cni/registry.go

Documentation: github.com/Microsoft/hcsshim/internal/cni

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

View as plain text