Skip to content

Commit

Permalink
Merge pull request #78 from SomtochiAma/kustomize-integration
Browse files Browse the repository at this point in the history
Makes file loading more flexible
  • Loading branch information
k8s-ci-robot authored Apr 13, 2020
2 parents 97d9e01 + 44e21ea commit 0284e6e
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 9 deletions.
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)
}
}
})
}
}



0 comments on commit 0284e6e

Please sign in to comment.