Skip to content

Commit

Permalink
build: fill up image platform (#1096)
Browse files Browse the repository at this point in the history
  • Loading branch information
kakaZhou719 authored Jan 17, 2022
1 parent 176ab99 commit 2357ebd
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 29 deletions.
89 changes: 62 additions & 27 deletions build/buildkit/buildimage/build_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@ package buildimage
import (
"context"
"fmt"
"path/filepath"

v2 "github.com/alibaba/sealer/types/api/v2"
"github.com/alibaba/sealer/pkg/runtime"
"golang.org/x/sync/errgroup"

"github.com/pkg/errors"
"sigs.k8s.io/yaml"

"github.com/alibaba/sealer/build/buildkit/buildinstruction"
"github.com/alibaba/sealer/common"
"github.com/alibaba/sealer/logger"
"github.com/alibaba/sealer/pkg/image"
"github.com/alibaba/sealer/pkg/image/store"
v1 "github.com/alibaba/sealer/types/api/v1"
"github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -144,23 +143,12 @@ func (b BuildImage) SaveBuildImage(name string, opts SaveOpts) error {
return err
}

cluster, err := b.getImageCluster()
if err != nil {
return err
}
cluster.Spec.Image = name
err = setClusterFileToImage(cluster, b.RawImage)
if err != nil {
return fmt.Errorf("failed to set image metadata, err: %v", err)
}

layers, err := b.collectLayers(opts)
if err != nil {
return err
}

b.RawImage.Spec.Layers = layers

err = b.save(name)
if err != nil {
return fmt.Errorf("failed to save image metadata, err: %v", err)
Expand Down Expand Up @@ -203,27 +191,27 @@ func (b BuildImage) collectRootfsDiff() (v1.Layer, error) {
return layer, nil
}

func (b BuildImage) getImageCluster() (*v2.Cluster, error) {
var cluster v2.Cluster
rawClusterFile, err := GetRawClusterFile(b.RawImage.Spec.Layers[0].Value, b.NewLayers)
func (b BuildImage) save(name string) error {
mi, err := GetLayerMountInfo(b.RawImage.Spec.Layers, b.BuildType)
if err != nil {
return nil, fmt.Errorf("failed to get base image err: %s", err)
return err
}
defer mi.CleanUp()

if err := yaml.Unmarshal([]byte(rawClusterFile), &cluster); err != nil {
return nil, err
rootfsPath := mi.GetMountTarget()
b.RawImage.Name = name
is := []ImageSetter{NewAnnotationSetter(rootfsPath), NewPlatformSetter(rootfsPath)}
for _, s := range is {
if err = s.Set(b.RawImage); err != nil {
return err
}
}

return &cluster, nil
}

func (b BuildImage) save(name string) error {
imageID, err := generateImageID(*b.RawImage)
if err != nil {
return err
}
b.RawImage.Spec.ID = imageID
b.RawImage.Name = name

return b.ImageStore.Save(*b.RawImage, name)
}

Expand Down Expand Up @@ -294,3 +282,50 @@ func NewBuildImage(kubefileName string, buildType string) (Interface, error) {
RootfsMountInfo: mountInfo,
}, nil
}

type annotation struct {
source string
}

func (a annotation) Set(ima *v1.Image) error {
return a.setClusterFile(ima)
}

func (a annotation) setClusterFile(ima *v1.Image) error {
cluster, err := LoadClusterFile(filepath.Join(a.source, "etc", common.DefaultClusterFileName))
if err != nil {
return fmt.Errorf("failed to load clusterfile, err: %v", err)
}
cluster.Spec.Image = ima.Name
err = setClusterFileToImage(cluster, ima)
if err != nil {
return fmt.Errorf("failed to set image metadata, err: %v", err)
}
return nil
}

func NewAnnotationSetter(rootfs string) ImageSetter {
return annotation{
source: rootfs,
}
}

type platform struct {
source string
}

func (p platform) Set(ima *v1.Image) error {
plat := runtime.GetCloudImagePlatform(p.source)
ima.Spec.Platform = v1.Platform{
Architecture: plat.Architecture,
OS: plat.OS,
OSVersion: plat.OSVersion,
Variant: plat.Variant,
}
return nil
}
func NewPlatformSetter(rootfs string) ImageSetter {
return platform{
source: rootfs,
}
}
6 changes: 6 additions & 0 deletions build/buildkit/buildimage/image_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package buildimage

import (
"github.com/alibaba/sealer/build/buildkit/buildinstruction"
v1 "github.com/alibaba/sealer/types/api/v1"
)

type Interface interface {
Expand All @@ -29,3 +30,8 @@ type Differ interface {
//diff Metadata file changes save to the base layer.generally dst is the rootfs.
Process(src, dst buildinstruction.MountTarget) error
}

type ImageSetter interface {
// Set :fill up v1.image struct, like image annotations, platform and so on.
Set(*v1.Image) error
}
17 changes: 17 additions & 0 deletions build/buildkit/buildimage/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ func InitImageSpec(kubefile string) (*v1.Image, error) {
return rawImage, nil
}

func LoadClusterFile(path string) (*v2.Cluster, error) {
var cluster v2.Cluster
rawClusterFile, err := ioutil.ReadFile(filepath.Clean(path))
if err != nil {
return nil, err
}
if len(rawClusterFile) == 0 {
return nil, fmt.Errorf("ClusterFile content is empty")
}

if err = yaml.Unmarshal(rawClusterFile, &cluster); err != nil {
return nil, err
}

return &cluster, nil
}

func setClusterFileToImage(cluster *v2.Cluster, image *v1.Image) error {
clusterData, err := yaml.Marshal(cluster)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion pkg/runtime/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,15 @@ func GetCloudImagePlatform(rootfs string) (cp ocispecs.Platform) {
Architecture: "amd64",
OS: "linux",
Variant: "",
OSVersion: "",
}
meta, err := LoadMetadata(rootfs)
if err != nil {
return
}

if meta == nil {
return
}
if meta.Arch != "" {
cp.Architecture = meta.Arch
}
Expand Down
6 changes: 5 additions & 1 deletion types/api/v1/image_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ type ImageList struct {
type Platform struct {
Architecture string `json:"architecture,omitempty"`
OS string `json:"os,omitempty"`
OSVersion string `json:"os_version,omitempty"`
// OSVersion is an optional field specifying the operating system version.
OSVersion string `json:"os_version,omitempty"`
// Variant is an optional field specifying a variant of the CPU, for
// example `v7` to specify ARMv7 when architecture is `arm`.
Variant string `json:"variant,omitempty"`
}

func init() {
Expand Down

0 comments on commit 2357ebd

Please sign in to comment.