add debug logging + fix a type in ingress controller

This commit is contained in:
Peter Jausovec 2017-01-17 11:22:07 -08:00
Родитель 889f909777
Коммит 7f2beaf931
5 изменённых файлов: 53 добавлений и 24 удалений

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

@ -219,7 +219,7 @@ class DockerComposeParser(object):
service_name = deployment_item['service_name']
existing_replicas = self.kubernetes.get_replicas(
existing_namespace, service_name)
logging.info('Updating replicas for "%s" to "%s"',
logging.info('Update replicas for "%s" to "%s"',
service_name, existing_replicas)
deployment_json = json.loads(
deployment_item['deployment']['json'])
@ -239,7 +239,7 @@ class DockerComposeParser(object):
if ingress_json:
self.kubernetes.create_ingress(ingress_json, new_namespace)
logging.info('Removing previous deployment')
logging.info('Remove previous deployment')
self._delete_all(existing_namespace)
else:
for deployment_item in all_deployments:
@ -257,4 +257,6 @@ class DockerComposeParser(object):
if needs_ingress_controller:
logging.info(
'ExternalIP of NGINX Ingress Loadbalancer: "%s"', self.ingress_controller.get_external_ip())
'ExternalIP of NGINX Ingress Loadbalancer: "%s"',
self.ingress_controller.get_external_ip())
logging.info('Deployment completed')

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

@ -16,15 +16,15 @@
}
],
"selector": {
"k8s-app": "nginx-ingress-lb"
"k8s-app": "nginx-ingress-controller"
},
"type": "LoadBalancer"
},
"apiVersion": "v1",
"metadata": {
"name": "nginx-ingress-lb",
"name": "nginx-ingress-controller",
"labels": {
"k8s-app": "nginx-ingress-lb"
"k8s-app": "nginx-ingress-controller"
}
}
}

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

