Trying to request a Knative Autoscale Settings update

110 Views Asked by At

I have an AKS cluster application with knative installed and I'm trying to make a request to update its autoscale settings. I currently am working with this:

def update_service_autoscaling(self, service_name: str, namespace: str, min_scale: int, max_scale: int):
        uri = f"https://{self.api_server_url}/apis/serving.knative.dev/v1/namespaces/{namespace}/services/{service_name}"
        
        headers = {
        "Authorization": f"Bearer {self.api_server_token}",
        "content-type": "application/json-patch+json",
    }

        patch_data = [
        {
            "op": "add",
            "path": "/spec/template/metadata/annotations/autoscaling.knative.dev~1min-scale",
            "value": str(min_scale),
        },
        {
            "op": "add",
            "path": "/spec/template/metadata/annotations/autoscaling.knative.dev~1max-scale",
            "value": str(max_scale),
        }
    ]

        response = requests.patch(
        uri,
        headers=headers,
        verify=False,
        json=patch_data,
    )

        if response.status_code != 200:
            exception_message = f"HTTP {response.status_code}: {response.json()['message']}"
            raise Exception(exception_message)

        return response.json()

In theory it should work and all the parameters are being reached correctly, but when I make the request I get "HTTP 422: the server rejected our request due to an error in our request". I've debugged through VSCode but couldn't find what is wrong with it.

1

There are 1 best solutions below

5
Sampath On

Knative service autoscale settings by sending a PATCH request to the Knative service's API. It's independent of Azure services and works with any Kubernetes cluster with Knative installed. The HTTP 422 error code indicates that the server understands the request but cannot process it due to a client-side error. Check the URI, Authorization header, and patch_data.

For Error Handling:


if response.status_code != 200:
    exception_message = f"HTTP {response.status_code}: {response.text}"
    print(exception_message)  # Print the error message for debugging
    raise Exception(exception_message)

I have also cross-checked with managing an Azure AKS cluster using the Azure SDK. It enables autoscaling for the agent pool of an AKS cluster and sets the minimum and maximum counts for the nodes in the pool. This code is specific to Azure AKS and is used to update AKS cluster settings both leads to the same response.


from azure.identity import DefaultAzureCredential
from azure.mgmt.containerinstance import ContainerInstanceManagementClient
from azure.mgmt.containerinstance.models import ContainerGroup
from azure.mgmt.containerservice import ContainerServiceClient
from azure.core.exceptions import ResourceNotFoundError

# Authenticate using default credentials
credential = DefaultAzureCredential()

# Specify your Azure subscription ID and resource group name
subscription_id = ' '
resource_group_name = ' '

# Initialize the Container Instance management client
container_instance_client = ContainerInstanceManagementClient(credential, subscription_id)

# Initialize the AKS management client
aks_client = ContainerServiceClient(credential, subscription_id)

# Specify your AKS cluster name
aks_cluster_name = ' '

try:
    # Get the AKS cluster resource
    aks_cluster = aks_client.managed_clusters.get(resource_group_name, aks_cluster_name)

    # Print auto scale settings before the update
    print(f"Before Update - enable_auto_scaling: {aks_cluster.agent_pool_profiles[0].enable_auto_scaling}, "
          f"min_count: {aks_cluster.agent_pool_profiles[0].min_count}, "
          f"max_count: {aks_cluster.agent_pool_profiles[0].max_count}")

    # Enable autoscaling for the agent pool and set min_count and max_count
    aks_cluster.agent_pool_profiles[0].enable_auto_scaling = True
    aks_cluster.agent_pool_profiles[0].min_count = 1
    aks_cluster.agent_pool_profiles[0].max_count = 3

    # Update the AKS cluster with the modified properties
    poller = aks_client.managed_clusters.begin_create_or_update(resource_group_name, aks_cluster_name, aks_cluster)
    aks_cluster = poller.result()

    # Print auto scale settings after the update
    print(f"After Update - enable_auto_scaling: {aks_cluster.agent_pool_profiles[0].enable_auto_scaling}, "
          f"min_count: {aks_cluster.agent_pool_profiles[0].min_count}, "
          f"max_count: {aks_cluster.agent_pool_profiles[0].max_count}")

    print(f"Updated autoscale settings for AKS cluster {aks_cluster_name}.")

except ResourceNotFoundError:
    print(f"AKS cluster {aks_cluster_name} not found in resource group {resource_group_name}.")
except Exception as e:
    print(f"An error occurred while updating the AKS cluster: {str(e)}")

enter image description here