From 5f5a559dbf0e673a8e3850001c5c87fc23f56111 Mon Sep 17 00:00:00 2001 From: Walter Behmann <40459646+WalBeh@users.noreply.github.com> Date: Fri, 10 Mar 2023 17:29:40 +0100 Subject: [PATCH] Remove deprecated APIs for pdb and batch (#477) --- CHANGES.rst | 2 ++ crate/operator/backup.py | 19 +++++++++---------- crate/operator/create.py | 12 ++++++------ crate/operator/operations.py | 11 +++++------ crate/operator/utils/typing.py | 4 ++-- tests/test_backup.py | 28 +++++++++++++--------------- tests/test_scale.py | 4 ++-- tests/utils.py | 5 ++--- 8 files changed, 41 insertions(+), 44 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index f8b270d9..4486e119 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,6 +5,8 @@ Changelog Unreleased ---------- +* Remove ``beta1`` from `PodDisruptionBudget` and ``Cronjob/Batch`` API version. + * Fixed a missing permission that was causing a warning on kopf startup. 2.23.0 (2023-02-28) diff --git a/crate/operator/backup.py b/crate/operator/backup.py index 320cb43f..e6db3008 100644 --- a/crate/operator/backup.py +++ b/crate/operator/backup.py @@ -24,17 +24,16 @@ from kubernetes_asyncio.client import ( AppsV1Api, - BatchV1beta1Api, - V1beta1CronJob, - V1beta1CronJobSpec, - V1beta1JobTemplateSpec, V1Container, V1ContainerPort, + V1CronJob, + V1CronJobSpec, V1Deployment, V1DeploymentSpec, V1EnvVar, V1EnvVarSource, V1JobSpec, + V1JobTemplateSpec, V1LabelSelector, V1LocalObjectReference, V1ObjectFieldSelector, @@ -131,7 +130,7 @@ def get_backup_cronjob( backup_aws: Dict[str, Any], image_pull_secrets: Optional[List[V1LocalObjectReference]], has_ssl: bool, -) -> V1beta1CronJob: +) -> V1CronJob: env = ( [ V1EnvVar( @@ -174,16 +173,16 @@ def get_backup_cronjob( ) ) - return V1beta1CronJob( + return V1CronJob( metadata=V1ObjectMeta( name=f"create-snapshot-{name}", labels=labels, owner_references=owner_references, ), - spec=V1beta1CronJobSpec( + spec=V1CronJobSpec( concurrency_policy="Forbid", failed_jobs_history_limit=1, - job_template=V1beta1JobTemplateSpec( + job_template=V1JobTemplateSpec( metadata=V1ObjectMeta(labels=labels, name=f"create-snapshot-{name}"), spec=V1JobSpec( template=V1PodTemplateSpec( @@ -284,10 +283,10 @@ async def create_backups( backup_aws = backups.get("aws") async with ApiClient() as api_client: apps = AppsV1Api(api_client) - batchv1_beta1 = BatchV1beta1Api(api_client) + batch = BatchV1Api(api_client) if backup_aws: await call_kubeapi( - batchv1_beta1.create_namespaced_cron_job, + batch.create_namespaced_cron_job, logger, continue_on_conflict=True, namespace=namespace, diff --git a/crate/operator/create.py b/crate/operator/create.py index 970112ce..aca55e96 100644 --- a/crate/operator/create.py +++ b/crate/operator/create.py @@ -30,10 +30,8 @@ from kubernetes_asyncio.client import ( AppsV1Api, CoreV1Api, - PolicyV1beta1Api, + PolicyV1Api, V1Affinity, - V1beta1PodDisruptionBudget, - V1beta1PodDisruptionBudgetSpec, V1ConfigMap, V1ConfigMapVolumeSource, V1Container, @@ -56,6 +54,8 @@ V1PersistentVolumeClaimSpec, V1PodAffinityTerm, V1PodAntiAffinity, + V1PodDisruptionBudget, + V1PodDisruptionBudgetSpec, V1PodSpec, V1PodTemplateSpec, V1Probe, @@ -836,13 +836,13 @@ async def create_statefulset( logger, ), ) - policy = PolicyV1beta1Api(api_client) - pdb = V1beta1PodDisruptionBudget( + policy = PolicyV1Api(api_client) + pdb = V1PodDisruptionBudget( metadata=V1ObjectMeta( name=f"crate-{name}", owner_references=owner_references, ), - spec=V1beta1PodDisruptionBudgetSpec( + spec=V1PodDisruptionBudgetSpec( max_unavailable=1, selector=V1LabelSelector( match_labels={ diff --git a/crate/operator/operations.py b/crate/operator/operations.py index d569e69c..a636f758 100644 --- a/crate/operator/operations.py +++ b/crate/operator/operations.py @@ -27,10 +27,9 @@ from kubernetes_asyncio.client import ( AppsV1Api, BatchV1Api, - BatchV1beta1Api, CoreV1Api, CustomObjectsApi, - V1beta1CronJobList, + V1CronJobList, V1JobList, V1JobStatus, V1PersistentVolumeClaimList, @@ -738,9 +737,9 @@ async def _ensure_cronjob_suspended( namespace: str, name: str, logger: logging.Logger ) -> Optional[Dict]: async with ApiClient() as api_client: - batch = BatchV1beta1Api(api_client) + batch = BatchV1Api(api_client) - jobs: V1beta1CronJobList = await batch.list_namespaced_cron_job(namespace) + jobs: V1CronJobList = await batch.list_namespaced_cron_job(namespace) for job in jobs.items: job_name = job.metadata.name @@ -895,9 +894,9 @@ async def _ensure_cronjob_reenabled( async with ApiClient() as api_client: job_name = disabler_job_status[CRONJOB_NAME] - batch = BatchV1beta1Api(api_client) + batch = BatchV1Api(api_client) - jobs: V1beta1CronJobList = await batch.list_namespaced_cron_job(namespace) + jobs: V1CronJobList = await batch.list_namespaced_cron_job(namespace) for job in jobs.items: if job.metadata.name == job_name: diff --git a/crate/operator/utils/typing.py b/crate/operator/utils/typing.py index 00515bd6..54ed3ff3 100644 --- a/crate/operator/utils/typing.py +++ b/crate/operator/utils/typing.py @@ -22,8 +22,8 @@ from typing import Dict, TypedDict, TypeVar from kubernetes_asyncio.client.models import ( - V1beta1CronJob, V1ConfigMap, + V1CronJob, V1Deployment, V1PersistentVolume, V1PersistentVolumeClaim, @@ -35,7 +35,7 @@ LabelType = Dict[str, str] K8sModel = TypeVar( "K8sModel", - V1beta1CronJob, + V1CronJob, V1ConfigMap, V1Deployment, V1PersistentVolume, diff --git a/tests/test_backup.py b/tests/test_backup.py index 982184f7..e3802027 100644 --- a/tests/test_backup.py +++ b/tests/test_backup.py @@ -22,7 +22,7 @@ import logging import pytest -from kubernetes_asyncio.client import AppsV1Api, BatchV1beta1Api, V1beta1CronJob +from kubernetes_asyncio.client import AppsV1Api, BatchV1Api, V1CronJob from crate.operator.backup import create_backups from crate.operator.constants import ( @@ -38,17 +38,15 @@ @pytest.mark.asyncio class TestBackup: async def does_cronjob_exist( - self, batchv1_beta1: BatchV1beta1Api, namespace: str, name: str + self, batch: BatchV1Api, namespace: str, name: str ) -> bool: - cjs = await batchv1_beta1.list_namespaced_cron_job(namespace=namespace) + cjs = await batch.list_namespaced_cron_job(namespace=namespace) return name in (cj.metadata.name for cj in cjs.items) async def get_cronjob( - selfself, batchv1_beta1: BatchV1beta1Api, namespace: str, name: str - ) -> V1beta1CronJob: - return await batchv1_beta1.read_namespaced_cron_job( - namespace=namespace, name=name - ) + self, batch: BatchV1Api, namespace: str, name: str + ) -> V1CronJob: + return await batch.read_namespaced_cron_job(namespace=namespace, name=name) async def does_deployment_exist( self, apps: AppsV1Api, namespace: str, name: str @@ -58,7 +56,7 @@ async def does_deployment_exist( async def test_create(self, faker, namespace, api_client): apps = AppsV1Api(api_client) - batchv1_beta1 = BatchV1beta1Api(api_client) + batch = BatchV1Api(api_client) name = faker.domain_word() backups_spec = { @@ -107,7 +105,7 @@ async def test_create(self, faker, namespace, api_client): await assert_wait_for( True, self.does_cronjob_exist, - batchv1_beta1, + batch, namespace.metadata.name, f"create-snapshot-{name}", ) @@ -122,7 +120,7 @@ async def test_create(self, faker, namespace, api_client): async def test_create_with_custom_backup_location( self, faker, namespace, api_client ): - batchv1_beta1 = BatchV1beta1Api(api_client) + batchv1 = BatchV1Api(api_client) name = faker.domain_word() backups_spec = { @@ -178,12 +176,12 @@ async def test_create_with_custom_backup_location( await assert_wait_for( True, self.does_cronjob_exist, - batchv1_beta1, + batchv1, namespace.metadata.name, f"create-snapshot-{name}", ) job = await self.get_cronjob( - batchv1_beta1, namespace.metadata.name, f"create-snapshot-{name}" + batchv1, namespace.metadata.name, f"create-snapshot-{name}" ) env_vars = [ env.name @@ -194,7 +192,7 @@ async def test_create_with_custom_backup_location( async def test_not_enabled(self, faker, namespace, api_client): name = faker.domain_word() apps = AppsV1Api(api_client) - batchv1_beta1 = BatchV1beta1Api(api_client) + batchv1 = BatchV1Api(api_client) await create_backups( None, @@ -210,7 +208,7 @@ async def test_not_enabled(self, faker, namespace, api_client): ) assert ( await self.does_cronjob_exist( - batchv1_beta1, namespace.metadata.name, f"create-snapshot-{name}" + batchv1, namespace.metadata.name, f"create-snapshot-{name}" ) is False ) diff --git a/tests/test_scale.py b/tests/test_scale.py index f3aa98e0..3a18cee6 100644 --- a/tests/test_scale.py +++ b/tests/test_scale.py @@ -26,7 +26,7 @@ import pytest from kubernetes_asyncio.client import ( AppsV1Api, - BatchV1beta1Api, + BatchV1Api, CoreV1Api, CustomObjectsApi, V1Namespace, @@ -457,7 +457,7 @@ async def _is_blocked_on_running_snapshot( async def _backup_cronjob_is_suspended(api_client, namespace: str): - batch = BatchV1beta1Api(api_client) + batch = BatchV1Api(api_client) jobs = await batch.list_namespaced_cron_job(namespace) for job in jobs.items: diff --git a/tests/utils.py b/tests/utils.py index 0f4ff146..914fc922 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -30,7 +30,6 @@ from aiopg import Connection from kubernetes_asyncio.client import ( BatchV1Api, - BatchV1beta1Api, CoreV1Api, CustomObjectsApi, V1Namespace, @@ -361,9 +360,9 @@ async def create_fake_cronjob(api_client, name, namespace): This can be used in tests to check for cronjobs existing, and their statuses. """ - batch = BatchV1beta1Api(api_client) + batch = BatchV1Api(api_client) body = { - "apiVersion": "batch/v1beta1", + "apiVersion": "batch/v1", "kind": "CronJob", "metadata": { "name": f"create-snapshot-{name}",