From e017d04a16cedcbfc39f26e4c023d24fc6f7e8cb Mon Sep 17 00:00:00 2001 From: Jingfang Liu Date: Mon, 11 Jun 2018 12:05:48 -0700 Subject: [PATCH 1/3] make generated configmaps composable --- pkg/resmap/resmap.go | 2 ++ pkg/resource/resource.go | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/pkg/resmap/resmap.go b/pkg/resmap/resmap.go index 98b8629c1..c282ac1d6 100644 --- a/pkg/resmap/resmap.go +++ b/pkg/resmap/resmap.go @@ -222,11 +222,13 @@ func MergeWithOverride(maps ...ResMap) (ResMap, error) { glog.V(4).Infof("Replace %v with %v", result[id].Object, r.Object) r.Replace(result[id]) result[id] = r + result[id].ChangeBehavior(resource.BehaviorCreate) case resource.BehaviorMerge: glog.V(4).Infof("Merging %v with %v", result[id].Object, r.Object) r.Merge(result[id]) result[id] = r glog.V(4).Infof("Merged object is %v", result[id].Object) + result[id].ChangeBehavior(resource.BehaviorCreate) default: return nil, fmt.Errorf("Id %#v exists; must merge or replace.", id) } diff --git a/pkg/resource/resource.go b/pkg/resource/resource.go index 1a444d8e3..869470617 100644 --- a/pkg/resource/resource.go +++ b/pkg/resource/resource.go @@ -61,6 +61,11 @@ func (r *Resource) Behavior() GenerationBehavior { return r.b } +// ChangeBehavior changes the resource to the new behavior +func (r *Resource) ChangeBehavior(b GenerationBehavior) { + r.b = b +} + // Id returns the ResId for the resource. func (r *Resource) Id() ResId { return NewResId(r.GroupVersionKind(), r.GetName()) From 37720765fc8ff3000a8bbdf25a418675f9604e09 Mon Sep 17 00:00:00 2001 From: Jingfang Liu Date: Mon, 11 Jun 2018 12:18:26 -0700 Subject: [PATCH 2/3] Add a test case --- .../base/myapp/mycomponent/kustomization.yaml | 6 ++++++ .../myapp/mycomponent2/kustomization.yaml | 6 ++++++ .../testcase-configmaps/expected.diff | 16 ++++++++++++++ .../testcase-configmaps/expected.yaml | 21 +++++++++++++++++++ .../overlay/dev/kustomization.yaml | 9 ++++++++ .../dev/myapp/mycomponent/kustomization.yaml | 7 +++++++ .../dev/myapp/mycomponent2/kustomization.yaml | 7 +++++++ .../testdata/testcase-configmaps/test.yaml | 5 +++++ 8 files changed, 77 insertions(+) create mode 100644 pkg/commands/testdata/testcase-configmaps/base/myapp/mycomponent/kustomization.yaml create mode 100644 pkg/commands/testdata/testcase-configmaps/base/myapp/mycomponent2/kustomization.yaml create mode 100644 pkg/commands/testdata/testcase-configmaps/expected.diff create mode 100644 pkg/commands/testdata/testcase-configmaps/expected.yaml create mode 100644 pkg/commands/testdata/testcase-configmaps/overlay/dev/kustomization.yaml create mode 100644 pkg/commands/testdata/testcase-configmaps/overlay/dev/myapp/mycomponent/kustomization.yaml create mode 100644 pkg/commands/testdata/testcase-configmaps/overlay/dev/myapp/mycomponent2/kustomization.yaml create mode 100644 pkg/commands/testdata/testcase-configmaps/test.yaml diff --git a/pkg/commands/testdata/testcase-configmaps/base/myapp/mycomponent/kustomization.yaml b/pkg/commands/testdata/testcase-configmaps/base/myapp/mycomponent/kustomization.yaml new file mode 100644 index 000000000..a1dafe5fb --- /dev/null +++ b/pkg/commands/testdata/testcase-configmaps/base/myapp/mycomponent/kustomization.yaml @@ -0,0 +1,6 @@ +namePrefix: com1- +configMapGenerator: +- name: com1 + behavior: create + literals: + - from=base diff --git a/pkg/commands/testdata/testcase-configmaps/base/myapp/mycomponent2/kustomization.yaml b/pkg/commands/testdata/testcase-configmaps/base/myapp/mycomponent2/kustomization.yaml new file mode 100644 index 000000000..2228e5053 --- /dev/null +++ b/pkg/commands/testdata/testcase-configmaps/base/myapp/mycomponent2/kustomization.yaml @@ -0,0 +1,6 @@ +namePrefix: com2- +configMapGenerator: +- name: com2 + behavior: create + literals: + - from=base \ No newline at end of file diff --git a/pkg/commands/testdata/testcase-configmaps/expected.diff b/pkg/commands/testdata/testcase-configmaps/expected.diff new file mode 100644 index 000000000..d47a3ee21 --- /dev/null +++ b/pkg/commands/testdata/testcase-configmaps/expected.diff @@ -0,0 +1,16 @@ +diff -u -N /tmp/noop/v1_ConfigMap_com1.yaml /tmp/transformed/v1_ConfigMap_com1.yaml +--- /tmp/noop/v1_ConfigMap_com1.yaml YYYY-MM-DD HH:MM:SS ++++ /tmp/transformed/v1_ConfigMap_com1.yaml YYYY-MM-DD HH:MM:SS +@@ -1,9 +1,11 @@ + apiVersion: v1 + data: ++ baz: qux ++ foo: bar + from: overlay + kind: ConfigMap + metadata: + annotations: {} + creationTimestamp: null + labels: {} +- name: com1-com1-8mk424df76 ++ name: com1-com1-687g9gmkdf diff --git a/pkg/commands/testdata/testcase-configmaps/expected.yaml b/pkg/commands/testdata/testcase-configmaps/expected.yaml new file mode 100644 index 000000000..ed24ea891 --- /dev/null +++ b/pkg/commands/testdata/testcase-configmaps/expected.yaml @@ -0,0 +1,21 @@ +apiVersion: v1 +data: + baz: qux + foo: bar + from: overlay +kind: ConfigMap +metadata: + annotations: {} + creationTimestamp: null + labels: {} + name: com1-com1-687g9gmkdf +--- +apiVersion: v1 +data: + from: overlay +kind: ConfigMap +metadata: + annotations: {} + creationTimestamp: null + labels: {} + name: com2-com2-bcd85m642t diff --git a/pkg/commands/testdata/testcase-configmaps/overlay/dev/kustomization.yaml b/pkg/commands/testdata/testcase-configmaps/overlay/dev/kustomization.yaml new file mode 100644 index 000000000..1ccbeefb7 --- /dev/null +++ b/pkg/commands/testdata/testcase-configmaps/overlay/dev/kustomization.yaml @@ -0,0 +1,9 @@ +bases: +- myapp/mycomponent +- myapp/mycomponent2 +configMapGenerator: +- name: com1 + behavior: merge + literals: + - foo=bar + - baz=qux \ No newline at end of file diff --git a/pkg/commands/testdata/testcase-configmaps/overlay/dev/myapp/mycomponent/kustomization.yaml b/pkg/commands/testdata/testcase-configmaps/overlay/dev/myapp/mycomponent/kustomization.yaml new file mode 100644 index 000000000..0ff7d2083 --- /dev/null +++ b/pkg/commands/testdata/testcase-configmaps/overlay/dev/myapp/mycomponent/kustomization.yaml @@ -0,0 +1,7 @@ +bases: +- ../../../../base/myapp/mycomponent +configMapGenerator: +- name: com1 + behavior: merge + literals: + - from=overlay diff --git a/pkg/commands/testdata/testcase-configmaps/overlay/dev/myapp/mycomponent2/kustomization.yaml b/pkg/commands/testdata/testcase-configmaps/overlay/dev/myapp/mycomponent2/kustomization.yaml new file mode 100644 index 000000000..45d1eedd1 --- /dev/null +++ b/pkg/commands/testdata/testcase-configmaps/overlay/dev/myapp/mycomponent2/kustomization.yaml @@ -0,0 +1,7 @@ +bases: +- ../../../../base/myapp/mycomponent2 +configMapGenerator: +- name: com2 + behavior: merge + literals: + - from=overlay diff --git a/pkg/commands/testdata/testcase-configmaps/test.yaml b/pkg/commands/testdata/testcase-configmaps/test.yaml new file mode 100644 index 000000000..633d415a7 --- /dev/null +++ b/pkg/commands/testdata/testcase-configmaps/test.yaml @@ -0,0 +1,5 @@ +description: configmap generator overlay +args: [] +filename: testdata/testcase-configmaps/overlay/dev +expectedStdout: testdata/testcase-configmaps/expected.yaml +expectedDiff: testdata/testcase-configmaps/expected.diff From 934e37d781d1199e5f56cd2b63bde72fcfd41d0c Mon Sep 17 00:00:00 2001 From: Jingfang Liu Date: Mon, 11 Jun 2018 14:52:31 -0700 Subject: [PATCH 3/3] add unit test and address comments --- .../base/myapp/mycomponent/kustomization.yaml | 2 +- .../myapp/mycomponent2/kustomization.yaml | 4 +- .../testcase-configmaps/expected.diff | 4 +- .../testcase-configmaps/expected.yaml | 4 +- pkg/resmap/resmap.go | 4 +- pkg/resmap/resmap_test.go | 62 ++++++++++++++++++- pkg/resource/resource.go | 4 +- 7 files changed, 72 insertions(+), 12 deletions(-) diff --git a/pkg/commands/testdata/testcase-configmaps/base/myapp/mycomponent/kustomization.yaml b/pkg/commands/testdata/testcase-configmaps/base/myapp/mycomponent/kustomization.yaml index a1dafe5fb..8b9f791b6 100644 --- a/pkg/commands/testdata/testcase-configmaps/base/myapp/mycomponent/kustomization.yaml +++ b/pkg/commands/testdata/testcase-configmaps/base/myapp/mycomponent/kustomization.yaml @@ -1,4 +1,4 @@ -namePrefix: com1- +namePrefix: p1- configMapGenerator: - name: com1 behavior: create diff --git a/pkg/commands/testdata/testcase-configmaps/base/myapp/mycomponent2/kustomization.yaml b/pkg/commands/testdata/testcase-configmaps/base/myapp/mycomponent2/kustomization.yaml index 2228e5053..da1615aad 100644 --- a/pkg/commands/testdata/testcase-configmaps/base/myapp/mycomponent2/kustomization.yaml +++ b/pkg/commands/testdata/testcase-configmaps/base/myapp/mycomponent2/kustomization.yaml @@ -1,6 +1,6 @@ -namePrefix: com2- +namePrefix: p2- configMapGenerator: - name: com2 behavior: create literals: - - from=base \ No newline at end of file + - from=base diff --git a/pkg/commands/testdata/testcase-configmaps/expected.diff b/pkg/commands/testdata/testcase-configmaps/expected.diff index d47a3ee21..7803718cb 100644 --- a/pkg/commands/testdata/testcase-configmaps/expected.diff +++ b/pkg/commands/testdata/testcase-configmaps/expected.diff @@ -12,5 +12,5 @@ diff -u -N /tmp/noop/v1_ConfigMap_com1.yaml /tmp/transformed/v1_ConfigMap_com1.y annotations: {} creationTimestamp: null labels: {} -- name: com1-com1-8mk424df76 -+ name: com1-com1-687g9gmkdf +- name: p1-com1-cmdb776d5b ++ name: p1-com1-dhbbm922gd diff --git a/pkg/commands/testdata/testcase-configmaps/expected.yaml b/pkg/commands/testdata/testcase-configmaps/expected.yaml index ed24ea891..be56db9c2 100644 --- a/pkg/commands/testdata/testcase-configmaps/expected.yaml +++ b/pkg/commands/testdata/testcase-configmaps/expected.yaml @@ -8,7 +8,7 @@ metadata: annotations: {} creationTimestamp: null labels: {} - name: com1-com1-687g9gmkdf + name: p1-com1-dhbbm922gd --- apiVersion: v1 data: @@ -18,4 +18,4 @@ metadata: annotations: {} creationTimestamp: null labels: {} - name: com2-com2-bcd85m642t + name: p2-com2-c4b8md75k9 diff --git a/pkg/resmap/resmap.go b/pkg/resmap/resmap.go index c282ac1d6..81c597a2f 100644 --- a/pkg/resmap/resmap.go +++ b/pkg/resmap/resmap.go @@ -222,13 +222,13 @@ func MergeWithOverride(maps ...ResMap) (ResMap, error) { glog.V(4).Infof("Replace %v with %v", result[id].Object, r.Object) r.Replace(result[id]) result[id] = r - result[id].ChangeBehavior(resource.BehaviorCreate) + result[id].SetBehavior(resource.BehaviorCreate) case resource.BehaviorMerge: glog.V(4).Infof("Merging %v with %v", result[id].Object, r.Object) r.Merge(result[id]) result[id] = r glog.V(4).Infof("Merged object is %v", result[id].Object) - result[id].ChangeBehavior(resource.BehaviorCreate) + result[id].SetBehavior(resource.BehaviorCreate) default: return nil, fmt.Errorf("Id %#v exists; must merge or replace.", id) } diff --git a/pkg/resmap/resmap_test.go b/pkg/resmap/resmap_test.go index 187a7c21c..19849000a 100644 --- a/pkg/resmap/resmap_test.go +++ b/pkg/resmap/resmap_test.go @@ -151,7 +151,7 @@ metadata: } } -func TestMerge(t *testing.T) { +func TestMergeWithoutOverride(t *testing.T) { input1 := ResMap{ resource.NewResId(deploy, "deploy1"): resource.NewResourceFromMap( map[string]interface{}{ @@ -199,3 +199,63 @@ func TestMerge(t *testing.T) { t.Fatalf("%#v doesn't equal expected %#v", merged, expected) } } + +func TestMergeWithOverride(t *testing.T) { + input1 := ResMap{ + resource.NewResId(cmap, "cmap"): resource.NewResourceFromMap( + map[string]interface{}{ + "apiVersion": "apps/v1", + "kind": "ConfigMap", + "metadata": map[string]interface{}{ + "name": "cmap", + }, + "data": map[string]interface{}{ + "a": "x", + "b": "y", + }, + }), + } + input2 := ResMap{ + resource.NewResId(cmap, "cmap"): resource.NewResourceFromMap( + map[string]interface{}{ + "apiVersion": "apps/v1", + "kind": "ConfigMap", + "metadata": map[string]interface{}{ + "name": "cmap", + }, + "data": map[string]interface{}{ + "a": "u", + "b": "v", + "c": "w", + }, + }), + } + input1[resource.NewResId(cmap, "cmap")].SetBehavior(resource.BehaviorCreate) + input2[resource.NewResId(cmap, "cmap")].SetBehavior(resource.BehaviorMerge) + input := []ResMap{input1, input2} + expected := ResMap{ + resource.NewResId(cmap, "cmap"): resource.NewResourceFromMap( + map[string]interface{}{ + "apiVersion": "apps/v1", + "kind": "ConfigMap", + "metadata": map[string]interface{}{ + "annotations": map[string]interface{}{}, + "labels": map[string]interface{}{}, + "name": "cmap", + }, + "data": map[string]interface{}{ + "a": "u", + "b": "v", + "c": "w", + }, + }), + } + expected[resource.NewResId(cmap, "cmap")].SetBehavior(resource.BehaviorCreate) + merged, err := MergeWithOverride(input...) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if !reflect.DeepEqual(merged, expected) { + t.Fatalf("%#v doesn't equal expected %#v", merged, expected) + } +} diff --git a/pkg/resource/resource.go b/pkg/resource/resource.go index 869470617..cdd8ec622 100644 --- a/pkg/resource/resource.go +++ b/pkg/resource/resource.go @@ -61,8 +61,8 @@ func (r *Resource) Behavior() GenerationBehavior { return r.b } -// ChangeBehavior changes the resource to the new behavior -func (r *Resource) ChangeBehavior(b GenerationBehavior) { +// SetBehavior changes the resource to the new behavior +func (r *Resource) SetBehavior(b GenerationBehavior) { r.b = b }