Kami commented on a change in pull request #1477:
URL: https://github.com/apache/libcloud/pull/1477#discussion_r464069328



##########
File path: libcloud/compute/drivers/kubevirt.py
##########
@@ -1022,3 +1096,143 @@ def _to_node(self, vm, is_stopped=False):
                     driver=driver, size=size,
                     image=image, extra=extra,
                     created_at=created_at)
+
+    def ex_list_services(self, namespace='default', node_name=None,
+                         service_name=None):
+        '''
+        If node_name is given then the services returned will be those that
+        concern the node
+        '''
+        params = None
+        if service_name is not None:
+            params = {'fieldSelector': 'metadata.name={}'.format(service_name)}
+        req = ROOT_URL + '/namespaces/{}/services'.format(namespace)
+        result = self.connection.request(req, params=params).object['items']
+        if node_name:
+            res = []
+            for service in result:
+                if node_name in service['metadata'].get('name', ""):
+                    res.append(service)
+            return res
+        return result
+
+    def ex_create_service(self, node, ports, service_type="NodePort",
+                          cluster_ip=None, load_balancer_ip=None,
+                          override_existing_ports=False):
+        '''
+        Each node has a single service of one type on which the exposed ports
+        are described. If a service exists then the port declared will be
+        exposed alongside the existing ones, set override_existing_ports=True
+        to delete existing exposed ports and expose just the ones in the port
+        variable.
+
+        param node: the libcloud node for which the ports will be exposed
+        type  node: libcloud `Node` class
+
+        param ports: a list of dictionaries with keys --> values:
+                     'port' --> port to be exposed on the service
+                     'target_port' --> port on the pod/node, optional
+                                       if empty then it gets the same
+                                       value as 'port' value
+                     'protocol' ---> either 'UDP' or 'TCP', defaults to TCP
+                     'name' --> A name for the service
+                     If ports is an empty `list` and a service exists of this
+                     type then the service will be deleted.
+        type  ports: `list` of `dict` where each `dict` has keys --> values:
+                     'port' --> `int`
+                     'target_port' --> `int`
+                     'protocol' --> `str
+                     'name' --> str
+
+        param service_type: Valid types are ClusterIP, NodePort, LoadBalancer
+        type  service_type: `str`
+
+        param cluster_ip: This can be set with an IP string value if you want
+                          manually set the service's internal IP. If the value
+                          is not correct the method will fail, this value can't
+                          be updated.
+        type  cluster_ip: `str`
+
+        param override_existing_ports: Set to True if you want to delete the
+                                       existing ports exposed by the service
+                                       and keep just the ones declared in the
+                                       present ports argument.
+                                       By default it is false and if the
+                                       service already exists the ports will be
+                                       added to the existing ones.
+        type  override_existing_ports: `boolean`
+        '''
+        # check if service exists first
+        namespace = node.extra.get('namespace', 'default')
+        service_name = 'service-{}-{}'.format(service_type.lower(), node.name)
+        service_list = self.ex_list_services(namespace=namespace,
+                                             service_name=service_name)
+
+        ports_to_expose = []
+        # if ports has a falsey value like None or 0
+        if not ports:
+            ports = []
+        for port_group in ports:
+            if not port_group.get('target_port', None):
+                port_group['target_port'] = port_group['port']
+            if not port_group.get('name', ""):
+                port_group['name'] = 'port-{}'.format(port_group['port'])
+            ports_to_expose.append(
+                {'protocol': port_group.get('protocol', 'TCP'),
+                 'port': int(port_group['port']),
+                 'targetPort': int(port_group['target_port']),
+                 'name': port_group['name']})
+        headers = None
+        data = None
+        if len(service_list) > 0:

Review comment:
       Per my comment above, I think it would be better to be explicit and have 
another ``ex_delete_service`` method.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to