Skip to content

Commit 17951a6

Browse files
committed
feat: support nydusify for build and push
Signed-off-by: chlins <[email protected]>
1 parent 264624f commit 17951a6

File tree

7 files changed

+76
-0
lines changed

7 files changed

+76
-0
lines changed

cmd/build.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ func init() {
5555
flags.BoolVarP(&buildConfig.OutputRemote, "output-remote", "", false, "turning on this flag will output model artifact to remote registry directly")
5656
flags.BoolVarP(&buildConfig.PlainHTTP, "plain-http", "", false, "turning on this flag will use plain HTTP instead of HTTPS")
5757
flags.BoolVarP(&buildConfig.Insecure, "insecure", "", false, "turning on this flag will disable TLS verification")
58+
flags.BoolVar(&buildConfig.Nydusify, "nydusify", false, "[EXPERIMENTAL] nydusify the model artifact")
59+
flags.MarkHidden("nydusify")
5860

5961
if err := viper.BindPFlags(flags); err != nil {
6062
panic(fmt.Errorf("bind cache list flags to viper: %w", err))

cmd/push.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ func init() {
5151
flags := pushCmd.Flags()
5252
flags.IntVar(&pushConfig.Concurrency, "concurrency", pushConfig.Concurrency, "specify the number of concurrent push operations")
5353
flags.BoolVar(&pushConfig.PlainHTTP, "plain-http", false, "use plain HTTP instead of HTTPS")
54+
flags.BoolVar(&pushConfig.Nydusify, "nydusify", false, "[EXPERIMENTAL] nydusify the model artifact")
55+
flags.MarkHidden("nydusify")
5456

5557
if err := viper.BindPFlags(flags); err != nil {
5658
panic(fmt.Errorf("bind cache push flags to viper: %w", err))

pkg/backend/build.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ func (b *backend) Build(ctx context.Context, modelfilePath, workDir, target stri
109109
return fmt.Errorf("failed to build image manifest: %w", err)
110110
}
111111

112+
// nydusify the model artifact if needed.
113+
if cfg.Nydusify {
114+
if err := Nydusify(ctx, target); err != nil {
115+
return fmt.Errorf("failed to nydusify %s: %w", target, err)
116+
}
117+
}
118+
112119
return nil
113120
}
114121

pkg/backend/nydusify.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2025 The CNAI Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package backend
18+
19+
import (
20+
"context"
21+
"os/exec"
22+
)
23+
24+
const (
25+
// NydusImageTagSuffix is the suffix for the Nydus image tag.
26+
NydusImageTagSuffix = "_nydus_v2"
27+
)
28+
29+
// Nydusify is a function that converts a given image to a Nydus image.
30+
func Nydusify(ctx context.Context, source string) error {
31+
cmd := exec.CommandContext(
32+
ctx,
33+
"nydusify",
34+
"convert",
35+
"--source-backend-type",
36+
"model-artifact",
37+
"--compressor",
38+
"lz4_block",
39+
"--fs-version",
40+
"5",
41+
"--source",
42+
source,
43+
"--target",
44+
source+NydusImageTagSuffix,
45+
)
46+
47+
return cmd.Run()
48+
}

pkg/backend/push.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ func (b *backend) Push(ctx context.Context, target string, cfg *config.Push) err
118118
return fmt.Errorf("failed to push manifest to remote: %w", err)
119119
}
120120

121+
// nydusify the model artifact if needed.
122+
if cfg.Nydusify {
123+
if err := Nydusify(ctx, target); err != nil {
124+
return fmt.Errorf("failed to nydusify %s: %w", target, err)
125+
}
126+
}
127+
121128
return nil
122129
}
123130

pkg/config/build.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Build struct {
3030
OutputRemote bool
3131
PlainHTTP bool
3232
Insecure bool
33+
Nydusify bool
3334
}
3435

3536
func NewBuild() *Build {
@@ -40,6 +41,7 @@ func NewBuild() *Build {
4041
OutputRemote: false,
4142
PlainHTTP: false,
4243
Insecure: false,
44+
Nydusify: false,
4345
}
4446
}
4547

@@ -56,5 +58,11 @@ func (b *Build) Validate() error {
5658
return fmt.Errorf("model file path is required")
5759
}
5860

61+
if b.Nydusify {
62+
if !b.OutputRemote {
63+
return fmt.Errorf("nydusify only works with output remote")
64+
}
65+
}
66+
5967
return nil
6068
}

pkg/config/push.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ const (
2626
type Push struct {
2727
Concurrency int
2828
PlainHTTP bool
29+
Nydusify bool
2930
}
3031

3132
func NewPush() *Push {
3233
return &Push{
3334
Concurrency: defaultPushConcurrency,
3435
PlainHTTP: false,
36+
Nydusify: false,
3537
}
3638
}
3739

0 commit comments

Comments
 (0)