Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Feb 20, 2024
1 parent fd757c3 commit b2a1969
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 25 deletions.
7 changes: 5 additions & 2 deletions IM/VirtualMachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class VirtualMachine(LoggerMixin):

SSH_REVERSE_BASE_PORT = 20000

NO_DNS_NAME_SET = ["Kubernetes", "OSCAR", "Lambda"]

logger = logging.getLogger('InfrastructureManager')

def __init__(self, inf, cloud_id, cloud, info, requested_radl, cloud_connector=None, im_id=None):
Expand Down Expand Up @@ -586,8 +588,9 @@ def update_status(self, auth, force=False):
self.state = new_state
self.info.systems[0].setValue("state", new_state)

# Replace the #N# in dns_names
self.replace_dns_name(self.info.systems[0])
if self.getCloudConnector().type not in self.NO_DNS_NAME_SET:
# Replace the #N# in dns_names
self.replace_dns_name(self.info.systems[0])

return updated

Expand Down
48 changes: 30 additions & 18 deletions IM/connectors/Kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ def _create_volumes(self, namespace, system, pod_name, auth_data, persistent=Fal

return res

def create_service_data(self, namespace, name, outports, auth_data, vm):
def create_service_data(self, namespace, name, outports, public, auth_data, vm):
try:
service_data = self._generate_service_data(namespace, name, outports)
service_data = self._generate_service_data(namespace, name, outports, public)
self.log_debug("Creating Service: %s/%s" % (namespace, name))
headers = {'Content-Type': 'application/json'}
uri = "/api/v1/namespaces/%s/%s" % (namespace, "services")
Expand All @@ -217,7 +217,7 @@ def create_service_data(self, namespace, name, outports, auth_data, vm):
self.error_messages += "Error creating service to access pod %s" % name
self.log_exception("Error creating service.")

def _generate_service_data(self, namespace, name, outports):
def _generate_service_data(self, namespace, name, outports, public):
service_data = {'apiVersion': 'v1', 'kind': 'Service'}
service_data['metadata'] = {
'name': name,
Expand All @@ -235,16 +235,19 @@ def _generate_service_data(self, namespace, name, outports):
'protocol': outport.get_protocol().upper(),
'targetPort': outport.get_local_port(),
'name': 'port%s' % outport.get_local_port()}
if outport.get_remote_port():
if public and outport.get_remote_port():
port['nodePort'] = outport.get_remote_port()
ports.append(port)

service_data['spec'] = {
'type': 'NodePort',
'type': 'ClusterIP',
'ports': ports,
'selector': {'name': name}
}

if public:
service_data['spec']['type'] = 'NodePort'

return service_data

def create_ingress(self, namespace, name, dns, port, auth_data):
Expand Down Expand Up @@ -377,15 +380,6 @@ def _get_namespace(inf):
def launch(self, inf, radl, requested_radl, num_vm, auth_data):
system = radl.systems[0]

public_net = None
for net in radl.networks:
if net.isPublic():
public_net = net

outports = None
if public_net:
outports = public_net.getOutPorts()

res = []
# First create the namespace for the infrastructure
namespace = self._get_namespace(inf)
Expand Down Expand Up @@ -419,12 +413,20 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):
vm = VirtualMachine(inf, None, self.cloud, radl, requested_radl, self)
vm.destroy = True
inf.add_vm(vm)
pod_name = system.name
pod_name = re.sub('[!"#$%&\'()*+,/:;<=>?@[\\]^`{|}~_]', '-', system.name)

volumes = self._create_volumes(namespace, system, pod_name, auth_data, True)

tags = self.get_instance_tags(system, auth_data, inf)

outports = []
pub_net = vm.getConnectedNet(public=True)
priv_net = vm.getConnectedNet(public=False)
if pub_net:
outports = pub_net.getOutPorts()
elif priv_net:
outports = priv_net.getOutPorts()

pod_data = self._generate_pod_data(namespace, pod_name, outports, system, volumes, tags)

self.log_debug("Creating POD: %s/%s" % (namespace, pod_name))
Expand All @@ -441,7 +443,7 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):
else:
dns_name = system.getValue("net_interface.0.dns_name")

self.create_service_data(namespace, pod_name, outports, auth_data, vm)
self.create_service_data(namespace, pod_name, outports, pub_net, auth_data, vm)

output = json.loads(resp.text)
vm.id = output["metadata"]["name"]
Expand Down Expand Up @@ -480,6 +482,12 @@ def _get_pod(self, vm, auth_data):
self.log_exception("Error connecting with Kubernetes API server")
return (False, None, "Error connecting with Kubernetes API server: " + str(ex))

def _get_float_cpu(self, cpu):
if cpu.endswith("m"):
return float(cpu[:-1]) / 1000
else:
return float(cpu)

