Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
adohe committed Mar 23, 2024
1 parent f782b17 commit 1924671
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,4 @@ require (
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace kcl-lang.io/kpm => github.com/KusionStack/kpm v0.8.2
replace kcl-lang.io/kpm => github.com/KusionStack/kpm v0.8.3
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/KusionStack/kpm v0.8.2 h1:chwnnIlw2PWO0GvJfCRIeh/CFmZlYmd/F2YKaJecoh0=
github.com/KusionStack/kpm v0.8.2/go.mod h1:3atE1tEbsSPaAuKslkADH1HTDi7SMWlDWllmuk2XsBA=
github.com/KusionStack/kpm v0.8.3 h1:3C0Alj79Rp3Xn3QLwh0IBlS5in5XMmB5Oasx3wqvwig=
github.com/KusionStack/kpm v0.8.3/go.mod h1:3atE1tEbsSPaAuKslkADH1HTDi7SMWlDWllmuk2XsBA=
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/MarvinJWendt/testza v0.1.0/go.mod h1:7AxNvlfeHP7Z/hDQ5JtE3OKYT3XFUeLCDE2DQninSqs=
Expand Down
58 changes: 54 additions & 4 deletions pkg/cmd/generate/generator/generator.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package generator

import (
"fmt"
"path/filepath"
"runtime"

"gopkg.in/yaml.v2"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"kcl-lang.io/kpm/pkg/env"
pkg "kcl-lang.io/kpm/pkg/package"

v1 "kusionstack.io/kusion/pkg/apis/core/v1"
"kusionstack.io/kusion/pkg/backend"
"kusionstack.io/kusion/pkg/cmd/build/builders"
"kusionstack.io/kusion/pkg/cmd/generate/run"
"kusionstack.io/kusion/pkg/project"
"kusionstack.io/kusion/pkg/util/io"
"kusionstack.io/kusion/pkg/util/kfile"
)

const (
Expand All @@ -28,22 +37,28 @@ type DefaultGenerator struct {

// Generate versioned Spec with target code runner.
func (g *DefaultGenerator) Generate(workDir string, params map[string]string) (*v1.Intent, error) {
// Parse project and stack of work directory
proj, stack, err := project.DetectProjectAndStack(workDir)
// Parse project and currentStack of work directory
currentProject, currentStack, err := project.DetectProjectAndStack(workDir)
if err != nil {
return nil, err
}

// Call code runner to generate raw data
if params == nil {
params = make(map[string]string, 1)
}
params[IncludeSchemaTypePath] = "true"

rawAppConfiguration, err := g.Runner.Run(workDir, params)
if err != nil {
return nil, err
}

// Copy dependent modules before call builder
err = copyDependentModules(workDir)
if err != nil {
return nil, err
}

// Note: we use the type of MapSlice in yaml.v2 to maintain the order of container
// environment variables, thus we unmarshal appConfigs with yaml.v2 rather than yaml.v3.
apps := map[string]v1.AppConfiguration{}
Expand All @@ -66,6 +81,41 @@ func (g *DefaultGenerator) Generate(workDir string, params map[string]string) (*
Workspace: ws,
Apps: apps,
}
return builder.Build(nil, currentProject, currentStack)
}

// copyDependentModules copies dependent Kusion modules' generators to destination.
func copyDependentModules(workDir string) error {
modFile := &pkg.ModFile{}
err := modFile.LoadModFile(filepath.Join(workDir, pkg.MOD_FILE))
if err != nil {
return fmt.Errorf("load kcl.mod failed: %v", err)
}

absPkgPath, _ := env.GetAbsPkgPath()
kusionHomePath, _ := kfile.KusionDataFolder()

var allErrs []error
for _, dep := range modFile.Deps {
if dep.Source.Oci != nil {
info := dep.Source.Oci
pkgDir := filepath.Join(absPkgPath, dep.FullName)
platform := fmt.Sprintf("%s-%s", runtime.GOOS, runtime.GOARCH)
source := filepath.Join(pkgDir, "_dist", platform, "generator")
moduleDir := filepath.Join(kusionHomePath, "modules", info.Repo, info.Tag, runtime.GOOS, runtime.GOARCH)
dest := filepath.Join(moduleDir, fmt.Sprintf("kusion-module-%s", dep.FullName))
if runtime.GOOS == "windows" {
source = fmt.Sprintf("%s.exe", source)
dest = fmt.Sprintf("%s.exe", dest)
}
err = io.CopyFile(source, dest)
allErrs = append(allErrs, err)
}
}

if allErrs != nil {
return utilerrors.NewAggregate(allErrs)
}

return builder.Build(nil, proj, stack)
return nil
}
3 changes: 2 additions & 1 deletion pkg/cmd/generate/run/testdata/prod/kcl.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ version = "0.1.0"

[dependencies]
catalog = { path = "/Users/linkedin/Documents/go/src/github.com/kusionstack.io/catalog" }
opsrule = { oci://ghcr.io/kusionstack/opsrule, tag = "0.0.9" }
opsrule = { oci = "oci://ghcr.io/kusionstack/opsrule", tag = "0.0.9" }

[profile]
entries = ["../base/base.k", "main.k"]

35 changes: 35 additions & 0 deletions pkg/util/io/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package io

import (
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -69,3 +71,36 @@ func RenamePath(oldPath, newPath string) error {

return nil
}

// CopyFile copies the file at source to dest
func CopyFile(source, dest string) error {
sf, err := os.Open(source)
if err != nil {
return fmt.Errorf("unable to open source file [%s]: %q", source, err)
}
defer sf.Close()
fi, err := sf.Stat()
if err != nil {
return fmt.Errorf("unable to stat source file [%s]: %q", source, err)
}

dir := filepath.Dir(dest)
if err := os.MkdirAll(dir, 0755); err != nil {

Check failure on line 88 in pkg/util/io/file.go

View workflow job for this annotation

GitHub Actions / Golang Lint

File is not `gofumpt`-ed (gofumpt)
return fmt.Errorf("unable to create directory [%s]: %q", dir, err)
}
df, err := os.Create(dest)
if err != nil {
return fmt.Errorf("unable to create destination file [%s]: %q", dest, err)
}
defer df.Close()

_, err = io.Copy(df, sf)
if err != nil {
return fmt.Errorf("unable to copy [%s] to [%s]: %q", source, dest, err)
}

if err := os.Chmod(dest, fi.Mode()); err != nil {
return fmt.Errorf("unable to close destination file: %q", err)
}
return nil
}

0 comments on commit 1924671

Please sign in to comment.