-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Isolate YAML/jsonnet encoding functions into their own package
- Loading branch information
Showing
9 changed files
with
123 additions
and
74 deletions.
There are no files selected for viewing
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package encoding | ||
|
||
import ( | ||
_ "embed" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/google/go-jsonnet" | ||
"github.com/grafana/tanka/pkg/jsonnet/native" | ||
"github.com/grafana/tanka/pkg/kubernetes/manifest" | ||
"github.com/grafana/tanka/pkg/process" | ||
) | ||
|
||
//go:embed grizzly.jsonnet | ||
var script string | ||
|
||
// ParseJsonnet evaluates a jsonnet file and parses it into an object tree | ||
func ParseJsonnet(jsonnetFile string, jsonnetPaths []string) (map[string]manifest.Manifest, error) { | ||
if _, err := os.Stat(jsonnetFile); os.IsNotExist(err) { | ||
return nil, fmt.Errorf("file does not exist: %s", jsonnetFile) | ||
} | ||
|
||
script := fmt.Sprintf(script, jsonnetFile) | ||
vm := jsonnet.MakeVM() | ||
currentWorkingDirectory, err := os.Getwd() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
vm.Importer(newExtendedImporter(jsonnetFile, currentWorkingDirectory, jsonnetPaths)) | ||
for _, nf := range native.Funcs() { | ||
vm.NativeFunction(nf) | ||
} | ||
|
||
result, err := vm.EvaluateSnippet(jsonnetFile, script) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var data interface{} | ||
if err := json.Unmarshal([]byte(result), &data); err != nil { | ||
return nil, err | ||
} | ||
|
||
extracted, err := process.Extract(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Unwrap *List types | ||
if err := process.Unwrap(extracted); err != nil { | ||
return nil, err | ||
} | ||
|
||
return extracted, nil | ||
} |
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,4 +1,4 @@ | ||
package grizzly | ||
package encoding | ||
|
||
import ( | ||
"path/filepath" | ||
|
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package encoding | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/goccy/go-yaml" | ||
) | ||
|
||
// NewYAMLDecoder returns a YAML decoder configured to unmarshal data from the given reader. | ||
func NewYAMLDecoder(reader io.Reader) *yaml.Decoder { | ||
return yaml.NewDecoder(reader) | ||
} | ||
|
||
// MarshalYAML takes an input and renders as a YAML string. | ||
func MarshalYAML(input any) (string, error) { | ||
y, err := yaml.Marshal(input) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(y), nil | ||
} | ||
|
||
// MarshalYAMLFile takes an input and renders it to a file as a YAML string. | ||
func MarshalYAMLFile(input any, filename string) error { | ||
y, err := MarshalYAML(input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dir := filepath.Dir(filename) | ||
err = os.MkdirAll(dir, 0755) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = os.WriteFile(filename, []byte(y), 0644) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// UnmarshalYAML takes YAML content as input unmarshals it into the destination. | ||
func UnmarshalYAML(input []byte, destination any) error { | ||
return yaml.Unmarshal(input, destination) | ||
} |
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
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