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

Merger options #53

Merged
merged 2 commits into from
Oct 21, 2021
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
1 change: 1 addition & 0 deletions docs/_docs/config/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ repo_auto_auth | Whether or not to try to auth authorize docker repo registry if
skip | List of resources to skip. Can also be set with the `KUBES_SKIP` env var. `KUBES_SKIP` should be a list of strings separated by spaces. It adds onto the `config.skip` option. | []
state.path | Where to store the state file with the last build Docker image. | .kubes/state/KUBES_ENV/data.json
suffix_hash | Whether or not to append suffix hash to ConfigMap and Secret | true
merger.options | Merger options that control how Hashes are merged. More info: [Merger Options]({% link _docs/layering/merge-options.md %}) | `{overwrite_arrays: true}`
2 changes: 1 addition & 1 deletion docs/_docs/dsl.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ In general:

## Merge Behavior

Generally, the merge should behave as expected. For example, map or Hash fields are merged together from multiple layers. Strings are simply replaced. See more details at [Layering Merge Behavior]({% link _docs/layering/merge.md %})
Generally, the merge should behave as expected. For example, map or Hash fields are merged together from multiple layers. Strings are simply replaced. See more details at [Layering Merge Behavior]({% link _docs/layering/merge-dsl.md %})
2 changes: 1 addition & 1 deletion docs/_docs/layering.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Kubes supports layering files together so you can use the same Kubernetes files
* [YAML Layering]({% link _docs/layering/yaml.md %})
* [DSL Layering]({% link _docs/layering/dsl.md %})
* [Mix Layering]({% link _docs/layering/mix.md %})
* [Merge Behavior]({% link _docs/layering/merge.md %})
* [Merge Behavior]({% link _docs/layering/merge-dsl.md %})
File renamed without changes.
76 changes: 76 additions & 0 deletions docs/_docs/layering/merge-options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
title: Merge Options
---

Underneath the hood, Kubes uses the [danielsdeleo/deep_merge](https://github.com/danielsdeleo/deep_merge) library to merge layers together. You can control the merge behavior options with [config.merger.options]({% link _docs/config/reference.md %}).

The default merge options is:

{overwrite_arrays: true}

You can control the merge behavior by setting:

.kubes/config.rb

```ruby
Kubes.configure do |config|
# ...
config.merger.options = {overwrite_arrays: false}
end
```

See the [danielsdeleo/deep_merge](https://github.com/danielsdeleo/deep_merge) docs for the different options.

## Example of overwrite_arrays false

An example of where you might want to use `{overwrite_arrays: false}` is if you are using YAML and want a base sidecar container in all of your deployments.

.kubes/resources/base/deployment.yaml

```yaml
spec:
template:
spec:
containers:
- name: sidecar
image: sidecar-image
```

.kubes/resources/web/deployment.yaml

```yaml
spec:
template:
metadata:
labels:
role: web
spec:
containers:
- name: web
image: <%= docker_image %>
```

Produces:

```yaml
# ...
spec:
template:
metadata:
labels:
role: web
spec:
containers:
- name: sidecar
image: sidecar-image
- name: web
image: gcr.io/boltops-learn/demo:kubes-2021-10-21T18-06-48
# ...
```

However, using this merge behavior will also add additional ports if you are assigning a `targetPort` in the DSL. See:

* [DSL in service creates always default port 80 #45](https://github.com/boltops-tools/kubes/issues/45)
* [use deep_merge overwrite_arrays option #48](https://github.com/boltops-tools/kubes/pull/48)

Will welcome PRs for improvements.
3 changes: 2 additions & 1 deletion docs/_includes/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ <h2><a href="{% link docs.md %}">Docs</a></h2>
<li><a href="{% link _docs/layering/yaml.md %}">YAML</a></li>
<li><a href="{% link _docs/layering/dsl.md %}">DSL</a></li>
<li><a href="{% link _docs/layering/mix.md %}">Mix</a></li>
<li><a href="{% link _docs/layering/merge.md %}">Merge Behavior</a></li>
<li><a href="{% link _docs/layering/merge-dsl.md %}">Merge DSL</a></li>
<li><a href="{% link _docs/layering/merge-options.md %}">Merge Options</a></li>
</ul>
</li>
<li><a href="{% link _docs/dsl.md %}">DSL</a>
Expand Down
3 changes: 3 additions & 0 deletions lib/kubes/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def defaults

config.suffix_hash = true # append suffix hash to ConfigMap and Secret

config.merger = ActiveSupport::OrderedOptions.new
config.merger.options = {overwrite_arrays: true}

config
end

Expand Down
2 changes: 1 addition & 1 deletion lib/kubes/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def check_project!

# wrapper to ensure we use the same deeper_merge options everywhere
def deep_merge!(a, b)
a.deeper_merge!(b, overwrite_arrays: true)
a.deeper_merge!(b, config.merger.options)
a
end
end
Expand Down