Skip to content

Commit aa9e896

Browse files
nwntaudrastump
andauthored
added fleet kubeconfig retriever and tests (#441)
* added fleet kubeconfig retriever and tests * refactored utility function * refactored * Delete common-npm-packages/azure-arm-rest/Tests/azure-arm-aks-service-tests.js * added tests * added additional params for tests * refactored tests * fixed nits * Version bump --------- Co-authored-by: audrastump <[email protected]> Co-authored-by: Audra Stump <[email protected]>
1 parent 239423f commit aa9e896

File tree

5 files changed

+102
-29
lines changed

5 files changed

+102
-29
lines changed

common-npm-packages/azure-arm-rest/Tests/azure-arm-aks-service-tests.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,25 @@ export class AksServiceTests {
4343
tl.setResult(tl.TaskResult.Failed, 'AksServiceTests.credentialsByCustomClusterUser() should have passed but failed');
4444
}
4545
}
46+
47+
public static async credentialsFleetUser() {
48+
let aksService: AzureAksService = new AzureAksService(endpoint);
49+
try {
50+
let result = await aksService.getFleetCredential("MOCK_RESOURCE_GROUP_NAME", "MOCK_FLEET");
51+
console.log(`Fleet Credential Found: ${result.name}`);
52+
}
53+
catch(error) {
54+
console.log(error);
55+
tl.setResult(tl.TaskResult.Failed, 'AksServiceTests.credentialsByCustomFleetUser() should have passed but failed');
56+
}
57+
}
4658
}
4759

4860
async function RUNTESTS() {
4961
await AksServiceTests.credentialsByClusterAdmin();
5062
await AksServiceTests.credentialsByClusterUser();
5163
await AksServiceTests.credentialsByCustomClusterUser();
64+
await AksServiceTests.credentialsFleetUser()
5265
}
5366

5467
RUNTESTS();

common-npm-packages/azure-arm-rest/Tests/mock_utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,4 +751,18 @@ export function mockAzureAksServiceTests() {
751751
}]
752752
}).persist();
753753

754+
nock('https://management.azure.com', {
755+
reqheaders: {
756+
"authorization": "Bearer DUMMY_ACCESS_TOKEN",
757+
"content-type": "application/json; charset=utf-8"
758+
}
759+
}).post("/subscriptions/MOCK_SUBSCRIPTION_ID/resourceGroups/MOCK_RESOURCE_GROUP_NAME/providers/Microsoft.ContainerService/fleets/MOCK_FLEET/listCredentials?api-version=2024-04-01")
760+
.reply(200, {
761+
kubeconfigs: [{
762+
name: "clusterAdmin",
763+
value: "base46kubeconfig"
764+
}]
765+
}).persist();
766+
767+
754768
}

common-npm-packages/azure-arm-rest/aksUtility.ts

100644100755
Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,39 @@ import { AzureRMEndpoint } from './azure-arm-endpoint';
44
import { AzureEndpoint, AKSClusterAccessProfile, AKSCredentialResult} from './azureModels';
55

66

