1
1
package kompose
2
2
3
3
import (
4
+ "fmt"
4
5
"os"
5
6
"path/filepath"
6
7
"strconv"
@@ -42,52 +43,93 @@ func Convert(dockerCompose DockerComposeFile) (string, error) {
42
43
tempFilePath := filepath .Join (mesheryDir , "temp.data" )
43
44
resultFilePath := filepath .Join (mesheryDir , "result.yaml" )
44
45
46
+ // Create the temp file
45
47
if err := utils .CreateFile (dockerCompose , "temp.data" , mesheryDir ); err != nil {
46
48
return "" , ErrCvrtKompose (err )
47
49
}
48
50
49
- defer func () {
50
- os .Remove (tempFilePath )
51
- os .Remove (resultFilePath )
52
- }()
51
+ // Format Docker Compose file for Kompose
52
+ err = formatComposeFile (& dockerCompose )
53
+ if err != nil {
54
+ return "" , ErrCvrtKompose (err )
55
+ }
53
56
54
- formatComposeFile ( & dockerCompose )
57
+ // Check version compatibility
55
58
err = versionCheck (dockerCompose )
56
59
if err != nil {
57
60
return "" , ErrCvrtKompose (err )
58
61
}
59
62
63
+ // Initialize Kompose client
60
64
komposeClient , err := client .NewClient ()
61
65
if err != nil {
62
66
return "" , ErrCvrtKompose (err )
63
67
}
64
68
69
+ // Set up Convert options
65
70
ConvertOpt := client.ConvertOptions {
66
71
InputFiles : []string {tempFilePath },
67
72
OutFile : resultFilePath ,
68
73
GenerateNetworkPolicies : true ,
69
74
}
70
75
76
+ // Convert using Kompose client
71
77
_ , err = komposeClient .Convert (ConvertOpt )
72
78
if err != nil {
73
79
return "" , ErrCvrtKompose (err )
74
80
}
75
81
82
+ // Read the result file
76
83
result , err := os .ReadFile (resultFilePath )
77
84
if err != nil {
78
85
return "" , ErrCvrtKompose (err )
79
86
}
80
87
88
+ // Clean up temporary files
89
+ cleanupErr := cleanup (tempFilePath , resultFilePath )
90
+ if cleanupErr != nil {
91
+ return "" , cleanupErr
92
+ }
93
+
81
94
return string (result ), nil
82
95
}
83
96
84
- type composeFile struct {
85
- Version string `yaml:"version,omitempty"`
97
+ // cleanup removes temporary files
98
+ func cleanup (tempFilePath , resultFilePath string ) error {
99
+ // Try to remove tempFilePath
100
+ if err := os .Remove (tempFilePath ); err != nil {
101
+ return fmt .Errorf ("failed to remove temp file %s: %w" , tempFilePath , err )
102
+ }
103
+
104
+ // Try to remove resultFilePath
105
+ if err := os .Remove (resultFilePath ); err != nil {
106
+ return fmt .Errorf ("failed to remove result file %s: %w" , resultFilePath , err )
107
+ }
108
+
109
+ return nil // No errors
110
+ }
111
+
112
+ // formatComposeFile takes in a pointer to the compose file byte array and formats it
113
+ // so that it is compatible with Kompose. It expects a validated Docker Compose file.
114
+ func formatComposeFile (yamlManifest * DockerComposeFile ) error {
115
+ data := composeFile {}
116
+ err := yaml .Unmarshal (* yamlManifest , & data )
117
+ if err != nil {
118
+ return fmt .Errorf ("failed to unmarshal compose file: %w" , err )
119
+ }
120
+
121
+ // Marshal it again to ensure it is in the correct format for Kompose
122
+ out , err := yaml .Marshal (data )
123
+ if err != nil {
124
+ return fmt .Errorf ("failed to marshal compose file: %w" , err )
125
+ }
126
+
127
+ * yamlManifest = out
128
+ return nil
86
129
}
87
130
88
- // checks if the version is compatible with `kompose`
89
- // expects a valid docker compose yaml
90
- // error = nil means it is compatible
131
+ // versionCheck checks if the version in the Docker Compose file is compatible with Kompose.
132
+ // It expects a valid Docker Compose YAML and returns an error if the version is not supported.
91
133
func versionCheck (dc DockerComposeFile ) error {
92
134
cf := composeFile {}
93
135
err := yaml .Unmarshal (dc , & cf )
@@ -108,18 +150,7 @@ func versionCheck(dc DockerComposeFile) error {
108
150
return nil
109
151
}
110
152
111
- // formatComposeFile takes in a pointer to the compose file byte array and formats it so that it is compatible with `Kompose`
112
- // it expects a validated docker compose file and does not validate
113
- func formatComposeFile (yamlManifest * DockerComposeFile ) {
114
- data := composeFile {}
115
- err := yaml .Unmarshal (* yamlManifest , & data )
116
- if err != nil {
117
- return
118
- }
119
- // so that "3.3" and 3.3 are treated differently by `Kompose`
120
- out , err := yaml .Marshal (data )
121
- if err != nil {
122
- return
123
- }
124
- * yamlManifest = out
153
+ // composeFile represents the structure of the Docker Compose file version.
154
+ type composeFile struct {
155
+ Version string `yaml:"version,omitempty"`
125
156
}
0 commit comments