Skip to content

Commit

Permalink
nydusify: add new option output-json
Browse files Browse the repository at this point in the history
During convert, we can collect the metric: image size and convert time, etc.
Nydusify can dump the metric to local file if user needs.

Signed-off-by: Yadong Ding <[email protected]>
  • Loading branch information
Desiki-high committed May 12, 2023
1 parent c583d23 commit 34ccdfc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
8 changes: 8 additions & 0 deletions contrib/nydusify/cmd/nydusify.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,12 @@ func main() {
Usage: "Path to the nydus-image binary, default to search in PATH",
EnvVars: []string{"NYDUS_IMAGE"},
},
&cli.BoolFlag{
Name: "output-json",
Value: false,
Usage: "Dump the metric colledted in convert",
EnvVars: []string{"OUTPUT_JSON"},
},
},
Action: func(c *cli.Context) error {
setupLogLevel(c)
Expand Down Expand Up @@ -503,6 +509,8 @@ func main() {
OCIRef: c.Bool("oci-ref"),
AllPlatforms: c.Bool("all-platforms"),
Platforms: c.String("platform"),

OutputJson: c.Bool("output-json"),
}

return converter.Convert(context.Background(), opt)
Expand Down
7 changes: 6 additions & 1 deletion contrib/nydusify/pkg/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type Opt struct {

AllPlatforms bool
Platforms string

OutputJson bool
}

func Convert(ctx context.Context, opt Opt) error {
Expand Down Expand Up @@ -87,6 +89,9 @@ func Convert(ctx context.Context, opt Opt) error {
return err
}

_, err = cvt.Convert(ctx, opt.Source, opt.Target)
metric, err := cvt.Convert(ctx, opt.Source, opt.Target)
if opt.OutputJson {
saveMetric(metric)
}
return err
}
29 changes: 29 additions & 0 deletions contrib/nydusify/pkg/converter/metric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2023 Nydus Developers. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0

package converter

import (
"encoding/json"
"os"

"github.com/goharbor/acceleration-service/pkg/converter"
"github.com/pkg/errors"
)

func saveMetric(metric *converter.Metric) error {

file, err := os.Create("output.json")
if err != nil {
return errors.Wrap(err, "Error creating file")
}
defer file.Close()

encoder := json.NewEncoder(file)
err = encoder.Encode(metric)
if err != nil {
return errors.Wrap(err, "Error encoding JSON")
}
return nil
}

0 comments on commit 34ccdfc

Please sign in to comment.