7-
async function getKubeConfigFromAKS(azureSubscriptionEndpoint: string, resourceGroup: string, clusterName: string, useClusterAdmin?: boolean) : Promise<string> {
7+
8+
async function getKubeConfigFromAKS(azureSubscriptionEndpoint: string, resourceGroup: string, name: string, isFleet: boolean, useClusterAdmin?: boolean): Promise<string> {
89
const azureEndpoint: AzureEndpoint = await (new AzureRMEndpoint(azureSubscriptionEndpoint)).getEndpoint();
910
const aks = new AzureAksService(azureEndpoint);
10-
const USE_AKS_CREDENTIAL_API = tl.getBoolFeatureFlag('USE_AKS_CREDENTIAL_API');
11-
tl.debug(tl.loc("KubernetesClusterResourceGroup", clusterName, resourceGroup));
11+
tl.debug(tl.loc("KubernetesClusterResourceGroup", name, resourceGroup));
1212
let base64Kubeconfig;
13+
14+
const USE_AKS_CREDENTIAL_API = tl.getBoolFeatureFlag('USE_AKS_CREDENTIAL_API');
1315
if (USE_AKS_CREDENTIAL_API) {
14-
let clusterInfo : AKSCredentialResult = await aks.getClusterCredential(resourceGroup, clusterName, useClusterAdmin);
16+
let clusterInfo: AKSCredentialResult = await aks.getClusterCredential(resourceGroup, name, useClusterAdmin);
1517
base64Kubeconfig = Buffer.from(clusterInfo.value, 'base64');
1618
} else {
17-
let clusterInfo : AKSClusterAccessProfile = await aks.getAccessProfile(resourceGroup, clusterName, useClusterAdmin);
19+
let clusterInfo: AKSClusterAccessProfile = await aks.getAccessProfile(resourceGroup, name, useClusterAdmin);
1820
base64Kubeconfig = Buffer.from(clusterInfo.properties.kubeConfig, 'base64');
1921
}
22+
2023
return base64Kubeconfig.toString();
2124
}
2225

26+
export async function getKubeConfigFromFleet(azureSubscriptionEndpoint, resourceGroup, fleetName): Promise<string> {
27+
tl.debug(tl.loc("KubernetesClusterResourceGroup", name, resourceGroup));
28+
const azureEndpoint: AzureEndpoint = await (new AzureRMEndpoint(azureSubscriptionEndpoint)).getEndpoint();
29+
const aks = new AzureAksService(azureEndpoint);
30+
let clusterInfo: AKSCredentialResult = await aks.getFleetCredential(resourceGroup, fleetName);
31+
let base64Kubeconfig = Buffer.from(clusterInfo.value, 'base64');
32+
return base64Kubeconfig.toString();
33+
}
34+
35+
2336
export async function getKubeConfig(azureSubscriptionEndpoint, resourceGroup, clusterName, useClusterAdmin): Promise<string> {
24-
return getKubeConfigFromAKS(azureSubscriptionEndpoint, resourceGroup, clusterName, useClusterAdmin);
37+
return getKubeConfigFromAKS(azureSubscriptionEndpoint, resourceGroup, clusterName, false, useClusterAdmin);
38+
}
39+
40+
export async function getKubeConfigForFleet(azureSubscriptionEndpoint, resourceGroup, fleetName): Promise<string> {
41+
return getKubeConfigFromFleet(azureSubscriptionEndpoint, resourceGroup, fleetName);
2542
}

common-npm-packages/azure-arm-rest/azure-arm-aks-service.ts

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class AzureAksService {
3939
throw ToError(response);
4040
}
4141
});
42-
}
42+
}
4343

4444
public getAccessProfile(resourceGroup : string , clusterName : string, useClusterAdmin?: boolean): Promise<Model.AKSClusterAccessProfile> {
4545
var accessProfileName = !!useClusterAdmin ? 'clusterAdmin' : 'clusterUser';
@@ -54,30 +54,59 @@ export class AzureAksService {
5454
throw Error(tl.loc('CantDownloadAccessProfile',clusterName, this._client.getFormattedError(reason)));
5555
});
5656
}
57+
private createFleetParameters(resourceGroup: string, name: string): { uri: string, parameters: any, apiVersion: string } {
58+
const uri = `//subscriptions/{subscriptionId}/resourceGroups/{ResourceGroupName}/providers/Microsoft.ContainerService/fleets/{FleetName}/listCredentials`;
59+
const parameters = {
60+
'{ResourceGroupName}': resourceGroup,
61+
'{FleetName}': name,
62+
};
63+
const apiVersion = '2024-04-01';
64+
return { uri, parameters, apiVersion };
65+
}
5766

