Skip to content

Commit

Permalink
Making minor edits to clean up code.
Browse files Browse the repository at this point in the history
  • Loading branch information
supriyagarg committed Jun 3, 2015
1 parent 96f4d12 commit a90cb06
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 85 deletions.
7 changes: 3 additions & 4 deletions collector/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@
# (Ctrl-C will stop and remove the container):
# sudo docker run --rm --net=host -p 5555:5555 --name cluster-insight kubernetes/cluster-insight python ./collector.py --debug
#
# To run a container from this image in detached production mode:
# To run as master:
# To run a container from this image in detached production mode as the master:
# sudo docker run -d --net=host -p 5555:5555 --name cluster-insight -e CLUSTER_INSIGHT_MODE=master kubernetes/cluster-insight
# (this assumes that the Web Server port specified in collector.py is 5555)
#
# To run as minion
# To run a container from this image in detached production mode as a minion:
# sudo docker run -d --net=host -p 4243:4243 --name cluster-insight -e CLUSTER_INSIGHT_MODE=minion -v /var/run/docker.sock:/var/run/docker.sock:ro kubernetes/cluster-insight
# (this assumes that the docker port specified in docker_proxy.py is 5555)
# (this assumes that the docker port specified in docker_proxy.py is 4243)

# To check if the Cluster-Insight data collector is running:
# sudo docker ps | grep cluster-insight
Expand Down
1 change: 1 addition & 0 deletions collector/cluster_insight.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@


"""Chooses which top level module to run, based on the mode passed in
the CLUSTER_INSIGHT_MODE environment variable
"""

import argparse
Expand Down
99 changes: 33 additions & 66 deletions collector/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
# limitations under the License.


"""Collects context metadata from multiple places and computes a graph from it.
"""Runs the cluster insight data collector in master mode. Collects context
metadata from multiple places and computes a graph from it.
"""

import argparse
Expand Down Expand Up @@ -56,40 +57,6 @@ def valid_id(x):
return utilities.valid_optional_string(x)


def make_response(value, attribute_name):
"""Makes the JSON response containing the given attribute name and value.
Args:
value: the value associated with 'attribute_name'.
attribute_name: a string containing the attribute name.
Returns:
A dictionary containing a context-graph successful response with the given
attribute name and value.
"""
assert utilities.valid_string(attribute_name)
return {'success': True,
'timestamp': datetime.datetime.now().isoformat(),
attribute_name: value}


def make_error(error_message):
"""Makes the JSON response indicating an error.
Args:
error_message: a string containing the error message describing the
failure.
Returns:
A dictionary containing an failed context-graph response with a given
error message.
"""
assert utilities.valid_string(error_message)
return {'success': False,
'timestamp': datetime.datetime.now().isoformat(),
'error_message': error_message}


@app.route('/', methods=['GET'])
def home():
"""Returns the response of the '/' endpoint.
Expand All @@ -111,13 +78,13 @@ def get_nodes():
try:
nodes_list = kubernetes.get_nodes_with_metrics(gs)
except collector_error.CollectorError as e:
return flask.jsonify(make_error(str(e)))
return flask.jsonify(utilities.make_error(str(e)))
except:
msg = 'kubernetes.get_nodes() failed with exception %s' % sys.exc_info()[0]
app.logger.exception(msg)
return flask.jsonify(make_error(msg))
return flask.jsonify(utilities.make_error(msg))

return flask.jsonify(make_response(nodes_list, 'resources'))
return flask.jsonify(utilities.make_response(nodes_list, 'resources'))


@app.route('/cluster/resources/services', methods=['GET'])
Expand All @@ -131,14 +98,14 @@ def get_services():
try:
services_list = kubernetes.get_services(gs)
except collector_error.CollectorError as e:
return flask.jsonify(make_error(str(e)))
return flask.jsonify(utilities.make_error(str(e)))
except:
msg = ('kubernetes.get_services() failed with exception %s' %
sys.exc_info()[0])
app.logger.exception(msg)
return flask.jsonify(make_error(msg))
return flask.jsonify(utilities.make_error(msg))

return flask.jsonify(make_response(services_list, 'resources'))
return flask.jsonify(utilities.make_response(services_list, 'resources'))


@app.route('/cluster/resources/rcontrollers', methods=['GET'])
Expand All @@ -152,14 +119,14 @@ def get_rcontrollers():
try:
rcontrollers_list = kubernetes.get_rcontrollers(gs)
except collector_error.CollectorError as e:
return flask.jsonify(make_error(str(e)))
return flask.jsonify(utilities.make_error(str(e)))
except:
msg = ('kubernetes.get_rcontrollers() failed with exception %s' %
sys.exc_info()[0])
app.logger.exception(msg)
return flask.jsonify(make_error(msg))
return flask.jsonify(utilities.make_error(msg))

