What happened (please include outputs or screenshots):
Got a 404 error from the Kubernetes API due to the fact that both ExtensionsV1beta1Api and AppsV1beta1Api are deprecated in Kubernetes 1.16
The recently released 12.0.0a1 is supposed to have Kubernetes 1.16 support, but also doesn't have rollback with the proper API AppsV1Api
https://github.com/kubernetes-client/python/blob/v12.0.0a1/kubernetes/README.md
File "/usr/lib/python3.8/site-packages/kubernetes/client/apis/apps_v1beta1_api.py", line 291, in create_namespaced_deployment_rollback
(data) = self.create_namespaced_deployment_rollback_with_http_info(name, namespace, body, **kwargs)
File "/usr/lib/python3.8/site-packages/kubernetes/client/apis/apps_v1beta1_api.py", line 375, in create_namespaced_deployment_rollback_with_http_info
return self.api_client.call_api('/apis/apps/v1beta1/namespaces/{namespace}/deployments/{name}/rollback', 'POST',
File "/usr/lib/python3.8/site-packages/kubernetes/client/api_client.py", line 330, in call_api
return self.__call_api(resource_path, method,
File "/usr/lib/python3.8/site-packages/kubernetes/client/api_client.py", line 163, in __call_api
response_data = self.request(method, url,
File "/usr/lib/python3.8/site-packages/kubernetes/client/api_client.py", line 371, in request
return self.rest_client.POST(url,
File "/usr/lib/python3.8/site-packages/kubernetes/client/rest.py", line 260, in POST
return self.request("POST", url,
File "/usr/lib/python3.8/site-packages/kubernetes/client/rest.py", line 222, in request
raise ApiException(http_resp=r)
kubernetes.client.rest.ApiException: (404)
Reason: Not Found
What you expected to happen:
There should be a create_namespaced_deployment_rollback in the AppsV1Api Class so that it uses the non-deprecated API
How to reproduce it (as minimally and precisely as possible):
Attempt to rollback a deployment with either API against a cluster running 1.16:
api_response = client.AppsV1beta1Api().create_namespaced_deployment_rollback(
name=deployment_name,
namespace=namespace,
body=body,
_preload_content=False)
OR
api_response = client.ExtensionsV1beta1Api().create_namespaced_deployment_rollback(
name=deployment_name,
namespace=namespace,
body=body,
_preload_content=False)
Environment:
kubectl version): 1.16.11python --version) 3.8.5pip list | grep kubernetes) 12.0.0a1There should be a create_namespaced_deployment_rollback in the AppsV1Api Class
There is no deployment rollback endpoint in apps/v1 API: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#deployment-v1-apps.
@roycaihw should we drop support for ExtensionsV1beta1Api and AppsV1beta1Api in 12.0 release? I'd like to contribute to this.
@pigletfly 12.0 release corresponds to Kubernetes 1.16, which still has those API groups.
Any update on this? Rollback is indeed an important feature. Release 12 is still in beta. These updates can be incorporated I believe
An interim solution that may help others who stumble upon this issue:
from kubernetes import client as k8s, config as k8s_config
k8s_config.load_kube_config()
def rollback_deployment(deployment: k8s.V1Deployment) -> None:
"""Rollback a deployment to its previous version.
:param deployment: A configured deployment object.
"""
name = deployment.metadata.name
namespace = deployment.metadata.namespace
associated_replica_sets = k8s.AppsV1Api().list_namespaced_replica_set(
namespace=namespace,
label_selector=f'app={deployment.spec.template.metadata.labels["app"]}'
)
revision_ordered_replica_sets = sorted(
associated_replica_sets.items,
key=lambda e: e.metadata.annotations['deployment.kubernetes.io/revision'],
reverse=True
)
rollback_replica_set = (
revision_ordered_replica_sets[0]
if len(revision_ordered_replica_sets) == 1
else revision_ordered_replica_sets[1]
)
rollback_revision_number = (
rollback_replica_set
.metadata
.annotations['deployment.kubernetes.io/revision']
)
patch = [
{
'op': 'replace',
'path': '/spec/template',
'value': rollback_replica_set.spec.template
},
{
'op': 'replace',
'path': '/metadata/annotations',
'value': {
'deployment.kubernetes.io/revision': rollback_revision_number,
**deployment.metadata.annotations
}
}
]
k8s.AppsV1Api().patch_namespaced_deployment(
body=patch,
name=name,
namespace=namespace
)
Basically, I've reverse-engineered what kubectl rollout unto ... appears to be doing.
Most helpful comment
An interim solution that may help others who stumble upon this issue:
Basically, I've reverse-engineered what
kubectl rollout unto ...appears to be doing.