Skip to content

Commit

Permalink
Merge pull request #244 from zong-zhe/add-git-commit
Browse files Browse the repository at this point in the history
feat: 'kpm add' supports adding the dependency tagged by git commit
  • Loading branch information
Peefy authored Jan 10, 2024
2 parents b4e4f7f + 8a23512 commit 48ada7b
Show file tree
Hide file tree
Showing 56 changed files with 906 additions and 45 deletions.
31 changes: 21 additions & 10 deletions pkg/api/kpm_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,25 @@ func TestRunPkgWithOpts(t *testing.T) {
}

func TestRunWithOptsAndNoSumCheck(t *testing.T) {
pkgPath := getTestDir("test_run_pkg_in_path")

res, err := RunWithOpts(
opt.WithNoSumCheck(true),
opt.WithEntries([]string{filepath.Join(pkgPath, "test_run_no_sum_check", "main.k")}),
opt.WithKclOption(kcl.WithWorkDir(filepath.Join(pkgPath, "test_run_no_sum_check"))),
)
assert.Equal(t, err, nil)
assert.Equal(t, res.GetRawYamlResult(), "a: Hello World!")
assert.Equal(t, err, nil)
pkgPath := filepath.Join(getTestDir("test_run_pkg_in_path"), "test_run_no_sum_check")
testCases := []string{"dep_git_commit", "dep_git_tag", "dep_oci"}

for _, testCase := range testCases {

pathMainK := filepath.Join(pkgPath, testCase, "main.k")
workDir := filepath.Join(pkgPath, testCase)
modLock := filepath.Join(workDir, "kcl.mod.lock")
expected, err := os.ReadFile(filepath.Join(pkgPath, testCase, "expected"))
assert.Equal(t, err, nil)

res, err := RunWithOpts(
opt.WithNoSumCheck(true),
opt.WithEntries([]string{pathMainK}),
opt.WithKclOption(kcl.WithWorkDir(workDir)),
)
assert.Equal(t, err, nil)
assert.Equal(t, utils.DirExists(modLock), false)
assert.Equal(t, utils.RmNewline(res.GetRawYamlResult()), utils.RmNewline(string(expected)))
assert.Equal(t, err, nil)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
workload:
containers:
nginx:
image: nginx:v1
command:
- /bin/sh
- -c
- echo hi
args:
- /bin/sh
- -c
- echo hi
env:
env1: VALUE
env2: secret://sec-name/key
workingDir: /tmp
readinessProbe:
probeHandler:
url: http://localhost:80
initialDelaySeconds: 10
replicas: 2
monitoring:
interval: 30s
timeout: 15s
path: /metrics
port: web
scheme: http
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "dep_git_commit"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
catalog = { git = "https://github.com/KusionStack/catalog.git", commit = "a29e3db" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import catalog.models.schema.v1 as ac
import catalog.models.schema.v1.workload as wl
import catalog.models.schema.v1.workload.container as c
import catalog.models.schema.v1.workload.container.probe as p
import catalog.models.schema.v1.monitoring as m

ac.AppConfiguration {
workload: wl.Service {
containers: {
"nginx": c.Container {
image: "nginx:v1"
# Run the following command as defined
command: ["/bin/sh", "-c", "echo hi"]
# Extra arguments append to command defined above
args: ["/bin/sh", "-c", "echo hi"]
env: {
# An environment variable of name "env1" and value "VALUE" will be set
"env1": "VALUE"
# An environment variable of name "env2" and value of the key "key" in the
# secret named "sec-name" will be set.
"env2": "secret://sec-name/key"
}
# Run the command "/bin/sh -c echo hi", as defined above, in the directory "/tmp"
workingDir: "/tmp"
# Configure a HTTP readiness probe
readinessProbe: p.Probe {
probeHandler: p.Http {
url: "http://localhost:80"
}
initialDelaySeconds: 10
}
}
}
# Set the replicas
replicas: 2
}
# Add the monitoring configuration backed by Prometheus
monitoring: m.Prometheus{
interval: "30s"
timeout: "15s"
path: "/metrics"
port: "web"
scheme: "http"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
workload:
containers:
nginx:
image: nginx:v1
command:
- /bin/sh
- -c
- echo hi
args:
- /bin/sh
- -c
- echo hi
env:
env1: VALUE
env2: secret://sec-name/key
workingDir: /tmp
readinessProbe:
probeHandler:
url: http://localhost:80
initialDelaySeconds: 10
replicas: 2
type: Deployment
monitoring:
interval: 30s
timeout: 15s
path: /metrics
port: web
scheme: http
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "dep_git_tag"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
catalog = { git = "https://github.com/KusionStack/catalog.git", tag = "0.1.1" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import catalog.models.schema.v1 as ac
import catalog.models.schema.v1.workload as wl
import catalog.models.schema.v1.workload.container as c
import catalog.models.schema.v1.workload.container.probe as p
import catalog.models.schema.v1.monitoring as m

ac.AppConfiguration {
workload: wl.Service {
containers: {
"nginx": c.Container {
image: "nginx:v1"
# Run the following command as defined
command: ["/bin/sh", "-c", "echo hi"]
# Extra arguments append to command defined above
args: ["/bin/sh", "-c", "echo hi"]
env: {
# An environment variable of name "env1" and value "VALUE" will be set
"env1": "VALUE"
# An environment variable of name "env2" and value of the key "key" in the
# secret named "sec-name" will be set.
"env2": "secret://sec-name/key"
}
# Run the command "/bin/sh -c echo hi", as defined above, in the directory "/tmp"
workingDir: "/tmp"
# Configure a HTTP readiness probe
readinessProbe: p.Probe {
probeHandler: p.Http {
url: "http://localhost:80"
}
initialDelaySeconds: 10
}
}
}
# Set the replicas
replicas: 2
}
# Add the monitoring configuration backed by Prometheus
monitoring: m.Prometheus{
interval: "30s"
timeout: "15s"
path: "/metrics"
port: "web"
scheme: "http"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a: Hello World!
27 changes: 24 additions & 3 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ func (c *KpmClient) Download(dep *pkg.Dependency, localPath string) (*pkg.Depend
if err != nil {
return nil, err
}
dep.Version = dep.Source.Git.Tag

dep.LocalFullPath = localPath
// Creating symbolic links in a global cache is not an optimal solution.
// This allows kclvm to locate the package by default.
Expand All @@ -746,6 +746,12 @@ func (c *KpmClient) Download(dep *pkg.Dependency, localPath string) (*pkg.Depend
return nil, err
}
dep.FullName = dep.GenDepFullName()
// If the dependency is from git commit, the version is the commit id.
// If the dependency is from git tag, the version is the tag.
dep.Version, err = dep.Source.Git.GetValidGitReference()
if err != nil {
return nil, err
}
}

if dep.Source.Oci != nil {
Expand Down Expand Up @@ -784,12 +790,27 @@ func (c *KpmClient) Download(dep *pkg.Dependency, localPath string) (*pkg.Depend

// DownloadFromGit will download the dependency from the git repository.
func (c *KpmClient) DownloadFromGit(dep *pkg.Git, localPath string) (string, error) {
var msg string
if len(dep.Tag) != 0 {
msg = fmt.Sprintf("with tag '%s'", dep.Tag)
}

if len(dep.Commit) != 0 {
msg = fmt.Sprintf("with commit '%s'", dep.Commit)
}

reporter.ReportMsgTo(
fmt.Sprintf("downloading '%s' with tag '%s'", dep.Url, dep.Tag),
fmt.Sprintf("cloning '%s' %s", dep.Url, msg),
c.logWriter,
)

_, err := git.Clone(dep.Url, dep.Tag, localPath, c.logWriter)
_, err := git.CloneWithOpts(
git.WithCommit(dep.Commit),
git.WithTag(dep.Tag),
git.WithRepoURL(dep.Url),
git.WithLocalPath(localPath),
git.WithWriter(c.logWriter),
)

if err != nil {
return localPath, reporter.NewErrorEvent(
Expand Down
63 changes: 63 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -998,3 +998,66 @@ func TestAddWithDiffVersionWithSumCheck(t *testing.T) {
_ = os.Remove(pkgWithSumCheckPathModLock)
}()
}

func TestAddWithGitCommit(t *testing.T) {
pkgPath := getTestDir("add_with_git_commit")

testPkgPath := filepath.Join(pkgPath, "test_pkg")
testPkgPathModBak := filepath.Join(testPkgPath, "kcl.mod.bak")
testPkgPathMod := filepath.Join(testPkgPath, "kcl.mod")
testPkgPathModExpect := filepath.Join(testPkgPath, "kcl.mod.expect")
testPkgPathModLock := filepath.Join(testPkgPath, "kcl.mod.lock")
testPkgPathModLockBak := filepath.Join(testPkgPath, "kcl.mod.lock.bak")
testPkgPathModLockExpect := filepath.Join(testPkgPath, "kcl.mod.lock.expect")

err := copy.Copy(testPkgPathModBak, testPkgPathMod)
assert.Equal(t, err, nil)
err = copy.Copy(testPkgPathModLockBak, testPkgPathModLock)
assert.Equal(t, err, nil)

kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)
kclPkg, err := kpmcli.LoadPkgFromPath(testPkgPath)
assert.Equal(t, err, nil)

opts := opt.AddOptions{
LocalPath: pkgPath,
RegistryOpts: opt.RegistryOptions{
Git: &opt.GitOptions{
Url: "https://github.com/KusionStack/catalog.git",
Commit: "a29e3db",
},
},
}
kpmcli.SetLogWriter(nil)
_, err = kpmcli.AddDepWithOpts(kclPkg, &opts)

assert.Equal(t, err, nil)

modContent, err := os.ReadFile(testPkgPathMod)
modContentStr := strings.ReplaceAll(string(modContent), "\r\n", "")
modContentStr = strings.ReplaceAll(modContentStr, "\n", "")
assert.Equal(t, err, nil)

modExpectContent, err := os.ReadFile(testPkgPathModExpect)
modExpectContentStr := strings.ReplaceAll(string(modExpectContent), "\r\n", "")
modExpectContentStr = strings.ReplaceAll(modExpectContentStr, "\n", "")

assert.Equal(t, err, nil)
assert.Equal(t, modContentStr, modExpectContentStr)

modLockContent, err := os.ReadFile(testPkgPathModLock)
modLockContentStr := strings.ReplaceAll(string(modLockContent), "\r\n", "")
modLockContentStr = strings.ReplaceAll(modLockContentStr, "\n", "")
assert.Equal(t, err, nil)
modLockExpectContent, err := os.ReadFile(testPkgPathModLockExpect)
modLockExpectContentStr := strings.ReplaceAll(string(modLockExpectContent), "\r\n", "")
modLockExpectContentStr = strings.ReplaceAll(modLockExpectContentStr, "\n", "")
assert.Equal(t, err, nil)
assert.Equal(t, modLockContentStr, modLockExpectContentStr)

defer func() {
_ = os.Remove(testPkgPathMod)
_ = os.Remove(testPkgPathModLock)
}()
}
6 changes: 6 additions & 0 deletions pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "with_sum_check"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "with_sum_check"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
catalog = { git = "https://github.com/KusionStack/catalog.git", commit = "a29e3db" }
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[dependencies]
[dependencies.catalog]
name = "catalog"
full_name = "catalog_a29e3db"
version = "a29e3db"
sum = "kFmlrYJbJUFFTEXjC9cquc80WB+UpZ/6oMPKrfgyeks="
url = "https://github.com/KusionStack/catalog.git"
commit = "a29e3db"
1 change: 1 addition & 0 deletions pkg/client/test_data/add_with_git_commit/test_pkg/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
Loading

0 comments on commit 48ada7b

Please sign in to comment.