Skip to content

Commit 54576cb

Browse files
authored
Merge pull request #1263 from merico-dev/fix-configmanager-log-error
fix: reposcaffolding raise error when variable not exist
2 parents be7a9be + e8a086c commit 54576cb

File tree

10 files changed

+38
-76
lines changed

10 files changed

+38
-76
lines changed

internal/pkg/plugin/argocdapp/tpl/argocd.tpl.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,3 @@ spec:
2121
automated:
2222
prune: true
2323
selfHeal: true
24-

internal/pkg/plugin/installer/kubectl/installer.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/devstream-io/devstream/internal/pkg/configmanager"
99
"github.com/devstream-io/devstream/internal/pkg/plugin/installer"
1010
"github.com/devstream-io/devstream/pkg/util/kubectl"
11+
"github.com/devstream-io/devstream/pkg/util/log"
12+
"github.com/devstream-io/devstream/pkg/util/pkgerror"
1113
"github.com/devstream-io/devstream/pkg/util/template"
1214
)
1315

@@ -61,6 +63,11 @@ func processByIOReader(action string, reader io.Reader) error {
6163
err = kubectl.KubeApplyFromIOReader(reader)
6264
case kubectl.Delete:
6365
err = kubectl.KubeDeleteFromIOReader(reader)
66+
// ignore resource not exist error
67+
if err != nil && pkgerror.CheckErrorMatchByMessage(err, kubectl.ArgocdApplicationNotExist) {
68+
log.Warnf("kubectl config is already deleted")
69+
err = nil
70+
}
6471
default:
6572
err = fmt.Errorf("kubectl not support this kind of action: %v", action)
6673
}

internal/pkg/plugin/installer/kubectl/installer_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,6 @@ var _ = Describe("ProcessByContent", Ordered, func() {
8181
err := op(options)
8282
Expect(err).To(HaveOccurred())
8383
})
84-
It("action is kubectl delete", func() {
85-
op := ProcessByURL(utilKubectl.Delete, s.URL())
86-
err := op(options)
87-
Expect(err).To(HaveOccurred())
88-
})
8984
It("action is not support", func() {
9085
op := ProcessByURL("", s.URL())
9186
err := op(options)

internal/pkg/plugin/installer/reposcaffolding/installer.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package reposcaffolding
22

33
import (
4+
"fmt"
5+
46
"github.com/devstream-io/devstream/internal/pkg/configmanager"
7+
"github.com/devstream-io/devstream/pkg/util/file"
8+
"github.com/devstream-io/devstream/pkg/util/log"
59
"github.com/devstream-io/devstream/pkg/util/scm"
610
"github.com/devstream-io/devstream/pkg/util/scm/git"
711
)
@@ -12,18 +16,30 @@ func InstallRepo(options configmanager.RawOptions) error {
1216
if err != nil {
1317
return err
1418
}
15-
16-
// 1. Download and render repo by SourceRepo
19+
// 1. Download repo by SourceRepo
1720
sourceClient, err := scm.NewClient(opts.SourceRepo)
1821
if err != nil {
1922
return err
2023
}
21-
gitMap, err := opts.downloadAndRenderScmRepo(sourceClient)
24+
repoDir, err := sourceClient.DownloadRepo()
2225
if err != nil {
26+
log.Debugf("reposcaffolding process files error: %s", err)
2327
return err
2428
}
2529

26-
// 2. push repo to DestinationRepo
30+
// 2. render repo with variables
31+
appName := opts.DestinationRepo.Repo
32+
gitMap, err := file.GetFileMapByWalkDir(
33+
repoDir, filterGitFiles,
34+
getRepoFileNameFunc(appName, opts.SourceRepo.GetRepoNameWithBranch()),
35+
processRepoFileFunc(appName, opts.renderTplConfig()),
36+
)
37+
if err != nil {
38+
log.Warnf("repoScaffolding render repoTemplate failed=> %+v", err)
39+
return fmt.Errorf("render RepoTemplate files failed")
40+
}
41+
42+
// 3. push repo to DestinationRepo
2743
dstClient, err := scm.NewClientWithAuth(opts.DestinationRepo)
2844
if err != nil {
2945
return err

internal/pkg/plugin/installer/reposcaffolding/option.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import (
44
"github.com/mitchellh/mapstructure"
55

66
"github.com/devstream-io/devstream/internal/pkg/configmanager"
7-
"github.com/devstream-io/devstream/pkg/util/file"
8-
"github.com/devstream-io/devstream/pkg/util/log"
9-
"github.com/devstream-io/devstream/pkg/util/scm"
107
"github.com/devstream-io/devstream/pkg/util/scm/git"
118
)
129

@@ -31,19 +28,3 @@ func (opts *Options) renderTplConfig() map[string]interface{} {
3128
}
3229
return renderConfig
3330
}
34-
35-
// downloadAndRenderScmRepo will download repo from source repo and render it locally
36-
func (opts *Options) downloadAndRenderScmRepo(scmClient scm.ClientOperation) (git.GitFileContentMap, error) {
37-
// 1. download zip file and unzip this file then render folders
38-
zipFilesDir, err := scmClient.DownloadRepo()
39-
if err != nil {
40-
log.Debugf("reposcaffolding process files error: %s", err)
41-
return nil, err
42-
}
43-
appName := opts.DestinationRepo.Repo
44-
return file.GetFileMapByWalkDir(
45-
zipFilesDir, filterGitFiles,
46-
getRepoFileNameFunc(appName, opts.SourceRepo.GetRepoNameWithBranch()),
47-
processRepoFileFunc(appName, opts.renderTplConfig()),
48-
)
49-
}

internal/pkg/plugin/installer/reposcaffolding/option_test.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package reposcaffolding
22

33
import (
4-
"fmt"
5-
64
. "github.com/onsi/ginkgo/v2"
75
. "github.com/onsi/gomega"
86

97
"github.com/devstream-io/devstream/internal/pkg/configmanager"
10-
"github.com/devstream-io/devstream/pkg/util/scm"
118
"github.com/devstream-io/devstream/pkg/util/scm/git"
129
)
1310

@@ -75,41 +72,4 @@ var _ = Describe("Options struct", func() {
7572
Expect(appName).Should(Equal(coverAppName))
7673
})
7774
})
78-
79-
Context("downloadAndRenderScmRepo method", func() {
80-
var (
81-
scmClient *scm.MockScmClient
82-
)
83-
When("download repo from scm failed", func() {
84-
var (
85-
errMsg string
86-
)
87-
BeforeEach(func() {
88-
errMsg = "test download repo failed"
89-
scmClient = &scm.MockScmClient{
90-
DownloadRepoError: fmt.Errorf(errMsg),
91-
}
92-
})
93-
It("should return error", func() {
94-
_, err := opts.downloadAndRenderScmRepo(scmClient)
95-
Expect(err).Error().Should(HaveOccurred())
96-
})
97-
})
98-
When("download repo success", func() {
99-
var (
100-
tempDir string
101-
)
102-
BeforeEach(func() {
103-
tempDir = GinkgoT().TempDir()
104-
scmClient = &scm.MockScmClient{
105-
DownloadRepoValue: tempDir,
106-
}
107-
})
108-
It("should work normal", func() {
109-
gitMap, err := opts.downloadAndRenderScmRepo(scmClient)
110-
Expect(err).Error().ShouldNot(HaveOccurred())
111-
Expect(len(gitMap)).Should(BeZero())
112-
})
113-
})
114-
})
11575
})

