question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to use create_namespaced_deployment_rollback in Kubernetes 1.16+

See original GitHub issue

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:

  • Kubernetes version (kubectl version): 1.16.11
  • Python version (python --version) 3.8.5
  • Python client version (pip list | grep kubernetes) 12.0.0a1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
AlexIoannidescommented, Nov 6, 2020

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.

0reactions
Mubangizicommented, Nov 11, 2022

Any update on this Issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kubernetes 1.16: Custom Resources, Overhauled Metrics ...
Major Themes Custom resources CRDs are in widespread use as a Kubernetes ... error: unable to recognize "deployment": no matches for kind ...
Read more >
Kubernetes 1.16 deprecated APIs - Google Cloud
After upgrading the clusters to Kubernetes 1.16, the same command will automatically use the new API. You can explicitly specify an API group...
Read more >
Troubleshoot Azure Kubernetes Services issues
Documentation for troubleshooting common issues with Azure Kubernetes ... error during an upgrade · Kubernetes 1.16 upgrades fail using node labels ...
Read more >
What's new in Kubernetes 1.16? - Sysdig
Ephemeral containers are a great way to debug running pods, as you can't add regular containers to a pod after creation (you should...
Read more >
Helm init fails on Kubernetes 1.16.0 · Issue #6374 - GitHub
mihivagyok commented on Sep 19, 2019. If you want to use one less sed :) helm init --service-account tiller --override ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found