github.com/uber/kraken@v0.1.4/lib/dockerregistry/transfer/ro_transferer.go (about)

     1  // Copyright (c) 2016-2019 Uber Technologies, Inc.
     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  package transfer
    15  
    16  import (
    17  	"errors"
    18  	"fmt"
    19  	"os"
    20  
    21  	"github.com/uber/kraken/build-index/tagclient"
    22  	"github.com/uber/kraken/core"
    23  	"github.com/uber/kraken/lib/store"
    24  	"github.com/uber/kraken/lib/torrent/scheduler"
    25  	"github.com/uber-go/tally"
    26  )
    27  
    28  var _ ImageTransferer = (*ReadOnlyTransferer)(nil)
    29  
    30  // ReadOnlyTransferer gets and posts manifest to tracker, and transfers blobs as torrent.
    31  type ReadOnlyTransferer struct {
    32  	stats tally.Scope
    33  	cads  *store.CADownloadStore
    34  	tags  tagclient.Client
    35  	sched scheduler.Scheduler
    36  }
    37  
    38  // NewReadOnlyTransferer creates a new ReadOnlyTransferer.
    39  func NewReadOnlyTransferer(
    40  	stats tally.Scope,
    41  	cads *store.CADownloadStore,
    42  	tags tagclient.Client,
    43  	sched scheduler.Scheduler) *ReadOnlyTransferer {
    44  
    45  	stats = stats.Tagged(map[string]string{
    46  		"module": "rotransferer",
    47  	})
    48  
    49  	return &ReadOnlyTransferer{stats, cads, tags, sched}
    50  }
    51  
    52  // Stat returns blob info from local cache, and triggers download if the blob is
    53  // not available locally.
    54  func (t *ReadOnlyTransferer) Stat(namespace string, d core.Digest) (*core.BlobInfo, error) {
    55  	fi, err := t.cads.Cache().GetFileStat(d.Hex())
    56  	if os.IsNotExist(err) || t.cads.InDownloadError(err) {
    57  		if err := t.sched.Download(namespace, d); err != nil {
    58  			return nil, fmt.Errorf("scheduler: %s", err)
    59  		}
    60  		fi, err = t.cads.Cache().GetFileStat(d.Hex())
    61  		if err != nil {
    62  			return nil, fmt.Errorf("stat cache: %s", err)
    63  		}
    64  	} else if err != nil {
    65  		return nil, fmt.Errorf("stat cache: %s", err)
    66  	}
    67  	return core.NewBlobInfo(fi.Size()), nil
    68  }
    69  
    70  // Download downloads blobs as torrent.
    71  func (t *ReadOnlyTransferer) Download(namespace string, d core.Digest) (store.FileReader, error) {
    72  	f, err := t.cads.Cache().GetFileReader(d.Hex())
    73  	if os.IsNotExist(err) || t.cads.InDownloadError(err) {
    74  		if err := t.sched.Download(namespace, d); err != nil {
    75  			return nil, fmt.Errorf("scheduler: %s", err)
    76  		}
    77  		f, err = t.cads.Cache().GetFileReader(d.Hex())
    78  		if err != nil {
    79  			return nil, fmt.Errorf("cache: %s", err)
    80  		}
    81  	} else if err != nil {
    82  		return nil, fmt.Errorf("cache: %s", err)
    83  	}
    84  	return f, nil
    85  }
    86  
    87  // Upload uploads blobs to a torrent network.
    88  func (t *ReadOnlyTransferer) Upload(namespace string, d core.Digest, blob store.FileReader) error {
    89  	return errors.New("unsupported operation")
    90  }
    91  
    92  // GetTag gets manifest digest for tag.
    93  func (t *ReadOnlyTransferer) GetTag(tag string) (core.Digest, error) {
    94  	d, err := t.tags.Get(tag)
    95  	if err != nil {
    96  		if err == tagclient.ErrTagNotFound {
    97  			t.stats.Counter("tag_not_found").Inc(1)
    98  			return core.Digest{}, ErrTagNotFound
    99  		}
   100  		t.stats.Counter("get_tag_error").Inc(1)
   101  		return core.Digest{}, fmt.Errorf("client get tag: %s", err)
   102  	}
   103  	return d, nil
   104  }
   105  
   106  // PutTag is not supported.
   107  func (t *ReadOnlyTransferer) PutTag(tag string, d core.Digest) error {
   108  	return errors.New("not supported")
   109  }
   110  
   111  // ListTags is not supported.
   112  func (t *ReadOnlyTransferer) ListTags(prefix string) ([]string, error) {
   113  	return nil, errors.New("not supported")
   114  }