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

[V1HorizontalPodAutoscaler] - 'Invalid value for conditions, must not be None' #1543

Closed
cradules opened this issue Sep 1, 2021 · 3 comments
Assignees
Labels
kind/bug Categorizes issue or PR as related to a bug.

Comments

@cradules
Copy link

cradules commented Sep 1, 2021

What happened (please include outputs or screenshots):
create_namespaced_horizontal_pod_autoscaler create exception 'Invalid value for `conditions`, must not be `None`'
What you expected to happen:
No exceptions.
How to reproduce it (as minimally and precisely as possible):

from flask import jsonify
from kubernetes import client, config

config.load_kube_config()

kube = client.AutoscalingV2beta1Api()


def create_hpa_object(name, owner, max_replicas, min_replicas, target_name,
                      target_cpu_utilization_percentage):
    hpa_body = client.V1HorizontalPodAutoscaler(
        metadata=client.V1ObjectMeta(
            name=name,
            labels={"app": name, "owner": owner},
        ),
        spec=client.V1HorizontalPodAutoscalerSpec(
            max_replicas=max_replicas,
            min_replicas=min_replicas,
            scale_target_ref=client.V1CrossVersionObjectReference(
                kind="Deployment",
                name=target_name,
            ),
            target_cpu_utilization_percentage=target_cpu_utilization_percentage
        )
    )
    return hpa_body


def create_hpa(namespace, body):
    resp = kube.create_namespaced_horizontal_pod_autoscaler(namespace=namespace, body=body)
    return jsonify(str(resp.metadata.name))
@app.route('/hpa', methods=['POST'])
def create_hpa():
    hpa_data = request.get_json()

    # Read data from body and used them as Local variables
    name = hpa_data['name']
    namespace = hpa_data['namespace']
    owner = hpa_data['owner']
    max_replicas = hpa_data['max_replicas']
    min_replicas = hpa_data['min_replicas']
    target_name = hpa_data['target_name']
    target_cpu_utilization_percentage = hpa_data['target_cpu_utilization_percentage']

    # Check if the method is POST
    if request.method == 'POST':
        try:
            # Create hpa
            hpa_body = AutoscalingV1Api.create_hpa_object(name=name,
                                                          owner=owner,
                                                          max_replicas=max_replicas,
                                                          min_replicas=min_replicas,
                                                          target_name=target_name,
                                                          target_cpu_utilization_percentage=
                                                          target_cpu_utilization_percentage)
            return AutoscalingV1Api.create_hpa(namespace=namespace, body=hpa_body)
        except Exception as e:
            # Bypass the exception to avoid misleading info
            # if str(e) == 'Invalid value for `conditions`, must not be `None`':
            #     return "Here you need a return status function to bypass the bug of AutoscalingV1Api"
            # else:
            return str(e)
    else:
        return Exception

Anything else we need to know?:

Environment:

  • Kubernetes version (v1.20.10):
  • OS (Windows 10):
  • Python version (3.8)
  • Python client version (18.20.0)
@cradules cradules added the kind/bug Categorizes issue or PR as related to a bug. label Sep 1, 2021
@roycaihw
Copy link
Member

/assign

@roycaihw
Copy link
Member

This sounds like another occurrence of kubernetes-client/gen#52. The upstream openapi spec marks the conditions as "required" but the apiserver doesn't always set the value when sending a response.

@cradules Would you mind sending a PR to fix the openapi spec in the upstream? An example is kubernetes/kubernetes#64996. This Python client can be fixed when we re-generate the client against the fixed openapi spec.

kube = client.AutoscalingV2beta1Api()

On a side note, it's weird to use an v1 V1HorizontalPodAutoscaler object with an v1beta1 API.

@cradules
Copy link
Author

cradules commented Oct 3, 2021

Thank you @roycaihw . You were perfectly right. I was using the wrong API version. I have fixed the issue by using the right API, more exactly v1

kube = client.AutoscalingV1Api()

@cradules cradules closed this as completed Oct 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug.
Projects
None yet
Development

No branches or pull requests

2 participants