return flask.jsonify(make_response(rcontrollers_list, 'resources'))
return flask.jsonify(utilities.make_response(rcontrollers_list, 'resources'))


@app.route('/cluster/resources/pods', methods=['GET'])
Expand All @@ -173,13 +140,13 @@ def get_pods():
try:
pods_list = kubernetes.get_pods(gs, None)
except collector_error.CollectorError as e:
return flask.jsonify(make_error(str(e)))
return flask.jsonify(utilities.make_error(str(e)))
except:
msg = 'kubernetes.get_pods() failed with exception %s' % sys.exc_info()[0]
app.logger.exception(msg)
return flask.jsonify(make_error(msg))
return flask.jsonify(utilities.make_error(msg))

return flask.jsonify(make_response(pods_list, 'resources'))
return flask.jsonify(utilities.make_response(pods_list, 'resources'))


@app.route('/cluster/resources/containers', methods=['GET'])
Expand All @@ -199,13 +166,13 @@ def get_containers():
containers.extend(docker.get_containers_with_metrics(gs, docker_host))

except collector_error.CollectorError as e:
return flask.jsonify(make_error(str(e)))
return flask.jsonify(utilities.make_error(str(e)))
except:
msg = 'get_containers() failed with exception %s' % sys.exc_info()[0]
app.logger.exception(msg)
return flask.jsonify(make_error(msg))
return flask.jsonify(utilities.make_error(msg))

return flask.jsonify(make_response(containers, 'resources'))
return flask.jsonify(utilities.make_response(containers, 'resources'))


@app.route('/cluster/resources/processes', methods=['GET'])
Expand All @@ -227,13 +194,13 @@ def get_processes():
processes.extend(docker.get_processes(gs, docker_host, container_id))

except collector_error.CollectorError as e:
return flask.jsonify(make_error(str(e)))
return flask.jsonify(utilities.make_error(str(e)))
except:
msg = 'get_processes() failed with exception %s' % sys.exc_info()[0]
app.logger.exception(msg)
return flask.jsonify(make_error(msg))
return flask.jsonify(utilities.make_error(msg))

return flask.jsonify(make_response(processes, 'resources'))
return flask.jsonify(utilities.make_response(processes, 'resources'))


@app.route('/cluster/resources/images', methods=['GET'])
Expand All @@ -255,15 +222,15 @@ def get_images():
images_dict[image['id']] = image

except collector_error.CollectorError as e:
return flask.jsonify(make_error(str(e)))
return flask.jsonify(utilities.make_error(str(e)))
except:
msg = 'kubernetes.get_images() failed with exception %s' % sys.exc_info()[0]
app.logger.exception(msg)
return flask.jsonify(make_error(msg))
return flask.jsonify(utilities.make_error(msg))

# The images list is sorted by increasing identifiers.
images_list = [images_dict[key] for key in sorted(images_dict.keys())]
return flask.jsonify(make_response(images_list, 'resources'))
return flask.jsonify(utilities.make_response(images_list, 'resources'))


@app.route('/debug', methods=['GET'])
Expand All @@ -277,12 +244,12 @@ def get_debug():
try:
return context.compute_graph(gs, 'dot')
except collector_error.CollectorError as e:
return flask.jsonify(make_error(str(e)))
return flask.jsonify(utilities.make_error(str(e)))
except:
msg = ('compute_graph(\"dot\") failed with exception %s' %
sys.exc_info()[0])
app.logger.exception(msg)
return flask.jsonify(make_error(msg))
return flask.jsonify(utilities.make_error(msg))


@app.route('/cluster/resources', methods=['GET'])
Expand All @@ -297,12 +264,12 @@ def get_resources():
response = context.compute_graph(gs, 'resources')
return flask.jsonify(response)
except collector_error.CollectorError as e:
return flask.jsonify(make_error(str(e)))
return flask.jsonify(utilities.make_error(str(e)))
except:
msg = ('compute_graph(\"resources\") failed with exception %s' %
sys.exc_info()[0])
app.logger.exception(msg)
return flask.jsonify(make_error(msg))
return flask.jsonify(utilities.make_error(msg))


