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

Revert "Allow to modify namespace when creating/updating/listing custom resources" #3939

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 0 additions & 6 deletions paasta_tools/cleanup_kubernetes_cr.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from paasta_tools.kubernetes_tools import list_custom_resources
from paasta_tools.kubernetes_tools import load_custom_resource_definitions
from paasta_tools.kubernetes_tools import paasta_prefixed
from paasta_tools.setup_kubernetes_cr import INSTANCE_TYPE_TO_NAMESPACE_LOADER
from paasta_tools.utils import DEFAULT_SOA_DIR
from paasta_tools.utils import load_all_configs
from paasta_tools.utils import load_system_paasta_config
Expand Down Expand Up @@ -106,16 +105,11 @@ def cleanup_all_custom_resources(
)
if not config_dicts:
continue
if crd.file_prefix in INSTANCE_TYPE_TO_NAMESPACE_LOADER:
namespace = INSTANCE_TYPE_TO_NAMESPACE_LOADER[crd.file_prefix]
else:
namespace = ""
crs = list_custom_resources(
kube_client=kube_client,
kind=crd.kube_kind,
version=crd.version,
group=crd.group,
namespace=namespace,
)
for cr in crs:
service = config_dicts.get(cr.service)
Expand Down
17 changes: 4 additions & 13 deletions paasta_tools/kubernetes_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3081,14 +3081,11 @@ def create_custom_resource(
version: str,
kind: KubeKind,
group: str,
namespace: str = "",
) -> None:
if not namespace:
namespace = f"paasta-{kind.plural}"
return kube_client.custom.create_namespaced_custom_object(
group=group,
version=version,
namespace=namespace,
namespace=f"paasta-{kind.plural}",
plural=kind.plural,
body=formatted_resource,
)
Expand All @@ -3101,15 +3098,12 @@ def update_custom_resource(
name: str,
kind: KubeKind,
group: str,
namespace: str = "",
) -> None:
if not namespace:
namespace = f"paasta-{kind.plural}"
co = kube_client.custom.get_namespaced_custom_object(
name=name,
group=group,
version=version,
namespace=namespace,
namespace=f"paasta-{kind.plural}",
plural=kind.plural,
)
formatted_resource["metadata"]["resourceVersion"] = co["metadata"][
Expand All @@ -3119,7 +3113,7 @@ def update_custom_resource(
name=name,
group=group,
version=version,
namespace=namespace,
namespace=f"paasta-{kind.plural}",
plural=kind.plural,
body=formatted_resource,
)
Expand All @@ -3131,16 +3125,13 @@ def list_custom_resources(
kube_client: KubeClient,
group: str,
label_selector: str = "",
namespace: str = "",
) -> Sequence[KubeCustomResource]:
if not namespace:
namespace = f"paasta-{kind.plural}"
crs = kube_client.custom.list_namespaced_custom_object(
group=group,
version=version,
label_selector=label_selector,
plural=kind.plural,
namespace=namespace,
namespace=f"paasta-{kind.plural}",
)
kube_custom_resources = []
for cr in crs["items"]:
Expand Down
30 changes: 3 additions & 27 deletions paasta_tools/setup_kubernetes_cr.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@
from paasta_tools.utils import load_all_configs
from paasta_tools.utils import load_system_paasta_config
from paasta_tools.vitesscell_tools import load_vitess_cell_instance_configs
from paasta_tools.vitesscell_tools import VITESSCELL_KUBERNETES_NAMESPACE
from paasta_tools.vitesscluster_tools import load_vitess_cluster_instance_configs
from paasta_tools.vitesscluster_tools import VITESSCLUSTER_KUBERNETES_NAMESPACE
from paasta_tools.vitesskeyspace_tools import load_vitess_keyspace_instance_configs
from paasta_tools.vitesskeyspace_tools import VITESSKEYSPACE_KUBERNETES_NAMESPACE


log = logging.getLogger(__name__)
Expand All @@ -67,13 +64,6 @@
}


INSTANCE_TYPE_TO_NAMESPACE_LOADER = {
"vitesscluster": VITESSCLUSTER_KUBERNETES_NAMESPACE,
"vitesscell": VITESSCELL_KUBERNETES_NAMESPACE,
"vitesskeyspace": VITESSKEYSPACE_KUBERNETES_NAMESPACE,
}


