Skip to content

Commit

Permalink
Merge pull request #649 from souvikinator/souvik/fix/permission-error…
Browse files Browse the repository at this point in the history
…-convert-docker-compose-to-k8-manifest

fix: use ~/.meshery directory for temporary files instead of working
  • Loading branch information
leecalcote authored Jan 21, 2025
2 parents 5580ec4 + e13116e commit fc0e2ba
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions utils/kubernetes/kompose/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kompose

import (
"os"
"path/filepath"
"strconv"

"github.com/kubernetes/kompose/client"
Expand Down Expand Up @@ -30,14 +31,24 @@ func IsManifestADockerCompose(manifest []byte, schemaURL string) error {
// converts a given docker-compose file into kubernetes manifests
// expects a validated docker-compose file
func Convert(dockerCompose DockerComposeFile) (string, error) {
err := utils.CreateFile(dockerCompose, "temp.data", "./")
// Get user's home directory
homeDir, err := os.UserHomeDir()
if err != nil {
return "", ErrCvrtKompose(err)
}

// Construct path to .meshery directory
mesheryDir := filepath.Join(homeDir, ".meshery")
tempFilePath := filepath.Join(mesheryDir, "temp.data")
resultFilePath := filepath.Join(mesheryDir, "result.yaml")

if err := utils.CreateFile(dockerCompose, "temp.data", mesheryDir); err != nil {
return "", ErrCvrtKompose(err)
}

defer func() {
os.Remove("temp.data")
os.Remove("result.yaml")
os.Remove(tempFilePath)
os.Remove(resultFilePath)
}()

formatComposeFile(&dockerCompose)
Expand All @@ -52,8 +63,8 @@ func Convert(dockerCompose DockerComposeFile) (string, error) {
}

ConvertOpt := client.ConvertOptions{
InputFiles: []string{"temp.data"},
OutFile: "result.yaml",
InputFiles: []string{tempFilePath},
OutFile: resultFilePath,
GenerateNetworkPolicies: true,
}

Expand All @@ -62,7 +73,7 @@ func Convert(dockerCompose DockerComposeFile) (string, error) {
return "", ErrCvrtKompose(err)
}

result, err := os.ReadFile("result.yaml")
result, err := os.ReadFile(resultFilePath)
if err != nil {
return "", ErrCvrtKompose(err)
}
Expand Down

0 comments on commit fc0e2ba

Please sign in to comment.