This commit is contained in:
Peter Jausovec 2016-12-28 16:29:50 -08:00
Родитель d82638adfe
Коммит 8163c2b7ee
3 изменённых файлов: 56 добавлений и 12 удалений

Просмотреть файл

@ -82,17 +82,19 @@ class DockerComposeParser(object):
Parses the docker-compose file and returns the initial marathon.json file
"""
group_name = self.group_info.get_id()
all_deployments =[]
registry_secret = self.registry_info.create_secret_json()
self.kubernetes.create_secret(registry_secret)
all_deployments = []
for service_name, service_info in self.compose_data['services'].items():
# Get the app_json for the service
service_parser = serviceparser.Parser(
self.group_info, self.registry_info, service_name, service_info)
deployment_json = service_parser.get_deployment_json()
all_deployments.append(deployment_json)
service_json = service_parser.get_service_json()
all_deployments.append({service_name: { 'deployment_json': deployment_json, 'service_json': service_json}})
return all_deployments
@ -119,6 +121,7 @@ class DockerComposeParser(object):
"""
all_deployments = self._parse_compose()
for deployment in all_deployments:
# logging.info(json.dumps(deployment))
self.kubernetes.create_deployment(deployment)
print all_deployments[1]
# for deployment in all_deployments:
# print deployment

Просмотреть файл

@ -46,3 +46,10 @@ class Kubernetes(object):
endpoint = '/apis/extensions/v1beta1'
url = 'namespaces/{}/deployments'.format(namespace)
return self.post_request(url, post_data=deployment_json, endpoint=endpoint)
def create_service(self, service_json, namespace='default'):
"""
Creates a service on Kubernetes
"""
url = 'namespaces/{}/services'.format(namespace)
return self.post_request(url, post_data=service_json)

Просмотреть файл

@ -11,7 +11,8 @@ class Parser(object):
self.service_info = service_info
self.registry_info = registry_info
self.group_info = group_info
self.deployment_json = {}
self.deployment_json = self._get_empty_deployment_json()
self.service_json = self._get_empty_service_json()
def _add_label(self, name, value):
"""
@ -25,8 +26,8 @@ class Parser(object):
Adds a container with name and image to the JSON
"""
self.deployment_json['spec']['template']['spec']['containers'].append({
'name': name,
'image': image
"name": name,
"image": image
})
def _add_image_pull_secret(self, name):
@ -34,7 +35,7 @@ class Parser(object):
Adds image pull secret to the deployment JSON
"""
self.deployment_json['spec']['template']['spec']['imagePullSecrets'].append({
'name': name})
"name": name})
def _add_container_port(self, container_port):
"""
@ -47,13 +48,25 @@ class Parser(object):
self.deployment_json['spec']['template']['spec']['containers'][0]['ports'] = []
self.deployment_json['spec']['template']['spec']['containers'][0]['ports'].append({
'containerPort': container_port})
"containerPort": container_port})
self.service_json['spec']['ports'].append({
"protocol": "TCP",
"targetPort": container_port
})
def get_service_json(self):
"""
Gets the service JSON for service
"""
return self.service_json
# TODO: This should return an object with everything that needs to be deployed
# e.g. deployment.json, service.json, ???
def get_deployment_json(self):
"""
Gets the app.json for the service in docker-compose
Gets the deployment JSON for the service in docker-compose
"""
self.deployment_json = self._get_empty_deployment_json()
self._add_label('group_name', self.group_info.name)
self._add_label('group_qualifier', self.group_info.qualifier)
self._add_label('group_version', self.group_info.version)
@ -77,6 +90,25 @@ class Parser(object):
method_to_call(key)
return json.dumps(self.deployment_json)
def _get_empty_service_json(self):
"""
Gets the empty service JSON for service that's being parsed
"""
return {
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": self.service_name
},
"spec": {
"selector": {
"group_id": self.group_info.get_id()
},
"ports": []
}
}
def _parse_image(self, key):
"""
Parses the 'image' key
@ -92,6 +124,8 @@ class Parser(object):
internal_ports = self._parse_internal_ports()
for port_tuple in internal_ports:
# TODO: What do we do with host port???
# (hostPort:containerPort)
# targetPort == containerPort
self._add_container_port(port_tuple[1])
def _parse_private_ports(self):