Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: automatically dowanload kusion modules in the mod add #1150

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions pkg/cmd/mod/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package mod
import (
"fmt"
"net/url"
"runtime"
"strings"

"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericiooptions"
"k8s.io/kubectl/pkg/util/templates"
"kcl-lang.io/kpm/pkg/api"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/downloader"
pkg "kcl-lang.io/kpm/pkg/package"

"kusionstack.io/kusion/pkg/cmd/meta"
Expand Down Expand Up @@ -64,13 +66,20 @@ func (o *AddOptions) Run() error {
if stack == nil {
return fmt.Errorf("cannot find stack with empty name")
}
kclPkg, err := api.GetKclPackage(stack.Path)

cli, err := client.NewKpmClient()
if err != nil {
return err
}
cli.DepDownloader = downloader.NewOciDownloader(runtime.GOOS + "/" + runtime.GOARCH)

kclPkg, err := cli.LoadPkgFromPath(stack.Path)
if err != nil {
return err
}
dependencies := kclPkg.GetDependenciesInModFile()
dependencies := kclPkg.ModFile.Dependencies.Deps
if dependencies == nil {
dependencies = &pkg.Dependencies{Deps: make(map[string]pkg.Dependency)}
dependencies = make(map[string]pkg.Dependency)
}

// path example: oci://ghcr.io/kusionstack/service
Expand All @@ -83,7 +92,7 @@ func (o *AddOptions) Run() error {
return fmt.Errorf("invalid module path: %s", m.Path)
}

dependencies.Deps[o.ModuleName] = pkg.Dependency{
dependencies[o.ModuleName] = pkg.Dependency{
Name: o.ModuleName,
FullName: o.ModuleName + "_" + m.Version,
Version: m.Version,
Expand All @@ -96,11 +105,12 @@ func (o *AddOptions) Run() error {
},
}

// Save the dependencies to the kcl.mod file
err = kclPkg.StoreModFile()
// update kcl.mod and download dependencies
err = cli.UpdateDeps(kclPkg)
if err != nil {
return err
}

return nil
}

Expand Down
14 changes: 1 addition & 13 deletions pkg/cmd/mod/add_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package mod

import (
"math/rand"
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/cli-runtime/pkg/genericiooptions"
"kcl-lang.io/kpm/pkg/api"

v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
"kusionstack.io/kusion/pkg/cmd/meta"
Expand Down Expand Up @@ -60,12 +58,6 @@ func TestAddOptions_Run(t *testing.T) {
})

t.Run("ValidModuleAddition", func(t *testing.T) {
b := make([]byte, 4)
letter := "abcdefghijklmnopqrstuvwxyz"
for i := range b {
b[i] = letter[rand.Intn(len(letter))]
}

o := &AddOptions{
MetaOptions: meta.MetaOptions{
RefStack: &v1.Stack{
Expand All @@ -76,7 +68,7 @@ func TestAddOptions_Run(t *testing.T) {
Modules: v1.ModuleConfigs{
"service": {
Path: "oci://ghcr.io/kusionstack/service",
Version: string(b),
Version: "0.1.0",
},
},
},
Expand All @@ -86,9 +78,5 @@ func TestAddOptions_Run(t *testing.T) {
}
err := o.Run()
assert.NoError(t, err)
kclPkg, err := api.GetKclPackage(o.RefStack.Path)
assert.NoError(t, err)
file := kclPkg.GetDependenciesInModFile()
assert.Equal(t, file.Deps["service"].Version, string(b))
})
}
1 change: 1 addition & 0 deletions pkg/engine/api/generate/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func CopyDependentModules(workDir string) error {
dest = fmt.Sprintf("%s.exe", dest)
}
// copy the module binary to the $KUSION_HOME modules directory
// todo: replace with symlink
err = io.CopyFile(source, dest)
if err == nil {
// mark the dest file as executable
Expand Down
Loading