@app.route('/cluster', methods=['GET'])
Expand All @@ -317,12 +284,12 @@ def get_cluster():
response = context.compute_graph(gs, 'context_graph')
return flask.jsonify(response)
except collector_error.CollectorError as e:
return flask.jsonify(make_error(str(e)))
return flask.jsonify(utilities.make_error(str(e)))
except:
msg = ('compute_graph(\"context_graph\") failed with exception %s' %
sys.exc_info()[0])
app.logger.exception(msg)
return flask.jsonify(make_error(msg))
return flask.jsonify(utilities.make_error(msg))


@app.route('/version', methods=['GET'])
Expand All @@ -335,13 +302,13 @@ def get_version():
gs = app.context_graph_global_state
try:
version = docker.get_version(gs)
return flask.jsonify(make_response(version, 'version'))
return flask.jsonify(utilities.make_response(version, 'version'))
except collector_error.CollectorError as e:
return flask.jsonify(make_error(str(e)))
return flask.jsonify(utilities.make_error(str(e)))
except:
msg = ('get_version() failed with exception %s' % sys.exc_info()[0])
app.logger.exception(msg)
return flask.jsonify(make_error(msg))
return flask.jsonify(utilities.make_error(msg))


@app.route('/healthz', methods=['GET'])
Expand All @@ -351,7 +318,7 @@ def get_health():
Returns:
A successful response containing the attribute 'health' and the value 'OK'.
"""
return flask.jsonify(make_response('OK', 'health'))
return flask.jsonify(utilities.make_response('OK', 'health'))


# Starts the web server and listen on all external IPs associated with this
Expand Down
23 changes: 8 additions & 15 deletions collector/docker_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
# limitations under the License.


"""Runs the cluster insight data collector in minion mode. """


import argparse
import datetime
import flask
Expand All @@ -26,6 +29,7 @@
import types

import constants
import utilities

app = flask.Flask(__name__)
logger = logging.getLogger(__name__)
Expand All @@ -41,17 +45,6 @@
LOCAL_DOCKER_HOST= 'http+unix://%2Fvar%2Frun%2Fdocker.sock'


def make_error(error_message):
"""Returns a JSON response indicating an error."""
assert isinstance(error_message, types.StringTypes) and error_message
result = {
'success': False,
'timestamp': datetime.datetime.now().isoformat(),
'error_message': error_message
}
return result


def get_response(api):
"""This method passes the API call to the docker unix host,
and returns the json response."""
Expand All @@ -70,13 +63,13 @@ def get_response(api):
except Exception as e:
logger.error(e, exc_info=True)
exc_type, value, _ = sys.exc_info()
return flask.jsonify(make_error(
return flask.jsonify(utilities.make_error(
'Failed to retrieve %s with exception %s: %s'
% (api, exc_type, value)))



# Calls to support
# Support the following calls and nothing else:
# 1. /containers/{container_id}/json
# 2. /containers/json
# 3. /containers/{container_id}/top?ps_args=aux
Expand All @@ -97,9 +90,9 @@ def get_one_image(image_id):
@app.route('/containers/<container_id>/top', methods=['GET'])
def get_one_container_processes(container_id):
qargs = flask.request.args.to_dict() if flask.request.args else {}
if qargs.get('ps_args') != 'aux' or len(qargs.keys()) > 1:
if len(qargs) != 1 or qargs.get('ps_args') != 'aux':
return flask.jsonify(make_error(
'For /container/{container_id}/top, the only allowed arg is ps_args=aux.'
'For /container/{container_id}/top, the sole mandatory arg is ps_args=aux.'
'%s is not allowed' % (qargs)))

return get_response('/containers/{cid}/top?ps_args=aux'.format(cid=container_id))
Expand Down
34 changes: 34 additions & 0 deletions collector/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,37 @@ def get_short_container_name(container, parent_pod):
return cname

return None


def make_response(value, attribute_name):
"""Makes the JSON response containing the given attribute name and value.
Args:
value: the value associated with 'attribute_name'.
attribute_name: a string containing the attribute name.
Returns:
A dictionary containing a context-graph successful response with the given
attribute name and value.
"""
assert valid_string(attribute_name)
return {'success': True,
'timestamp': datetime.datetime.now().isoformat(),
attribute_name: value}


def make_error(error_message):
"""Makes the JSON response indicating an error.
Args:
error_message: a string containing the error message describing the
failure.
Returns:
A dictionary containing an failed context-graph response with a given
error message.
"""
assert valid_string(error_message)
return {'success': False,
'timestamp': datetime.datetime.now().isoformat(),
'error_message': error_message}

0 comments on commit a90cb06

Please sign in to comment.