From 7fb72b8280db14f41cd61dcda76adf6f437653e9 Mon Sep 17 00:00:00 2001 From: kakzhou719 Date: Mon, 16 Jan 2023 17:26:01 +0800 Subject: [PATCH] bugfix: use deep walk to get launch files for shell and kube --- build/kubefile/parser/app_handler.go | 2 +- build/kubefile/parser/utils.go | 27 ++++++++++--------- pkg/define/application/v1/application.go | 26 ++++++++++-------- .../image/v1/sealer_image_extension_test.go | 6 ++--- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/build/kubefile/parser/app_handler.go b/build/kubefile/parser/app_handler.go index c0736de3b62..d16c1f9cf61 100644 --- a/build/kubefile/parser/app_handler.go +++ b/build/kubefile/parser/app_handler.go @@ -96,7 +96,7 @@ func (kp *KubefileParser) processApp(node *Node, result *KubefileResult) (versio tmpLine := strings.Join(append([]string{command.Copy}, append(filesToCopy, destDir)...), " ") result.Dockerfile = mergeLines(result.Dockerfile, tmpLine) result.legacyContext.apps2Files[appName] = append([]string{}, filesToCopy...) - appType, launchFiles, err := getApplicationType(filesToCopy) + appType, launchFiles, err := getApplicationTypeAndFiles(appName, filesToCopy) if err != nil { return nil, fmt.Errorf("error in judging the application type: %v", err) } diff --git a/build/kubefile/parser/utils.go b/build/kubefile/parser/utils.go index 7563ab5b8b1..69b39a1e399 100644 --- a/build/kubefile/parser/utils.go +++ b/build/kubefile/parser/utils.go @@ -122,7 +122,8 @@ func isHelm(sources ...string) (bool, error) { } // isYaml sources slice only has one element -func isYaml(sources ...string) (bool, error) { +func isYaml(sources ...string) (bool, []string, error) { + var yamlFiles []string isYamlType := func(fileName string) bool { ext := strings.ToLower(filepath.Ext(fileName)) if ext == ".yaml" || ext == ".yml" { @@ -134,7 +135,7 @@ func isYaml(sources ...string) (bool, error) { for _, source := range sources { s, err := os.Stat(source) if err != nil { - return false, fmt.Errorf("failed to stat %s: %v", source, err) + return false, nil, fmt.Errorf("failed to stat %s: %v", source, err) } if s.IsDir() { @@ -151,23 +152,24 @@ func isYaml(sources ...string) (bool, error) { isAllYamlFiles = false return filepath.SkipDir } + yamlFiles = append(yamlFiles, strings.TrimPrefix(path, source)) return nil }) if err != nil { - return false, fmt.Errorf("failed to walk yaml dir %s: %v", source, err) + return false, nil, fmt.Errorf("failed to walk yaml dir %s: %v", source, err) } if isAllYamlFiles { - return true, nil + return true, yamlFiles, nil } - return false, nil + return false, nil, nil } if isYamlType(source) { - return true, nil + return true, []string{source}, nil } } - return false, nil + return false, nil, nil } // isShell sources slice only has one element @@ -195,8 +197,7 @@ func isShell(sources ...string) (bool, []string, error) { if !isShellType(f.Name()) { return filepath.SkipDir } - - launchFiles = append(launchFiles, path) + launchFiles = append(launchFiles, strings.TrimPrefix(path, source)) return nil }) @@ -217,10 +218,10 @@ func isShell(sources ...string) (bool, []string, error) { return false, nil, nil } -func getApplicationType(sources []string) (string, []string, error) { - isYamlType, yamlErr := isYaml(sources...) +func getApplicationTypeAndFiles(appName string, sources []string) (string, []string, error) { + isYamlType, files, yamlErr := isYaml(sources...) if isYamlType { - return application.KubeApp, sources, nil + return application.KubeApp, files, nil } isShellType, files, shellErr := isShell(sources...) @@ -230,7 +231,7 @@ func getApplicationType(sources []string) (string, []string, error) { isHelmType, helmErr := isHelm(sources...) if isHelmType { - return application.HelmApp, sources, nil + return application.HelmApp, []string{appName}, nil } if yamlErr != nil { diff --git a/pkg/define/application/v1/application.go b/pkg/define/application/v1/application.go index 381a4f5eba3..59ea59e9a60 100644 --- a/pkg/define/application/v1/application.go +++ b/pkg/define/application/v1/application.go @@ -24,10 +24,10 @@ import ( ) type Application struct { - NameVar string `json:"name"` - TypeVar string `json:"type,omitempty"` - LaunchFilesVar []string `json:"launchfiles,omitempty"` - VersionVar string `json:"version,omitempty"` + NameVar string `json:"name"` + TypeVar string `json:"type,omitempty"` + FilesVar []string `json:"files,omitempty"` + VersionVar string `json:"version,omitempty"` } func (app *Application) Version() string { @@ -51,12 +51,16 @@ func (app *Application) LaunchCmd(appRoot string, launchCmds []string) string { } switch app.Type() { case application.KubeApp: - return fmt.Sprintf("kubectl apply -f %s", appRoot) + var cmds []string + for _, file := range app.FilesVar { + cmds = append(cmds, fmt.Sprintf("kubectl apply -f %s", filepath.Join(appRoot, file))) + } + return strings.Join(cmds, " && ") case application.HelmApp: return fmt.Sprintf("helm install %s %s", app.Name(), appRoot) case application.ShellApp: var cmds []string - for _, file := range app.LaunchFilesVar { + for _, file := range app.FilesVar { cmds = append(cmds, fmt.Sprintf("bash %s", filepath.Join(appRoot, file))) } return strings.Join(cmds, " && ") @@ -67,11 +71,11 @@ func (app *Application) LaunchCmd(appRoot string, launchCmds []string) string { func NewV1Application( name string, - appType string, launchFiles []string) version.VersionedApplication { + appType string, files []string) version.VersionedApplication { return &Application{ - NameVar: name, - TypeVar: appType, - LaunchFilesVar: launchFiles, - VersionVar: "v1", + NameVar: name, + TypeVar: appType, + FilesVar: files, + VersionVar: "v1", } } diff --git a/pkg/define/image/v1/sealer_image_extension_test.go b/pkg/define/image/v1/sealer_image_extension_test.go index 99939d28edc..0d9f49b3bba 100644 --- a/pkg/define/image/v1/sealer_image_extension_test.go +++ b/pkg/define/image/v1/sealer_image_extension_test.go @@ -165,7 +165,7 @@ func TestNewImageSpec(t *testing.T) { &v1.Application{ NameVar: "app1", TypeVar: application.ShellApp, - LaunchFilesVar: []string{ + FilesVar: []string{ "cnstack-acos-0.0.5-beta-install.sh", }, VersionVar: "v1", @@ -173,7 +173,7 @@ func TestNewImageSpec(t *testing.T) { &v1.Application{ NameVar: "app2", TypeVar: application.HelmApp, - LaunchFilesVar: []string{ + FilesVar: []string{ "chart.tgz", }, VersionVar: "v1", @@ -181,7 +181,7 @@ func TestNewImageSpec(t *testing.T) { &v1.Application{ NameVar: "app3", TypeVar: application.KubeApp, - LaunchFilesVar: []string{ + FilesVar: []string{ "mysql.yaml", }, VersionVar: "v1",