Skip to content

Commit

Permalink
CAPA v1beta2 API version support (#4625)
Browse files Browse the repository at this point in the history
* Generate capav1beta2 types and functions

* Use capav1beta2 types and functions
  • Loading branch information
gusevda authored Sep 30, 2024
1 parent b0de361 commit a60e845
Show file tree
Hide file tree
Showing 37 changed files with 4,967 additions and 1,999 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"storybook": "storybook dev -p 6006",
"storybook:build": "storybook build",
"postinstall": "husky install",
"generate-mapi": "./node_modules/.bin/ts-node-esm scripts/generate/index.ts && yarn format:staged \"src/model/services/mapi/**/*.{js,ts,tsx}\" --loglevel error"
"generate-mapi": "npx ts-node scripts/generate/index.ts && yarn format:staged \"src/model/services/mapi/**/*.{js,ts,tsx}\" --loglevel error"
},
"eslintIgnore": [
"node_modules",
Expand Down
19 changes: 5 additions & 14 deletions scripts/generate/mapi-resources.yaml
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
- apiVersionAlias: capav1beta1
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
- apiVersionAlias: capav1beta2
apiVersion: infrastructure.cluster.x-k8s.io/v1beta2
resources:
- name: AWSCluster
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
apiVersion: infrastructure.cluster.x-k8s.io/v1beta2
crdURL: https://raw.githubusercontent.com/kubernetes-sigs/cluster-api-provider-aws/main/config/crd/bases/infrastructure.cluster.x-k8s.io_awsclusters.yaml
verbs:
- get
- name: AWSClusterRoleIdentity
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
crdURL: https://raw.githubusercontent.com/kubernetes-sigs/cluster-api-provider-aws/main/config/crd/bases/infrastructure.cluster.x-k8s.io_awsclusterroleidentities.yaml
verbs:
- get
- name: AWSMachinePool
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
apiVersion: infrastructure.cluster.x-k8s.io/v1beta2
crdURL: https://raw.githubusercontent.com/kubernetes-sigs/cluster-api-provider-aws/main/config/crd/bases/infrastructure.cluster.x-k8s.io_awsmachinepools.yaml
verbs:
- get
- list
- name: AWSMachineTemplate
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
apiVersion: infrastructure.cluster.x-k8s.io/v1beta2
crdURL: https://raw.githubusercontent.com/kubernetes-sigs/cluster-api-provider-aws/main/config/crd/bases/infrastructure.cluster.x-k8s.io_awsmachinetemplates.yaml
verbs:
- get
- list

- apiVersionAlias: capav1beta2
apiVersion: infrastructure.cluster.x-k8s.io/v1beta2
resources:
- name: AWSManagedCluster
apiVersion: infrastructure.cluster.x-k8s.io/v1beta2
crdURL: https://raw.githubusercontent.com/kubernetes-sigs/cluster-api-provider-aws/main/config/crd/bases/infrastructure.cluster.x-k8s.io_awsmanagedclusters.yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
} from 'MAPI/types';
import { extractErrorMessage } from 'MAPI/utils';
import { GenericResponseError } from 'model/clients/GenericResponseError';
import * as capav1beta1 from 'model/services/mapi/capav1beta1';
import * as capav1beta2 from 'model/services/mapi/capav1beta2';
import * as capgv1beta1 from 'model/services/mapi/capgv1beta1';
import * as capzv1beta1 from 'model/services/mapi/capzv1beta1';
Expand Down Expand Up @@ -117,11 +116,11 @@ const ClusterDetailWidgetProvider: React.FC<
>
{isLoading ? (
<ClusterDetailWidgetProviderLoader />
) : kind === capav1beta1.AWSCluster ? (
) : kind === capav1beta2.AWSCluster ? (
<ClusterDetailWidgetProviderAWS
providerCluster={providerCluster as capav1beta1.IAWSCluster}
providerCluster={providerCluster as capav1beta2.IAWSCluster}
providerCredential={
providerCredential as capav1beta1.IAWSClusterRoleIdentity
providerCredential as capav1beta2.IAWSClusterRoleIdentity
}
/>
) : kind === capav1beta2.AWSManagedCluster ? (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Text } from 'grommet';
import { normalizeColor } from 'grommet/utils';
import { getProviderClusterLocation } from 'MAPI/utils';
import * as capav1beta1 from 'model/services/mapi/capav1beta1';
import * as capav1beta2 from 'model/services/mapi/capav1beta2';
import * as infrav1alpha3 from 'model/services/mapi/infrastructurev1alpha3';
import * as legacyCredentials from 'model/services/mapi/legacy/credentials';
import React from 'react';
Expand All @@ -20,9 +20,9 @@ const StyledLink = styled.a`
`;

interface IClusterDetailWidgetProviderAWSProps {
providerCluster: capav1beta1.IAWSCluster | infrav1alpha3.IAWSCluster;
providerCluster: capav1beta2.IAWSCluster | infrav1alpha3.IAWSCluster;
providerCredential?:
| capav1beta1.IAWSClusterRoleIdentity
| capav1beta2.IAWSClusterRoleIdentity
| legacyCredentials.ICredential;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { Providers, StatusCodes } from 'model/constants';
import nock from 'nock';
import React from 'react';
import { SWRConfig } from 'swr';
import * as capa1beta1Mocks from 'test/mockHttpCalls/capav1beta1';
import * as capg1beta1Mocks from 'test/mockHttpCalls/capgv1beta1';
import * as capav1beta2Mocks from 'test/mockHttpCalls/capav1beta2';
import * as capgv1beta1Mocks from 'test/mockHttpCalls/capgv1beta1';
import * as capiv1beta1Mocks from 'test/mockHttpCalls/capiv1beta1';
import * as capzv1beta1Mocks from 'test/mockHttpCalls/capzv1beta1';
import * as infrav1alpha3Mocks from 'test/mockHttpCalls/infrastructurev1alpha3';
Expand Down Expand Up @@ -311,7 +311,7 @@ describe('ClusterDetailWidgetControlPlaneNodes on GCP', () => {
)
.reply(
StatusCodes.Ok,
capg1beta1Mocks.randomClusterGCP1GCPMachineTemplateListCP
capgv1beta1Mocks.randomClusterGCP1GCPMachineTemplateListCP
);

render(
Expand All @@ -337,7 +337,7 @@ describe('ClusterDetailWidgetControlPlaneNodes on GCP', () => {
)
.reply(
StatusCodes.Ok,
capg1beta1Mocks.randomClusterGCP1GCPMachineTemplateListCP
capgv1beta1Mocks.randomClusterGCP1GCPMachineTemplateListCP
);

render(
Expand Down Expand Up @@ -375,11 +375,11 @@ describe('ClusterDetailWidgetControlPlaneNodes on CAPA', () => {
.reply(StatusCodes.Ok, capiv1beta1Mocks.randomClusterCAPA1MachineList);
nock(window.config.mapiEndpoint)
.get(
`/apis/infrastructure.cluster.x-k8s.io/v1beta1/namespaces/org-org1/awsmachinetemplates/?labelSelector=cluster.x-k8s.io%2Fcluster-name%3Dasdf1%2Ccluster.x-k8s.io%2Frole%3Dcontrol-plane`
`/apis/infrastructure.cluster.x-k8s.io/v1beta2/namespaces/org-org1/awsmachinetemplates/?labelSelector=cluster.x-k8s.io%2Fcluster-name%3Dasdf1%2Ccluster.x-k8s.io%2Frole%3Dcontrol-plane`
)
.reply(
StatusCodes.Ok,
capa1beta1Mocks.randomClusterCAPA1AWSMachineTemplateList
capav1beta2Mocks.randomClusterCAPA1AWSMachineTemplateList
);

render(
Expand All @@ -401,11 +401,11 @@ describe('ClusterDetailWidgetControlPlaneNodes on CAPA', () => {
.reply(StatusCodes.Ok, capiv1beta1Mocks.randomClusterCAPA1MachineList);
nock(window.config.mapiEndpoint)
.get(
`/apis/infrastructure.cluster.x-k8s.io/v1beta1/namespaces/org-org1/awsmachinetemplates/?labelSelector=cluster.x-k8s.io%2Fcluster-name%3Dasdf1%2Ccluster.x-k8s.io%2Frole%3Dcontrol-plane`
`/apis/infrastructure.cluster.x-k8s.io/v1beta2/namespaces/org-org1/awsmachinetemplates/?labelSelector=cluster.x-k8s.io%2Fcluster-name%3Dasdf1%2Ccluster.x-k8s.io%2Frole%3Dcontrol-plane`
)
.reply(
StatusCodes.Ok,
capa1beta1Mocks.randomClusterCAPA1AWSMachineTemplateList
capav1beta2Mocks.randomClusterCAPA1AWSMachineTemplateList
);

render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import nock from 'nock';
import React from 'react';
import { useParams } from 'react-router';
import { SWRConfig } from 'swr';
import * as capav1beta1Mocks from 'test/mockHttpCalls/capav1beta1';
import * as capav1beta2Mocks from 'test/mockHttpCalls/capav1beta2';
import * as capgv1beta1Mocks from 'test/mockHttpCalls/capgv1beta1';
import * as capiv1beta1Mocks from 'test/mockHttpCalls/capiv1beta1';
import * as capzv1beta1Mocks from 'test/mockHttpCalls/capzv1beta1';
Expand Down Expand Up @@ -166,7 +166,7 @@ async function setupGCP() {
async function setupCAPA() {
const utils = setup(
capiv1beta1Mocks.randomClusterCAPA1,
capav1beta1Mocks.randomAWSCluster1
capav1beta2Mocks.randomAWSCluster1
);

if (screen.queryAllByText('Loading...').length > 0) {
Expand Down Expand Up @@ -447,9 +447,9 @@ describe('ClusterDetailWidgetProvider when user can get AWSClusterRoleIdentity o
it('displays cluster region and account ID', async () => {
nock(window.config.mapiEndpoint)
.get(
'/apis/infrastructure.cluster.x-k8s.io/v1beta1/awsclusterroleidentities/default/'
'/apis/infrastructure.cluster.x-k8s.io/v1beta2/awsclusterroleidentities/default/'
)
.reply(StatusCodes.Ok, capav1beta1Mocks.defaultAWSClusterRoleIdentity);
.reply(StatusCodes.Ok, capav1beta2Mocks.defaultAWSClusterRoleIdentity);
await setupCAPA();
const providerInfo = screen.getByTestId('provider-info');
expect(within(providerInfo).getByText('AWS region')).toBeInTheDocument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Providers, StatusCodes } from 'model/constants';
import nock from 'nock';
import React from 'react';
import { SWRConfig } from 'swr';
import * as capav1beta1Mocks from 'test/mockHttpCalls/capav1beta1';
import * as capav1beta2Mocks from 'test/mockHttpCalls/capav1beta2';
import * as capgv1beta1Mocks from 'test/mockHttpCalls/capgv1beta1';
import * as capiv1beta1Mocks from 'test/mockHttpCalls/capiv1beta1';
import * as capzv1beta1Mocks from 'test/mockHttpCalls/capzv1beta1';
Expand Down Expand Up @@ -166,7 +166,7 @@ describe('ClusterDetailWidgetVersions on CAPA', () => {
render(
getComponent({
cluster: capiv1beta1Mocks.randomClusterCAPA1,
providerCluster: capav1beta1Mocks.randomAWSCluster1,
providerCluster: capav1beta2Mocks.randomAWSCluster1,
})
);

Expand All @@ -179,7 +179,7 @@ describe('ClusterDetailWidgetVersions on CAPA', () => {
render(
getComponent({
cluster: capiv1beta1Mocks.randomClusterCAPA1,
providerCluster: capav1beta1Mocks.randomAWSCluster1,
providerCluster: capav1beta2Mocks.randomAWSCluster1,
})
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import nock from 'nock';
import React from 'react';
import { useRouteMatch } from 'react-router';
import { SWRConfig } from 'swr';
import * as capav1beta1Mocks from 'test/mockHttpCalls/capav1beta1';
import * as capav1beta2Mocks from 'test/mockHttpCalls/capav1beta2';
import * as capgv1beta1Mocks from 'test/mockHttpCalls/capgv1beta1';
import * as capiv1beta1Mocks from 'test/mockHttpCalls/capiv1beta1';
import * as capzv1beta1Mocks from 'test/mockHttpCalls/capzv1beta1';
Expand Down Expand Up @@ -447,7 +447,7 @@ describe('ClusterDetail', () => {
],
},
};
const providerCluster = capav1beta1Mocks.randomAWSCluster1;
const providerCluster = capav1beta2Mocks.randomAWSCluster1;

(useRouteMatch as jest.Mock).mockReturnValue(
getRouteMatch(cluster.metadata.name)
Expand All @@ -465,7 +465,7 @@ describe('ClusterDetail', () => {

nock(window.config.mapiEndpoint)
.get(
`/apis/infrastructure.cluster.x-k8s.io/v1beta1/namespaces/${
`/apis/infrastructure.cluster.x-k8s.io/v1beta2/namespaces/${
cluster.metadata.namespace
}/awsclusters/${cluster.spec!.infrastructureRef!.name}/`
)
Expand Down
20 changes: 9 additions & 11 deletions src/components/MAPI/clusters/ClusterDetail/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
import { GenericResponseError } from 'model/clients/GenericResponseError';
import { IHttpClient } from 'model/clients/HttpClient';
import { Constants, Providers } from 'model/constants';
import * as capav1beta1 from 'model/services/mapi/capav1beta1';
import * as capav1beta2 from 'model/services/mapi/capav1beta2';
import * as capgv1beta1 from 'model/services/mapi/capgv1beta1';
import * as capiv1beta1 from 'model/services/mapi/capiv1beta1';
Expand Down Expand Up @@ -334,16 +333,16 @@ export async function fetchProviderCredential(
const apiGroup = getApiGroupFromApiVersion(apiVersion);

switch (true) {
case kind === capav1beta1.AWSCluster &&
apiGroup === capav1beta1.ApiGroup: {
const identityRef = (providerCluster as capav1beta1.IAWSCluster).spec
case kind === capav1beta2.AWSCluster &&
apiGroup === capav1beta2.ApiGroup: {
const identityRef = (providerCluster as capav1beta2.IAWSCluster).spec
?.identityRef;

if (identityRef?.kind !== 'AWSClusterRoleIdentity') {
throw new Error('Unsupported AWS cluster role identity reference.');
}

return capav1beta1.getAWSClusterRoleIdentity(
return capav1beta2.getAWSClusterRoleIdentity(
httpClientFactory(),
auth,
identityRef.name
Expand Down Expand Up @@ -427,15 +426,15 @@ export function fetchProviderCredentialKey(
const apiGroup = getApiGroupFromApiVersion(apiVersion);

switch (true) {
case kind === capav1beta1.AWSCluster && apiGroup === capav1beta1.ApiGroup: {
const identityRef = (providerCluster as capav1beta1.IAWSCluster).spec
case kind === capav1beta2.AWSCluster && apiGroup === capav1beta2.ApiGroup: {
const identityRef = (providerCluster as capav1beta2.IAWSCluster).spec
?.identityRef;

if (identityRef?.kind !== 'AWSClusterRoleIdentity') {
return null;
}

return capav1beta1.getAWSClusterRoleIdentityKey(identityRef.name);
return capav1beta2.getAWSClusterRoleIdentityKey(identityRef.name);
}

case kind === capav1beta2.AWSManagedCluster &&
Expand Down Expand Up @@ -492,17 +491,16 @@ function getMainCredential(credentials: legacyCredentials.ICredential[]) {
export function getAWSCredentialAccountID(
credential?:
| legacyCredentials.ICredential
| capav1beta1.IAWSClusterRoleIdentity
| capav1beta2.IAWSClusterRoleIdentity
) {
if (!credential) return '';

switch (true) {
case credential.hasOwnProperty('kind') &&
(credential as capav1beta1.IAWSClusterRoleIdentity).kind ===
(credential as capav1beta2.IAWSClusterRoleIdentity).kind ===
'AWSClusterRoleIdentity':
return extractIDFromARN(
(credential as capav1beta1.IAWSClusterRoleIdentity).spec?.roleARN
(credential as capav1beta2.IAWSClusterRoleIdentity).spec?.roleARN
);
case credential.hasOwnProperty('awsOperatorRole'):
return (credential as legacyCredentials.ICredential).awsOperatorRole;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import nock from 'nock';
import React from 'react';
import { SWRConfig } from 'swr';
import { withMarkup } from 'test/assertUtils';
import * as capav1beta1Mocks from 'test/mockHttpCalls/capav1beta1';
import * as capav1beta2Mocks from 'test/mockHttpCalls/capav1beta2';
import * as capgv1beta1Mocks from 'test/mockHttpCalls/capgv1beta1';
import * as capiexpv1alpha3Mocks from 'test/mockHttpCalls/capiv1alpha3/exp';
import * as capiv1beta1Mocks from 'test/mockHttpCalls/capiv1beta1';
Expand Down Expand Up @@ -745,7 +745,7 @@ describe('ClusterListItem on CAPA', () => {
render(
getComponent({
cluster: capiv1beta1Mocks.randomClusterCAPA1,
providerCluster: capav1beta1Mocks.randomAWSCluster1,
providerCluster: capav1beta2Mocks.randomAWSCluster1,
})
);

Expand Down Expand Up @@ -774,7 +774,7 @@ describe('ClusterListItem on CAPA', () => {
],
},
},
providerCluster: capav1beta1Mocks.randomAWSCluster1,
providerCluster: capav1beta2Mocks.randomAWSCluster1,
})
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import nock from 'nock';
import * as React from 'react';
import { SWRConfig } from 'swr';
import * as authorizationv1Mocks from 'test/mockHttpCalls/authorizationv1';
import * as capav1beta1Mocks from 'test/mockHttpCalls/capav1beta1';
import * as capav1beta2Mocks from 'test/mockHttpCalls/capav1beta2';
import * as capgv1beta1Mocks from 'test/mockHttpCalls/capgv1beta1';
import * as capiexpv1alpha3Mocks from 'test/mockHttpCalls/capiv1alpha3/exp';
import * as capiv1beta1Mocks from 'test/mockHttpCalls/capiv1beta1';
Expand Down Expand Up @@ -964,15 +964,15 @@ describe('OrganizationDetailGeneral on CAPA', () => {

nock(window.config.mapiEndpoint)
.get(
`/apis/infrastructure.cluster.x-k8s.io/v1beta1/namespaces/org-org1/awsclusters/${capiv1beta1Mocks.randomClusterListCAPA.items[0].metadata.name}/`
`/apis/infrastructure.cluster.x-k8s.io/v1beta2/namespaces/org-org1/awsclusters/${capiv1beta1Mocks.randomClusterListCAPA.items[0].metadata.name}/`
)
.reply(StatusCodes.Ok, capav1beta1Mocks.randomAWSCluster1);
.reply(StatusCodes.Ok, capav1beta2Mocks.randomAWSCluster1);

nock(window.config.mapiEndpoint)
.get(
`/apis/infrastructure.cluster.x-k8s.io/v1beta1/namespaces/org-org1/awsclusters/${capiv1beta1Mocks.randomClusterListCAPA.items[1].metadata.name}/`
`/apis/infrastructure.cluster.x-k8s.io/v1beta2/namespaces/org-org1/awsclusters/${capiv1beta1Mocks.randomClusterListCAPA.items[1].metadata.name}/`
)
.reply(StatusCodes.Ok, capav1beta1Mocks.randomAWSCluster2);
.reply(StatusCodes.Ok, capav1beta2Mocks.randomAWSCluster2);

nock(window.config.mapiEndpoint)
.get(
Expand All @@ -985,9 +985,9 @@ describe('OrganizationDetailGeneral on CAPA', () => {

nock(window.config.mapiEndpoint)
.get(
`/apis/infrastructure.cluster.x-k8s.io/v1beta1/namespaces/org-org1/awsmachinepools/${capiv1beta1Mocks.randomClusterCAPA1MachinePoolList.items[0].spec?.template.spec?.infrastructureRef.name}/`
`/apis/infrastructure.cluster.x-k8s.io/v1beta2/namespaces/org-org1/awsmachinepools/${capiv1beta1Mocks.randomClusterCAPA1MachinePoolList.items[0].spec?.template.spec?.infrastructureRef.name}/`
)
.reply(StatusCodes.Ok, capav1beta1Mocks.randomClusterCAPA1AWSMachinePool);
.reply(StatusCodes.Ok, capav1beta2Mocks.randomClusterCAPA1AWSMachinePool);

render(
getComponent({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
mapNodePoolsToProviderNodePools,
} from 'MAPI/workernodes/utils';
import { GenericResponse } from 'model/clients/GenericResponse';
import * as capav1beta1 from 'model/services/mapi/capav1beta1';
import * as capav1beta2 from 'model/services/mapi/capav1beta2';
import * as capgv1beta1 from 'model/services/mapi/capgv1beta1';
import * as capiv1beta1 from 'model/services/mapi/capiv1beta1';
import * as capzv1beta1 from 'model/services/mapi/capzv1beta1';
Expand Down Expand Up @@ -152,7 +152,7 @@ function appendControlPlaneNodeStats(

for (const cpNode of controlPlaneNodes) {
switch (cpNode.kind) {
case capav1beta1.AWSMachineTemplate:
case capav1beta2.AWSMachineTemplate:
case capgv1beta1.GCPMachineTemplate:
if (cpNode.spec?.template?.spec.instanceType) {
instanceTypes.push(cpNode.spec.template.spec.instanceType);
Expand Down
Loading

0 comments on commit a60e845

Please sign in to comment.