Skip to content

Commit

Permalink
Bugfix for app parser (#1988)
Browse files Browse the repository at this point in the history
* tmp save

* bugfix: app parser

bugfix: app parser
  • Loading branch information
kakaZhou719 authored Jan 17, 2023
1 parent 47dbc02 commit 8470f90
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 73 deletions.
15 changes: 12 additions & 3 deletions build/kubefile/parser/app_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ func (kp *KubefileParser) processApp(node *Node, result *KubefileResult) (versio
fileRel2Cxt := filepath.Join(remoteCxtBase, fileBase)
filesToCopy = append(filesToCopy, fileRel2Cxt)
}

// append it to the legacy.
// it will be deleted by CleanContext
legacyCxt.directories = append(legacyCxt.directories, remoteCxtAbs)
Expand All @@ -96,9 +95,19 @@ 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 := getApplicationTypeAndFiles(appName, filesToCopy)

return makeItAsApp(appName, filesToCopy, result)
}

func makeItAsApp(appName string, filesToJudge []string, result *KubefileResult) (version.VersionedApplication, error) {
appType, err := getApplicationType(filesToJudge)
if err != nil {
return nil, fmt.Errorf("failed to judge the application type for %s: %v", appName, err)
}

launchFiles, err := getApplicationFiles(appName, appType, filesToJudge)
if err != nil {
return nil, fmt.Errorf("error in judging the application type: %v", err)
return nil, fmt.Errorf("failed to get app (%s)launch files: %v", appName, err)
}

v1App := v1.NewV1Application(
Expand Down
147 changes: 77 additions & 70 deletions build/kubefile/parser/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,128 +121,135 @@ func isHelm(sources ...string) (bool, error) {
return chartInTargetsRoot == 7, nil
}

// isYaml sources slice only has one element
func isYaml(sources ...string) (bool, []string, error) {
var yamlFiles []string
isYamlType := func(fileName string) bool {
func getApplicationType(sources []string) (string, error) {
isHelmType, helmErr := isHelm(sources...)
if helmErr != nil {
return "", helmErr
}

if isHelmType {
return application.HelmApp, nil
}

appTypeFunc := func(fileName string) string {
ext := strings.ToLower(filepath.Ext(fileName))
if ext == ".sh" {
return application.ShellApp
}

if ext == ".yaml" || ext == ".yml" {
return true
return application.KubeApp
}
return false

return "invalid"
}

var appTypeList []string
for _, source := range sources {
s, err := os.Stat(source)
if err != nil {
return false, nil, fmt.Errorf("failed to stat %s: %v", source, err)
return "", fmt.Errorf("failed to stat %s: %v", source, err)
}

// get app type by dir
if s.IsDir() {
isAllYamlFiles := true
err = filepath.Walk(source, func(path string, f fs.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() {
return nil
}
// make sure all files under source dir is yaml type.
if !isYamlType(f.Name()) {
isAllYamlFiles = false
return filepath.SkipDir
}
yamlFiles = append(yamlFiles, strings.TrimPrefix(path, source))

appTypeList = append(appTypeList, appTypeFunc(f.Name()))
return nil
})

if err != nil {
return false, nil, fmt.Errorf("failed to walk yaml dir %s: %v", source, err)
return "", fmt.Errorf("failed to walk shell dir %s: %v", source, err)
}
continue
}

if isAllYamlFiles {
return true, yamlFiles, nil
}
return false, nil, nil
// get app type by file
appTypeList = append(appTypeList, appTypeFunc(source))
}

var isShell, isKube bool
for _, appType := range appTypeList {
if appType == "invalid" {
return "", fmt.Errorf("unsupported application type in %s,%s,%s", application.KubeApp, application.HelmApp, application.ShellApp)
}
if isYamlType(source) {
return true, []string{source}, nil

if appType == application.ShellApp {
isShell = true
}

if appType == application.KubeApp {
isKube = true
}
}

return false, nil, nil
if isShell && isKube {
return "", fmt.Errorf("mixed app type is not supportted")
}

if isShell && !isKube {
return application.ShellApp, nil
}

if isKube && !isShell {
return application.KubeApp, nil
}

return "", fmt.Errorf("application type not found")
}

// isShell sources slice only has one element
func isShell(sources ...string) (bool, []string, error) {
var launchFiles []string
isShellType := func(fileName string) bool {
ext := strings.ToLower(filepath.Ext(fileName))
return ext == ".sh"
func getApplicationFiles(appName, appType string, sources []string) ([]string, error) {
if appType == application.HelmApp {
return []string{appName}, nil
}

var launchFiles []string

for _, source := range sources {
s, err := os.Stat(source)
if err != nil {
return false, nil, fmt.Errorf("failed to stat %s: %v", source, err)
return nil, fmt.Errorf("failed to stat %s: %v", source, err)
}

// get app launchFile if source is a dir
if s.IsDir() {
err = filepath.Walk(source, func(path string, f fs.FileInfo, err error) error {
if err != nil {
return err
}

if f.IsDir() {
return nil
}
// todo optimize: use more accurate methods to determine file types.
if !isShellType(f.Name()) {
return filepath.SkipDir
}

launchFiles = append(launchFiles, strings.TrimPrefix(path, source))
return nil
})

if err != nil {
return false, nil, fmt.Errorf("failed to walk shell dir %s: %v", source, err)
return nil, fmt.Errorf("failed to walk shell dir %s: %v", source, err)
}

if len(launchFiles) > 0 {
return true, launchFiles, nil
// if type is shell, only use first build context
if appType == application.ShellApp {
return launchFiles, nil
}
return false, nil, nil
}
if isShellType(source) {
return true, []string{source}, nil
continue
}
}

return false, nil, nil
}
// get app launchFile if source is a file
launchFiles = append(launchFiles, filepath.Base(source))

func getApplicationTypeAndFiles(appName string, sources []string) (string, []string, error) {
isYamlType, files, yamlErr := isYaml(sources...)
if isYamlType {
return application.KubeApp, files, nil
}

isShellType, files, shellErr := isShell(sources...)
if isShellType {
return application.ShellApp, files, nil
}

isHelmType, helmErr := isHelm(sources...)
if isHelmType {
return application.HelmApp, []string{appName}, nil
}

if yamlErr != nil {
return "", nil, yamlErr
}
if shellErr != nil {
return "", nil, shellErr
}
if helmErr != nil {
return "", nil, helmErr
// if type is shell, only use first build context
if appType == application.ShellApp {
return launchFiles, nil
}
}

return "", nil, fmt.Errorf("unsupported application type in %s,%s,%s", application.KubeApp, application.HelmApp, application.ShellApp)
return launchFiles, nil
}

0 comments on commit 8470f90

Please sign in to comment.