-
Notifications
You must be signed in to change notification settings - Fork 19
aksd: fix: return full cluster list for AKS cluster registration #296
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1622,72 +1622,117 @@ export async function getClusterResourceGroupViaGraph( | |||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Fetches a single page of AKS clusters from Azure Resource Graph. | ||||||||||
| * | ||||||||||
| * The Resource Graph query returns at most 1000 results per call with --first 1000. (Maximum) | ||||||||||
| * If more results exist, the raw response includes a `skip_token` cursor that can | ||||||||||
| * be used to fetch the next page of results. This function returns both the clusters and | ||||||||||
| * a `skipToken` (mapped from the raw `skip_token` field) when pagination is required to | ||||||||||
| * fetch all clusters in larger subscriptions. | ||||||||||
| * | ||||||||||
| * @param query - Azure Resource Graph query to execute. | ||||||||||
| * @param skipToken - Pagination token from a previous call to fetch the next page. | ||||||||||
| * @returns The cluster records and an optional `skipToken` for the next page. | ||||||||||
| */ | ||||||||||
| async function fetchGraphPage( | ||||||||||
| query: string, | ||||||||||
| skipToken?: string | ||||||||||
| ): Promise<{ clusters: any[]; skipToken?: string }> { | ||||||||||
| const pageSize = '1000'; | ||||||||||
| const args = ['graph', 'query', '-q', query, '--first', pageSize, '--output', 'json']; | ||||||||||
| // Append skip token for pagination if provided | ||||||||||
| if (skipToken) { | ||||||||||
| args.push('--skip-token', skipToken); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const { stdout, stderr } = await runCommandAsync('az', args); | ||||||||||
|
|
||||||||||
| if (stderr && needsRelogin(stderr)) { | ||||||||||
| throw new Error('Authentication required. Please log in to Azure CLI: az login'); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (stderr && stderr.toLowerCase().includes('error')) { | ||||||||||
| throw new Error(`Resource Graph query failed: ${stderr}`); | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
tejhan marked this conversation as resolved.
|
||||||||||
| try { | ||||||||||
| const result = JSON.parse(stdout); | ||||||||||
| const clusters = result.data || []; | ||||||||||
|
|
||||||||||
| return { clusters, skipToken: result.skip_token }; | ||||||||||
| } catch (parseError: unknown) { | ||||||||||
| const parseErrorMessage = parseError instanceof Error ? parseError.message : String(parseError); | ||||||||||
| const stdoutPreview = stdout.length > 500 ? stdout.slice(0, 500) + '…' : stdout; | ||||||||||
| throw new Error( | ||||||||||
| `Failed to parse Resource Graph query response: ${parseErrorMessage}. ` + | ||||||||||
| `Stdout length=${stdout.length}, preview=${JSON.stringify(stdoutPreview)}` | ||||||||||
| ); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Get clusters using Azure Resource Graph | ||||||||||
| export async function getClustersViaGraph( | ||||||||||
| subscriptionId: string, | ||||||||||
| filterAad: boolean = false | ||||||||||
| ): Promise<any[]> { | ||||||||||
| try { | ||||||||||
| if (!isValidGuid(subscriptionId)) { | ||||||||||
| throw new Error('Invalid subscription ID format'); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const aadFilter = filterAad ? '| where isnotnull(properties.aadProfile)' : ''; | ||||||||||
|
|
||||||||||
| const query = ` | ||||||||||
| Resources | ||||||||||
| | where type =~ 'microsoft.containerservice/managedclusters' | ||||||||||
| | where subscriptionId == '${subscriptionId}' | ||||||||||
| ${aadFilter} | ||||||||||
| | extend nodeCount = array_length(properties.agentPoolProfiles) | ||||||||||
| | project | ||||||||||
| if (!isValidGuid(subscriptionId)) { | ||||||||||
| throw new Error('Invalid subscription ID format'); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const aadFilter = filterAad ? '| where isnotnull(properties.aadProfile)' : ''; | ||||||||||
|
|
||||||||||
| const query = ` | ||||||||||
| Resources | ||||||||||
| | where type =~ 'microsoft.containerservice/managedclusters' | ||||||||||
|
illume marked this conversation as resolved.
|
||||||||||
| | where subscriptionId == '${subscriptionId}' | ||||||||||
| ${aadFilter} | ||||||||||
| | extend agentPools = properties.agentPoolProfiles | ||||||||||
| | mv-expand agentPools | ||||||||||
| | extend poolNodeCount = toint(agentPools['count']) | ||||||||||
|
Comment on lines
+1691
to
+1692
|
||||||||||
| | mv-expand agentPools | |
| | extend poolNodeCount = toint(agentPools['count']) | |
| | mv-expand kind=outer agentPools | |
| | extend poolNodeCount = toint(coalesce(agentPools['count'], 0)) |
Uh oh!
There was an error while loading. Please reload this page.