-
Notifications
You must be signed in to change notification settings - Fork 3
Refactor KubectlClient module with focus on logging #17
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
Refactor KubectlClient module with focus on logging #17
Conversation
Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
8384b6b to
fb5e1a9
Compare
Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
kubectl_client shard PR: cnf-testsuite/kubectl_client#17 Signed-off-by: Rafal Lal <[email protected]>
- Also change regex error constants so they are more generic - Update docker_client shard to latest release Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
- Use log.debug for most of the 'get something' methods - Use log.info for methods that are changing something on the cluster - Use log.trace for very detailed logs - Use log.warn for non-critical issues Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor inconsistencies:
- Sometimes when running a function from the same module you prefix it with
KubectlClient::Module_name.functionand at other times you only usefunction. - Occasionally there is no
infomessage printed when running a function, this is kind of inconsistent. Especially inGetthe very first message is usuallydebugwhile in other modules the first message isinfo. - Sometimes you print the result of function with
infoand other times not. Look atpods_by_resource_labelsfor example. - I don't quite understand why you sometimes specify the amount of resources returned (this is only done with pods, I believe). I have no qualms about this but it feels a little exotic/purposeless.
- In the future we could consider making
pod_readyandnode_readyfunctions "unified", similarly to how it has been done withget_resource.
None of these propositions are too important so I'm approving.
| class K8sClientCMDException < Exception | ||
| MSG_TEMPLATE = "kubectl CMD failed, exit code: %s, error: %s" | ||
|
|
||
| def initialize(message : String?, exit_code : Int32, cause : Exception? = nil) | ||
| super(MSG_TEMPLATE % {exit_code, message}, cause) | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like using % is not exactly idiomatic to crystal, consider using #{}.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Different use case, here Im filling previously defined template string and not create new string with variables.
| cmd = "kubectl get #{kind} #{resource_name}" | ||
| cmd = "#{cmd} --field-selector #{field_selector}" if field_selector && !resource_name | ||
| cmd = "#{cmd} --selector #{selector}" if selector && !resource_name | ||
| cmd = "#{cmd} -n #{namespace}" if namespace && !all_namespaces | ||
| cmd = "#{cmd} -A" if !namespace && all_namespaces | ||
| cmd = "#{cmd} -o json" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will probably result in some uneven formatting if cmd ever gets printed (kubectl get #{kind} --field-selector #{field_selector}), consider normalizing the spacing in some final command.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes there will be one space more printed in log.trace. This is so NIT that I honestly don't want to bother with pushing new commit.
| # todo check for success/fail | ||
| JSON.parse(result[:output]) | ||
| end | ||
| def self.privileged_containers(namespace : String? = nil, all_namespaces : Bool? = true) : Array(JSON::Any) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defaulting all_namespaces to true here is a little destructive if a user passes namespace. Although I have no idea how to resolve it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true, Im hoping people will look up the signature before using. Hypothetically I could add validator, but that would mean adding validators to all methods for consistency - not something I would like to do.
|
rich-l
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Refactor KubectlClient module with focus on logging Signed-off-by: Rafal Lal <[email protected]>
* Split kubectl_client.cr into smaller files (#16) Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
* Update kubectl_client version to v1.0.8 Signed-off-by: Rafal Lal <[email protected]>
Update shard.* files Signed-off-by: Rafal Lal <[email protected]>
Updates according to cnf-testsuite/kubectl_client#17 (#5) Signed-off-by: Rafal Lal <[email protected]>
Updates according to cnf-testsuite/kubectl_client#17 Signed-off-by: Rafal Lal <[email protected]>
Signed-off-by: Rafal Lal <[email protected]>
) * Update shard.yml with new versions of shards (which were also updated accordingly to cnf-testsuite/kubectl_client#17) Signed-off-by: Rafal Lal <[email protected]>
) * Update shard.yml with new versions of shards (which were also updated accordingly to cnf-testsuite/kubectl_client#17) Signed-off-by: Rafal Lal <[email protected]>
Generally ready to review, but not yet ready to merge as changes made here needs to be reflected in main cnti-testsuite.
Changes:
Log.for()log.infoshould be clear message in english,log.debugshould hold informations useful to debugging and not produce wall of logs with values of variables or other development leftovers. Also addlog.traceusage for very verbose logs like outputs of commands etc.Process::Status,stdoutandstderrwere returned in format of NamedTuple - so that was ok (some of the methods returned NamedTuple, some just Bool - this was standarised as well). It is not idomatic to crystal-langExceptionsbased approach but acceptable. In case of more complex methods, CLI command that fetches some resource from the kubernetes could fail, and method would ignore this fact and continue with the logic and return some value - in most cases empty one. This leaves the caller of the method in complete unknown - did the method fail?, did it finish succesfully and returned empty value?, is the empty value valid response or not?. Error handling is actaully very important in testsuite - lets look at scenario in which test case that checks if number of replicas can be changed is executed. Lets say some 3rd or 4th call to KubectlClient shard fails due to random connectivity error to the cluster. Testcase fails. Is the user notified that testcase failed due to network error and should be restarted or it fails without proper feedback leaving users with 0 points for it? Exceptions should be caught somewhere at testcase level and clearly described to users. In situation where exception is somewhat expected, it can berescuedand some adequate to sitation action can be taken. Reference Crystal's way to do error handling is by raising and rescuing exceptions.Getsubmodule methods with universalGet::resourceone - there were couple of methodsrepeating the very same pattern:
kubectl get <kind> <resource_name>, but the kind was hardcoded to deployment, service, pods, nodes... The only changing value waskind. This is making this shard harder to maintain, debug and keep in consistent state, I've seen many duplicated functionality across this shard mostly due to lack of universality in methods.container_digests_by_nodesand others - the reason was mainly that these methods were not used at all in main cnti-testsuite. The second reason was that these methods are just too specific to make sense. We can take golang kubectl-client package as reference. It mostly provides functions to fetch or post the resources, any aggregations or filtering is mainly specific to certain use cases and is done inside solutions code, not inside library. Some reasonable helpers are definitely okay, but here we have situation when we have those quite specific helpers, and they are just not used anywhere. Instead main cnti-testsuite uses this shard to fetch the resources (as it should be) and leaves tons of these helpers here, forgotten, unmainted, unused, filled with TODOs and develop time logs.Waitsubmodule with methods extracted from widerGetsubmodule - there were enough of them that creation of new submodule made senseThis is not everything but enough as first step. Second would be to move this shard to main cnti-testsuite as maintaining those shards outside of main codebase is problematic in itself.