forked from komodorio/validkube
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to generate png with inframap (#38)
The APIs for map generation via inframap are updated to support generating png representations of the map using GraphViz's `dot` command. For this purpose, graphviz is installed in the Docker image, and an option struct is added to `api.InfraMap()`. The command line interface is slightly refactored to allow for tool-specific options in order to support this new feature. The ability to generate png is available both via the command line and the REST API. The /map endpoint will accept a "png" boolean attribute in the JSON body. Co-authored-by: Ido Perlmuter <ido.perlmuter@gofirefly.io> Approved-by: Sefi Genis <sefi@gofirefly.io>
- Loading branch information
Showing
9 changed files
with
253 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,37 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"github.com/thoas/go-funk" | ||
"os" | ||
"os/exec" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/thoas/go-funk" | ||
) | ||
|
||
var InfraCostExec = getEnv("INFRACOST_EXEC", fmt.Sprintf("%s/infracost", BIN_PATH)) | ||
|
||
func InfraCost(in []byte) ([]byte, error) { | ||
var infraCostApiKey = funk.GetOrElse(os.Getenv("INFRACOST_API_KEY"), "infracost-api-key") | ||
var infraCostApiKey = funk.GetOrElse(os.Getenv("INFRACOST_API_KEY"), "infracost-api-key") | ||
path, err := asTempDir(".tf", in) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer os.Remove(path) // nolint: errcheck | ||
|
||
cmd := exec.Command(InfraCostExec, "breakdown", "--path", path, "--terraform-parse-hcl", "--no-color", "--log-level=error") | ||
cmd.Env = append(cmd.Env, fmt.Sprintf("INFRACOST_API_KEY=%s", infraCostApiKey), fmt.Sprintf("PATH=%s", os.Getenv("PATH"))) | ||
cmd := exec.Command( | ||
InfraCostExec, | ||
"breakdown", | ||
"--path", | ||
path, | ||
"--terraform-parse-hcl", | ||
"--no-color", | ||
"--log-level=error", | ||
) | ||
cmd.Env = append( | ||
cmd.Env, | ||
fmt.Sprintf("INFRACOST_API_KEY=%s", infraCostApiKey), | ||
fmt.Sprintf("PATH=%s", os.Getenv("PATH")), | ||
) | ||
return cmd.CombinedOutput() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,86 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
var InfraMapExec = getEnv("INFRAMAP_EXEC", fmt.Sprintf("%s/inframap", BIN_PATH)) | ||
var InfraMapExec = getEnv("INFRAMAP_EXEC", fmt.Sprintf("%s/inframap", BIN_PATH)) | ||
|
||
func InfraMap(in []byte) ([]byte, error) { | ||
type InfraMapOpts struct { | ||
Png bool | ||
} | ||
|
||
func InfraMap(in []byte, opts InfraMapOpts) ([]byte, error) { | ||
path, err := asTempFile("", "", in) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer os.Remove(path) // nolint: errcheck | ||
|
||
return exec.Command(InfraMapExec, "generate", "--raw", "--show-icons=true", "--connections=false", "--clean=false", path).CombinedOutput() | ||
args := []string{ | ||
"generate", | ||
"--show-icons=true", | ||
"--connections=false", | ||
"--clean=false", | ||
} | ||
|
||
if !opts.Png { | ||
args = append(args, "--raw") | ||
} | ||
|
||
cmd := exec.Command(InfraMapExec, append(args, path)...) | ||
|
||
if opts.Png { | ||
// get a pipe of stdout, we're going to pipe output to dot (from graphviz) | ||
stdout, err := cmd.StdoutPipe() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed getting stdout pipe of inframap: %w", err) | ||
} | ||
|
||
if err = cmd.Start(); err != nil { | ||
return nil, fmt.Errorf("failed starting inframap: %w", err) | ||
} | ||
|
||
dot := exec.Command("dot", "-Tpng") | ||
stdin, err := dot.StdinPipe() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed getting stdin pipe for dot: %w", err) | ||
} | ||
|
||
var pipeErr error | ||
|
||
go func() { | ||
defer stdin.Close() | ||
|
||
// copy from inframap's stdout to dot's stdin | ||
r := io.TeeReader(stdout, stdin) | ||
if _, err := io.ReadAll(r); err != nil { | ||
pipeErr = fmt.Errorf("failed reading from inframap: %w", err) | ||
return | ||
} | ||
|
||
// wait for inframap to exit | ||
if err = cmd.Wait(); err != nil { | ||
pipeErr = fmt.Errorf("inframap failed: %w", err) | ||
return | ||
} | ||
}() | ||
|
||
out, err := dot.CombinedOutput() | ||
if err != nil { | ||
return nil, fmt.Errorf("dot failed: %w", err) | ||
} | ||
|
||
if pipeErr != nil { | ||
return nil, pipeErr | ||
} | ||
|
||
return out, nil | ||
} | ||
|
||
return cmd.CombinedOutput() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
|
Oops, something went wrong.