github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/image/distributionutil/imagemanifest.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 distributionutil 16 17 import ( 18 "encoding/json" 19 "fmt" 20 21 "github.com/alibaba/sealer/logger" 22 v1 "github.com/alibaba/sealer/types/api/v1" 23 platUtil "github.com/alibaba/sealer/utils/platform" 24 "github.com/opencontainers/go-digest" 25 ) 26 27 // ManifestList this package unmarshal manifests from json into a ManifestList struct 28 //then choose corresponding manifest by platform 29 type ManifestList struct { 30 List []ImageManifest `json:"manifests"` 31 MediaType string `json:"mediaType"` 32 Schema int `json:"schemaVersion"` 33 } 34 35 type ImageManifest struct { 36 Digest string `json:"digest"` 37 MediaType string `json:"mediaType"` 38 Platform v1.Platform 39 Size int 40 } 41 42 func GetImageManifestDigest(payload []byte, plat v1.Platform) (digest.Digest, error) { 43 var ( 44 manifestList ManifestList 45 ) 46 47 err := json.Unmarshal(payload, &manifestList) 48 if err != nil { 49 return "", fmt.Errorf("json unmarshal error: %v", err) 50 } 51 52 var resDigest []digest.Digest 53 for _, item := range manifestList.List { 54 if platUtil.Matched(item.Platform, plat) { 55 resDigest = append(resDigest, digest.Digest(item.Digest)) 56 } 57 } 58 59 if len(resDigest) == 0 { 60 return "", fmt.Errorf("no manifest of the corresponding platform") 61 } 62 63 if len(resDigest) > 1 { 64 logger.Warn("multiple matches in manifest list") 65 } 66 return resDigest[0], nil 67 }