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

bugfix: use deep walk to get launch files for shell and kube #1983

Merged
merged 1 commit into from
Jan 16, 2023
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
2 changes: 1 addition & 1 deletion build/kubefile/parser/app_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
27 changes: 14 additions & 13 deletions build/kubefile/parser/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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() {
Expand All @@ -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
Expand Down Expand Up @@ -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
})

Expand All @@ -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...)
Expand All @@ -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 {
Expand Down
26 changes: 15 additions & 11 deletions pkg/define/application/v1/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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, " && ")
Expand All @@ -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",
}
}
6 changes: 3 additions & 3 deletions pkg/define/image/v1/sealer_image_extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,23 +165,23 @@ 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",
},
&v1.Application{
NameVar: "app2",
TypeVar: application.HelmApp,
LaunchFilesVar: []string{
FilesVar: []string{
"chart.tgz",
},
VersionVar: "v1",
},
&v1.Application{
NameVar: "app3",
TypeVar: application.KubeApp,
LaunchFilesVar: []string{
FilesVar: []string{
"mysql.yaml",
},
VersionVar: "v1",
Expand Down