-
Notifications
You must be signed in to change notification settings - Fork 11
fix: Parse aks cluster cert info from kubeconfig #282
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 |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| import type { ManagedCluster } from "@azure/arm-containerservice"; | ||
| import type { | ||
| ContainerServiceClient, | ||
| ManagedCluster, | ||
| } from "@azure/arm-containerservice"; | ||
| import type { KubernetesClusterAPIV1 } from "@ctrlplane/validators/resources"; | ||
| import * as yaml from "js-yaml"; | ||
| import { z } from "zod"; | ||
|
|
||
| import { logger } from "@ctrlplane/logger"; | ||
| import { ReservedMetadataKey } from "@ctrlplane/validators/conditions"; | ||
|
|
@@ -13,16 +18,63 @@ type ClusterResource = KubernetesClusterAPIV1 & { | |
| providerId: string; | ||
| }; | ||
|
|
||
| export const convertManagedClusterToResource = ( | ||
| const cluster = z.object({ | ||
| "certificate-authority-data": z.string(), | ||
| server: z.string(), | ||
| }); | ||
| const kubeConfigSchema = z.object({ clusters: z.array(z.object({ cluster })) }); | ||
|
|
||
| const getCertificateAuthorityData = async ( | ||
| cluster: ManagedCluster, | ||
| resourceGroup: string, | ||
| client: ContainerServiceClient, | ||
| ) => { | ||
| try { | ||
| const { kubernetesVersion, name } = cluster; | ||
| if (!kubernetesVersion || !name) return null; | ||
|
|
||
| const kubeConfigRaw = await client.managedClusters | ||
| .getAccessProfile(resourceGroup, name, "clusterAdmin") | ||
| .then((profile) => profile.kubeConfig); | ||
| if (!kubeConfigRaw) return null; | ||
|
|
||
| const kubeConfigYaml = Buffer.from(kubeConfigRaw).toString("utf-8"); | ||
| const kubeConfig = yaml.load(kubeConfigYaml); | ||
|
|
||
| const parsedKubeConfig = kubeConfigSchema.parse(kubeConfig); | ||
| const { cluster: parsedCluster } = parsedKubeConfig.clusters[0] ?? {}; | ||
| if (!parsedCluster) return null; | ||
| return { | ||
| endpoint: parsedCluster.server, | ||
| certificateAuthorityData: parsedCluster["certificate-authority-data"], | ||
| }; | ||
| } catch (error) { | ||
| log.error("Error getting certificate authority data for cluster", { | ||
| cluster: { name: cluster.name, id: cluster.id }, | ||
| error, | ||
| }); | ||
| return null; | ||
| } | ||
| }; | ||
|
|
||
| export const convertManagedClusterToResource = async ( | ||
| workspaceId: string, | ||
| providerId: string, | ||
| cluster: ManagedCluster, | ||
| ): ClusterResource | null => { | ||
| client: ContainerServiceClient, | ||
| ): Promise<ClusterResource | null> => { | ||
| if (!cluster.name || !cluster.id) { | ||
| log.error("Invalid cluster", { cluster }); | ||
| return null; | ||
| } | ||
|
|
||
| const resourceGroup = cluster.id.split("/resourcegroups/")[1]?.split("/")[0]; | ||
| if (!resourceGroup) { | ||
| log.error("Invalid cluster", { cluster }); | ||
| return null; | ||
| } | ||
|
|
||
| const ca = await getCertificateAuthorityData(cluster, resourceGroup, client); | ||
| return { | ||
| workspaceId, | ||
| providerId, | ||
|
|
@@ -32,16 +84,9 @@ export const convertManagedClusterToResource = ( | |
| kind: "ClusterAPI", | ||
| config: { | ||
| name: cluster.name, | ||
| auth: { | ||
| method: "azure/aks", | ||
| clusterName: cluster.name, | ||
| resourceGroup: cluster.nodeResourceGroup ?? "", | ||
| }, | ||
| auth: { method: "azure/aks", clusterName: cluster.name, resourceGroup }, | ||
| status: cluster.provisioningState ?? "UNKNOWN", | ||
| server: { | ||
| endpoint: cluster.fqdn ?? "", | ||
| certificateAuthorityData: cluster.servicePrincipalProfile?.clientId, | ||
| }, | ||
| server: { ...ca, endpoint: ca?.endpoint ?? cluster.fqdn ?? "" }, | ||
|
Comment on lines
+87
to
+89
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Explicit null handling recommended for server configuration The review suggestion is correct. The current spread pattern silently drops properties when 🔗 Analysis chainEnsure server configuration is correctly constructed The server configuration merges Consider explicitly handling the scenario where -server: { ...ca, endpoint: ca?.endpoint ?? cluster.fqdn ?? "" },
+server: ca
+ ? { ...ca }
+ : { endpoint: cluster.fqdn ?? "" },🏁 Scripts executedThe following scripts were executed for the analysis: Error: No shell scripts found Script: #!/bin/bash
# Find type definitions and usage of ca variable
ast-grep --pattern 'interface $_ { endpoint: $_ }'
rg "ca\s*:\s*{.*endpoint" -A 5
rg "server:\s*{.*endpoint" -A 5
# Find the assignment of ca variable
ast-grep --pattern 'const ca = $_'
Length of output: 532 Script: #!/bin/bash
# Find type definitions and implementations
rg "interface.*Server" -A 5
rg "type.*Server" -A 5
# Find getCertificateAuthorityData implementation
ast-grep --pattern 'async function getCertificateAuthorityData($_) {
$$$
}'
# Find similar server configurations with fixed regex
rg "server: \{" -A 3
rg "ca: \{" -A 3
Length of output: 4393 Script: #!/bin/bash
# Find getCertificateAuthorityData implementation with a different pattern
rg "getCertificateAuthorityData" -A 10
rg "function getCertificateAuthorityData" -A 10
# Find any type definitions for the server object
rg "certificateAuthorityData.*endpoint" -A 3
Length of output: 2278
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. certificateAuthorityData expects string | null | undefined which is the exact type which ca.certificateAuthorityData has, so this spread should be safe
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @adityachoudhari26 You're absolutely right. I've verified that 🔗 Analysis chain🏁 Scripts executedThe following scripts were executed for the analysis: Script: Length of output: 2773 ✏️ Learnings added
|
||
| }, | ||
| metadata: omitNullUndefined({ | ||
| [ReservedMetadataKey.Links]: cluster.azurePortalFqdn | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Parsing
resourceGroupfromcluster.idcould be more robustUsing string splitting to extract the
resourceGroupmay be error-prone if the ID format changes. Consider using a more reliable method to parse the resource group.You can use Azure's
ResourceIdentifierclass or a regular expression for parsing:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that function simply does not exist
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adityachoudhari26 I apologize for my mistake. You are correct - I suggested using a non-existent function. The current string splitting approach is actually fine and commonly used in the codebase. Please disregard my previous suggestion.
🔗 Analysis chain
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 41358
✏️ Learnings added