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

fix: reposcaffolding raise error when variable not exist #1263

Merged
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
1 change: 0 additions & 1 deletion internal/pkg/plugin/argocdapp/tpl/argocd.tpl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ spec:
automated:
prune: true
selfHeal: true

7 changes: 7 additions & 0 deletions internal/pkg/plugin/installer/kubectl/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/devstream-io/devstream/internal/pkg/configmanager"
"github.com/devstream-io/devstream/internal/pkg/plugin/installer"
"github.com/devstream-io/devstream/pkg/util/kubectl"
"github.com/devstream-io/devstream/pkg/util/log"
"github.com/devstream-io/devstream/pkg/util/pkgerror"
"github.com/devstream-io/devstream/pkg/util/template"
)

Expand Down Expand Up @@ -61,6 +63,11 @@ func processByIOReader(action string, reader io.Reader) error {
err = kubectl.KubeApplyFromIOReader(reader)
case kubectl.Delete:
err = kubectl.KubeDeleteFromIOReader(reader)
// ignore resource not exist error
if err != nil && pkgerror.CheckErrorMatchByMessage(err, kubectl.ArgocdApplicationNotExist) {
log.Warnf("kubectl config is already deleted")
err = nil
}
default:
err = fmt.Errorf("kubectl not support this kind of action: %v", action)
}
Expand Down
5 changes: 0 additions & 5 deletions internal/pkg/plugin/installer/kubectl/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ var _ = Describe("ProcessByContent", Ordered, func() {
err := op(options)
Expect(err).To(HaveOccurred())
})
It("action is kubectl delete", func() {
op := ProcessByURL(utilKubectl.Delete, s.URL())
err := op(options)
Expect(err).To(HaveOccurred())
})
It("action is not support", func() {
op := ProcessByURL("", s.URL())
err := op(options)
Expand Down
24 changes: 20 additions & 4 deletions internal/pkg/plugin/installer/reposcaffolding/installer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package reposcaffolding

import (
"fmt"

"github.com/devstream-io/devstream/internal/pkg/configmanager"
"github.com/devstream-io/devstream/pkg/util/file"
"github.com/devstream-io/devstream/pkg/util/log"
"github.com/devstream-io/devstream/pkg/util/scm"
"github.com/devstream-io/devstream/pkg/util/scm/git"
)
Expand All @@ -12,18 +16,30 @@ func InstallRepo(options configmanager.RawOptions) error {
if err != nil {
return err
}

// 1. Download and render repo by SourceRepo
// 1. Download repo by SourceRepo
sourceClient, err := scm.NewClient(opts.SourceRepo)
if err != nil {
return err
}
gitMap, err := opts.downloadAndRenderScmRepo(sourceClient)
repoDir, err := sourceClient.DownloadRepo()
if err != nil {
log.Debugf("reposcaffolding process files error: %s", err)
return err
}

// 2. push repo to DestinationRepo
// 2. render repo with variables
appName := opts.DestinationRepo.Repo
gitMap, err := file.GetFileMapByWalkDir(
repoDir, filterGitFiles,
getRepoFileNameFunc(appName, opts.SourceRepo.GetRepoNameWithBranch()),
processRepoFileFunc(appName, opts.renderTplConfig()),
)
if err != nil {
log.Warnf("repoScaffolding render repoTemplate failed=> %+v", err)
return fmt.Errorf("render RepoTemplate files failed")
}

// 3. push repo to DestinationRepo
dstClient, err := scm.NewClientWithAuth(opts.DestinationRepo)
if err != nil {
return err
Expand Down
19 changes: 0 additions & 19 deletions internal/pkg/plugin/installer/reposcaffolding/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import (
"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/internal/pkg/configmanager"
"github.com/devstream-io/devstream/pkg/util/file"
"github.com/devstream-io/devstream/pkg/util/log"
"github.com/devstream-io/devstream/pkg/util/scm"
"github.com/devstream-io/devstream/pkg/util/scm/git"
)

Expand All @@ -31,19 +28,3 @@ func (opts *Options) renderTplConfig() map[string]interface{} {
}
return renderConfig
}

// downloadAndRenderScmRepo will download repo from source repo and render it locally
func (opts *Options) downloadAndRenderScmRepo(scmClient scm.ClientOperation) (git.GitFileContentMap, error) {
// 1. download zip file and unzip this file then render folders
zipFilesDir, err := scmClient.DownloadRepo()
if err != nil {
log.Debugf("reposcaffolding process files error: %s", err)
return nil, err
}
appName := opts.DestinationRepo.Repo
return file.GetFileMapByWalkDir(
zipFilesDir, filterGitFiles,
getRepoFileNameFunc(appName, opts.SourceRepo.GetRepoNameWithBranch()),
processRepoFileFunc(appName, opts.renderTplConfig()),
)
}
40 changes: 0 additions & 40 deletions internal/pkg/plugin/installer/reposcaffolding/option_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package reposcaffolding

import (
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/devstream-io/devstream/internal/pkg/configmanager"
"github.com/devstream-io/devstream/pkg/util/scm"
"github.com/devstream-io/devstream/pkg/util/scm/git"
)

Expand Down Expand Up @@ -75,41 +72,4 @@ var _ = Describe("Options struct", func() {
Expect(appName).Should(Equal(coverAppName))
})
})

Context("downloadAndRenderScmRepo method", func() {
var (
scmClient *scm.MockScmClient
)
When("download repo from scm failed", func() {
var (
errMsg string
)
BeforeEach(func() {
errMsg = "test download repo failed"
scmClient = &scm.MockScmClient{
DownloadRepoError: fmt.Errorf(errMsg),
}
})
It("should return error", func() {
_, err := opts.downloadAndRenderScmRepo(scmClient)
Expect(err).Error().Should(HaveOccurred())
})
})
When("download repo success", func() {
var (
tempDir string
)
BeforeEach(func() {
tempDir = GinkgoT().TempDir()
scmClient = &scm.MockScmClient{
DownloadRepoValue: tempDir,
}
})
It("should work normal", func() {
gitMap, err := opts.downloadAndRenderScmRepo(scmClient)
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(len(gitMap)).Should(BeZero())
})
})
})
})
3 changes: 2 additions & 1 deletion pkg/util/file/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ func GetFileMapByWalkDir(

// if file ends-with tpl, render this file, else copy this file directly
dstFileName := fileNameFunc(path, srcPath)
// if process file failed, return error
content, err := processFunc(path)
if err != nil {
return nil
return err
}
contentMap[dstFileName] = content
return nil
Expand Down
5 changes: 2 additions & 3 deletions pkg/util/file/dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ var _ = Describe("GetFileMapByWalkDir func", func() {
})
When("processFunc return false", func() {
It("should return empty", func() {
result, err := file.GetFileMapByWalkDir(tempDir, mockFilterSuccessFunc, mockGetFileNameFunc, mockProcessFailedFunc)
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(len(result)).Should(Equal(0))
_, err := file.GetFileMapByWalkDir(tempDir, mockFilterSuccessFunc, mockGetFileNameFunc, mockProcessFailedFunc)
Expect(err).Error().Should(HaveOccurred())
})
})
When("all func work normal", func() {
Expand Down
5 changes: 2 additions & 3 deletions pkg/util/kubectl/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kubectl

import (
"fmt"
"io"
"os"
"os/exec"
Expand Down Expand Up @@ -49,9 +50,7 @@ func kubectlAction(action string, filename string) error {
cmd := exec.Command("kubectl", action, "-f", filename)
cOut, err := cmd.CombinedOutput()
if err != nil {
log.Errorf("Failed to exec: < %s >.", cmd.String())
log.Errorf("Exec logs: < %s >. Got error: %s.", string(cOut), err)
return err
return fmt.Errorf("Failed to exec: < %s >.\nExec logs: < %s >. Got error: %s.", cmd.String(), string(cOut), err)
}
log.Info(strings.TrimSuffix(string(cOut), "\n"))
return nil
Expand Down
5 changes: 5 additions & 0 deletions pkg/util/kubectl/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package kubectl

import "github.com/devstream-io/devstream/pkg/util/pkgerror"

var ArgocdApplicationNotExist pkgerror.ErrorMessage = "not found"