...

Source file src/github.com/Microsoft/hcsshim/internal/schemaversion/schemaversion.go

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

     1  //go:build windows
     2  // +build windows
     3  
     4  package schemaversion
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  
    10  	hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
    11  	"github.com/Microsoft/hcsshim/osversion"
    12  	"github.com/sirupsen/logrus"
    13  )
    14  
    15  // SchemaV10 makes it easy for callers to get a v1.0 schema version object
    16  func SchemaV10() *hcsschema.Version {
    17  	return &hcsschema.Version{Major: 1, Minor: 0}
    18  }
    19  
    20  // SchemaV21 makes it easy for callers to get a v2.1 schema version object
    21  func SchemaV21() *hcsschema.Version {
    22  	return &hcsschema.Version{Major: 2, Minor: 1}
    23  }
    24  
    25  // SchemaV25  makes it easy for callers to get a v2.5 schema version object.
    26  func SchemaV25() *hcsschema.Version {
    27  	return &hcsschema.Version{Major: 2, Minor: 5}
    28  }
    29  
    30  // isSupported determines if a given schema version is supported
    31  func IsSupported(sv *hcsschema.Version) error {
    32  	if IsV10(sv) {
    33  		return nil
    34  	}
    35  	if IsV21(sv) {
    36  		if osversion.Build() < osversion.RS5 {
    37  			return fmt.Errorf("unsupported on this Windows build")
    38  		}
    39  		return nil
    40  	}
    41  
    42  	if IsV25(sv) {
    43  		if osversion.Build() < osversion.LTSC2022 { // pending solution to question over version numbers re osversion.V21H2
    44  			return fmt.Errorf("unsupported on this Windows build")
    45  		}
    46  		return nil
    47  	}
    48  	return fmt.Errorf("unknown schema version %s", String(sv))
    49  }
    50  
    51  // IsV10 determines if a given schema version object is 1.0. This was the only thing
    52  // supported in RS1..3. It lives on in RS5, but will be deprecated in a future release.
    53  func IsV10(sv *hcsschema.Version) bool {
    54  	if sv.Major == 1 && sv.Minor == 0 {
    55  		return true
    56  	}
    57  	return false
    58  }
    59  
    60  // IsV21 determines if a given schema version object is 2.0. This was introduced in
    61  // RS4, but not fully implemented. Recommended for applications using HCS in RS5
    62  // onwards.
    63  func IsV21(sv *hcsschema.Version) bool {
    64  	if sv.Major == 2 && sv.Minor == 1 {
    65  		return true
    66  	}
    67  	return false
    68  }
    69  
    70  // V25 schema introduced much later. Required to support SNP.
    71  func IsV25(sv *hcsschema.Version) bool {
    72  	if sv.Major == 2 && sv.Minor == 5 {
    73  		return true
    74  	}
    75  	return false
    76  }
    77  
    78  // String returns a JSON encoding of a schema version object
    79  func String(sv *hcsschema.Version) string {
    80  	b, err := json.Marshal(sv)
    81  	if err != nil {
    82  		return ""
    83  	}
    84  	return string(b[:])
    85  }
    86  
    87  // DetermineSchemaVersion works out what schema version to use based on build and
    88  // requested option.
    89  func DetermineSchemaVersion(requestedSV *hcsschema.Version) *hcsschema.Version {
    90  	sv := SchemaV10()
    91  	if osversion.Build() >= osversion.RS5 {
    92  		sv = SchemaV21()
    93  	}
    94  	if requestedSV != nil {
    95  		if err := IsSupported(requestedSV); err == nil {
    96  			sv = requestedSV
    97  		} else {
    98  			logrus.WithField("schemaVersion", requestedSV).Warn("Ignoring unsupported requested schema version")
    99  		}
   100  	}
   101  	return sv
   102  }
   103  

View as plain text