Skip to content

Commit

Permalink
Fixing setup_kubernetes_cr exception (#3658)
Browse files Browse the repository at this point in the history
* Fixing setup_kubernetes_cr exception

* Fixing tests and logic
  • Loading branch information
wilmer05 authored Jul 20, 2023
1 parent b870a9c commit a6a19b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
20 changes: 14 additions & 6 deletions paasta_tools/setup_kubernetes_cr.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from typing import Sequence

import yaml
from kubernetes.client.exceptions import ApiException

from paasta_tools.cli.utils import LONG_RUNNING_INSTANCE_TYPE_HANDLERS
from paasta_tools.flink_tools import get_flink_ingress_url_root
Expand Down Expand Up @@ -154,6 +155,7 @@ def setup_all_custom_resources(
) -> bool:

got_results = False
succeeded = False
# We support two versions due to our upgrade to 1.22
# this functions runs succefully when any of the two apiextensions
# succeed to update the CRDs as the cluster could be in any version
Expand All @@ -162,12 +164,18 @@ def setup_all_custom_resources(
kube_client.apiextensions,
kube_client.apiextensions_v1_beta1,
]:
cluster_crds = {
crd.spec.names.kind
for crd in apiextension.list_custom_resource_definition(

try:
crds_list = apiextension.list_custom_resource_definition(
label_selector=paasta_prefixed("service")
).items
}
except ApiException:
log.debug(
"Listing CRDs with apiextensions/v1 not supported on this cluster, falling back to v1beta1"
)
crds_list = []

cluster_crds = {crd.spec.names.kind for crd in crds_list}
log.debug(f"CRDs found: {cluster_crds}")
results = []
for crd in custom_resource_definitions:
Expand Down Expand Up @@ -202,11 +210,11 @@ def setup_all_custom_resources(
if results:
got_results = True
if any(results):
return True
succeeded = True
# we want to return True if we never called `setup_custom_resources`
# (i.e., we noop'd) or if any call to `setup_custom_resources`
# succeed (handled above) - otherwise, we want to return False
return not got_results
return succeeded or not got_results


def setup_custom_resources(
Expand Down
4 changes: 4 additions & 0 deletions tests/test_setup_kubernetes_cr.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ def test_setup_all_custom_resources_flink():
mock.Mock(items=[flink_crd])
)

mock_client.apiextensions_v1_beta1.list_custom_resource_definition.return_value = mock.Mock(
items=[]
)

custom_resource_definitions = [
mock.Mock(
kube_kind=mock.Mock(plural="flinks", singular="flink", kind="Flink")
Expand Down

0 comments on commit a6a19b3

Please sign in to comment.