diff --git a/robusta_krr/core/integrations/kubernetes.py b/robusta_krr/core/integrations/kubernetes.py index 74a38560..8ee8afab 100644 --- a/robusta_krr/core/integrations/kubernetes.py +++ b/robusta_krr/core/integrations/kubernetes.py @@ -1,6 +1,6 @@ import asyncio from concurrent.futures import ThreadPoolExecutor -from typing import AsyncGenerator, Optional, Union +from typing import AsyncGenerator, Optional, Union, Callable, AsyncIterator import aiostream from kubernetes import client, config # type: ignore @@ -8,22 +8,17 @@ from kubernetes.client.models import ( V1Container, V1DaemonSet, - V1DaemonSetList, V1Deployment, - V1DeploymentList, V1Job, - V1JobList, V1LabelSelector, V1Pod, - V1PodList, V1StatefulSet, - V1StatefulSetList, V1HorizontalPodAutoscalerList, V2HorizontalPodAutoscaler, V2HorizontalPodAutoscalerList, ) -from robusta_krr.core.models.objects import HPAData, K8sObjectData +from robusta_krr.core.models.objects import HPAData, K8sObjectData, KindLiteral from robusta_krr.core.models.result import ResourceAllocations from robusta_krr.utils.configurable import Configurable @@ -53,6 +48,8 @@ def __init__(self, cluster: Optional[str], *args, **kwargs): self.autoscaling_v1 = client.AutoscalingV1Api(api_client=self.api_client) self.autoscaling_v2 = client.AutoscalingV2Api(api_client=self.api_client) + self.__rollouts_available = True + async def list_scannable_objects(self) -> AsyncGenerator[K8sObjectData, None]: """List all scannable objects. @@ -62,31 +59,25 @@ async def list_scannable_objects(self) -> AsyncGenerator[K8sObjectData, None]: self.info(f"Listing scannable objects in {self.cluster}") self.debug(f"Namespaces: {self.config.namespaces}") + self.debug(f"Resources: {self.config.resources}") self.__hpa_list = await self._try_list_hpa() - tasks = [ + # https://stackoverflow.com/questions/55299564/join-multiple-async-generators-in-python + # This will merge all the streams from all the cluster loaders into a single stream + objects_combined = aiostream.stream.merge( self._list_deployments(), self._list_rollouts(), self._list_all_statefulsets(), self._list_all_daemon_set(), self._list_all_jobs(), - ] - - for fut in asyncio.as_completed(tasks): - try: - object_list = await fut - except Exception as e: - self.error(f"Error {e.__class__.__name__} listing objects in cluster {self.cluster}: {e}") - self.debug_exception() - self.error("Will skip this object type and continue.") - continue + ) - for object in object_list: + async with objects_combined.stream() as streamer: + async for object in streamer: + # NOTE: By default we will filter out kube-system namespace if self.config.namespaces == "*" and object.namespace == "kube-system": continue - elif self.config.namespaces != "*" and object.namespace not in self.config.namespaces: - continue yield object @staticmethod @@ -127,108 +118,103 @@ def __build_obj( hpa=self.__hpa_list.get((namespace, kind, name)), ) - async def _list_deployments(self) -> list[K8sObjectData]: - self.debug(f"Listing deployments in {self.cluster}") - loop = asyncio.get_running_loop() - ret: V1DeploymentList = await loop.run_in_executor( - self.executor, - lambda: self.apps.list_deployment_for_all_namespaces( - watch=False, - label_selector=self.config.selector, - ), - ) - self.debug(f"Found {len(ret.items)} deployments in {self.cluster}") + def _should_list_resource(self, resource: str): + if self.config.resources == "*": + return True + return resource.lower() in self.config.resources + + async def _list_workflows( + self, kind: KindLiteral, all_namespaces_request: Callable, namespaced_request: Callable + ) -> AsyncIterator[K8sObjectData]: + if not self._should_list_resource(kind): + self.debug(f"Skipping {kind}s in {self.cluster}") + return - return [ - self.__build_obj(item, container) for item in ret.items for container in item.spec.template.spec.containers - ] + if kind == "rollout" and not self.__rollouts_available: + return - async def _list_rollouts(self) -> list[K8sObjectData]: - self.debug(f"Listing ArgoCD rollouts in {self.cluster}") + self.debug(f"Listing {kind}s in {self.cluster}") loop = asyncio.get_running_loop() + try: - ret: V1DeploymentList = await loop.run_in_executor( - self.executor, - lambda: self.rollout.list_rollout_for_all_namespaces( - watch=False, - label_selector=self.config.selector, - ), - ) + if self.config.namespaces == "*": + ret_multi = await loop.run_in_executor( + self.executor, + lambda: all_namespaces_request( + watch=False, + label_selector=self.config.selector, + ), + ) + self.debug(f"Found {len(ret_multi.items)} {kind} in {self.cluster}") + for item in ret_multi.items: + for container in item.spec.template.spec.containers: + yield self.__build_obj(item, container) + else: + tasks = [ + loop.run_in_executor( + self.executor, + lambda: namespaced_request( + namespace=namespace, + watch=False, + label_selector=self.config.selector, + ), + ) + for namespace in self.config.namespaces + ] + + total_items = 0 + for task in asyncio.as_completed(tasks): + ret_single = await task + total_items += len(ret_single.items) + for item in ret_single.items: + for container in item.spec.template.spec.containers: + yield self.__build_obj(item, container) + + self.debug(f"Found {total_items} {kind} in {self.cluster}") except ApiException as e: - if e.status in [400, 401, 403, 404]: - self.debug(f"Rollout API not available in {self.cluster}") - return [] - raise - - self.debug(f"Found {len(ret.items)} rollouts in {self.cluster}") - - return [ - self.__build_obj(item, container) for item in ret.items for container in item.spec.template.spec.containers - ] + if kind == "rollout" and e.status in [400, 401, 403, 404]: + if self.__rollouts_available: + self.debug(f"Rollout API not available in {self.cluster}") + self.__rollouts_available = False + else: + self.error(f"Error {e.status} listing {kind} in cluster {self.cluster}: {e.reason}") + self.debug_exception() + self.error("Will skip this object type and continue.") - async def _list_all_statefulsets(self) -> list[K8sObjectData]: - self.debug(f"Listing statefulsets in {self.cluster}") - loop = asyncio.get_running_loop() - ret: V1StatefulSetList = await loop.run_in_executor( - self.executor, - lambda: self.apps.list_stateful_set_for_all_namespaces( - watch=False, - label_selector=self.config.selector, - ), + def _list_deployments(self) -> AsyncIterator[K8sObjectData]: + return self._list_workflows( + kind="deployment", + all_namespaces_request=self.apps.list_deployment_for_all_namespaces, + namespaced_request=self.apps.list_namespaced_deployment, ) - self.debug(f"Found {len(ret.items)} statefulsets in {self.cluster}") - - return [ - self.__build_obj(item, container) for item in ret.items for container in item.spec.template.spec.containers - ] - async def _list_all_daemon_set(self) -> list[K8sObjectData]: - self.debug(f"Listing daemonsets in {self.cluster}") - loop = asyncio.get_running_loop() - ret: V1DaemonSetList = await loop.run_in_executor( - self.executor, - lambda: self.apps.list_daemon_set_for_all_namespaces( - watch=False, - label_selector=self.config.selector, - ), + def _list_rollouts(self) -> AsyncIterator[K8sObjectData]: + return self._list_workflows( + kind="rollout", + all_namespaces_request=self.rollout.list_rollout_for_all_namespaces, + namespaced_request=self.rollout.list_namespaced_rollout, ) - self.debug(f"Found {len(ret.items)} daemonsets in {self.cluster}") - - return [ - self.__build_obj(item, container) for item in ret.items for container in item.spec.template.spec.containers - ] - async def _list_all_jobs(self) -> list[K8sObjectData]: - self.debug(f"Listing jobs in {self.cluster}") - loop = asyncio.get_running_loop() - ret: V1JobList = await loop.run_in_executor( - self.executor, - lambda: self.batch.list_job_for_all_namespaces( - watch=False, - label_selector=self.config.selector, - ), + def _list_all_statefulsets(self) -> AsyncIterator[K8sObjectData]: + return self._list_workflows( + kind="statefulset", + all_namespaces_request=self.apps.list_stateful_set_for_all_namespaces, + namespaced_request=self.apps.list_namespaced_stateful_set, ) - self.debug(f"Found {len(ret.items)} jobs in {self.cluster}") - return [ - self.__build_obj(item, container) for item in ret.items for container in item.spec.template.spec.containers - ] - - async def _list_pods(self) -> list[K8sObjectData]: - """For future use, not supported yet.""" - - self.debug(f"Listing pods in {self.cluster}") - loop = asyncio.get_running_loop() - ret: V1PodList = await loop.run_in_executor( - self.executor, - lambda: self.apps.list_pod_for_all_namespaces( - watch=False, - label_selector=self.config.selector, - ), + def _list_all_daemon_set(self) -> AsyncIterator[K8sObjectData]: + return self._list_workflows( + kind="daemonset", + all_namespaces_request=self.apps.list_daemon_set_for_all_namespaces, + namespaced_request=self.apps.list_namespaced_daemon_set, ) - self.debug(f"Found {len(ret.items)} pods in {self.cluster}") - return [self.__build_obj(item, container) for item in ret.items for container in item.spec.containers] + def _list_all_jobs(self) -> AsyncIterator[K8sObjectData]: + return self._list_workflows( + kind="job", + all_namespaces_request=self.batch.list_job_for_all_namespaces, + namespaced_request=self.batch.list_namespaced_job, + ) async def __list_hpa_v1(self) -> dict[HPAKey, HPAData]: loop = asyncio.get_running_loop() @@ -367,7 +353,7 @@ def _try_create_cluster_loader(self, cluster: Optional[str]) -> Optional[Cluster self.error(f"Could not load cluster {cluster} and will skip it: {e}") return None - async def list_scannable_objects(self, clusters: Optional[list[str]]) -> AsyncGenerator[K8sObjectData, None]: + async def list_scannable_objects(self, clusters: Optional[list[str]]) -> AsyncIterator[K8sObjectData]: """List all scannable objects. Yields: diff --git a/robusta_krr/core/integrations/rollout.py b/robusta_krr/core/integrations/rollout.py index 3ac55138..677e00d9 100644 --- a/robusta_krr/core/integrations/rollout.py +++ b/robusta_krr/core/integrations/rollout.py @@ -1,10 +1,7 @@ from kubernetes import client import six -from kubernetes.client.exceptions import ( # noqa: F401 - ApiTypeError, - -) +from kubernetes.client.exceptions import ApiTypeError, ApiValueError class RolloutAppsV1Api(client.AppsV1Api): @@ -42,7 +39,7 @@ def list_rollout_for_all_namespaces(self, **kwargs): # noqa: E501 If the method is called asynchronously, returns the request thread. """ - kwargs['_return_http_data_only'] = True + kwargs["_return_http_data_only"] = True return self.list_rollout_for_all_namespaces_with_http_info(**kwargs) # noqa: E501 def list_rollout_for_all_namespaces_with_http_info(self, **kwargs): # noqa: E501 @@ -82,60 +79,228 @@ def list_rollout_for_all_namespaces_with_http_info(self, **kwargs): # noqa: E50 local_var_params = locals() all_params = [ - 'allow_watch_bookmarks', - '_continue', - 'field_selector', - 'label_selector', - 'limit', - 'pretty', - 'resource_version', - 'resource_version_match', - 'timeout_seconds', - 'watch' + "allow_watch_bookmarks", + "_continue", + "field_selector", + "label_selector", + "limit", + "pretty", + "resource_version", + "resource_version_match", + "timeout_seconds", + "watch", ] - all_params.extend( + all_params.extend(["async_req", "_return_http_data_only", "_preload_content", "_request_timeout"]) + + for key, val in six.iteritems(local_var_params["kwargs"]): + if key not in all_params: + raise ApiTypeError( + "Got an unexpected keyword argument '%s'" " to method list_deployment_for_all_namespaces" % key + ) + local_var_params[key] = val + del local_var_params["kwargs"] + + collection_formats = {} + + path_params = {} + + query_params = [] + if ( + "allow_watch_bookmarks" in local_var_params and local_var_params["allow_watch_bookmarks"] is not None + ): # noqa: E501 + query_params.append(("allowWatchBookmarks", local_var_params["allow_watch_bookmarks"])) # noqa: E501 + if "_continue" in local_var_params and local_var_params["_continue"] is not None: # noqa: E501 + query_params.append(("continue", local_var_params["_continue"])) # noqa: E501 + if "field_selector" in local_var_params and local_var_params["field_selector"] is not None: # noqa: E501 + query_params.append(("fieldSelector", local_var_params["field_selector"])) # noqa: E501 + if "label_selector" in local_var_params and local_var_params["label_selector"] is not None: # noqa: E501 + query_params.append(("labelSelector", local_var_params["label_selector"])) # noqa: E501 + if "limit" in local_var_params and local_var_params["limit"] is not None: # noqa: E501 + query_params.append(("limit", local_var_params["limit"])) # noqa: E501 + if "pretty" in local_var_params and local_var_params["pretty"] is not None: # noqa: E501 + query_params.append(("pretty", local_var_params["pretty"])) # noqa: E501 + if "resource_version" in local_var_params and local_var_params["resource_version"] is not None: # noqa: E501 + query_params.append(("resourceVersion", local_var_params["resource_version"])) # noqa: E501 + if ( + "resource_version_match" in local_var_params and local_var_params["resource_version_match"] is not None + ): # noqa: E501 + query_params.append(("resourceVersionMatch", local_var_params["resource_version_match"])) # noqa: E501 + if "timeout_seconds" in local_var_params and local_var_params["timeout_seconds"] is not None: # noqa: E501 + query_params.append(("timeoutSeconds", local_var_params["timeout_seconds"])) # noqa: E501 + if "watch" in local_var_params and local_var_params["watch"] is not None: # noqa: E501 + query_params.append(("watch", local_var_params["watch"])) # noqa: E501 + + header_params = {} + + form_params = [] + local_var_files = {} + + body_params = None + # HTTP header `Accept` + header_params["Accept"] = self.api_client.select_header_accept( [ - 'async_req', - '_return_http_data_only', - '_preload_content', - '_request_timeout' + "application/json", + "application/yaml", + "application/vnd.kubernetes.protobuf", + "application/json;stream=watch", + "application/vnd.kubernetes.protobuf;stream=watch", ] + ) # noqa: E501 + + # Authentication setting + auth_settings = ["BearerToken"] # noqa: E501 + + return self.api_client.call_api( + "/apis/argoproj.io/v1alpha1/rollouts", + "GET", + path_params, + query_params, + header_params, + body=body_params, + post_params=form_params, + files=local_var_files, + response_type="V1DeploymentList", # noqa: E501 + auth_settings=auth_settings, + async_req=local_var_params.get("async_req"), + _return_http_data_only=local_var_params.get("_return_http_data_only"), # noqa: E501 + _preload_content=local_var_params.get("_preload_content", True), + _request_timeout=local_var_params.get("_request_timeout"), + collection_formats=collection_formats, ) - for key, val in six.iteritems(local_var_params['kwargs']): + def list_namespaced_rollout(self, namespace, **kwargs): # noqa: E501 + """list_namespaced_rollout # noqa: E501 + + list or watch objects of kind ControllerRevision # noqa: E501 + This method makes a synchronous HTTP request by default. To make an + asynchronous HTTP request, please pass async_req=True + >>> thread = api.list_namespaced_rollout(namespace, async_req=True) + >>> result = thread.get() + + :param async_req bool: execute request asynchronously + :param str namespace: object name and auth scope, such as for teams and projects (required) + :param str pretty: If 'true', then the output is pretty printed. + :param bool allow_watch_bookmarks: allowWatchBookmarks requests watch events with type \"BOOKMARK\". Servers that do not implement bookmarks may ignore this flag and bookmarks are sent at the server's discretion. Clients should not assume bookmarks are returned at any specific interval, nor may they assume the server will send any BOOKMARK event during a session. If this is not a watch, this field is ignored. + :param str _continue: The continue option should be set when retrieving more results from the server. Since this value is server defined, clients may only use the continue value from a previous query result with identical query parameters (except for the value of continue) and the server may reject a continue value it does not recognize. If the specified continue value is no longer valid whether due to expiration (generally five to fifteen minutes) or a configuration change on the server, the server will respond with a 410 ResourceExpired error together with a continue token. If the client needs a consistent list, it must restart their list without the continue field. Otherwise, the client may send another list request with the token received with the 410 error, the server will respond with a list starting from the next key, but from the latest snapshot, which is inconsistent from the previous list results - objects that are created, modified, or deleted after the first list request will be included in the response, as long as their keys are after the \"next key\". This field is not supported when watch is true. Clients may start a watch from the last resourceVersion value returned by the server and not miss any modifications. + :param str field_selector: A selector to restrict the list of returned objects by their fields. Defaults to everything. + :param str label_selector: A selector to restrict the list of returned objects by their labels. Defaults to everything. + :param int limit: limit is a maximum number of responses to return for a list call. If more items exist, the server will set the `continue` field on the list metadata to a value that can be used with the same initial query to retrieve the next set of results. Setting a limit may return fewer than the requested amount of items (up to zero items) in the event all requested objects are filtered out and clients should only use the presence of the continue field to determine whether more results are available. Servers may choose not to support the limit argument and will return all of the available results. If limit is specified and the continue field is empty, clients may assume that no more results are available. This field is not supported if watch is true. The server guarantees that the objects returned when using continue will be identical to issuing a single list call without a limit - that is, no objects created, modified, or deleted after the first request is issued will be included in any subsequent continued requests. This is sometimes referred to as a consistent snapshot, and ensures that a client that is using limit to receive smaller chunks of a very large result can ensure they see all possible objects. If objects are updated during a chunked list the version of the object that was present at the time the first list result was calculated is returned. + :param str resource_version: resourceVersion sets a constraint on what resource versions a request may be served from. See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details. Defaults to unset + :param str resource_version_match: resourceVersionMatch determines how resourceVersion is applied to list calls. It is highly recommended that resourceVersionMatch be set for list calls where resourceVersion is set See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details. Defaults to unset + :param int timeout_seconds: Timeout for the list/watch call. This limits the duration of the call, regardless of any activity or inactivity. + :param bool watch: Watch for changes to the described resources and return them as a stream of add, update, and remove notifications. Specify resourceVersion. + :param _preload_content: if False, the urllib3.HTTPResponse object will + be returned without reading/decoding response + data. Default is True. + :param _request_timeout: timeout setting for this request. If one + number provided, it will be total request + timeout. It can also be a pair (tuple) of + (connection, read) timeouts. + :return: V1ControllerRevisionList + If the method is called asynchronously, + returns the request thread. + """ + kwargs["_return_http_data_only"] = True + return self.list_namespaced_rollout_with_http_info(namespace, **kwargs) # noqa: E501 + + def list_namespaced_rollout_with_http_info(self, namespace, **kwargs): # noqa: E501 + """list_namespaced_rollout # noqa: E501 + + list or watch objects of kind ControllerRevision # noqa: E501 + This method makes a synchronous HTTP request by default. To make an + asynchronous HTTP request, please pass async_req=True + >>> thread = api.list_namespaced_rollout_with_http_info(namespace, async_req=True) + >>> result = thread.get() + + :param async_req bool: execute request asynchronously + :param str namespace: object name and auth scope, such as for teams and projects (required) + :param str pretty: If 'true', then the output is pretty printed. + :param bool allow_watch_bookmarks: allowWatchBookmarks requests watch events with type \"BOOKMARK\". Servers that do not implement bookmarks may ignore this flag and bookmarks are sent at the server's discretion. Clients should not assume bookmarks are returned at any specific interval, nor may they assume the server will send any BOOKMARK event during a session. If this is not a watch, this field is ignored. + :param str _continue: The continue option should be set when retrieving more results from the server. Since this value is server defined, clients may only use the continue value from a previous query result with identical query parameters (except for the value of continue) and the server may reject a continue value it does not recognize. If the specified continue value is no longer valid whether due to expiration (generally five to fifteen minutes) or a configuration change on the server, the server will respond with a 410 ResourceExpired error together with a continue token. If the client needs a consistent list, it must restart their list without the continue field. Otherwise, the client may send another list request with the token received with the 410 error, the server will respond with a list starting from the next key, but from the latest snapshot, which is inconsistent from the previous list results - objects that are created, modified, or deleted after the first list request will be included in the response, as long as their keys are after the \"next key\". This field is not supported when watch is true. Clients may start a watch from the last resourceVersion value returned by the server and not miss any modifications. + :param str field_selector: A selector to restrict the list of returned objects by their fields. Defaults to everything. + :param str label_selector: A selector to restrict the list of returned objects by their labels. Defaults to everything. + :param int limit: limit is a maximum number of responses to return for a list call. If more items exist, the server will set the `continue` field on the list metadata to a value that can be used with the same initial query to retrieve the next set of results. Setting a limit may return fewer than the requested amount of items (up to zero items) in the event all requested objects are filtered out and clients should only use the presence of the continue field to determine whether more results are available. Servers may choose not to support the limit argument and will return all of the available results. If limit is specified and the continue field is empty, clients may assume that no more results are available. This field is not supported if watch is true. The server guarantees that the objects returned when using continue will be identical to issuing a single list call without a limit - that is, no objects created, modified, or deleted after the first request is issued will be included in any subsequent continued requests. This is sometimes referred to as a consistent snapshot, and ensures that a client that is using limit to receive smaller chunks of a very large result can ensure they see all possible objects. If objects are updated during a chunked list the version of the object that was present at the time the first list result was calculated is returned. + :param str resource_version: resourceVersion sets a constraint on what resource versions a request may be served from. See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details. Defaults to unset + :param str resource_version_match: resourceVersionMatch determines how resourceVersion is applied to list calls. It is highly recommended that resourceVersionMatch be set for list calls where resourceVersion is set See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details. Defaults to unset + :param int timeout_seconds: Timeout for the list/watch call. This limits the duration of the call, regardless of any activity or inactivity. + :param bool watch: Watch for changes to the described resources and return them as a stream of add, update, and remove notifications. Specify resourceVersion. + :param _return_http_data_only: response data without head status code + and headers + :param _preload_content: if False, the urllib3.HTTPResponse object will + be returned without reading/decoding response + data. Default is True. + :param _request_timeout: timeout setting for this request. If one + number provided, it will be total request + timeout. It can also be a pair (tuple) of + (connection, read) timeouts. + :return: tuple(V1ControllerRevisionList, status_code(int), headers(HTTPHeaderDict)) + If the method is called asynchronously, + returns the request thread. + """ + + local_var_params = locals() + + all_params = [ + "namespace", + "pretty", + "allow_watch_bookmarks", + "_continue", + "field_selector", + "label_selector", + "limit", + "resource_version", + "resource_version_match", + "timeout_seconds", + "watch", + ] + all_params.extend(["async_req", "_return_http_data_only", "_preload_content", "_request_timeout"]) + + for key, val in six.iteritems(local_var_params["kwargs"]): if key not in all_params: raise ApiTypeError( - "Got an unexpected keyword argument '%s'" - " to method list_deployment_for_all_namespaces" % key + "Got an unexpected keyword argument '%s'" " to method list_namespaced_controller_revision" % key ) local_var_params[key] = val - del local_var_params['kwargs'] + del local_var_params["kwargs"] + # verify the required parameter 'namespace' is set + if self.api_client.client_side_validation and ( + "namespace" not in local_var_params or local_var_params["namespace"] is None # noqa: E501 + ): # noqa: E501 + raise ApiValueError( + "Missing the required parameter `namespace` when calling `list_namespaced_controller_revision`" + ) # noqa: E501 collection_formats = {} path_params = {} + if "namespace" in local_var_params: + path_params["namespace"] = local_var_params["namespace"] # noqa: E501 query_params = [] - if 'allow_watch_bookmarks' in local_var_params and local_var_params['allow_watch_bookmarks'] is not None: # noqa: E501 - query_params.append(('allowWatchBookmarks', local_var_params['allow_watch_bookmarks'])) # noqa: E501 - if '_continue' in local_var_params and local_var_params['_continue'] is not None: # noqa: E501 - query_params.append(('continue', local_var_params['_continue'])) # noqa: E501 - if 'field_selector' in local_var_params and local_var_params['field_selector'] is not None: # noqa: E501 - query_params.append(('fieldSelector', local_var_params['field_selector'])) # noqa: E501 - if 'label_selector' in local_var_params and local_var_params['label_selector'] is not None: # noqa: E501 - query_params.append(('labelSelector', local_var_params['label_selector'])) # noqa: E501 - if 'limit' in local_var_params and local_var_params['limit'] is not None: # noqa: E501 - query_params.append(('limit', local_var_params['limit'])) # noqa: E501 - if 'pretty' in local_var_params and local_var_params['pretty'] is not None: # noqa: E501 - query_params.append(('pretty', local_var_params['pretty'])) # noqa: E501 - if 'resource_version' in local_var_params and local_var_params['resource_version'] is not None: # noqa: E501 - query_params.append(('resourceVersion', local_var_params['resource_version'])) # noqa: E501 - if 'resource_version_match' in local_var_params and local_var_params['resource_version_match'] is not None: # noqa: E501 - query_params.append(('resourceVersionMatch', local_var_params['resource_version_match'])) # noqa: E501 - if 'timeout_seconds' in local_var_params and local_var_params['timeout_seconds'] is not None: # noqa: E501 - query_params.append(('timeoutSeconds', local_var_params['timeout_seconds'])) # noqa: E501 - if 'watch' in local_var_params and local_var_params['watch'] is not None: # noqa: E501 - query_params.append(('watch', local_var_params['watch'])) # noqa: E501 + if "pretty" in local_var_params and local_var_params["pretty"] is not None: # noqa: E501 + query_params.append(("pretty", local_var_params["pretty"])) # noqa: E501 + if ( + "allow_watch_bookmarks" in local_var_params and local_var_params["allow_watch_bookmarks"] is not None + ): # noqa: E501 + query_params.append(("allowWatchBookmarks", local_var_params["allow_watch_bookmarks"])) # noqa: E501 + if "_continue" in local_var_params and local_var_params["_continue"] is not None: # noqa: E501 + query_params.append(("continue", local_var_params["_continue"])) # noqa: E501 + if "field_selector" in local_var_params and local_var_params["field_selector"] is not None: # noqa: E501 + query_params.append(("fieldSelector", local_var_params["field_selector"])) # noqa: E501 + if "label_selector" in local_var_params and local_var_params["label_selector"] is not None: # noqa: E501 + query_params.append(("labelSelector", local_var_params["label_selector"])) # noqa: E501 + if "limit" in local_var_params and local_var_params["limit"] is not None: # noqa: E501 + query_params.append(("limit", local_var_params["limit"])) # noqa: E501 + if "resource_version" in local_var_params and local_var_params["resource_version"] is not None: # noqa: E501 + query_params.append(("resourceVersion", local_var_params["resource_version"])) # noqa: E501 + if ( + "resource_version_match" in local_var_params and local_var_params["resource_version_match"] is not None + ): # noqa: E501 + query_params.append(("resourceVersionMatch", local_var_params["resource_version_match"])) # noqa: E501 + if "timeout_seconds" in local_var_params and local_var_params["timeout_seconds"] is not None: # noqa: E501 + query_params.append(("timeoutSeconds", local_var_params["timeout_seconds"])) # noqa: E501 + if "watch" in local_var_params and local_var_params["watch"] is not None: # noqa: E501 + query_params.append(("watch", local_var_params["watch"])) # noqa: E501 header_params = {} @@ -144,24 +309,33 @@ def list_rollout_for_all_namespaces_with_http_info(self, **kwargs): # noqa: E50 body_params = None # HTTP header `Accept` - header_params['Accept'] = self.api_client.select_header_accept( - ['application/json', 'application/yaml', 'application/vnd.kubernetes.protobuf', 'application/json;stream=watch', 'application/vnd.kubernetes.protobuf;stream=watch']) # noqa: E501 + header_params["Accept"] = self.api_client.select_header_accept( + [ + "application/json", + "application/yaml", + "application/vnd.kubernetes.protobuf", + "application/json;stream=watch", + "application/vnd.kubernetes.protobuf;stream=watch", + ] + ) # noqa: E501 # Authentication setting - auth_settings = ['BearerToken'] # noqa: E501 + auth_settings = ["BearerToken"] # noqa: E501 return self.api_client.call_api( - '/apis/argoproj.io/v1alpha1/rollouts', 'GET', + "/apis/argoproj.io/v1alpha1/namespaces/{namespace}/rollouts", + "GET", path_params, query_params, header_params, body=body_params, post_params=form_params, files=local_var_files, - response_type='V1DeploymentList', # noqa: E501 + response_type="V1DeploymentList", # noqa: E501 auth_settings=auth_settings, - async_req=local_var_params.get('async_req'), - _return_http_data_only=local_var_params.get('_return_http_data_only'), # noqa: E501 - _preload_content=local_var_params.get('_preload_content', True), - _request_timeout=local_var_params.get('_request_timeout'), - collection_formats=collection_formats) + async_req=local_var_params.get("async_req"), + _return_http_data_only=local_var_params.get("_return_http_data_only"), # noqa: E501 + _preload_content=local_var_params.get("_preload_content", True), + _request_timeout=local_var_params.get("_request_timeout"), + collection_formats=collection_formats, + ) diff --git a/robusta_krr/core/models/config.py b/robusta_krr/core/models/config.py index 70ae7a9f..8aef1a72 100644 --- a/robusta_krr/core/models/config.py +++ b/robusta_krr/core/models/config.py @@ -8,6 +8,8 @@ from robusta_krr.core.abstract import formatters from robusta_krr.core.abstract.strategies import AnyStrategy, BaseStrategy +from robusta_krr.core.models.objects import KindLiteral + class Config(pd.BaseSettings): quiet: bool = pd.Field(False) @@ -16,6 +18,7 @@ class Config(pd.BaseSettings): clusters: Union[list[str], Literal["*"], None] = None kubeconfig: Optional[str] = None namespaces: Union[list[str], Literal["*"]] = pd.Field("*") + resources: Union[list[KindLiteral], Literal["*"]] = pd.Field("*") selector: Optional[str] = None # Value settings @@ -75,7 +78,14 @@ def validate_namespaces(cls, v: Union[list[str], Literal["*"]]) -> Union[list[st if v == []: return "*" - return v + return [val.lower() for val in v] + + @pd.validator("resources", pre=True) + def validate_resources(cls, v: Union[list[str], Literal["*"]]) -> Union[list[str], Literal["*"]]: + if v == []: + return "*" + + return [val.lower() for val in v] def create_strategy(self) -> AnyStrategy: StrategyType = AnyStrategy.find(self.strategy) diff --git a/robusta_krr/core/models/objects.py b/robusta_krr/core/models/objects.py index ede4be8d..63049c41 100644 --- a/robusta_krr/core/models/objects.py +++ b/robusta_krr/core/models/objects.py @@ -1,10 +1,13 @@ -from typing import Optional +from typing import Optional, Literal import pydantic as pd from robusta_krr.core.models.allocations import ResourceAllocations +KindLiteral = Literal["deployment", "daemonset", "statefulset", "job", "rollout"] + + class PodData(pd.BaseModel): name: str deleted: bool diff --git a/robusta_krr/main.py b/robusta_krr/main.py index 5fbe4566..2522fafe 100644 --- a/robusta_krr/main.py +++ b/robusta_krr/main.py @@ -5,6 +5,8 @@ from datetime import datetime from typing import List, Literal, Optional, Union from uuid import UUID +from pydantic import ValidationError # noqa: F401 +from rich import print # noqa: F401 import typer import urllib3 @@ -72,6 +74,13 @@ def {func_name}( help="List of namespaces to run on. By default, will run on all namespaces.", rich_help_panel="Kubernetes Settings" ), + resources: List[str] = typer.Option( + None, + "--resource", + "-r", + help="List of resources to run on (Deployment, StatefullSet, DaemonSet, Job, Rollout). By default, will run on all resources. Case insensitive.", + rich_help_panel="Kubernetes Settings" + ), selector: Optional[str] = typer.Option( None, "--selector", @@ -176,38 +185,42 @@ def {func_name}( {strategy_settings}, ) -> None: '''Run KRR using the `{func_name}` strategy''' - - config = Config( - kubeconfig=kubeconfig, - clusters="*" if all_clusters else clusters, - namespaces="*" if "*" in namespaces else namespaces, - selector=selector, - prometheus_url=prometheus_url, - prometheus_auth_header=prometheus_auth_header, - prometheus_other_headers=prometheus_other_headers, - prometheus_ssl_enabled=prometheus_ssl_enabled, - prometheus_cluster_label=prometheus_cluster_label, - prometheus_label=prometheus_label, - eks_managed_prom=eks_managed_prom, - eks_managed_prom_region=eks_managed_prom_region, - eks_managed_prom_profile_name=eks_managed_prom_profile_name, - eks_access_key=eks_access_key, - eks_secret_key=eks_secret_key, - eks_service_name=eks_service_name, - coralogix_token=coralogix_token, - max_workers=max_workers, - format=format, - verbose=verbose, - quiet=quiet, - log_to_stderr=log_to_stderr, - file_output=file_output, - slack_output=slack_output, - strategy="{func_name}", - other_args={strategy_args}, - ) - runner = Runner(config) - - asyncio.run(runner.run()) + + try: + config = Config( + kubeconfig=kubeconfig, + clusters="*" if all_clusters else clusters, + namespaces="*" if "*" in namespaces else namespaces, + resources="*" if "*" in resources else resources, + selector=selector, + prometheus_url=prometheus_url, + prometheus_auth_header=prometheus_auth_header, + prometheus_other_headers=prometheus_other_headers, + prometheus_ssl_enabled=prometheus_ssl_enabled, + prometheus_cluster_label=prometheus_cluster_label, + prometheus_label=prometheus_label, + eks_managed_prom=eks_managed_prom, + eks_managed_prom_region=eks_managed_prom_region, + eks_managed_prom_profile_name=eks_managed_prom_profile_name, + eks_access_key=eks_access_key, + eks_secret_key=eks_secret_key, + eks_service_name=eks_service_name, + coralogix_token=coralogix_token, + max_workers=max_workers, + format=format, + verbose=verbose, + quiet=quiet, + log_to_stderr=log_to_stderr, + file_output=file_output, + slack_output=slack_output, + strategy="{func_name}", + other_args={strategy_args}, + ) + except ValidationError as e: + print(str(e)) + else: + runner = Runner(config) + asyncio.run(runner.run()) """ )