58-
public getClusterCredentials(resourceGroup : string , clusterName : string, useClusterAdmin?: boolean): Promise<Model.AKSCredentialResults> {
59-
var credentialAction = !!useClusterAdmin ? 'listClusterAdminCredential' : 'listClusterUserCredential';
60-
return this.beginRequest(`//subscriptions/{subscriptionId}/resourceGroups/{ResourceGroupName}/providers/Microsoft.ContainerService/managedClusters/{ClusterName}/{CredentialAction}`,
61-
{
67+
private createManagedClusterParameters(resourceGroup: string, name: string, useClusterAdmin?: boolean): { uri: string, parameters: any, apiVersion: string } {
68+
const credentialAction = !!useClusterAdmin ? 'listClusterAdminCredential' : 'listClusterUserCredential';
69+
const uri = `//subscriptions/{subscriptionId}/resourceGroups/{ResourceGroupName}/providers/Microsoft.ContainerService/managedClusters/{ClusterName}/{CredentialAction}`;
70+
const parameters = {
6271
'{ResourceGroupName}': resourceGroup,
63-
'{ClusterName}': clusterName,
64-
'{CredentialAction}': credentialAction
65-
}, '2024-05-01', "POST").then((response) => {
66-
return response.body;
72+
'{ClusterName}': name,
73+
'{CredentialAction}': credentialAction,
74+
};
75+
const apiVersion = '2024-05-01';
76+
return { uri, parameters, apiVersion };
77+
}
78+
79+
public getCredentials(resourceGroup: string, name: string, uri: string, parameters: any, apiVersion: string): Promise<Model.AKSCredentialResults> {
80+
return this.beginRequest(uri, parameters, apiVersion, "POST").then((response) => {
81+
return response.body;
6782
}, (reason) => {
68-
throw Error(tl.loc('CantDownloadClusterCredentials', clusterName, this._client.getFormattedError(reason)));
83+
throw Error(tl.loc('CantDownloadClusterCredentials', name, this._client.getFormattedError(reason)));
6984
});
70-
}
85+
}
86+
87+
public getClusterCredential(resourceGroup: string, name: string, useClusterAdmin?: boolean, credentialName?: string): Promise<Model.AKSCredentialResult> {
88+
const { uri, parameters, apiVersion } = this.createManagedClusterParameters(resourceGroup, name, useClusterAdmin);
89+
const credentialsPromise = this.getCredentials(resourceGroup, name, uri, parameters, apiVersion);
90+
return credentialsPromise.then((credentials) => {
91+
const credential = credentials.kubeconfigs.find(cred => cred.name === (credentialName || (!!useClusterAdmin ? 'clusterAdmin' : 'clusterUser')));
92+
if (credential === undefined) {
93+
throw Error(tl.loc('CantDownloadClusterCredentials', name, `${credentialName || 'default'} not found in the list of credentials.`));
94+
}
95+
return credential;
96+
});
97+
}
98+
99+
public getFleetCredential(resourceGroup: string, name: string): Promise<Model.AKSCredentialResult> {
100+
101+
const { uri, parameters, apiVersion } = this.createFleetParameters(resourceGroup, name);
102+
const credentialsPromise = this.getCredentials(resourceGroup, name, uri, parameters, apiVersion);
103+
return credentialsPromise.then((credentials) => {
104+
const credential = credentials.kubeconfigs[0];
105+
if (credential === undefined) {
106+
throw Error(tl.loc('CantDownloadClusterCredentials'));
107+
}
108+
return credential;
109+
});
110+
}
71111

72-
public getClusterCredential(resourceGroup : string , clusterName : string, useClusterAdmin?: boolean, credentialName?: string): Promise<Model.AKSCredentialResult> {
73-
var credentialName = !!credentialName ? credentialName : !!useClusterAdmin ? 'clusterAdmin' : 'clusterUser';
74-
var clusterCredentials = this.getClusterCredentials(resourceGroup, clusterName, useClusterAdmin)
75-
return clusterCredentials.then((credentials) => {
76-
var credential = credentials.kubeconfigs.find(credential => credential.name == credentialName)
77-
if (credential === undefined) {
78-
throw Error(tl.loc('CantDownloadClusterCredentials', clusterName, `${credentialName} not found in the list of cluster credentials.`));
79-
}
80-
return credential;
81-
})
82-
}
83112
}

common-npm-packages/azure-arm-rest/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "azure-pipelines-tasks-azure-arm-rest",
3-
"version": "3.254.0",
3+
"version": "3.254.2",
44
"description": "Common Lib for Azure ARM REST apis",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)