Skip to content

Commit

Permalink
change DimgHeader format
Browse files Browse the repository at this point in the history
Signed-off-by: Naoki MATSUMOTO <[email protected]>
  • Loading branch information
naoki9911 committed Mar 29, 2024
1 parent f2d5368 commit 8816972
Show file tree
Hide file tree
Showing 16 changed files with 378 additions and 127 deletions.
6 changes: 3 additions & 3 deletions cmd/ctr-cli/load/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ var Flags = []cli.Flag{
Required: true,
},
&cli.StringFlag{
Name: "dimg",
Usage: "path to dimg to be loaded",
Name: "cdimg",
Usage: "path to cdimg to be loaded",
Required: true,
},
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func Load(ctx context.Context, imgNameWithVersion, imgPath string) error {

func Action(c *cli.Context) error {
imgName := c.String("image")
imgPath := c.String("dimg")
imgPath := c.String("cdimg")
err := Load(context.TODO(), imgName, imgPath)
if err != nil {
return err
Expand Down
6 changes: 5 additions & 1 deletion cmd/ctr-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/naoki9911/fuse-diff-containerd/cmd/ctr-cli/pack"
"github.com/naoki9911/fuse-diff-containerd/cmd/ctr-cli/patch"
"github.com/naoki9911/fuse-diff-containerd/cmd/ctr-cli/pull"
"github.com/naoki9911/fuse-diff-containerd/cmd/ctr-cli/show"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -38,7 +39,6 @@ func NewApp() *cli.App {
pack.Command(),
load.Command(),
pull.Command(),
merge.Command(),
}

return app
Expand All @@ -52,6 +52,8 @@ func dimgCommand() *cli.Command {
Subcommands: []*cli.Command{
patch.DimgCommand(),
diff.DimgCommand(),
merge.DimgCommand(),
show.DimgCommand(),
},
}
return &cmd
Expand All @@ -65,6 +67,8 @@ func cdimgCommand() *cli.Command {
Subcommands: []*cli.Command{
patch.CdimgCommand(),
diff.CdimgCommand(),
merge.CdimgCommand(),
show.CdimgCommand(),
},
}
return &cmd
Expand Down
140 changes: 112 additions & 28 deletions cmd/ctr-cli/merge/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,42 @@ import (

var logger = log.G(context.TODO())

var (
Flags = []cli.Flag{
&cli.StringFlag{
Name: "lowerDimg",
Usage: "path to lower dimg",
Required: true,
},
&cli.StringFlag{
Name: "upperDimg",
Usage: "path to upper dimg",
Required: true,
},
&cli.StringFlag{
Name: "outDimg",
Usage: "path to merged dimg",
Required: true,
func DimgCommand() *cli.Command {
cmd := cli.Command{
Name: "merge",
Usage: "merge dimgs",
Action: func(context *cli.Context) error {
return dimgAction(context)
},
&cli.BoolFlag{
Name: "benchmark",
Usage: "enable benchmark",
Value: false,
Required: false,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "lowerDimg",
Usage: "path to lower dimg",
Required: true,
},
&cli.StringFlag{
Name: "upperDimg",
Usage: "path to upper dimg",
Required: true,
},
&cli.StringFlag{
Name: "outDimg",
Usage: "path to merged dimg",
Required: true,
},
&cli.BoolFlag{
Name: "benchmark",
Usage: "enable benchmark",
Value: false,
Required: false,
},
},
}
)

func Action(c *cli.Context) error {
return &cmd
}

func dimgAction(c *cli.Context) error {
logger.Logger.SetLevel(logrus.WarnLevel)
lowerDimg := c.String("lowerDimg")
upperDimg := c.String("upperDimg")
Expand Down Expand Up @@ -69,7 +78,7 @@ func Action(c *cli.Context) error {
panic(err)
}
defer mergeFile.Close()
err = image.MergeDimg(lowerDimg, upperDimg, mergeFile)
_, err = image.MergeDimg(lowerDimg, upperDimg, mergeFile)
if err != nil {
panic(err)
}
Expand All @@ -93,15 +102,90 @@ func Action(c *cli.Context) error {
return nil
}

func Command() *cli.Command {
func CdimgCommand() *cli.Command {
cmd := cli.Command{
Name: "merge",
Usage: "merge dimgs",
Usage: "merge cdimgs",
Action: func(context *cli.Context) error {
return Action(context)
return cdimgAction(context)
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "lowerCdimg",
Usage: "path to lower cdimg",
Required: true,
},
&cli.StringFlag{
Name: "upperCdimg",
Usage: "path to upper cdimg",
Required: true,
},
&cli.StringFlag{
Name: "outCdimg",
Usage: "path to merged cdimg",
Required: true,
},
&cli.BoolFlag{
Name: "benchmark",
Usage: "enable benchmark",
Value: false,
Required: false,
},
},
Flags: Flags,
}

return &cmd
}

func cdimgAction(c *cli.Context) error {
logger.Logger.SetLevel(logrus.WarnLevel)
lowerCdimg := c.String("lowerCdimg")
upperCdimg := c.String("upperCdimg")
outCdimg := c.String("outCdimg")
enableBench := c.Bool("benchmark")
logger.WithFields(logrus.Fields{
"lowerCdimg": lowerCdimg,
"upperCdimg": upperCdimg,
"outCdimg": outCdimg,
}).Info("starting to merge")

var b *benchmark.Benchmark = nil
var err error
if enableBench {
b, err = benchmark.NewBenchmark("./benchmark.log")
if err != nil {
return err
}
defer b.Close()
}

start := time.Now()

mergeFile, err := os.Create(outCdimg)
if err != nil {
panic(err)
}
defer mergeFile.Close()
err = image.MergeCdimg(lowerCdimg, upperCdimg, mergeFile)
if err != nil {
panic(err)
}

if b != nil {
metric := benchmark.Metric{
TaskName: "patch",
ElapsedMilli: int(time.Since(start).Milliseconds()),
Labels: []string{
"lowerCdimg:" + lowerCdimg,
"upperCdimg:" + upperCdimg,
"outCdimg:" + outCdimg,
},
}
err = b.AppendResult(metric)
if err != nil {
panic(err)
}
}
logger.Info("merge done")
return nil
}
4 changes: 2 additions & 2 deletions cmd/ctr-cli/patch/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func dimgAction(c *cli.Context) error {
dimgHeader := dimgFile.Header()

start := time.Now()
err = image.ApplyPatch(baseDir, outDir, &dimgHeader.FileEntry, dimgFile, dimgHeader.BaseId == "")
err = image.ApplyPatch(baseDir, outDir, &dimgHeader.FileEntry, dimgFile, dimgHeader.ParentId == "")
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -172,7 +172,7 @@ func cdimgAction(c *cli.Context) error {
dimgHeader := dimgFile.Header()

start := time.Now()
err = image.ApplyPatch(baseDir, outDir, &dimgHeader.FileEntry, dimgFile, dimgHeader.BaseId == "")
err = image.ApplyPatch(baseDir, outDir, &dimgHeader.FileEntry, dimgFile, dimgHeader.ParentId == "")
if err != nil {
panic(err)
}
Expand Down
87 changes: 87 additions & 0 deletions cmd/ctr-cli/show/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package show

import (
"context"
"fmt"

"github.com/containerd/containerd/log"
"github.com/naoki9911/fuse-diff-containerd/pkg/image"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)

var logger = log.G(context.TODO())

func DimgCommand() *cli.Command {
cmd := cli.Command{
Name: "show",
Usage: "show dimg info",
Action: func(context *cli.Context) error {
return dimgAction(context)
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "dimg",
Usage: "path to dimg",
Required: true,
},
},
}

return &cmd
}

func dimgAction(c *cli.Context) error {
logger.Logger.SetLevel(logrus.WarnLevel)
dimgPath := c.String("dimg")

dimgFile, err := image.OpenDimgFile(dimgPath)
if err != nil {
return err
}
defer dimgFile.Close()
header := dimgFile.Header()
fmt.Printf("ID: %s\n", header.Id)
fmt.Printf("ParentID: %s\n", header.ParentId)
return nil
}

func CdimgCommand() *cli.Command {
cmd := cli.Command{
Name: "show",
Usage: "show cdimg info",
Action: func(context *cli.Context) error {
return cdimgAction(context)
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "cdimg",
Usage: "path to cdimg",
Required: true,
},
},
}

return &cmd
}

func cdimgAction(c *cli.Context) error {
logger.Logger.SetLevel(logrus.WarnLevel)
cdimgPath := c.String("cdimg")
cdimgFile, err := image.OpenCdimgFile(cdimgPath)
if err != nil {
return err
}
defer cdimgFile.Close()
header := cdimgFile.Header
head := header.Head

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("DimgID: %s\n", cdimgFile.Dimg.Header().Id)
fmt.Printf("DimgParentID: %s\n", cdimgFile.Dimg.Header().ParentId)

return nil
}
Loading

0 comments on commit 8816972

Please sign in to comment.