Browse Source

Merge "Add cluster transformations"

Jenkins 2 years ago
parent
commit
1b63629862

+ 42
- 0
cluster_upgrade/tests/test_transformations.py View File

@@ -17,6 +17,7 @@ from nailgun.test import base as nailgun_test_base
17 17
 import six
18 18
 
19 19
 from .. import transformations
20
+from ..transformations import cluster
20 21
 
21 22
 
22 23
 class TestTransformations(nailgun_test_base.BaseUnitTest):
@@ -177,3 +178,44 @@ class TestLazy(nailgun_test_base.BaseUnitTest):
177 178
         lazy_obj = transformations.Lazy(mgr_cls_mock)
178 179
         lazy_obj.apply()
179 180
         self.assertEqual(lazy_obj.apply, mgr_cls_mock.return_value.apply)
181
+
182
+
183
+class TestClusterTransformers(nailgun_test_base.BaseUnitTest):
184
+    def setUp(self):
185
+        self.data = {
186
+            'editable': {
187
+                'external_dns': {
188
+                    'dns_list': {'type': 'text', 'value': 'a,b,\nc, d'}},
189
+                'external_ntp': {
190
+                    'ntp_list': {'type': 'text', 'value': 'a,b,\nc, d'}},
191
+            },
192
+            'generated': {
193
+                'provision': {},
194
+            },
195
+        }
196
+
197
+    def test_dns_list(self):
198
+        res = cluster.transform_dns_list(self.data)
199
+        self.assertEqual(
200
+            res['editable']['external_dns']['dns_list'],
201
+            {'type': 'text_list', 'value': ['a', 'b', 'c', 'd']},
202
+        )
203
+
204
+    def test_ntp_list(self):
205
+        res = cluster.transform_ntp_list(self.data)
206
+        self.assertEqual(
207
+            res['editable']['external_ntp']['ntp_list'],
208
+            {'type': 'text_list', 'value': ['a', 'b', 'c', 'd']},
209
+        )
210
+
211
+    def test_provision(self):
212
+        res = cluster.drop_generated_provision(self.data)
213
+        self.assertNotIn('provision', res['generated'])
214
+
215
+    def test_manager(self):
216
+        man = cluster.Manager()  # verify default config and entry points
217
+        self.assertEqual(man.transformers, [(version.StrictVersion('9.0'), [
218
+            cluster.transform_dns_list,
219
+            cluster.transform_ntp_list,
220
+            cluster.drop_generated_provision,
221
+        ])])

+ 1
- 4
cluster_upgrade/tests/test_upgrade.py View File

@@ -50,7 +50,7 @@ class TestUpgradeHelperCloneCluster(base_tests.BaseCloneClusterTest):
50 50
                 {"metadata": "src_fake",
51 51
                  "key":
52 52
                      {"type": "text",
53
-                      "value": "fake1, fake2,fake3 , fake4"},
53
+                      "value": "fake"},
54 54
                  "src_key": "src_data"
55 55
                  },
56 56
             "repo_setup": "src_data"
@@ -69,9 +69,6 @@ class TestUpgradeHelperCloneCluster(base_tests.BaseCloneClusterTest):
69 69
         result = upgrade.merge_attributes(
70 70
             src_editable_attrs, new_editable_attrs
71 71
         )
72
-        new_editable_attrs["test"]["key"]["value"] = [
73
-            "fake1", "fake2", "fake3", "fake4"
74
-        ]
75 72
         self.assertEqual(result, new_editable_attrs)
76 73
 
77 74
     def test_create_cluster_clone(self):

+ 52
- 0
cluster_upgrade/transformations/cluster.py View File

@@ -0,0 +1,52 @@
1
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
2
+# not use this file except in compliance with the License. You may obtain
3
+# a copy of the License at
4
+#
5
+#      http://www.apache.org/licenses/LICENSE-2.0
6
+#
7
+# Unless required by applicable law or agreed to in writing, software
8
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10
+# License for the specific language governing permissions and limitations
11
+# under the License.
12
+
13
+from cluster_upgrade import transformations
14
+
15
+# NOTE: In the mitaka-9.0 release types of values dns_list and
16
+# ntp_list were changed from 'text'
17
+# (a string of comma-separated IP-addresses)
18
+# to 'text_list' (a list of strings of IP-addresses).
19
+
20
+
21
+def transform_to_text_list(data):
22
+    if data['type'] == 'text':
23
+        data['type'] = 'text_list'
24
+        data['value'] = [
25
+            part.strip() for part in data['value'].split(',')
26
+        ]
27
+
28
+    return data
29
+
30
+
31
+def transform_dns_list(data):
32
+    dns_list = data['editable']['external_dns']['dns_list']
33
+    transform_to_text_list(dns_list)
34
+    return data
35
+
36
+
37
+def transform_ntp_list(data):
38
+    ntp_list = data['editable']['external_ntp']['ntp_list']
39
+    transform_to_text_list(ntp_list)
40
+    return data
41
+
42
+
43
+def drop_generated_provision(data):
44
+    data['generated'].pop('provision', None)
45
+    return data
46
+
47
+
48
+class Manager(transformations.Manager):
49
+    default_config = {
50
+        '9.0': ['dns_list', 'ntp_list', 'drop_provision'],
51
+    }
52
+    name = 'cluster'

