~avishai-ish-shalom/cloud-init/chef-refactor

« back to all changes in this revision

Viewing changes to doc/merging.txt

support different and user-suppliable merging algorithms for cloud-config

This adds a very useful mechanism for merging cloud-config, allowing
the user to append to lists (ie, just add more 'run_cmd') or other
things.

See doc/merging.txt for more information, it is intended to be backwards
compatible by default.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Arriving in 0.7.2 is a new way to handle dictionary merging in cloud-init.
 
2
---
 
3
 
 
4
Overview
 
5
--------
 
6
 
 
7
This was done because it has been a common feature request that there be a
 
8
way to specify how cloud-config yaml "dictionaries" are merged together when
 
9
there are multiple yamls to merge together (say when performing an #include).
 
10
 
 
11
Since previously the merging algorithm was very simple and would only overwrite
 
12
and not append lists, or strings, and so on it was decided to create a new and
 
13
improved way to merge dictionaries (and there contained objects) together in a
 
14
way that is customizable, thus allowing for users who provide cloud-config data
 
15
to determine exactly how there objects will be merged.
 
16
 
 
17
For example.
 
18
 
 
19
#cloud-config (1)
 
20
run_cmd:
 
21
  - bash1
 
22
  - bash2
 
23
 
 
24
#cloud-config (2)
 
25
run_cmd:
 
26
  - bash3
 
27
  - bash4
 
28
 
 
29
The previous way of merging the following 2 objects would result in a final 
 
30
cloud-config object that contains the following.
 
31
 
 
32
#cloud-config (merged)
 
33
run_cmd:
 
34
  - bash3
 
35
  - bash4
 
36
 
 
37
Typically this is not what users want, instead they would likely prefer:
 
38
 
 
39
#cloud-config (merged)
 
40
run_cmd:
 
41
  - bash1
 
42
  - bash2
 
43
  - bash3
 
44
  - bash4
 
45
 
 
46
This way makes it easier to combine the various cloud-config objects you have
 
47
into a more useful list, thus reducing duplication that would have had to
 
48
occur in the previous method to accomplish the same result.
 
49
 
 
50
Customizability
 
51
---------------
 
52
 
 
53
Since the above merging algorithm may not always be the desired merging
 
54
algorithm (like how the merging algorithm in < 0.7.2 was not always the preferred
 
55
one) the concept of customizing how merging can be done was introduced through
 
56
a new concept call 'merge classes'. 
 
57
 
 
58
A merge class is a class defintion which provides functions that can be used
 
59
to merge a given type with another given type.
 
60
 
 
61
An example of one of these merging classes is the following:
 
62
 
 
63
class Merger(object):
 
64
    def __init__(self, merger, opts):
 
65
        self._merger = merger
 
66
        self._overwrite = 'overwrite' in opts
 
67
 
 
68
    # This merging algorithm will attempt to merge with
 
69
    # another dictionary, on encountering any other type of object
 
70
    # it will not merge with said object, but will instead return
 
71
    # the original value
 
72
    #
 
73
    # On encountering a dictionary, it will create a new dictionary
 
74
    # composed of the original and the one to merge with, if 'overwrite'
 
75
    # is enabled then keys that exist in the original will be overwritten
 
76
    # by keys in the one to merge with (and associated values). Otherwise
 
77
    # if not in overwrite mode the 2 conflicting keys themselves will
 
78
    # be merged.
 
79
    def _on_dict(self, value, merge_with):
 
80
        if not isinstance(merge_with, (dict)):
 
81
            return value
 
82
        merged = dict(value)
 
83
        for (k, v) in merge_with.items():
 
84
            if k in merged:
 
85
                if not self._overwrite:
 
86
                    merged[k] = self._merger.merge(merged[k], v)
 
87
                else:
 
88
                    merged[k] = v
 
89
            else:
 
90
                merged[k] = v
 
91
        return merged
 
92
 
 
93
As you can see there is a '_on_dict' method here that will be given a source value
 
94
and a value to merge with. The result will be the merged object. This code itself
 
95
is called by another merging class which 'directs' the merging to happen by
 
96
analyzing the types of the objects to merge and attempting to find a know object
 
97
that will merge that type. I will avoid pasting that here, but it can be found
 
98
in the mergers/__init__.py file (see LookupMerger and UnknownMerger).
 
99
 
 
100
So following the typical cloud-init way of allowing source code to be downloaded
 
101
and used dynamically, it is possible for users to inject there own merging files
 
102
to handle specific types of merging as they choose (the basic ones included will
 
103
handle lists, dicts, and strings). Note how each merge can have options associated
 
104
with it which affect how the merging is performed, for example a dictionary merger
 
105
can be told to overwrite instead of attempt to merge, or a string merger can be
 
106
told to append strings instead of discarding other strings to merge with.
 
107
 
 
108
How to activate
 
109
---------------
 
110
 
 
111
There are a few ways to activate the merging algorithms, and to customize them
 
112
for your own usage.
 
113
 
 
114
1. The first way involves the usage of MIME messages in cloud-init to specify
 
115
   multipart documents (this is one way in which multiple cloud-config is joined
 
116
   together into a single cloud-config). Two new headers are looked for, both
 
117
   of which can define the way merging is done (the first header to exist wins).
 
118
   These new headers (in lookup order) are 'Merge-Type' and 'X-Merge-Type'. The value
 
119
   should be a string which will satisfy the new merging format defintion (see
 
120
   below for this format).
 
121
2. The second way is actually specifying the merge-type in the body of the
 
122
   cloud-config dictionary. There are 2 ways to specify this, either as a string
 
123
   or as a dictionary (see format below). The keys that are looked up for this
 
124
   definition are the following (in order), 'merge_how', 'merge_type'.
 
125
 
 
126
*String format*
 
127
 
 
128
The string format that is expected is the following.
 
129
 
 
130
"classname(option1,option2)+classname2(option3,option4)" (and so on)
 
131
 
 
132
The class name there will be connected to class names used when looking for the
 
133
class that can be used to merge and options provided will be given to the class
 
134
on construction of that class.
 
135
 
 
136
For example, the default string that is used when none is provided is the following:
 
137
 
 
138
"list(extend)+dict()+str(append)"
 
139
 
 
140
*Dictionary format*
 
141
 
 
142
In cases where a dictionary can be used to specify the same information as the
 
143
string format (ie option #2 of above) it can be used, for example.
 
144
 
 
145
merge_how:
 
146
 - name: list
 
147
   settings: [extend]
 
148
 - name: dict
 
149
   settings: []
 
150
 - name: str
 
151
   settings: [append]
 
152
 
 
153
This would be the equivalent format for default string format but in dictionary
 
154
form instead of string form.
 
155
 
 
156
Specifying multiple types and its effect
 
157
----------------------------------------
 
158
 
 
159
Now you may be asking yourself, if I specify a merge-type header or dictionary
 
160
for every cloud-config that I provide, what exactly happens?
 
161
 
 
162
The answer is that when merging, a stack of 'merging classes' is kept, the
 
163
first one on that stack is the default merging classes, this set of mergers
 
164
will be used when the first cloud-config is merged with the initial empty
 
165
cloud-config dictionary. If the cloud-config that was just merged provided a 
 
166
set of merging classes (via the above formats) then those merging classes will
 
167
be pushed onto the stack. Now if there is a second cloud-config to be merged then
 
168
the merging classes from the cloud-config before the first will be used (not the
 
169
default) and so on. This way a cloud-config can decide how it will merge with a
 
170
cloud-config dictionary coming after it.
 
171
 
 
172
Other uses
 
173
----------
 
174
 
 
175
The default merging algorithm for merging conf.d yaml files (which form a initial
 
176
yaml config for cloud-init) was also changed to use this mechanism so its full
 
177
benefits (and customization) can also be used there as well. Other places that
 
178
used the previous merging are also similar now extensible (metadata merging for
 
179
example).