@ -6,7 +6,7 @@
"labels": {
"k8s-app": "nginx-ingress-controller"
},
"namespace": "kube-system"
"namespace": "default"
},
"spec": {
"replicas": 1,

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

@ -24,7 +24,7 @@ class IngressController(object):
NGINX_INGRESS_SERVICE_FILE = 'ingress/nginx-ingress-lb-svc.json'
DEFAULT_BACKEND_NAME = 'default-http-backend'
NGINX_INGRESS_LB_NAME = 'nginx-ingress-lb'
NGINX_INGRESS_LB_NAME = 'nginx-ingress-controller'
def __init__(self, kubernetes):
self.kubernetes = kubernetes
@ -48,7 +48,7 @@ class IngressController(object):
Gets the ExternalIP where the Nginx loadbalacer is exposed on
"""
service = self.kubernetes.get_service(
IngressController.NGINX_INGRESS_LB_NAME)
IngressController.NGINX_INGRESS_LB_NAME, IngressController.DEFAULT_NAMESPACE)
external_ip = None
try:

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

@ -4,7 +4,7 @@ import logging
class Kubernetes(object):
"""
Class used for working with Kubernetes API
Class used for working with Kubernetes API
"""
deployment_max_wait_time = 5 * 60
@ -47,31 +47,38 @@ class Kubernetes(object):
"""
Creates a secret on Kubernetes
"""
logging.debug('Create secret in namespace "%s"', namespace)
url = 'namespaces/{}/secrets'.format(namespace)
return self.post_request(url, post_data=secret_json)
response = self.post_request(url, post_data=secret_json).json()
return response
def secret_exists(self, label_selector, namespace):
"""
Checks if secret defined by label_selector ("name=secret_name") exists or not
"""
logging.debug('Check if secret in namespace "%s" with selector "%s" exists',
namespace, label_selector)
url = 'namespaces/{}/secrets?labelSelector={}'.format(
namespace, label_selector)
response = self.get_request(url).json()
if len(response['items']) == 0:
logging.debug('\tSecret does not exist')
return False
logging.debug('\tSecret exists')
return True
def create_deployment(self, deployment_json, namespace, wait_for_complete=False):
"""
Creates a deployment on Kubernetes
"""
logging.debug('Create deployment in namespace "%s"', namespace)
start_timestamp = time.time()
url = 'namespaces/{}/deployments'.format(namespace)
response = self.post_request(
url, post_data=deployment_json, endpoint=self._beta_endpoint()).json()
if wait_for_complete:
print 'RESPONSE', response
self._wait_for_deployment_complete(
start_timestamp, namespace, response['metadata']['name'])
return response
@ -80,20 +87,21 @@ class Kubernetes(object):
"""
Checks if deployment exists in a namespace or not
"""
logging.debug('Check if deployment "%s.%s" exists', name, namespace)
response = self.get_deployment(namespace, name)
if self._has_failed(response):
logging.debug('\tDeployment "%s.%s" does not exists', name, namespace)
return False
if response['kind'] is 'Deployment':
if response['kind'] == 'Deployment':
logging.debug('\tDeployment "%s.%s" exists', name, namespace)
return True
print 'RESPONSE: ', response['kind']
raise ValueError('Unknown response kind: "%s"', response)
def delete_deployment(self, name, namespace):
"""
Deletes a deployment
"""
logging.debug('Delete deployment "%s.%s', name, namespace)
url = 'namespaces/{}/deployments/{}'.format(namespace, name)
response = self.delete_request(url)
return response
@ -102,6 +110,7 @@ class Kubernetes(object):
"""
Deletes all deployments from a namespace
"""
logging.debug('Delete all deployments from "%s"', namespace)
url = 'namespaces/{}/deployments'.format(namespace)
response = self.delete_request(url, endpoint=self._beta_endpoint()).json()
return response
@ -110,7 +119,7 @@ class Kubernetes(object):
"""
Deletes all ReplicaSets in a namespace
"""
logging.debug('Deleting replicasets')
logging.debug('Delete replicasets from "%s"', namespace)
url = 'namespaces/{}/replicasets'.format(namespace)
return self.delete_request(url, endpoint=self._beta_endpoint())
@ -118,6 +127,7 @@ class Kubernetes(object):
"""
Creates an ingress resource on Kubernetes
"""
logging.debug('Create ingress in "%s"', namespace)
url = 'namespaces/{}/ingresses'.format(namespace)
return self.post_request(url, post_data=ingress_json, endpoint=self._beta_endpoint())
@ -125,7 +135,7 @@ class Kubernetes(object):
"""
Deletes all ingresses from a namespace
"""
logging.debug('Deleting ingresses')
logging.debug('Delete ingresses from namespace "%s"', namespace)
url = 'namespaces/{}/ingresses'.format(namespace)
return self.delete_request(url, endpoint=self._beta_endpoint())
@ -133,6 +143,7 @@ class Kubernetes(object):
"""
Creates a service on Kubernetes
"""
logging.debug('Create a service in namespace "%s"', namespace)
url = 'namespaces/{}/services'.format(namespace)
return self.post_request(url, post_data=service_json)
@ -140,6 +151,7 @@ class Kubernetes(object):
"""
Gets the service
"""
logging.debug('Get service "%s.%s"', name, namespace)
url = 'namespaces/{}/services/{}'.format(namespace, name)
return self.get_request(url).json()
@ -147,6 +159,7 @@ class Kubernetes(object):
"""
Deletes a service in specified namespace
"""
logging.debug('Delete service "%s.%s"', name, namespace)
url = 'namespaces/{}/services/{}'.format(namespace, name)
return self.delete_request(url)
@ -154,35 +167,45 @@ class Kubernetes(object):
"""
Checks if service exists in the namespace
"""
logging.debug('Check if service "%s.%s" exists', name, namespace)
url = 'namespaces/{}/services/{}'.format(namespace, name)
response = self.get_request(url).json()
if self._has_failed(response):
logging.debug('\tService "%s.%s" does not exist', name, namespace)
return False
if response['kind'] == 'Service':
logging.debug('\tService "%s.%s" exists', name, namespace)
return True
raise ValueError('Unknown response kind: "%s"', response)
def delete_services(self, namespace):
"""
Deletes all service in specified namespace
"""
logging.debug('Delete all services from namespace "%s"', namespace)
url = 'namespaces/{}/services'.format(namespace)
response = self.get_request(url).json()
all_services = response['items']
for service in all_services:
service_name = service['metadata']['name']
logging.debug('Deleting service "%s"', service_name)
logging.debug('Delete service "%s.%s"', service_name, namespace)
self.delete_service(service_name, namespace)
def namespace_exists(self, label_selector):
"""
Checks if a namespace defined by the label_selector exists or not
"""
logging.debug('Check if namespace with selector "%s" exists', label_selector)
response = self.get_request(
'namespaces?labelSelector={}'.format(label_selector)).json()
if len(response['items'] == 0):
logging.debug('\tNamespace with selector "%s" does not exist', label_selector)
return False
logging.debug('\tNamespace with selector "%s" exists', label_selector)
return True
def get_namespaces(self, label_selector):
@ -197,6 +220,7 @@ class Kubernetes(object):
"""
Deletes a namespace
"""
logging.debug('Delete namespace "%s"', name)
response = self.delete_request(
'namespaces?labelSelector=name={}'.format(name)).json()
return response
@ -205,6 +229,7 @@ class Kubernetes(object):
"""
Creates a new namespace
"""
logging.debug('Create namespace "%s"', name)
namespace_json = {
"kind": "Namespace",
"apiVersion": "v1",
@ -214,12 +239,15 @@ class Kubernetes(object):
}
}
return self.post_request('namespaces', post_data=json.dumps(namespace_json)).json()
response = self.post_request(
'namespaces', post_data=json.dumps(namespace_json)).json()
return response
def get_deployment(self, namespace, deployment_name):
"""
Gets a specific deployment in a namespace
"""
logging.debug('Get deployment "%s.%s', deployment_name, namespace)
response = self.get_request(
'namespaces/{}/deployments/{}'.format(
namespace, deployment_name), self._beta_endpoint())
@ -237,7 +265,7 @@ class Kubernetes(object):
deployment_completed = False
timeout_exceeded = False
logging.info('Waiting for deployment "%s.%s" to complete', namespace, deployment_name)
logging.info('Wait for deployment "%s.%s" to complete', namespace, deployment_name)
while not deployment_completed:
if self._wait_time_exceeded(self.deployment_max_wait_time, start_timestamp):
timeout_exceeded = True
@ -245,7 +273,6 @@ class Kubernetes(object):
deployment = self.get_deployment(namespace, deployment_name)
status = deployment['status']
logging.info('Checking if deployment is completed ...')
if not status or 'observedGeneration' not in status or 'updatedReplicas' not in status:
time.sleep(1)
@ -262,7 +289,7 @@ class Kubernetes(object):
'Timeout exceeded waiting for deployment to complete')
if deployment_completed:
logging.info('Deployment completed')
logging.info('Deployment "%s.%s" completed', namespace, deployment_name)
def _wait_time_exceeded(self, max_wait, timestamp):
"""
@ -275,6 +302,6 @@ class Kubernetes(object):
Checks if the response failed (404) or not
"""
if response['kind'] == 'Status':
if response['code'] == 404:
if 'code' in response and response['code'] == 404:
return True
return False