class StdoutKubeClient:
"""Replace all destructive operations in Kubernetes APIs with
writing out YAML to stdout."""
Expand Down Expand Up @@ -250,16 +240,8 @@ def setup_custom_resources(
) -> bool:
succeded = True
if config_dicts:
if crd.file_prefix in INSTANCE_TYPE_TO_NAMESPACE_LOADER:
namespace = INSTANCE_TYPE_TO_NAMESPACE_LOADER[crd.file_prefix]
else:
namespace = f"paasta-{kind.plural}"
crs = list_custom_resources(
kube_client=kube_client,
kind=kind,
version=version,
group=group,
namespace=namespace,
kube_client=kube_client, kind=kind, version=version, group=group
)
for svc, config in config_dicts.items():
if service is not None and service != svc:
Expand Down Expand Up @@ -387,10 +369,6 @@ def reconcile_kubernetes_resource(
cluster=cluster,
soa_dir=DEFAULT_SOA_DIR,
)
if crd.file_prefix in INSTANCE_TYPE_TO_NAMESPACE_LOADER:
namespace = INSTANCE_TYPE_TO_NAMESPACE_LOADER[crd.file_prefix]
else:
namespace = f"paasta-{kind.plural}"
git_sha = get_git_sha_from_dockerurl(soa_config.get_docker_url(), long=True)
formatted_resource = format_custom_resource(
instance_config=config,
Expand All @@ -400,7 +378,7 @@ def reconcile_kubernetes_resource(
kind=kind.singular,
version=version,
group=group,
namespace=namespace,
namespace=f"paasta-{kind.plural}",
git_sha=git_sha,
is_eks=is_eks,
)
Expand All @@ -415,7 +393,7 @@ def reconcile_kubernetes_resource(
),
kind=kind.singular,
name=formatted_resource["metadata"]["name"],
namespace=namespace,
namespace=f"paasta-{kind.plural}",
)
if not (service, inst, kind.singular) in [
(c.service, c.instance, c.kind) for c in custom_resources
Expand All @@ -427,7 +405,6 @@ def reconcile_kubernetes_resource(
kind=kind,
formatted_resource=formatted_resource,
group=group,
namespace=namespace,
)
elif desired_resource not in custom_resources:
sanitised_service = sanitise_kubernetes_name(service)
Expand All @@ -440,7 +417,6 @@ def reconcile_kubernetes_resource(
kind=kind,
formatted_resource=formatted_resource,
group=group,
namespace=namespace,
)
else:
log.info(f"{desired_resource} is up to date, no action taken")
Expand Down
4 changes: 2 additions & 2 deletions paasta_tools/vitesscell_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
log.addHandler(logging.NullHandler())


VITESSCELL_KUBERNETES_NAMESPACE = "paasta-vitessclusters"
KUBERNETES_NAMESPACE = "paasta-vitessclusters"


# Global variables
Expand Down Expand Up @@ -283,7 +283,7 @@ def cr_id(service: str, instance: str) -> Mapping[str, str]:
return dict(
group="planetscale.com",
version="v2",
namespace=VITESSCELL_KUBERNETES_NAMESPACE,
namespace=KUBERNETES_NAMESPACE,
plural="vitesscells",
name=sanitised_cr_name(service, instance),
)
6 changes: 3 additions & 3 deletions paasta_tools/vitesscluster_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
log.addHandler(logging.NullHandler())


VITESSCLUSTER_KUBERNETES_NAMESPACE = "paasta-vitessclusters"
KUBERNETES_NAMESPACE = "paasta-vitessclusters"


# Global variables
Expand Down Expand Up @@ -249,7 +249,7 @@ class VitessClusterInstanceConfigDict(KubernetesDeploymentConfigDict, total=Fals

class VitessDeploymentConfig(KubernetesDeploymentConfig):
def get_namespace(self) -> str:
return VITESSCLUSTER_KUBERNETES_NAMESPACE
return KUBERNETES_NAMESPACE

def get_env_variables(self) -> List[Union[KVEnvVar, KVEnvVarValueFrom]]:
# get all K8s container env vars and format their keys to camel case
Expand Down Expand Up @@ -493,7 +493,7 @@ def cr_id(service: str, instance: str) -> Mapping[str, str]:
return dict(
group="planetscale.com",
version="v2",
namespace=VITESSCLUSTER_KUBERNETES_NAMESPACE,
namespace=KUBERNETES_NAMESPACE,
plural="vitessclusters",
name=sanitised_cr_name(service, instance),
)
4 changes: 2 additions & 2 deletions paasta_tools/vitesskeyspace_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
log.addHandler(logging.NullHandler())


VITESSKEYSPACE_KUBERNETES_NAMESPACE = "paasta-vitessclusters"
KUBERNETES_NAMESPACE = "paasta-vitessclusters"


# Global variables
Expand Down Expand Up @@ -533,7 +533,7 @@ def cr_id(service: str, instance: str) -> Mapping[str, str]:
return dict(
group="planetscale.com",
version="v2",
namespace=VITESSKEYSPACE_KUBERNETES_NAMESPACE,
namespace=KUBERNETES_NAMESPACE,
plural="vitesskeyspaces",
name=sanitised_cr_name(service, instance),
)
3 changes: 0 additions & 3 deletions tests/test_setup_kubernetes_cr.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,6 @@ def test_reconcile_kubernetes_resource(mock_LONG_RUNNING_INSTANCE_TYPE_HANDLERS)
kind=mock_kind,
formatted_resource=mock_format_custom_resource.return_value,
group="yelp.com",
namespace="paasta-flinks",
)

# instance not exist, create
Expand All @@ -478,7 +477,6 @@ def test_reconcile_kubernetes_resource(mock_LONG_RUNNING_INSTANCE_TYPE_HANDLERS)
kind=mock_kind,
formatted_resource=mock_format_custom_resource.return_value,
group="yelp.com",
namespace="paasta-flinks",
)

# instance not exist, create but error with k8s
Expand All @@ -500,5 +498,4 @@ def test_reconcile_kubernetes_resource(mock_LONG_RUNNING_INSTANCE_TYPE_HANDLERS)
kind=mock_kind,
formatted_resource=mock_format_custom_resource.return_value,
group="yelp.com",
namespace="paasta-flinks",
)
Loading