-
Notifications
You must be signed in to change notification settings - Fork 156
fix: resolve warnings and improve file extraction logic #773
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
base: master
Are you sure you want to change the base?
Changes from all commits
1f71e83
eefc216
67c1f36
a499769
41f3586
ac17449
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package kompose | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
|
@@ -42,37 +43,55 @@ func Convert(dockerCompose DockerComposeFile) (string, error) { | |
tempFilePath := filepath.Join(mesheryDir, "temp.data") | ||
resultFilePath := filepath.Join(mesheryDir, "result.yaml") | ||
|
||
// Defer cleanup to ensure temp files are removed even on error. | ||
defer func() { | ||
// We log cleanup errors but do not return them, as the primary function error is more important. | ||
if err := os.Remove(tempFilePath); err != nil && !os.IsNotExist(err) { | ||
// A logger should be used here to warn about the failed cleanup. | ||
// e.g., log.Warnf("failed to remove temporary file %s: %v", tempFilePath, err) | ||
Comment on lines
+50
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not log here ? |
||
} | ||
if err := os.Remove(resultFilePath); err != nil && !os.IsNotExist(err) { | ||
// e.g., log.Warnf("failed to remove result file %s: %v", resultFilePath, err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
} | ||
}() | ||
|
||
// Create the temp file | ||
if err := utils.CreateFile(dockerCompose, "temp.data", mesheryDir); err != nil { | ||
return "", ErrCvrtKompose(err) | ||
} | ||
|
||
defer func() { | ||
os.Remove(tempFilePath) | ||
os.Remove(resultFilePath) | ||
}() | ||
// Format Docker Compose file for Kompose | ||
err = formatComposeFile(&dockerCompose) | ||
if err != nil { | ||
return "", ErrCvrtKompose(err) | ||
} | ||
|
||
formatComposeFile(&dockerCompose) | ||
// Check version compatibility | ||
err = versionCheck(dockerCompose) | ||
if err != nil { | ||
return "", ErrCvrtKompose(err) | ||
} | ||
|
||
// Initialize Kompose client | ||
komposeClient, err := client.NewClient() | ||
if err != nil { | ||
return "", ErrCvrtKompose(err) | ||
} | ||
|
||
// Set up Convert options | ||
ConvertOpt := client.ConvertOptions{ | ||
InputFiles: []string{tempFilePath}, | ||
OutFile: resultFilePath, | ||
GenerateNetworkPolicies: true, | ||
} | ||
|
||
// Convert using Kompose client | ||
_, err = komposeClient.Convert(ConvertOpt) | ||
if err != nil { | ||
return "", ErrCvrtKompose(err) | ||
} | ||
|
||
// Read the result file | ||
result, err := os.ReadFile(resultFilePath) | ||
if err != nil { | ||
return "", ErrCvrtKompose(err) | ||
|
@@ -81,13 +100,42 @@ func Convert(dockerCompose DockerComposeFile) (string, error) { | |
return string(result), nil | ||
} | ||
|
||
type composeFile struct { | ||
Version string `yaml:"version,omitempty"` | ||
// cleanup removes temporary files | ||
func cleanup(tempFilePath, resultFilePath string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see the advantage to extract this in a dedicate function since it is bind to those files. Also it should be done in a defer like previous implementation to ensure the files are closed properly. Here if one of the previous action fail the files will never be closed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No more used, write ? |
||
// Try to remove tempFilePath | ||
if err := os.Remove(tempFilePath); err != nil { | ||
return fmt.Errorf("failed to remove temp file %s: %w", tempFilePath, err) | ||
} | ||
|
||
// Try to remove resultFilePath | ||
if err := os.Remove(resultFilePath); err != nil { | ||
return fmt.Errorf("failed to remove result file %s: %w", resultFilePath, err) | ||
} | ||
|
||
return nil // No errors | ||
} | ||
|
||
// checks if the version is compatible with `kompose` | ||
// expects a valid docker compose yaml | ||
// error = nil means it is compatible | ||
// formatComposeFile takes in a pointer to the compose file byte array and formats it | ||
// so that it is compatible with Kompose. It expects a validated Docker Compose file. | ||
func formatComposeFile(yamlManifest *DockerComposeFile) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have we evaluate the impact on services calling this function since the signature has changed ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you're right, I did not notice it was a local function. Sorry for the noise |
||
data := composeFile{} | ||
err := yaml.Unmarshal(*yamlManifest, &data) | ||
if err != nil { | ||
return fmt.Errorf("failed to unmarshal compose file: %w", err) | ||
} | ||
|
||
// Marshal it again to ensure it is in the correct format for Kompose | ||
out, err := yaml.Marshal(data) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal compose file: %w", err) | ||
} | ||
|
||
*yamlManifest = out | ||
return nil | ||
} | ||
|
||
// versionCheck checks if the version in the Docker Compose file is compatible with Kompose. | ||
// It expects a valid Docker Compose YAML and returns an error if the version is not supported. | ||
func versionCheck(dc DockerComposeFile) error { | ||
cf := composeFile{} | ||
err := yaml.Unmarshal(dc, &cf) | ||
|
@@ -108,18 +156,7 @@ func versionCheck(dc DockerComposeFile) error { | |
return nil | ||
} | ||
|
||
// formatComposeFile takes in a pointer to the compose file byte array and formats it so that it is compatible with `Kompose` | ||
// it expects a validated docker compose file and does not validate | ||
func formatComposeFile(yamlManifest *DockerComposeFile) { | ||
data := composeFile{} | ||
err := yaml.Unmarshal(*yamlManifest, &data) | ||
if err != nil { | ||
return | ||
} | ||
// so that "3.3" and 3.3 are treated differently by `Kompose` | ||
out, err := yaml.Marshal(data) | ||
if err != nil { | ||
return | ||
} | ||
*yamlManifest = out | ||
// composeFile represents the structure of the Docker Compose file version. | ||
type composeFile struct { | ||
Version string `yaml:"version,omitempty"` | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not util IMO