Skip to content

Commit

Permalink
Compose transpilation checkpoint
Browse files Browse the repository at this point in the history
mieubrisse committed Oct 27, 2023
1 parent 7094c56 commit 23156f4
Showing 3 changed files with 57 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -40,11 +40,11 @@ var supportedComposeFilenames = []string{

// TODO actually take in a Compose file
func TranspileDockerComposePackageToStarlark(packageAbsDirpath string) (string, error) {
composeFilepath, err := getComposeFilepath(packageAbsDirpath)
// Useful for logging, to not leak internals of APIC
composeFilename, composeBytes, err := getComposeFilenameAndContent(packageAbsDirpath)
if err != nil {
return "", stacktrace.Propagate(err, "An error occurred getting the Compose file to parse")
return "", stacktrace.Propagate(err, "An error occurred reading the Compose file")
}
composeFilename := path.Base(composeFilepath) // Useful for logging, to not leak internals of APIC

// Use the envvars file next to the Compose if it exists
envVarsFilepath := path.Join(packageAbsDirpath, envVarsFilename)
@@ -58,20 +58,9 @@ func TranspileDockerComposePackageToStarlark(packageAbsDirpath string) (string,
}
envVars = envVarsInFile

project, err := loader.Load(types.ConfigDetails{ //nolint:exhaustruct
// Note that we might be able to use the WorkingDir property instead, to parse the entire directory
ConfigFiles: []types.ConfigFile{{
Filename: composeFilepath,
}},
Environment: envVars,
})
script, err := convertComposeToStarlark(composeBytes, envVars)
if err != nil {
return "", stacktrace.Propagate(err, "An error occurred parsing the '%v' Compose file in preparation for Starlark transpilation", composeFilename)
}

script, _, err := convertComposeProjectToStarlark(project)
if err != nil {
return "", stacktrace.Propagate(err, "An error occurred transpiling the '%v' Compose file to Starlark", composeFilename)
return "", stacktrace.Propagate(err, "An error occurred transpiling Compose file '%v' to Starlark", composeFilename)
}
return script, nil
}
@@ -80,25 +69,34 @@ func TranspileDockerComposePackageToStarlark(packageAbsDirpath string) (string,
// Private Helper Functions
// ====================================================================================================

func getComposeFilepath(packageAbsDirpath string) (string, error) {
func getComposeFilenameAndContent(packageAbsDirpath string) (string, []byte, error) {
for _, composeFilename := range supportedComposeFilenames {
composeFilepath := path.Join(packageAbsDirpath, composeFilename)
if _, err := os.Stat(composeFilepath); err != nil {
return composeFilepath, nil
composeBytes, err := os.ReadFile(composeFilepath)
if err != nil {
continue
}

return composeFilename, composeBytes, nil
}

joinedComposeFilenames := strings.Join(supportedComposeFilenames, ", ")
return "", stacktrace.NewError("Failed to transpile Docker Compose package to Starlark because no Compose file was found at the package root after looking for the following files: %s", joinedComposeFilenames)
return "", nil, stacktrace.NewError("Failed to transpile Docker Compose package to Starlark because no Compose file was found at the package root after looking for the following files: %s", joinedComposeFilenames)
}

// TODO(victor.colombo): Have a better UX letting people know ports have been remapped
// NOTE: This returns Go errors, not
func convertComposeProjectToStarlark(compose *types.Project) (string, map[string]string, error) {
return `
def run(plan):
plan.print("It's working!")
`, nil, nil
func convertComposeToStarlark(composeBytes []byte, envVars map[string]string) (string, error) {
project, err := loader.Load(types.ConfigDetails{ //nolint:exhaustruct
// Note that we might be able to use the WorkingDir property instead, to parse the entire directory
ConfigFiles: []types.ConfigFile{{
Content: composeBytes,
}},
Environment: envVars,
})
if err != nil {
return "", stacktrace.Propagate(err, "An error occurred parsing the Compose file in preparation for Stalrark transpilation")
}

serviceStarlarks := map[string]string{}
requiredFileUploads := map[string]string{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package docker_compose_tranpsiler

import (
"github.com/stretchr/testify/require"
"testing"
)

func TestMinecraftCompose(t *testing.T) {
composeBytes := []byte(`
services:
minecraft:
image: itzg/minecraft-server
ports:
- "25565:25565"
environment:
EULA: "TRUE"
deploy:
resources:
limits:
memory: 1.5G
volumes:
- "~/minecraft_data:/data"
`)
expectedResult := `def run(plan):
plan.add_service(name="minecraft",config=ServiceConfig(image="itzg/minecraft-server",ports={"port0":PortSpec(number=25565)},env_vars={"EULA":"TRUE"}))
`

result, err := convertComposeToStarlark(composeBytes, map[string]string{})
require.NoError(t, err)
require.Equal(t, expectedResult, result)
}
Original file line number Diff line number Diff line change
@@ -526,6 +526,8 @@ func TestCloneReplacedPackagesIfNeeded_Succeeds(t *testing.T) {
require.True(t, fileInfo.IsDir())
}

// TODO https://github.com/kurtosis-tech/kurtosis/issues/1637 needs to be fixed and these uncommented!
/*
func Test_isSamePackageLocalAbsoluteLocator_TestDetectionInSubpath(t *testing.T) {
result := isSamePackageLocalAbsoluteLocator("github.com/foo/bar/bang/lib.star", "github.com/foo/bar/main.star")
require.True(t, result)
@@ -535,6 +537,7 @@ func Test_isSamePackageLocalAbsoluteLocator_TestDetectionInDifferentSubdirectori
result := isSamePackageLocalAbsoluteLocator("github.com/author/package/subdir1/file1.star", "github.com/author/package/subdir2/file2.star")
require.True(t, result)
}
*/

func createKurtosisYml(packageName string) *yaml_parser.KurtosisYaml {
return &yaml_parser.KurtosisYaml{

0 comments on commit 23156f4

Please sign in to comment.