github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/image/cache/cache.go (about)

     1  // Copyright © 2021 Alibaba Group Holding Ltd.
     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 cache
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/opencontainers/go-digest"
    21  	"github.com/pkg/errors"
    22  
    23  	"github.com/alibaba/sealer/logger"
    24  	v1 "github.com/alibaba/sealer/types/api/v1"
    25  )
    26  
    27  //LocalImageCache saves all the layer
    28  type LocalImageCache struct {
    29  	chainStore ChainStore
    30  }
    31  
    32  type NopImageCache struct {
    33  }
    34  
    35  func NewLocalImageCache(chainStore ChainStore) (*LocalImageCache, error) {
    36  	return &LocalImageCache{
    37  		chainStore: chainStore,
    38  	}, nil
    39  }
    40  
    41  func (NopImageCache) GetCache(parentID string, layer *Layer) (layerID digest.Digest, err error) {
    42  	return "", errors.Errorf("nop cache")
    43  }
    44  
    45  func (lic *LocalImageCache) GetCache(parentID string, layer *Layer) (layerID digest.Digest, err error) {
    46  	curChainID, err := layer.ChainID(ChainID(parentID))
    47  	if err != nil {
    48  		return "", fmt.Errorf("failed to get cur chain id, err: %s", err)
    49  	}
    50  	logger.Debug("current layer %+v, chain id %s", layer, curChainID)
    51  
    52  	tmpLayer, err := getLocalCachedImage(lic.chainStore, curChainID)
    53  	if err != nil {
    54  		return "", err
    55  	}
    56  
    57  	return tmpLayer.ID, nil
    58  }
    59  
    60  func getLocalCachedImage(imageChain ChainStore, layerChainID ChainID) (v1.Layer, error) {
    61  	return imageChain.GetChainLayer(layerChainID)
    62  }