Skip to content

Commit

Permalink
Merge pull request #11 from thestormforge/data-url-fix
Browse files Browse the repository at this point in the history
Fix inline resources
  • Loading branch information
jgustie authored Aug 12, 2021
2 parents ab90eab + 6882f12 commit 7aa8eac
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
5 changes: 5 additions & 0 deletions internal/spec/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ func (p *Parser) Decode(spec string) (interface{}, error) {
return &kio.ByteReader{Reader: p.Reader}, nil
}

// Inline resources
if strings.ContainsRune(spec, '\n') {
return &kio.ByteReader{Reader: strings.NewReader(spec)}, nil
}

// Absolute file path
if filepath.IsAbs(spec) {
return &konjurev1beta2.File{Path: spec}, nil
Expand Down
7 changes: 7 additions & 0 deletions internal/spec/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ func TestParser_Decode(t *testing.T) {
Reader: bytes.NewReader([]byte("Hello, World!")),
},
},
{
desc: "data URI base64",
spec: "data:;base64,SGVsbG8sIFdvcmxkIQ==",
expected: &kio.ByteReader{
Reader: bytes.NewReader([]byte("Hello, World!")),
},
},
{
desc: "helm repo without path",
parser: Parser{HelmRepositoryConfig: HelmRepositoryConfig{Repositories: []HelmRepository{
Expand Down
26 changes: 21 additions & 5 deletions pkg/konjure/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ type Resource struct {
HTTP *konjurev1beta2.HTTP `json:"http,omitempty" yaml:"http,omitempty"`
File *konjurev1beta2.File `json:"file,omitempty" yaml:"file,omitempty"`

// Some specs (default reader, `data:` URLs, inline resources) resolve to a stream.
raw kio.Reader // NOTE: when this is non-nil there MUST be a value for `str`!

// Original string representation this resource was parsed from.
str string
}
Expand All @@ -57,12 +60,20 @@ func NewResource(arg ...string) Resource {
return r
}

// GetRNode returns a KYAML resource node representing this Konjure resource.
func (r *Resource) GetRNode() (*yaml.RNode, error) {
// Read returns a KYAML resource nodes representing this Konjure resource.
func (r *Resource) Read() ([]*yaml.RNode, error) {
if r.raw != nil {
return r.raw.Read()
}

rv := reflect.Indirect(reflect.ValueOf(r))
for i := 0; i < rv.NumField(); i++ {
if f := rv.Field(i); f.Kind() != reflect.String && !f.IsNil() {
return konjurev1beta2.GetRNode(rv.Field(i).Interface())
n, err := konjurev1beta2.GetRNode(rv.Field(i).Interface())
if err != nil {
return nil, err
}
return []*yaml.RNode{n}, nil
}
}

Expand All @@ -78,6 +89,11 @@ func (r *Resource) UnmarshalJSON(bytes []byte) error {
return err
}

if raw, ok := rr.(kio.Reader); ok {
r.raw = raw
return nil
}

rv := reflect.Indirect(reflect.ValueOf(r))
rrv := reflect.ValueOf(rr)
for i := 0; i < rv.NumField(); i++ {
Expand Down Expand Up @@ -143,11 +159,11 @@ var _ kio.Reader = Resources{}
func (rs Resources) Read() ([]*yaml.RNode, error) {
result := make([]*yaml.RNode, 0, len(rs))
for i := range rs {
n, err := rs[i].GetRNode()
nodes, err := rs[i].Read()
if err != nil {
return nil, err
}
result = append(result, n)
result = append(result, nodes...)
}

return result, nil
Expand Down
27 changes: 22 additions & 5 deletions pkg/konjure/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,54 @@ limitations under the License.
package konjure

import (
"bytes"
"encoding/base64"
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
konjurev1beta2 "github.com/thestormforge/konjure/pkg/api/core/v1beta2"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

func TestResource_GetRNode(t *testing.T) {
func TestResource_Read(t *testing.T) {
cases := []struct {
desc string
resource Resource
expected *yaml.RNode
expected []*yaml.RNode
}{
{
desc: "helm",
resource: Resource{
Helm: &konjurev1beta2.Helm{Chart: "test"},
},
expected: mustRNode(&konjurev1beta2.Helm{Chart: "test"}),
expected: []*yaml.RNode{mustRNode(&konjurev1beta2.Helm{Chart: "test"})},
},
{
desc: "git",
resource: Resource{
Git: &konjurev1beta2.Git{Repository: "http://example.com/repo"},
},
expected: mustRNode(&konjurev1beta2.Git{Repository: "http://example.com/repo"}),
expected: []*yaml.RNode{mustRNode(&konjurev1beta2.Git{Repository: "http://example.com/repo"})},
},
}
for _, c := range cases {
t.Run(c.desc, func(t *testing.T) {
actual, err := c.resource.GetRNode()
actual, err := c.resource.Read()
if assert.NoError(t, err) {
assert.Equal(t, c.expected, actual)
}
})
}
}

const testResource = `apiVersion: invalid.example.com/v1
kind: Test
metadata:
name: this-is-a-test
`

func TestResource_UnmarshalJSON(t *testing.T) {
cases := []struct {
desc string
Expand All @@ -81,6 +90,14 @@ func TestResource_UnmarshalJSON(t *testing.T) {
},
},
},
{
desc: "data",
rawJSON: `"data:;base64,` + base64.URLEncoding.EncodeToString([]byte(testResource)) + `"`,
expected: Resource{
raw: &kio.ByteReader{Reader: bytes.NewReader([]byte(testResource))},
str: `data:;base64,` + base64.URLEncoding.EncodeToString([]byte(testResource)),
},
},
}
for _, c := range cases {
t.Run(c.desc, func(t *testing.T) {
Expand Down

0 comments on commit 7aa8eac

Please sign in to comment.