Browse Source

Support new version of Calico (2.0)

With new Golang version of `calicoctl` (v.1.0.0-beta
and older), command line interface was changed.
Modified tests, so both old and new versions are
supported.

Change-Id: Id33089d3b184a04624b99a160467e860fa1e0556
Artem Panchenko 2 years ago
parent
commit
28b1e600c0
1 changed files with 60 additions and 7 deletions
  1. 60
    7
      fuel_ccp_tests/helpers/netchecker.py

+ 60
- 7
fuel_ccp_tests/helpers/netchecker.py View File

@@ -12,6 +12,7 @@
12 12
 #    License for the specific language governing permissions and limitations
13 13
 #    under the License.
14 14
 
15
+from distutils import version
15 16
 import requests
16 17
 
17 18
 from devops.helpers import helpers
@@ -183,6 +184,30 @@ NETCHECKER_DS_CFG = [
183 184
     }
184 185
 ]
185 186
 
187
+NETCHECKER_BLOCK_POLICY = {
188
+    "kind": "policy",
189
+    "spec": {
190
+        "ingress": [
191
+            {
192
+                "action": "allow"
193
+            },
194
+            {
195
+                "action": "deny",
196
+                "destination": {
197
+                    "ports": [
198
+                        NETCHECKER_SERVICE_PORT
199
+                    ]
200
+                },
201
+                "protocol": "tcp"
202
+            }
203
+        ]
204
+    },
205
+    "apiVersion": "v1",
206
+    "metadata": {
207
+        "name": "deny-netchecker"
208
+    }
209
+}
210
+
186 211
 
187 212
 def start_server(k8s, namespace=None,
188 213
                  pod_spec=NETCHECKER_POD_CFG,
@@ -270,17 +295,45 @@ def wait_check_network(kube_host_ip, works=True, timeout=120, interval=5):
270 295
 
271 296
 
272 297
 def calico_block_traffic_on_node(underlay, target_node):
298
+    if is_calico_version_new(calico_get_version(underlay, target_node)):
299
+        cmd = "echo '{0}' | calicoctl create -f -".format(
300
+            NETCHECKER_BLOCK_POLICY)
301
+    else:
302
+        cmd = ('calicoctl profile calico-k8s-network rule add --at=1 outbound '
303
+               'deny tcp to ports {0}'.format(NETCHECKER_SERVICE_PORT))
304
+    underlay.sudo_check_call(cmd, node_name=target_node)
273 305
     LOG.info('Blocked traffic to the network checker service from '
274 306
              'containers on node "{}".'.format(target_node))
275
-    underlay.sudo_check_call(
276
-        'calicoctl profile calico-k8s-network rule add --at=1 outbound '
277
-        'deny tcp to ports {0}'.format(NETCHECKER_SERVICE_PORT),
278
-        node_name=target_node)
279 307
 
280 308
 
281 309
 def calico_unblock_traffic_on_node(underlay, target_node):
310
+    if is_calico_version_new(calico_get_version(underlay, target_node)):
311
+        cmd = "echo '{0}' | calicoctl delete -f -".format(
312
+            NETCHECKER_BLOCK_POLICY)
313
+    else:
314
+        cmd = ('calicoctl profile calico-k8s-network '
315
+               'rule remove outbound --at=1')
316
+    underlay.sudo_check_call(cmd, node_name=target_node)
282 317
     LOG.info('Unblocked traffic to the network checker service from '
283 318
              'containers on node "{}".'.format(target_node))
284
-    underlay.sudo_check_call(
285
-        'calicoctl profile calico-k8s-network rule remove outbound --at=1',
286
-        node_name=target_node)
319
+
320
+
321
+def calico_get_version(underlay, target_node):
322
+    raw_version = underlay.sudo_check_call('calicoctl version',
323
+                                           node_name=target_node)
324
+
325
+    assert raw_version['exit_code'] == 0 and len(raw_version['stdout']) > 0, \
326
+        "Unable to get calico version!"
327
+
328
+    if len(raw_version['stdout']) > 1:
329
+        ctl_version = raw_version['stdout'][0].split()[1].strip()
330
+    else:
331
+        ctl_version = raw_version['stdout'][0].strip()
332
+
333
+    LOG.debug("Calico (calicoctl) version on '{0}': '{1}'".format(target_node,
334
+                                                                  ctl_version))
335
+    return ctl_version
336
+
337
+
338
+def is_calico_version_new(calico_version):
339
+    return version.LooseVersion(calico_version) >= version.LooseVersion('v1')

Loading…
Cancel
Save