Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignore errors to communicate with live cluster if it is not an explicit requirement #354

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions nca/Utils/CmdlineRunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,26 @@ class CmdlineRunner:
"""
A stateless class with only static functions to easily get k8s and calico resources using kubectl and calicoctl
"""
# a static variable to indicate if we want to ignore errors from running executable command
# a static variable to indicate if we want to ignore errors from running executable command - i.e. run silently
ignore_live_cluster_err = False

@staticmethod
def run_and_get_output(cmdline_list, helm_flag=False):
def run_and_get_output(cmdline_list, check_for_silent_exec=False):
"""
Run an executable with specific arguments and return its output to stdout
if a communicate error occurs, it will be ignored in case this is a silent try to communicate with live cluster,
otherwise, will be printed to stderr
:param list[str] cmdline_list: A list of arguments, the first of which is the executable path
:param helm_flag: indicates if the executable is helm - communicate errors are always considered for helm
:param check_for_silent_exec: when true consider the static variable that indicates whether to ignore errors
or not
:return: The executable's output to stdout ( a list-resources on success, otherwise empty value)
:rtype: str
"""
cmdline_process = subprocess.Popen(cmdline_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = cmdline_process.communicate()
if err and (not CmdlineRunner.ignore_live_cluster_err or helm_flag):
print_err_flag = not check_for_silent_exec or \
(check_for_silent_exec and not CmdlineRunner.ignore_live_cluster_err)
if err and print_err_flag:
print(err.decode().strip('\n'), file=sys.stderr)
return out

Expand Down Expand Up @@ -95,7 +98,7 @@ def get_k8s_resources(resource):
cmdline_list = ['kubectl', 'get', resource, '-o=json']
if resource in ['networkPolicy', 'authorizationPolicy', 'pod', 'ingress', 'Gateway', 'VirtualService', 'sidecar']:
cmdline_list.append('--all-namespaces')
return CmdlineRunner.run_and_get_output(cmdline_list)
return CmdlineRunner.run_and_get_output(cmdline_list, check_for_silent_exec=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't also be set True from get_calico_resources, not only from get_k8s_resources ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently if the topology section is missing / or user runs cmdline without resources or without policies - our default is to load missing resources from k8s live cluster - so it is the only relevant case for a silent attempt

we load from calico only if the input is calico - a provided "live cluster" input does not ignore errors for any of the layers


@staticmethod
def resolve_helm_chart(chart_dir):
Expand All @@ -105,4 +108,4 @@ def resolve_helm_chart(chart_dir):
:return: The resolved yaml files generated from the chart file
"""
cmdline_list = ['helm', 'template', 'nca-extract', chart_dir]
return CmdlineRunner.run_and_get_output(cmdline_list, helm_flag=True)
return CmdlineRunner.run_and_get_output(cmdline_list)