github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/image/save/distributionpkg/proxy/proxymanifeststore.go (about)

     1  // Copyright © 2021 https://github.com/distribution/distribution
     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 proxy
    16  
    17  import (
    18  	"context"
    19  	"time"
    20  
    21  	"github.com/distribution/distribution/v3"
    22  	"github.com/distribution/distribution/v3/reference"
    23  	"github.com/distribution/distribution/v3/registry/proxy/scheduler"
    24  	"github.com/opencontainers/go-digest"
    25  )
    26  
    27  // todo(richardscothern): from cache control header or config
    28  const repositoryTTL = 24 * 7 * time.Hour
    29  
    30  type proxyManifestStore struct {
    31  	ctx             context.Context
    32  	localManifests  distribution.ManifestService
    33  	remoteManifests distribution.ManifestService
    34  	repositoryName  reference.Named
    35  	scheduler       *scheduler.TTLExpirationScheduler
    36  	authChallenger  authChallenger
    37  }
    38  
    39  var _ distribution.ManifestService = &proxyManifestStore{}
    40  
    41  func (pms proxyManifestStore) Exists(ctx context.Context, dgst digest.Digest) (bool, error) {
    42  	exists, err := pms.localManifests.Exists(ctx, dgst)
    43  	if err != nil {
    44  		return false, err
    45  	}
    46  	if exists {
    47  		return true, nil
    48  	}
    49  	if err := pms.authChallenger.tryEstablishChallenges(ctx); err != nil {
    50  		return false, err
    51  	}
    52  	return pms.remoteManifests.Exists(ctx, dgst)
    53  }
    54  
    55  func (pms proxyManifestStore) Get(ctx context.Context, dgst digest.Digest, options ...distribution.ManifestServiceOption) (distribution.Manifest, error) {
    56  	// At this point `dgst` was either specified explicitly, or returned by the
    57  	// tagstore with the most recent association.
    58  	var fromRemote bool
    59  	manifest, err := pms.localManifests.Get(ctx, dgst, options...)
    60  	if err != nil {
    61  		if err := pms.authChallenger.tryEstablishChallenges(ctx); err != nil {
    62  			return nil, err
    63  		}
    64  
    65  		manifest, err = pms.remoteManifests.Get(ctx, dgst, options...)
    66  		if err != nil {
    67  			return nil, err
    68  		}
    69  		fromRemote = true
    70  	}
    71  
    72  	_, payload, err := manifest.Payload()
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  
    77  	proxyMetrics.ManifestPush(uint64(len(payload)))
    78  	if fromRemote {
    79  		proxyMetrics.ManifestPull(uint64(len(payload)))
    80  
    81  		_, err = pms.localManifests.Put(ctx, manifest)
    82  		if err != nil {
    83  			return nil, err
    84  		}
    85  
    86  		// Schedule the manifest blob for removal
    87  		// repoBlob, err := reference.WithDigest(pms.repositoryName, dgst)
    88  		// if err != nil {
    89  		// 	dcontext.GetLogger(ctx).Errorf("Error creating reference: %s", err)
    90  		// 	return nil, err
    91  		// }
    92  
    93  		// pms.scheduler.AddManifest(repoBlob, repositoryTTL)
    94  		// Ensure the manifest blob is cleaned up
    95  		//pms.scheduler.AddBlob(blobRef, repositoryTTL)
    96  	}
    97  
    98  	return manifest, err
    99  }
   100  
   101  func (pms proxyManifestStore) Put(ctx context.Context, manifest distribution.Manifest, options ...distribution.ManifestServiceOption) (digest.Digest, error) {
   102  	var d digest.Digest
   103  	return d, distribution.ErrUnsupported
   104  }
   105  
   106  func (pms proxyManifestStore) Delete(ctx context.Context, dgst digest.Digest) error {
   107  	return distribution.ErrUnsupported
   108  }