Skip to content

Commit

Permalink
Remove deprecated APIs for pdb and batch (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
WalBeh authored Mar 10, 2023
1 parent 0a6061f commit 5f5a559
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 44 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 9 additions & 10 deletions crate/operator/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions crate/operator/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@
from kubernetes_asyncio.client import (
AppsV1Api,
CoreV1Api,
PolicyV1beta1Api,
PolicyV1Api,
V1Affinity,
V1beta1PodDisruptionBudget,
V1beta1PodDisruptionBudgetSpec,
V1ConfigMap,
V1ConfigMapVolumeSource,
V1Container,
Expand All @@ -56,6 +54,8 @@
V1PersistentVolumeClaimSpec,
V1PodAffinityTerm,
V1PodAntiAffinity,
V1PodDisruptionBudget,
V1PodDisruptionBudgetSpec,
V1PodSpec,
V1PodTemplateSpec,
V1Probe,
Expand Down Expand Up @@ -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={
Expand Down
11 changes: 5 additions & 6 deletions crate/operator/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
from kubernetes_asyncio.client import (
AppsV1Api,
BatchV1Api,
BatchV1beta1Api,
CoreV1Api,
CustomObjectsApi,
V1beta1CronJobList,
V1CronJobList,
V1JobList,
V1JobStatus,
V1PersistentVolumeClaimList,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions crate/operator/utils/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from typing import Dict, TypedDict, TypeVar

from kubernetes_asyncio.client.models import (
V1beta1CronJob,
V1ConfigMap,
V1CronJob,
V1Deployment,
V1PersistentVolume,
V1PersistentVolumeClaim,
Expand All @@ -35,7 +35,7 @@
LabelType = Dict[str, str]
K8sModel = TypeVar(
"K8sModel",
V1beta1CronJob,
V1CronJob,
V1ConfigMap,
V1Deployment,
V1PersistentVolume,
Expand Down
28 changes: 13 additions & 15 deletions tests/test_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand All @@ -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 = {
Expand Down Expand Up @@ -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}",
)
Expand All @@ -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 = {
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import pytest
from kubernetes_asyncio.client import (
AppsV1Api,
BatchV1beta1Api,
BatchV1Api,
CoreV1Api,
CustomObjectsApi,
V1Namespace,
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from aiopg import Connection
from kubernetes_asyncio.client import (
BatchV1Api,
BatchV1beta1Api,
CoreV1Api,
CustomObjectsApi,
V1Namespace,
Expand Down Expand Up @@ -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}",
Expand Down

0 comments on commit 5f5a559

Please sign in to comment.