Add a descriptive description at the changeset and add some unit test for three different scenarios

Signed-off-by: armandocomellas1 <cgarmando@google.com>
This commit is contained in:
armandocomellas1
2024-01-15 19:27:09 -06:00
parent 7278d80169
commit 5d06333af2
3 changed files with 109 additions and 13 deletions
@@ -382,5 +382,108 @@ describe('GkeClusterLocator', () => {
parent: 'projects/some-project/locations/some-region',
});
});
it('return google login when no authProvider is specified', async () => {
mockedListClusters.mockReturnValueOnce([
{
clusters: [
{
name: 'some-cluster',
endpoint: '1.2.3.4',
},
],
},
]);
const config: Config = new ConfigReader({
type: 'gke',
projectId: 'some-project',
});
const sut = GkeClusterLocator.fromConfigWithClient(config, {
listClusters: mockedListClusters,
} as any);
const result = await sut.getClusters();
expect(result).toStrictEqual([
{
name: 'some-cluster',
url: 'https://1.2.3.4',
authMetadata: { [ANNOTATION_KUBERNETES_AUTH_PROVIDER]: 'google' },
skipTLSVerify: false,
skipMetricsLookup: false,
},
]);
});
it('return googleServiceAccount login when authProvider is specified', async () => {
mockedListClusters.mockReturnValueOnce([
{
clusters: [
{
name: 'some-cluster',
endpoint: '1.2.3.4',
},
],
},
]);
const config: Config = new ConfigReader({
type: 'gke',
projectId: 'some-project',
authProvider: 'googleServiceAccount',
});
const sut = GkeClusterLocator.fromConfigWithClient(config, {
listClusters: mockedListClusters,
} as any);
const result = await sut.getClusters();
expect(result).toStrictEqual([
{
name: 'some-cluster',
url: 'https://1.2.3.4',
authMetadata: {
[ANNOTATION_KUBERNETES_AUTH_PROVIDER]: 'googleServiceAccount',
},
skipTLSVerify: false,
skipMetricsLookup: false,
},
]);
});
it('return google login when authProvider property has invalid value', async () => {
mockedListClusters.mockReturnValueOnce([
{
clusters: [
{
name: 'some-cluster',
endpoint: '1.2.3.4',
},
],
},
]);
const config: Config = new ConfigReader({
type: 'gke',
projectId: 'some-project',
authProvider: 'differentValue',
});
const sut = GkeClusterLocator.fromConfigWithClient(config, {
listClusters: mockedListClusters,
} as any);
const result = await sut.getClusters();
expect(result).toStrictEqual([
{
name: 'some-cluster',
url: 'https://1.2.3.4',
authMetadata: { [ANNOTATION_KUBERNETES_AUTH_PROVIDER]: 'google' },
skipTLSVerify: false,
skipMetricsLookup: false,
},
]);
});
});
});
@@ -55,18 +55,11 @@ export class GkeClusterLocator implements KubernetesClustersSupplier {
return { key: mrl.getString('key'), value: mrl.getString('value') };
}) ?? [];
let storeAuthProviderString: string;
let getGkeProperty;
try {
getGkeProperty = config.getString('authProvider');
} catch (err) {
getGkeProperty = 'google';
}
if (getGkeProperty === 'googleServiceAccount') {
storeAuthProviderString = 'googleServiceAccount';
} else {
storeAuthProviderString = 'google';
}
const storeAuthProviderString =
config.getOptionalString('authProvider') === 'googleServiceAccount'
? 'googleServiceAccount'
: 'google';
const options = {
projectId: config.getString('projectId'),
authProvider: storeAuthProviderString,