Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes file loading more flexible #78

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions pkg/patterns/declarative/pkg/manifest/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
// Objects holds a collection of objects, so that we can filter / sequence them
type Objects struct {
Items []*Object
Blobs [][] byte
}

type Object struct {
Expand Down Expand Up @@ -331,17 +332,18 @@ func ParseObjects(ctx context.Context, manifest string) (*Objects, error) {
out := &unstructured.Unstructured{}
err := decoder.Decode(out)
if err != nil {
log.WithValues("yaml", yaml).Error(err, "error decoding object")
return nil, fmt.Errorf("error decoding object: %v", err)
log.V(2).Info("Unable to parse into Unstructured, Storing as blob")
objects.Blobs = append(objects.Blobs, []byte(yaml))
} else {
// We don't reuse the manifest because it's probably yaml, and we want to use json
// json = yaml
o, err := NewObject(out)
if err != nil {
return nil, err
}
objects.Items = append(objects.Items, o)
}

// We don't reuse the manifest because it's probably yaml, and we want to use json
// json = yaml
o, err := NewObject(out)
if err != nil {
return nil, err
}
objects.Items = append(objects.Items, o)
}

return objects, nil
Expand Down
155 changes: 155 additions & 0 deletions pkg/patterns/declarative/pkg/manifest/objects_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package manifest

import (
"context"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"testing"
)

func Test_Object(t *testing.T) {
tests := []struct{
name string
inputManifest string
expectedObject []*Object
expectedBlobs []string
}{
{
name: "simple applied manifest",
inputManifest: `---
apiVersion: v1
kind: ServiceAccount
metadata:
name: foo-operator
namespace: kube-system`,
expectedObject: []*Object{
{
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "ServiceAccount",
"metadata": map[string]interface{}{
"name": "foo-operator",
"namespace": "kube-system",
},
},
},
},
},
expectedBlobs: []string{},
},
{
name: "simple kustomization manifest",
inputManifest: `---
resources:
- services.yaml
- deployment.yaml
configMapGenerator:
- name: coredns
namespace: kube-system
files:
- Corefile`,
expectedObject: []*Object{},
expectedBlobs:[]string{
`resources:
- services.yaml
- deployment.yaml
configMapGenerator:
- name: coredns
namespace: kube-system
files:
- Corefile
`,
},
},
{
name: "a simple and kustomization manifest",
inputManifest: `---
apiVersion: v1
kind: ServiceAccount
metadata:
name: foo-operator
namespace: kube-system
---
resources:
- services.yaml
- deployment.yaml
configMapGenerator:
- name: coredns
namespace: kube-system
files:
- Corefile`,
expectedObject: []*Object{
{
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "ServiceAccount",
"metadata": map[string]interface{}{
"name": "foo-operator",
"namespace": "kube-system",
},
},
},
},
},
expectedBlobs:[]string{
`resources:
- services.yaml
- deployment.yaml
configMapGenerator:
- name: coredns
namespace: kube-system
files:
- Corefile
`,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
returnedObj, err := ParseObjects(ctx, tt.inputManifest)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}

if len(tt.expectedObject) != len(returnedObj.Items) {
t.Errorf("Expected length of %v to be %v but is %v", returnedObj.Items, len(tt.expectedObject),
len(returnedObj.Items))
}

if len(tt.expectedBlobs) != len(returnedObj.Blobs) {
t.Errorf("Expected length of %v to be %v but is %v", returnedObj.Blobs, len(tt.expectedBlobs),
len(returnedObj.Blobs))
}

for i, actual := range returnedObj.Blobs {
actualStr := string(actual)
expectedStr := tt.expectedBlobs[i]
if expectedStr != actualStr {
t.Fatalf("unexpected result, expected ========\n%v\n\nactual ========\n%v\n", expectedStr, actualStr)
}
}

for i, actual := range returnedObj.Items {
actualBytes, err := actual.JSON()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
expectedBytes, err := tt.expectedObject[i].JSON()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
actualStr := string(actualBytes)
expectedStr := string(expectedBytes)
if expectedStr != actualStr {
t.Fatalf("unexpected result, expected ========\n%v\n\nactual ========\n%v\n", expectedStr, actualStr)
}
}
})
}
}