AKS Preview API lifecycle – Script to check if deprecated API versions are being used

As an Azure Kubernetes Service (AKS) user, you may have recently received emails about upcoming Preview API retirements. In this post, I’ll share a useful script I created to check which API versions are currently in use.

According to Microsoft documentation, AKS preview APIs have a lifespan of approximately one year. This policy applies to APIs ending in -preview. For instance, you can expect the 2023-01-02-preview API to be deprecated around January 1st, 2024.

Checking API Version Usage

As documented, there is some az cli to assist –

API_VERSION=<impacted API version, such as 2022-04-01-preview>
az monitor activity-log list --offset 30d --max-events 10000 --namespace microsoft.containerservice --query "[?eventName.value == 'EndRequest' && contains(not_null(httpRequest.uri,''), '${API_VERSION}')]"

But that is fine for just one API_VERSION, I wanted to ensure to not be using any upcoming deprecated versions as listed

The Script

#!/bin/bash

# Output file
output_file="aks_api_usage_report.txt"

# Array of API versions to check
api_versions=(
    "2022-07-02-preview"
    "2022-08-02-preview"
    "2022-08-03-preview"
    "2022-09-02-preview"
    "2022-10-02-preview"
    "2022-11-02-preview"
    "2023-01-02-preview"
    "2023-02-02-preview"
    "2023-03-02-preview"
    "2023-04-02-preview"
    "2023-05-02-preview"
    "2023-06-02-preview"
    "2023-07-02-preview"
    "2023-08-02-preview"
)

# Function to check API usage
check_api_usage() {
    local api_version=$1
    echo "Checking usage for API version: $api_version" >> "$output_file"
    az monitor activity-log list --offset 30d --max-events 10000 --namespace microsoft.containerservice --query "[?eventName.value == 'EndRequest' && contains(not_null(httpRequest.uri,''), '${api_version}')]" >> "$output_file"
}

# Clear the output file if it exists
> "$output_file"

# Loop through each API version and check usage
for version in "${api_versions[@]}"; do
    check_api_usage "$version"
    echo "----------------------------------------" >> "$output_file"
done

echo "Report generated in $output_file"

What does the above script do?

  • Creates an output file named “aks_api_usage_report.txt
  • Defines an array of AKS API versions to check, ranging from 2022-07-02-preview to 2023-08-02-preview (Taken from this list provided by learn.microsoft.com)
  • Defines a function check_api_usage that:
    • Takes an API version as an argument
    • Logs the API version being checked to the output file
    • Uses Azure CLI to query the activity log for the specified API version
    • Filters for ‘EndRequest‘ events in the microsoft.containerservice namespace
    • Limits the search to the last 30 days and up to 10,000 events
    • Appends the results to the output file
  • Clears the output file if it already exists
  • Loops through each API version in the array:
    • Calls the check_api_usage function for each version
    • Adds a separator line to the output file after each API version check
  • Prints a message indicating that the report has been generated and specifies the output file name

Snippet from output example of aks_api_usage_report.txt:

Checking usage for API version: 2022-07-02-preview
[]
----------------------------------------
Checking usage for API version: 2022-08-02-preview
[]
----------------------------------------
Checking usage for API version: 2022-08-03-preview
[]
----------------------------------------
Checking usage for API version: 2022-09-02-preview
[
  {
    "authorization": {
      "action": "Microsoft.ContainerService/managedClusters/accessProfiles/listCredential/action",
      "scope": "/subscriptions/XXXXXXXXXX/resourceGroups/XXXXXXXXXX/providers/Microsoft.ContainerService/managedClusters/XXXXXXXXXX/accessProfiles/clusterAdmin"
    },

This output allows you to quickly identify which accounts are using older or soon-to-be deprecated API versions.

By running this script, you can proactively manage your AKS API usage and plan for necessary updates before deprecations occur. Feel free to adapt the script to your needs and stay ahead of API retirements in your Azure environment.

Repository containing script above

2 thoughts on “AKS Preview API lifecycle – Script to check if deprecated API versions are being used”

  1. Extreme timesaver! Found out today that you can view Resource -> Diagnose and Solve Problem -> Deprecated preview API versions — and this will tell you exactly what it is that’s using these deprecated apis (i.e a terraform provider). Maybe useful to note too

    Reply

Leave a Reply to ReesCancel reply

Discover more from Thomas Thornton Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from Thomas Thornton Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading