Skip to content

Commit

Permalink
remove use of ocutil
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvallejo committed Jun 29, 2017
1 parent eeed320 commit 7c0b63f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 183 deletions.
98 changes: 4 additions & 94 deletions roles/openshift_health_checker/openshift_checks/fluentd_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
Module for performing checks on an Fluentd logging deployment configuration
"""

from openshift_checks import OpenShiftCheck, OpenShiftCheckException, get_var

import json
import os
from openshift_checks import OpenShiftCheck, get_var


class FluentdConfig(OpenShiftCheck):
Expand All @@ -23,23 +20,7 @@ def run(self, tmp, task_vars):
"""Check that Fluentd has running pods, and that its logging config matches Docker's logging config."""

self.logging_namespace = get_var(task_vars, "openshift_logging_namespace", default="logging")
fluentd_pods, error = self.get_pods_for_component(
self.execute_module,
self.logging_namespace,
"fluentd",
task_vars,
)
if error:
return {"failed": True, "changed": False, "msg": error}

running_fluentd_pods = self.running_pods(fluentd_pods)
if not len(running_fluentd_pods):
msg = ('No Fluentd pods were found to be in the "Running" state.'
'At least one Fluentd pod is required in order to perform this check.')

return {"failed": True, "changed": False, "msg": msg}

config_error = self.check_logging_config(fluentd_pods, task_vars)
config_error = self.check_logging_config(task_vars)
if config_error:
msg = ("The following Fluentd logging configuration problem was found:"
"\n-------\n"
Expand All @@ -48,7 +29,7 @@ def run(self, tmp, task_vars):

return {"failed": False, "changed": False, "msg": 'No problems found with Fluentd logging configuration.'}

def check_logging_config(self, fluentd_pods, task_vars):
def check_logging_config(self, task_vars):
"""Ensure that the configured Docker logging driver matches fluentd settings.
This means that, at least for now, if the following condition is met:
Expand All @@ -57,21 +38,7 @@ def check_logging_config(self, fluentd_pods, task_vars):
then the value of the configured Docker logging driver should be "journald".
Otherwise, the value of the Docker logging driver should be "json-file".
Returns an error string if the above condition is not met, or None otherwise."""

pod_name = fluentd_pods[0]["metadata"]["name"]
try:
use_journald = self.exec_oc(
self.execute_module,
self.logging_namespace,
"exec {} /bin/printenv USE_JOURNAL".format(pod_name),
[], task_vars
)
except OpenShiftCheckException as error:
if "false" not in str(error):
return str(error)

use_journald = False

use_journald = get_var(task_vars, "openshift_logging_fluentd_use_journal", default=True)
docker_info = self.execute_module("docker_info", {}, task_vars)
if not docker_info.get("info", False):
return "No information was returned by the Docker client. Unable to verify the logging driver in use."
Expand Down Expand Up @@ -114,60 +81,3 @@ def check_logging_config(self, fluentd_pods, task_vars):
'for more information.').format(driver=recommended_logging_driver)

return error

def get_pods_for_component(self, execute_module, namespace, logging_component, task_vars):
"""Get all pods for a given component. Returns: list of pods for component, error string."""
pod_output = self.exec_oc(
execute_module,
namespace,
"get pods -l component={} -o json".format(logging_component),
[],
task_vars
)
try:
pods = json.loads(pod_output)
if not pods or not pods.get('items'):
raise ValueError()
except ValueError:
# successful run but non-parsing data generally means there were no pods in the namespace
return None, 'There are no pods in the {} namespace. Is logging deployed?'.format(namespace)

return pods['items'], None

@staticmethod
def running_pods(pods):
"""Returns: list of pods in a running state."""
return [
pod for pod in pods
if pod['status']['phase'] == 'Running'
]

@staticmethod
def exec_oc(execute_module=None, namespace="logging", cmd_str="", extra_args=None, task_vars=None):
"""Execute an 'oc' command in the remote host.
Returns: output of command and namespace,
or raises OpenShiftCheckException on error."""
config_base = get_var(task_vars, "openshift", "common", "config_base")
args = {
"namespace": namespace,
"config_file": os.path.join(config_base, "master", "admin.kubeconfig"),
"cmd": cmd_str,
"extra_args": list(extra_args) if extra_args else [],
}

