Vendor Microsoft/hcsshim with CGO removal

Signed-off-by: Paul "TBBle" Hampson <Paul.Hampson@Pobox.com>
This commit is contained in:
Paul "TBBle" Hampson
2020-01-08 04:02:12 +11:00
parent 592a617f3d
commit 45a17cffb6
9 changed files with 206 additions and 120 deletions

View File

@@ -35,7 +35,7 @@ func checkForErrors(methodName string, hr error, resultBuffer *uint16) error {
}
if errorFound {
returnError := hcserror.New(hr, methodName, result)
returnError := new(hr, methodName, result)
logrus.Debugf(returnError.Error()) // HCN errors logged for debugging.
return returnError
}
@@ -43,6 +43,52 @@ func checkForErrors(methodName string, hr error, resultBuffer *uint16) error {
return nil
}
type ErrorCode uint32
// For common errors, define the error as it is in windows, so we can quickly determine it later
const (
ERROR_NOT_FOUND = 0x490
HCN_E_PORT_ALREADY_EXISTS ErrorCode = 0x803b0013
)
type HcnError struct {
*hcserror.HcsError
code ErrorCode
}
func (e *HcnError) Error() string {
return e.HcsError.Error()
}
func CheckErrorWithCode(err error, code ErrorCode) bool {
hcnError, ok := err.(*HcnError)
if ok {
return hcnError.code == code
}
return false
}
func IsElementNotFoundError(err error) bool {
return CheckErrorWithCode(err, ERROR_NOT_FOUND)
}
func IsPortAlreadyExistsError(err error) bool {
return CheckErrorWithCode(err, HCN_E_PORT_ALREADY_EXISTS)
}
func new(hr error, title string, rest string) error {
err := &HcnError{}
hcsError := hcserror.New(hr, title, rest)
err.HcsError = hcsError.(*hcserror.HcsError)
err.code = ErrorCode(hcserror.Win32FromError(hr))
return err
}
//
// Note that the below errors are not errors returned by hcn itself
// we wish to seperate them as they are shim usage error
//
// NetworkNotFoundError results from a failed seach for a network by Id or Name
type NetworkNotFoundError struct {
NetworkName string
@@ -50,10 +96,10 @@ type NetworkNotFoundError struct {
}
func (e NetworkNotFoundError) Error() string {
if e.NetworkName == "" {
return fmt.Sprintf("Network Name %s not found", e.NetworkName)
if e.NetworkName != "" {
return fmt.Sprintf("Network name %q not found", e.NetworkName)
}
return fmt.Sprintf("Network Id %s not found", e.NetworkID)
return fmt.Sprintf("Network ID %q not found", e.NetworkID)
}
// EndpointNotFoundError results from a failed seach for an endpoint by Id or Name
@@ -63,10 +109,10 @@ type EndpointNotFoundError struct {
}
func (e EndpointNotFoundError) Error() string {
if e.EndpointName == "" {
return fmt.Sprintf("Endpoint Name %s not found", e.EndpointName)
if e.EndpointName != "" {
return fmt.Sprintf("Endpoint name %q not found", e.EndpointName)
}
return fmt.Sprintf("Endpoint Id %s not found", e.EndpointID)
return fmt.Sprintf("Endpoint ID %q not found", e.EndpointID)
}
// NamespaceNotFoundError results from a failed seach for a namsepace by Id
@@ -75,7 +121,7 @@ type NamespaceNotFoundError struct {
}
func (e NamespaceNotFoundError) Error() string {
return fmt.Sprintf("Namespace %s not found", e.NamespaceID)
return fmt.Sprintf("Namespace ID %q not found", e.NamespaceID)
}
// LoadBalancerNotFoundError results from a failed seach for a loadbalancer by Id
@@ -84,7 +130,7 @@ type LoadBalancerNotFoundError struct {
}
func (e LoadBalancerNotFoundError) Error() string {
return fmt.Sprintf("LoadBalancer %s not found", e.LoadBalancerId)
return fmt.Sprintf("LoadBalancer %q not found", e.LoadBalancerId)
}
// IsNotFoundError returns a boolean indicating whether the error was caused by

View File

@@ -247,11 +247,23 @@ func ListNamespacesQuery(query HostComputeQuery) ([]HostComputeNamespace, error)
// GetNamespaceByID returns the Namespace specified by Id.
func GetNamespaceByID(namespaceId string) (*HostComputeNamespace, error) {
g, err := guid.FromString(namespaceId)
hcnQuery := defaultQuery()
mapA := map[string]string{"ID": namespaceId}
filter, err := json.Marshal(mapA)
if err != nil {
return nil, errInvalidNamespaceID
return nil, err
}
return getNamespace(g, defaultQueryJson())
hcnQuery.Filter = string(filter)
namespaces, err := ListNamespacesQuery(hcnQuery)
if err != nil {
return nil, err
}
if len(namespaces) == 0 {
return nil, NamespaceNotFoundError{NamespaceID: namespaceId}
}
return &namespaces[0], err
}
// GetNamespaceEndpointIds returns the endpoints of the Namespace specified by Id.