def updateVMInfo(self, vm, auth_data):
success, status, output = self._get_pod(vm, auth_data)
if success:
Expand All @@ -488,7 +496,7 @@ def updateVMInfo(self, vm, auth_data):

pod_limits = output['spec']['containers'][0].get('resources', {}).get('limits')
if pod_limits:
vm.info.systems[0].setValue('cpu.count', float(pod_limits['cpu']))
vm.info.systems[0].setValue('cpu.count', self._get_float_cpu(pod_limits['cpu']))
memory = self.convert_memory_unit(pod_limits['memory'], "B")
vm.info.systems[0].setValue('memory.size', memory)

Expand All @@ -509,7 +517,6 @@ def setIPs(self, vm, pod_info):
- vm(:py:class:`IM.VirtualMachine`): VM information.
- pod_info(dict): JSON information about the POD
"""

public_ips = []
private_ips = []
if 'hostIP' in pod_info["status"]:
Expand All @@ -522,6 +529,11 @@ def setIPs(self, vm, pod_info):
if 'podIP' in pod_info["status"]:
private_ips = [str(pod_info["status"]["podIP"])]

if not vm.getConnectedNet(public=True):
public_ips = []
if not vm.getConnectedNet(public=False):
private_ips = []

vm.setIps(public_ips, private_ips)

def finalize(self, vm, last, auth_data):
Expand Down
4 changes: 2 additions & 2 deletions IM/tosca/Tosca.py
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,7 @@ def _get_attribute_result(self, func, node, inf_info):
res.append(url + ":%s" % outport.get_remote_port())
if priv_net:
# set the internal DNS name
url = "%s.%s" % (vm.info.systems[0].name, inf_info.id)
url = re.sub('[!"#$%&\'()*+,/:;<=>?@[\\]^`{|}~_]', '-', node.name)
if priv_net.getOutPorts():
for outport in priv_net.getOutPorts():
res.append(url + ":%s" % outport.get_local_port())
Expand Down Expand Up @@ -1474,7 +1474,7 @@ def _get_attribute_result(self, func, node, inf_info):
outport.get_remote_port())
else:
# set the internal DNS name
url = node.name
url = re.sub('[!"#$%&\'()*+,/:;<=>?@[\\]^`{|}~_]', '-', node.name)
if net.getOutPorts():
for outport in net.getOutPorts():
res.append(url + ":%s" % outport.get_local_port())
Expand Down
2 changes: 2 additions & 0 deletions test/files/tosca_k8s.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ topology_template:
- { get_input: mysql_root_password }
- "@"
- { get_attribute: [ mysql_container, endpoints, 0 ] }
- "/im-db"
requirements:
- host: im_runtime
artifacts:
Expand All @@ -66,6 +67,7 @@ topology_template:
properties:
environment:
MYSQL_ROOT_PASSWORD: { get_input: mysql_root_password }
MYSQL_DATABASE: "im-db"
requirements:
- host: mysql_runtime
artifacts:
Expand Down
4 changes: 2 additions & 2 deletions test/unit/Tosca.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ def test_tosca_k8s(self):
self.assertEqual(node.getValue("memory.size"), 1000000000)
self.assertEqual(node.getValue("disk.1.size"), 10000000000)
self.assertEqual(node.getValue("disk.1.mount_path"), '/var/lib/mysql')
self.assertEqual(node.getValue("environment.variables"), 'MYSQL_ROOT_PASSWORD=my-secret')
self.assertEqual(node.getValue("environment.variables"), 'MYSQL_ROOT_PASSWORD=my-secret,MYSQL_DATABASE=im-db')
net = radl.get_network_by_id('mysql_container_priv')
self.assertEqual(net.getValue("outports"), '3306/tcp-3306/tcp')
self.assertEqual(net.getValue("outbound"), 'no')
Expand All @@ -454,7 +454,7 @@ def test_tosca_k8s(self):
self.assertEqual(net.getValue("outports"), '30880/tcp-8800/tcp')
self.assertEqual(net.getValue("outbound"), 'yes')
self.assertEqual(node.getValue("environment.variables"),
'IM_DATA_DB=mysql://root:my-secret@mysql_container:3306')
'IM_DATA_DB=mysql://root:my-secret@mysql-container:3306/im-db')
conf = radl.get_configure_by_name('im_container')
self.assertEqual(conf.recipes, None)

Expand Down
1 change: 0 additions & 1 deletion test/unit/connectors/Kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ def test_30_updateVMInfo(self, requests):

self.assertTrue(success, msg="ERROR: updating VM info.")
self.assertEqual(vm.info.systems[0].getValue("net_interface.0.ip"), "158.42.1.1")
self.assertEqual(vm.info.systems[0].getValue("net_interface.1.ip"), "10.0.0.1")
self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue())

@patch('requests.request')
Expand Down

0 comments on commit b2a1969

Please sign in to comment.