github.com/cvmfs/docker-graphdriver@v0.0.0-20181206110523-155ec6df0521/repository-manager/docker-api/util.go (about)

     1  package dockerutil
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  type ConfigType struct {
     9  	MediaType string
    10  	Size      int
    11  	Digest    string
    12  }
    13  
    14  type Layer struct {
    15  	MediaType string
    16  	Size      int
    17  	Digest    string
    18  }
    19  
    20  type Manifest struct {
    21  	SchemaVersion int
    22  	MediaType     string
    23  	Config        ConfigType
    24  	Layers        []Layer
    25  }
    26  
    27  type ThinImageLayer struct {
    28  	Digest string `json:"digest"`
    29  	Url    string `json:"url,omitempty"`
    30  }
    31  
    32  type ThinImage struct {
    33  	Version    string           `json:"version"`
    34  	MinVersion string           `json:"min_version,omitempty"`
    35  	Origin     string           `json:"origin,omitempty"`
    36  	Layers     []ThinImageLayer `json:"layers"`
    37  	Comment    string           `json:"comment,omitempty"`
    38  }
    39  
    40  var thinImageVersion = "1.0"
    41  
    42  // m is the manifest of the original image
    43  // repoLocation is where inside the repo we saved the several layers
    44  // origin is an ecoding fo the original referencese and original registry
    45  // I believe origin is quite useless but maybe is better to preserv it for
    46  // ergonomic reasons.
    47  func MakeThinImage(m Manifest, layersMapping map[string]string, origin string) (ThinImage, error) {
    48  	layers := make([]ThinImageLayer, len(m.Layers))
    49  
    50  	url_base := "cvmfs://"
    51  
    52  	fmt.Println(layersMapping)
    53  	fmt.Println(m.Layers)
    54  
    55  	for i, layer := range m.Layers {
    56  		digest := strings.Split(layer.Digest, ":")[1]
    57  		location, ok := layersMapping[layer.Digest]
    58  		if !ok {
    59  			err := fmt.Errorf("Impossible to create thin image, missing layer")
    60  			return ThinImage{}, err
    61  		}
    62  		// the location comes as /cvmfs/$reponame/$path
    63  		// we need to remove the /cvmfs/ part, which are 7 chars
    64  		url := url_base + location[7:]
    65  		layers[i] = ThinImageLayer{Digest: digest, Url: url}
    66  	}
    67  
    68  	return ThinImage{Layers: layers,
    69  		Origin:  origin,
    70  		Version: thinImageVersion}, nil
    71  }