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

     1  // Copyright © 2022 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  	"github.com/alibaba/sealer/pkg/image/types"
    19  	v1 "github.com/alibaba/sealer/types/api/v1"
    20  	distribution "github.com/distribution/distribution/v3"
    21  	"github.com/distribution/distribution/v3/manifest/manifestlist"
    22  	"github.com/opencontainers/go-digest"
    23  	"github.com/pkg/errors"
    24  )
    25  
    26  // PlatformSpecFromOCI creates a platform spec from OCI platform
    27  func PlatformSpecFromOCI(p *v1.Platform) *manifestlist.PlatformSpec {
    28  	if p == nil {
    29  		return nil
    30  	}
    31  	return &manifestlist.PlatformSpec{
    32  		Architecture: p.Architecture,
    33  		OS:           p.OS,
    34  		OSVersion:    p.OSVersion,
    35  		Variant:      p.Variant,
    36  	}
    37  }
    38  
    39  func buildManifestDescriptor(dgst digest.Digest, imageManifest *types.ManifestDescriptor) (manifestlist.ManifestDescriptor, error) {
    40  	manifest := manifestlist.ManifestDescriptor{
    41  		Descriptor: distribution.Descriptor{
    42  			Digest:    dgst,
    43  			Size:      imageManifest.SIZE,
    44  			MediaType: manifestV2,
    45  		},
    46  	}
    47  
    48  	platform := PlatformSpecFromOCI(&imageManifest.Platform)
    49  	if platform != nil {
    50  		manifest.Platform = *platform
    51  	}
    52  
    53  	if err := manifest.Descriptor.Digest.Validate(); err != nil {
    54  		return manifestlist.ManifestDescriptor{}, errors.Wrap(err, "digest parse of image  failed")
    55  	}
    56  
    57  	return manifest, nil
    58  }