...

Source file src/github.com/google/go-containerregistry/pkg/v1/random/index.go

Documentation: github.com/google/go-containerregistry/pkg/v1/random

     1  // Copyright 2018 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package random
    16  
    17  import (
    18  	"bytes"
    19  	"encoding/json"
    20  	"fmt"
    21  
    22  	v1 "github.com/google/go-containerregistry/pkg/v1"
    23  	"github.com/google/go-containerregistry/pkg/v1/partial"
    24  	"github.com/google/go-containerregistry/pkg/v1/types"
    25  )
    26  
    27  type randomIndex struct {
    28  	images   map[v1.Hash]v1.Image
    29  	manifest *v1.IndexManifest
    30  }
    31  
    32  // Index returns a pseudo-randomly generated ImageIndex with count images, each
    33  // having the given number of layers of size byteSize.
    34  func Index(byteSize, layers, count int64, options ...Option) (v1.ImageIndex, error) {
    35  	manifest := v1.IndexManifest{
    36  		SchemaVersion: 2,
    37  		MediaType:     types.OCIImageIndex,
    38  		Manifests:     []v1.Descriptor{},
    39  	}
    40  
    41  	images := make(map[v1.Hash]v1.Image)
    42  	for i := int64(0); i < count; i++ {
    43  		img, err := Image(byteSize, layers, options...)
    44  		if err != nil {
    45  			return nil, err
    46  		}
    47  
    48  		rawManifest, err := img.RawManifest()
    49  		if err != nil {
    50  			return nil, err
    51  		}
    52  		digest, size, err := v1.SHA256(bytes.NewReader(rawManifest))
    53  		if err != nil {
    54  			return nil, err
    55  		}
    56  		mediaType, err := img.MediaType()
    57  		if err != nil {
    58  			return nil, err
    59  		}
    60  
    61  		manifest.Manifests = append(manifest.Manifests, v1.Descriptor{
    62  			Digest:    digest,
    63  			Size:      size,
    64  			MediaType: mediaType,
    65  		})
    66  
    67  		images[digest] = img
    68  	}
    69  
    70  	return &randomIndex{
    71  		images:   images,
    72  		manifest: &manifest,
    73  	}, nil
    74  }
    75  
    76  func (i *randomIndex) MediaType() (types.MediaType, error) {
    77  	return i.manifest.MediaType, nil
    78  }
    79  
    80  func (i *randomIndex) Digest() (v1.Hash, error) {
    81  	return partial.Digest(i)
    82  }
    83  
    84  func (i *randomIndex) Size() (int64, error) {
    85  	return partial.Size(i)
    86  }
    87  
    88  func (i *randomIndex) IndexManifest() (*v1.IndexManifest, error) {
    89  	return i.manifest, nil
    90  }
    91  
    92  func (i *randomIndex) RawManifest() ([]byte, error) {
    93  	m, err := i.IndexManifest()
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  	return json.Marshal(m)
    98  }
    99  
   100  func (i *randomIndex) Image(h v1.Hash) (v1.Image, error) {
   101  	if img, ok := i.images[h]; ok {
   102  		return img, nil
   103  	}
   104  
   105  	return nil, fmt.Errorf("image not found: %v", h)
   106  }
   107  
   108  func (i *randomIndex) ImageIndex(h v1.Hash) (v1.ImageIndex, error) {
   109  	// This is a single level index (for now?).
   110  	return nil, fmt.Errorf("image not found: %v", h)
   111  }
   112  

View as plain text