pkg/util/file/dir.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ func GetFileMapByWalkDir(
3636

3737
// if file ends-with tpl, render this file, else copy this file directly
3838
dstFileName := fileNameFunc(path, srcPath)
39+
// if process file failed, return error
3940
content, err := processFunc(path)
4041
if err != nil {
41-
return nil
42+
return err
4243
}
4344
contentMap[dstFileName] = content
4445
return nil

pkg/util/file/dir_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ var _ = Describe("GetFileMapByWalkDir func", func() {
6262
})
6363
When("processFunc return false", func() {
6464
It("should return empty", func() {
65-
result, err := file.GetFileMapByWalkDir(tempDir, mockFilterSuccessFunc, mockGetFileNameFunc, mockProcessFailedFunc)
66-
Expect(err).Error().ShouldNot(HaveOccurred())
67-
Expect(len(result)).Should(Equal(0))
65+
_, err := file.GetFileMapByWalkDir(tempDir, mockFilterSuccessFunc, mockGetFileNameFunc, mockProcessFailedFunc)
66+
Expect(err).Error().Should(HaveOccurred())
6867
})
6968
})
7069
When("all func work normal", func() {

pkg/util/kubectl/cmd.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package kubectl
22

33
import (
4+
"fmt"
45
"io"
56
"os"
67
"os/exec"
@@ -49,9 +50,7 @@ func kubectlAction(action string, filename string) error {
4950
cmd := exec.Command("kubectl", action, "-f", filename)
5051
cOut, err := cmd.CombinedOutput()
5152
if err != nil {
52-
log.Errorf("Failed to exec: < %s >.", cmd.String())
53-
log.Errorf("Exec logs: < %s >. Got error: %s.", string(cOut), err)
54-
return err
53+
return fmt.Errorf("Failed to exec: < %s >.\nExec logs: < %s >. Got error: %s.", cmd.String(), string(cOut), err)
5554
}
5655
log.Info(strings.TrimSuffix(string(cOut), "\n"))
5756
return nil

pkg/util/kubectl/errors.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package kubectl
2+
3+
import "github.com/devstream-io/devstream/pkg/util/pkgerror"
4+
5+
var ArgocdApplicationNotExist pkgerror.ErrorMessage = "not found"

0 commit comments

Comments
 (0)