github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/image/store/distribution.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 store 16 17 import ( 18 "encoding/json" 19 "os" 20 "path/filepath" 21 22 "github.com/alibaba/sealer/logger" 23 24 "github.com/opencontainers/go-digest" 25 26 "github.com/alibaba/sealer/utils" 27 ) 28 29 type DistributionMetadataItem struct { 30 SourceRepository string `json:"source_repository"` 31 LayerContentDigest digest.Digest `json:"layer_content_digest"` 32 } 33 34 // DistributionMetadata is the data from {layerdb}/distribution_layer_digest 35 // which indicate that digest of compressedlayerStream in specific registry and repository 36 type DistributionMetadata []DistributionMetadataItem 37 38 func (fs *filesystem) LoadDistributionMetadata(layerID LayerID) (map[string]digest.Digest, error) { 39 var ( 40 layerDBPath = fs.LayerDBDir(layerID.ToDigest()) 41 metadatas = DistributionMetadata{} 42 res = map[string]digest.Digest{} 43 ) 44 distributionMetadataFile, err := os.Open(filepath.Clean(filepath.Join(layerDBPath, "distribution_layer_digest"))) 45 if err != nil { 46 //lint:ignore nilerr https://github.com/alibaba/sealer/issues/610 47 return res, nil // ignore 48 } 49 defer func() { 50 if err := distributionMetadataFile.Close(); err != nil { 51 logger.Fatal("failed to close file") 52 } 53 }() 54 err = json.NewDecoder(distributionMetadataFile).Decode(&metadatas) 55 if err != nil { 56 return res, err 57 } 58 59 for _, item := range metadatas { 60 res[item.SourceRepository] = item.LayerContentDigest 61 } 62 63 return res, nil 64 } 65 66 func (fs *filesystem) addDistributionMetadata(layerID LayerID, newMetadatas map[string]digest.Digest) error { 67 // load from distribution_layer_digest 68 metadataMap, err := fs.LoadDistributionMetadata(layerID) 69 if err != nil { 70 return err 71 } 72 // override metadata items, and add new metadata 73 for key, value := range newMetadatas { 74 metadataMap[key] = value 75 } 76 77 distributionMetadatas := DistributionMetadata{} 78 for key, value := range metadataMap { 79 distributionMetadatas = append(distributionMetadatas, DistributionMetadataItem{ 80 SourceRepository: key, 81 LayerContentDigest: value, 82 }) 83 } 84 85 distributionMetadatasJSON, err := json.Marshal(&distributionMetadatas) 86 if err != nil { 87 return err 88 } 89 90 return utils.WriteFile(filepath.Join(fs.LayerDBDir(layerID.ToDigest()), "distribution_layer_digest"), distributionMetadatasJSON) 91 }