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