result = execute_module("ocutil", args, task_vars)
if result.get("failed"):
msg = (
'Unexpected error using `oc` to validate the logging stack components.\n'
'Error executing `oc {cmd}`:\n'
'{error}'
).format(cmd=args['cmd'], error=result['result'])

if result['result'] == '[Errno 2] No such file or directory':
msg = (
"This host is supposed to be a master but does not have the `oc` command where expected.\n"
"Has an installation been run on this host yet?"
)
raise OpenShiftCheckException(msg)

return result.get("result", "")
103 changes: 14 additions & 89 deletions roles/openshift_health_checker/test/fluentd_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,114 +11,38 @@ def canned_fluentd_config(exec_oc=None):
return check


fluentd_pod = {
"metadata": {
"labels": {"component": "fluentd", "deploymentconfig": "logging-fluentd"},
"name": "logging-fluentd-1",
},
"spec": {"host": "node1", "nodeName": "node1"},
"status": {
"phase": "Running",
"containerStatuses": [{"ready": True}],
"conditions": [{"status": "True", "type": "Ready"}],
}
}

not_running_fluentd_pod = {
"metadata": {
"labels": {"component": "fluentd", "deploymentconfig": "logging-fluentd"},
"name": "logging-fluentd-2",
},
"status": {
"phase": "Unknown",
"containerStatuses": [{"ready": True}, {"ready": False}],
"conditions": [{"status": "True", "type": "Ready"}],
}
}


@pytest.mark.parametrize('pods, expect_pods', [
@pytest.mark.parametrize('use_journald, logging_driver, failed, extra_words', [
(
[not_running_fluentd_pod],
[],
),
(
[fluentd_pod],
[fluentd_pod],
),
(
[],
[],
)
])
def test_check_running_pods(pods, expect_pods):
check = canned_fluentd_config(None)
pods = check.running_pods(pods)
assert pods == expect_pods


@pytest.mark.parametrize('pods, response, logging_driver, failed, extra_words', [
(
[fluentd_pod],
{
"failed": True,
"result": "false",
},
False,
"journald",
True,
['json log files', 'has been set to use "journald"'],
),
# result from ocutil returns false (not using journald), but check succeeds
# since docker is set to use json-file
# # result from ocutil returns false (not using journald), but check succeeds
# # since docker is set to use json-file
(
[fluentd_pod],
{
"failed": True,
"result": "false",
},
False,
"json-file",
False,
[],
),
# fluentd not set to check journald, docker not set to use default json-file
# # fluentd not set to check journald, docker not set to use default json-file
(
[fluentd_pod],
{
"failed": True,
"result": "false",
},
False,
"unsupported",
True,
["json log files", 'has been set to use "unsupported"'],
),
# fluentd set to aggregate from journald, but docker config set to use json-file
(
[fluentd_pod],
{
"failed": False,
"result": "true",
},
"json-file",
True,
['logs from "journald"', 'has been set to use "json-file"'],
),
# unexpected response from fluentd pod (not "true" or "false")
(
[fluentd_pod],
{
"failed": True,
"result": "unexpected",
},
"json-file",
True,
['Unexpected error', 'unexpected'],
True,
"json-file",
True,
['logs from "journald"', 'has been set to use "json-file"'],
),
])
def test_check_logging_config(pods, response, logging_driver, failed, extra_words):
def test_check_logging_config(use_journald, logging_driver, failed, extra_words):
def execute_module(module_name, args, task_vars):
if module_name == "ocutil":
return response

if module_name == "docker_info":
return {
"info": {
Expand All @@ -129,14 +53,15 @@ def execute_module(module_name, args, task_vars):
return {}

task_vars = dict(
openshift_logging_fluentd_use_journal=use_journald,
openshift=dict(
common=dict(config_base=""),
),
)

check = canned_fluentd_config(None)
check.execute_module = execute_module
error = check.check_logging_config(pods, task_vars)
error = check.check_logging_config(task_vars)

if failed:
assert error is not None
Expand Down

0 comments on commit 7c0b63f

Please sign in to comment.