Skip to content

Commit

Permalink
Isolate YAML/jsonnet encoding functions into their own package
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Jun 13, 2023
1 parent aa6b2fb commit 0a3fa15
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 74 deletions.
File renamed without changes.
57 changes: 57 additions & 0 deletions pkg/encoding/jsonnet.go
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
}
2 changes: 1 addition & 1 deletion pkg/grizzly/jsonnet.go → pkg/encoding/jsonnetplumbing.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package grizzly
package encoding

import (
"path/filepath"
Expand Down
50 changes: 50 additions & 0 deletions pkg/encoding/yaml.go
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)
}
10 changes: 5 additions & 5 deletions pkg/grafana/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os/exec"
"strings"

"github.com/goccy/go-yaml"
"github.com/grafana/grizzly/pkg/encoding"
"github.com/grafana/grizzly/pkg/grizzly"
)

Expand Down Expand Up @@ -42,7 +42,7 @@ func getRemoteRuleGroup(uid string) (*grizzly.Resource, error) {
return nil, err
}
groupings := map[string][]PrometheusRuleGroup{}
err = yaml.Unmarshal(out, &groupings)
err = encoding.UnmarshalYAML(out, &groupings)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -71,7 +71,7 @@ func getRemoteRuleGroupList() ([]string, error) {
return nil, err
}
groupings := map[string][]PrometheusRuleGroup{}
err = yaml.Unmarshal(out, &groupings)
err = encoding.UnmarshalYAML(out, &groupings)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -118,11 +118,11 @@ func writeRuleGroup(resource grizzly.Resource) error {
Namespace: resource.GetMetadata("namespace"),
Groups: []PrometheusRuleGroup{newGroup},
}
out, err := yaml.Marshal(grouping)
out, err := encoding.MarshalYAML(grouping)
if err != nil {
return err
}
os.WriteFile(tmpfile.Name(), out, 0644)
os.WriteFile(tmpfile.Name(), []byte(out), 0644)
output, err := cortexTool("rules", "load", tmpfile.Name())
if err != nil {
log.Println("OUTPUT", output)
Expand Down
6 changes: 3 additions & 3 deletions pkg/grafana/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"testing"

"github.com/goccy/go-yaml"
"github.com/grafana/grizzly/pkg/encoding"
"github.com/grafana/grizzly/pkg/grizzly"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestRules(t *testing.T) {
spec := make(map[string]interface{})
file, err := os.ReadFile("testdata/rules.yaml")
require.NoError(t, err)
err = yaml.Unmarshal(file, &spec)
err = encoding.UnmarshalYAML(file, &spec)
require.NoError(t, err)

resource := grizzly.NewResource("apiV", "kind", "name", spec)
Expand All @@ -89,7 +89,7 @@ func TestRules(t *testing.T) {
spec := make(map[string]interface{})
file, err := os.ReadFile("testdata/rules.yaml")
require.NoError(t, err)
err = yaml.Unmarshal(file, &spec)
err = encoding.UnmarshalYAML(file, &spec)
require.NoError(t, err)

resource := grizzly.NewResource("apiV", "kind", "name", spec)
Expand Down
61 changes: 3 additions & 58 deletions pkg/grizzly/parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@ package grizzly

import (
"bufio"
_ "embed"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strconv"

"github.com/goccy/go-yaml"
"github.com/google/go-jsonnet"
"github.com/grafana/tanka/pkg/jsonnet/native"
"github.com/grafana/grizzly/pkg/encoding"
"github.com/grafana/tanka/pkg/kubernetes/manifest"
"github.com/grafana/tanka/pkg/process"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -68,7 +63,7 @@ func ParseYAML(yamlFile string, opts Opts) (Resources, error) {
return nil, err
}
reader := bufio.NewReader(f)
decoder := yaml.NewDecoder(reader)
decoder := encoding.NewYAMLDecoder(reader)
manifests := map[string]manifest.Manifest{}
var m manifest.Manifest
var resources Resources
Expand Down Expand Up @@ -100,44 +95,12 @@ func ParseYAML(yamlFile string, opts Opts) (Resources, error) {
return resources, nil
}

//go:embed grizzly.jsonnet
var script string

// ParseJsonnet evaluates a jsonnet file and parses it into an object tree
func ParseJsonnet(jsonnetFile string, opts Opts) (Resources, 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()
extracted, err := encoding.ParseJsonnet(jsonnetFile, opts.JsonnetPaths)
if err != nil {
return nil, err
}
vm.Importer(newExtendedImporter(jsonnetFile, currentWorkingDirectory, opts.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
}

resources := Resources{}
for _, m := range extracted {
Expand All @@ -159,21 +122,3 @@ func ParseJsonnet(jsonnetFile string, opts Opts) (Resources, error) {
sort.Sort(resources)
return resources, nil
}

// MarshalYAML takes a resource and renders it to a source file as a YAML string
func MarshalYAML(resource Resource, filename string) error {
y, err := resource.YAML()
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
}
8 changes: 2 additions & 6 deletions pkg/grizzly/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"

"github.com/gobwas/glob"
"github.com/goccy/go-yaml"
"github.com/grafana/grizzly/pkg/encoding"
"github.com/grafana/tanka/pkg/kubernetes/manifest"
)

Expand Down Expand Up @@ -124,11 +124,7 @@ func (r *Resource) SpecAsJSON() (string, error) {

// YAML Gets the string representation for this resource
func (r *Resource) YAML() (string, error) {
y, err := yaml.Marshal(*r)
if err != nil {
return "", err
}
return string(y), nil
return encoding.MarshalYAML(*r)
}

// MatchesTarget identifies whether a resource is in a target list
Expand Down
3 changes: 2 additions & 1 deletion pkg/grizzly/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"text/tabwriter"

"github.com/grafana/grizzly/pkg/encoding"
"github.com/grafana/grizzly/pkg/grizzly/notifier"
"github.com/grafana/grizzly/pkg/term"
"github.com/pmezard/go-difflib/difflib"
Expand Down Expand Up @@ -139,7 +140,7 @@ func Pull(resourcePath string, opts Opts) error {
}

path := filepath.Join(resourcePath, handler.ResourceFilePath(*resource, "yaml"))
err = MarshalYAML(*resource, path)
err = encoding.MarshalYAMLFile(*resource, path)
if err != nil {
return err
}
Expand Down

0 comments on commit 0a3fa15

Please sign in to comment.