github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/pkg/system/lcow.go (about)

     1  package system
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"strings"
     7  
     8  	specs "github.com/opencontainers/image-spec/specs-go/v1"
     9  )
    10  
    11  // ValidatePlatform determines if a platform structure is valid.
    12  // TODO This is a temporary function - can be replaced by parsing from
    13  // https://github.com/containerd/containerd/pull/1403/files at a later date.
    14  // @jhowardmsft
    15  func ValidatePlatform(platform *specs.Platform) error {
    16  	platform.Architecture = strings.ToLower(platform.Architecture)
    17  	platform.OS = strings.ToLower(platform.OS)
    18  	// Based on https://github.com/moby/moby/pull/34642#issuecomment-330375350, do
    19  	// not support anything except operating system.
    20  	if platform.Architecture != "" {
    21  		return fmt.Errorf("invalid platform architecture %q", platform.Architecture)
    22  	}
    23  	if platform.OS != "" {
    24  		if !(platform.OS == runtime.GOOS || (LCOWSupported() && platform.OS == "linux")) {
    25  			return fmt.Errorf("invalid platform os %q", platform.OS)
    26  		}
    27  	}
    28  	if len(platform.OSFeatures) != 0 {
    29  		return fmt.Errorf("invalid platform osfeatures %q", platform.OSFeatures)
    30  	}
    31  	if platform.OSVersion != "" {
    32  		return fmt.Errorf("invalid platform osversion %q", platform.OSVersion)
    33  	}
    34  	if platform.Variant != "" {
    35  		return fmt.Errorf("invalid platform variant %q", platform.Variant)
    36  	}
    37  	return nil
    38  }
    39  
    40  // ParsePlatform parses a platform string in the format os[/arch[/variant]
    41  // into an OCI image-spec platform structure.
    42  // TODO This is a temporary function - can be replaced by parsing from
    43  // https://github.com/containerd/containerd/pull/1403/files at a later date.
    44  // @jhowardmsft
    45  func ParsePlatform(in string) *specs.Platform {
    46  	p := &specs.Platform{}
    47  	elements := strings.SplitN(strings.ToLower(in), "/", 3)
    48  	if len(elements) == 3 {
    49  		p.Variant = elements[2]
    50  	}
    51  	if len(elements) >= 2 {
    52  		p.Architecture = elements[1]
    53  	}
    54  	if len(elements) >= 1 {
    55  		p.OS = elements[0]
    56  	}
    57  	return p
    58  }