Skip to content

Commit

Permalink
introduce new DiffServer
Browse files Browse the repository at this point in the history
Signed-off-by: Naoki MATSUMOTO <[email protected]>
  • Loading branch information
naoki9911 committed Apr 3, 2024
1 parent 11c8d6e commit 0e91c57
Show file tree
Hide file tree
Showing 17 changed files with 535 additions and 539 deletions.
9 changes: 8 additions & 1 deletion cmd/ctr-cli/load/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package load
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -72,6 +73,12 @@ func LoadImage(snClient *sns.Client, ctx context.Context, imageName, imageVersio
}
log.G(ctx).Debug("load config done")

manifest := v1.Manifest{}
err = json.Unmarshal(imageHeader.ManifestBytes, &manifest)
if err != nil {
return err
}

// register image
is := snClient.CtrClient.ImageService()
_, err = is.Create(ctx, images.Image{
Expand All @@ -94,7 +101,7 @@ func LoadImage(snClient *sns.Client, ctx context.Context, imageName, imageVersio
}

// now ready to create snapshot
err = sns.CreateSnapshot(ctx, snClient.SnClient, &imageHeader.Head.ManifestDigest, &imageHeader.DimgDigest, imageName+":"+imageVersion, dimgPath)
err = sns.CreateSnapshot(ctx, snClient.SnClient, imageHeader.Head.ManifestDigest, manifest.Layers[0].Digest, imageName+":"+imageVersion, dimgPath)
if err != nil {
return err
}
Expand Down
15 changes: 4 additions & 11 deletions cmd/ctr-cli/pack/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ var logger = log.G(context.TODO())

var (
Flags = []cli.Flag{
&cli.StringFlag{
Name: "manifest",
Usage: "path to manifest file",
Required: true,
},
&cli.StringFlag{
Name: "config",
Usage: "path to config file",
Expand All @@ -30,23 +25,21 @@ var (
},
&cli.StringFlag{
Name: "out",
Usage: "output file name",
Usage: "output cdimg name",
Required: true,
},
}
)

func Action(c *cli.Context) error {
logger.Logger.SetLevel(logrus.DebugLevel)
manifestPath := c.String("manifest")
configPath := c.String("config")
dimgPath := c.String("dimg")
outPath := c.String("out")
logger.WithFields(logrus.Fields{
"manifestPath": manifestPath,
"configPath": configPath,
"dimg": dimgPath,
"outPath": outPath,
"configPath": configPath,
"dimg": dimgPath,
"outPath": outPath,
}).Info("starting to pack")

err := image.PackCdimg(configPath, dimgPath, outPath)
Expand Down
60 changes: 36 additions & 24 deletions cmd/ctr-cli/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import (
"github.com/naoki9911/fuse-diff-containerd/cmd/ctr-cli/load"
"github.com/naoki9911/fuse-diff-containerd/pkg/benchmark"
"github.com/naoki9911/fuse-diff-containerd/pkg/image"
"github.com/naoki9911/fuse-diff-containerd/pkg/server"
sns "github.com/naoki9911/fuse-diff-containerd/pkg/snapshotter"
"github.com/naoki9911/fuse-diff-containerd/pkg/update"
"github.com/naoki9911/fuse-diff-containerd/pkg/utils"
"github.com/opencontainers/go-digest"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -107,7 +109,9 @@ func pullImage(c *cli.Context, host string, imageNameWithVersion string, bench b
return err
}

localImages := make([]update.Image, 0)
contentStore := snClient.CtrClient.ContentStore()

localDimgs := make([]digest.Digest, 0)
for _, img := range images {
targetSns, ok := img.Labels[sns.TargetSnapshotLabel]
if !ok {
Expand All @@ -116,29 +120,37 @@ func pullImage(c *cli.Context, host string, imageNameWithVersion string, bench b
if targetSns != "di3fs" {
continue
}
localImgName := img.Labels[sns.SnapshotLabelImageName]
localImgVersion := img.Labels[sns.SnapshotLabelImageVersion]
manReader, err := contentStore.ReaderAt(context.TODO(), img.Target)
if err != nil {
logger.Errorf("failed to reade target %s from content store: %v", img.Target.Digest, err)
continue
}
defer manReader.Close()

// if the requested image exists local, nothing to do.
if localImgName == reqImgName && localImgVersion == reqImgVersion {
logger.Infof("%s is already pulled", imageNameWithVersion)
return nil
manifestBytes := make([]byte, manReader.Size())
_, err = manReader.ReadAt(manifestBytes, 0)
if err != nil {
logger.Errorf("failed to ReadAll from manifest reader: %v", err)
continue
}

localImg := update.Image{
Name: localImgName,
Version: localImgVersion,
manifest := v1.Manifest{}
err = json.Unmarshal(manifestBytes, &manifest)
if err != nil {
logger.Errorf("failed to unmarshal manifest: %v", err)
continue
}
localImages = append(localImages, localImg)

localDimgs = append(localDimgs, manifest.Layers[0].Digest)
}
logger.WithField("localImages", localImages).Debug("local images collected")
logger.WithField("localDimgs", localDimgs).Debug("local images collected")

reqBody := update.UpdateDataRequest{
RequestImage: update.Image{
reqBody := server.UpdateDataRequest{
RequestImage: server.ImageTag{
Name: reqImgName,
Version: reqImgVersion,
},
LocalImages: localImages,
LocalDimgs: localDimgs,
}

reqBodyBytes, err := json.Marshal(reqBody)
Expand Down Expand Up @@ -173,12 +185,12 @@ func pullImage(c *cli.Context, host string, imageNameWithVersion string, bench b
if resJsonLength != int(readSize) {
return fmt.Errorf("invalid length response expected=%d actual=%d", resJsonLength, readSize)
}
var resJson update.UpdateDataResponse
var resJson server.UpdateDataResponse
err = json.Unmarshal(resJsonBytes, &resJson)
if err != nil {
return err
}
logger.Infof("recieved response imageName=%s Version=%s baseVersion=%s", resJson.Name, resJson.Version, resJson.BaseVersion)
logger.Infof("recieved response imageName=%s Version=%s", resJson.Name, resJson.Version)

header, _, err := image.LoadCdimgHeader(resp.Body)
if err != nil {
Expand All @@ -205,9 +217,9 @@ func pullImage(c *cli.Context, host string, imageNameWithVersion string, bench b
TaskName: "pull-download",
ElapsedMilli: int(time.Since(start).Milliseconds()),
Labels: map[string]string{
"imageName": resJson.Name,
"version": resJson.Version,
"baseVersion": resJson.BaseVersion,
"imageName": resJson.Name,
"version": resJson.Version,
//"baseVersion": resJson.BaseVersion,
},
}
metricDownload.AddLabels(utils.ParseLabels(c.StringSlice("labels")))
Expand All @@ -226,9 +238,9 @@ func pullImage(c *cli.Context, host string, imageNameWithVersion string, bench b
TaskName: "pull",
ElapsedMilli: int(time.Since(start).Milliseconds()),
Labels: map[string]string{
"imageName": resJson.Name,
"version": resJson.Version,
"baseVersion": resJson.BaseVersion,
"imageName": resJson.Name,
"version": resJson.Version,
//"baseVersion": resJson.BaseVersion,
},
}
metricDownload.AddLabels(utils.ParseLabels(c.StringSlice("labels")))
Expand Down
2 changes: 1 addition & 1 deletion cmd/ctr-cli/show/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func cdimgAction(c *cli.Context) error {
fmt.Printf("Manifest Digest: %s\n", head.ManifestDigest)
fmt.Printf("Manifest: %v\n", header.Manifest)
fmt.Printf("Config: %v\n", header.Config)
fmt.Printf("DimgDigest: %s\n", header.DimgDigest)
fmt.Printf("DimgDigest: %s\n", header.Head.DimgDigest)
fmt.Printf("DimgID: %s\n", cdimgFile.Dimg.Header().Id)
fmt.Printf("DimgParentID: %s\n", cdimgFile.Dimg.Header().ParentId)

Expand Down
Loading

0 comments on commit 0e91c57

Please sign in to comment.