+ 19
- 28
cluster_upgrade/upgrade.py View File

@@ -25,7 +25,9 @@ from nailgun.extensions.network_manager.objects.serializers import \
25 25
 from nailgun import objects
26 26
 from nailgun import utils
27 27
 
28
+from . import transformations  # That's weird, but that's how hacking likes
28 29
 from .objects import adapters
30
+from .transformations import cluster as cluster_trs
29 31
 
30 32
 
31 33
 def merge_attributes(a, b):
@@ -42,25 +44,9 @@ def merge_attributes(a, b):
42 44
         for key, values in six.iteritems(pairs):
43 45
             if key != "metadata" and key in a_values:
44 46
                 values["value"] = a_values[key]["value"]
45
-                # NOTE: In the mitaka-9.0 release types of values dns_list and
46
-                # ntp_list were changed from 'text'
47
-                # (a string of comma-separated IP-addresses)
48
-                # to 'text_list' (a list of strings of IP-addresses).
49
-                if a_values[key]['type'] == 'text' and \
50
-                        values['type'] == 'text_list':
51
-                    values["value"] = [
52
-                        value.strip() for value in values['value'].split(',')
53
-                    ]
54 47
     return attrs
55 48
 
56 49
 
57
-def merge_generated_attrs(new_attrs, orig_attrs):
58
-    # skip attributes that should be generated for new cluster
59
-    attrs = copy.deepcopy(orig_attrs)
60
-    attrs.pop('provision', None)
61
-    return utils.dict_merge(new_attrs, attrs)
62
-
63
-
64 50
 def merge_nets(a, b):
65 51
     new_settings = copy.deepcopy(b)
66 52
     source_networks = dict((n["name"], n) for n in a["networks"])
@@ -88,6 +74,7 @@ class UpgradeHelper(object):
88 74
         consts.CLUSTER_NET_PROVIDERS.nova_network:
89 75
         network_configuration.NovaNetworkConfigurationSerializer,
90 76
     }
77
+    cluster_transformations = transformations.Lazy(cluster_trs.Manager)
91 78
 
92 79
     @classmethod
93 80
     def clone_cluster(cls, orig_cluster, data):
@@ -111,20 +98,24 @@ class UpgradeHelper(object):
111 98
 
112 99
     @classmethod
113 100
     def copy_attributes(cls, orig_cluster, new_cluster):
114
-        # TODO(akscram): Attributes should be copied including
115
-        #                borderline cases when some parameters are
116
-        #                renamed or moved into plugins. Also, we should
117
-        #                to keep special steps in copying of parameters
118
-        #                that know how to translate parameters from one
119
-        #                version to another. A set of this kind of steps
120
-        #                should define an upgrade path of a particular
121
-        #                cluster.
122
-        new_cluster.generated_attrs = merge_generated_attrs(
101
+        attrs = cls.cluster_transformations.apply(
102
+            orig_cluster.release.environment_version,
103
+            new_cluster.release.environment_version,
104
+            {
105
+                'editable': orig_cluster.editable_attrs,
106
+                'generated': orig_cluster.generated_attrs,
107
+            },
108
+        )
109
+
110
+        new_cluster.generated_attrs = utils.dict_merge(
123 111
             new_cluster.generated_attrs,
124
-            orig_cluster.generated_attrs)
112
+            attrs['generated'],
113
+        )
114
+
125 115
         new_cluster.editable_attrs = merge_attributes(
126
-            orig_cluster.editable_attrs,
127
-            new_cluster.editable_attrs)
116
+            attrs['editable'],
117
+            new_cluster.editable_attrs,
118
+        )
128 119
 
129 120
     @classmethod
130 121
     def change_env_settings(cls, orig_cluster, new_cluster):

+ 4
- 0
setup.cfg View File

@@ -25,3 +25,7 @@ packages =
25 25
 [entry_points]
26 26
 nailgun.extensions =
27 27
     cluster_upgrade = cluster_upgrade.extension:ClusterUpgradeExtension
28
+nailgun.cluster_upgrade.transformations.cluster.9.0 =
29
+    dns_list = cluster_upgrade.transformations.cluster:transform_dns_list
30
+    ntp_list = cluster_upgrade.transformations.cluster:transform_ntp_list
31
+    drop_provision = cluster_upgrade.transformations.cluster:drop_generated_provision

Loading…
Cancel
Save