diff --git a/src/brokers/broker-details/components/Overview/Overview.container.tsx b/src/brokers/broker-details/components/Overview/Overview.container.tsx index c6f4336c..0f1692a4 100644 --- a/src/brokers/broker-details/components/Overview/Overview.container.tsx +++ b/src/brokers/broker-details/components/Overview/Overview.container.tsx @@ -19,7 +19,7 @@ import { Spinner, Title, } from '@patternfly/react-core'; -import { FC, useState } from 'react'; +import { FC, useContext, useState } from 'react'; import { getIssuerForAcceptor, getIssuerIngressHostForAcceptor, @@ -32,6 +32,7 @@ import { SecretResource, } from '../../../../k8s/types'; import { Metrics } from './Metrics/Metrics'; +import { AuthContext } from '../../../../jolokia/context'; const useGetIssuerCa = ( cr: BrokerCR, @@ -106,8 +107,9 @@ const HelpConnectAcceptor: FC = ({ cr, acceptor, }) => { + const { podOrdinal } = useContext(AuthContext); const secret = useGetTlsSecret(cr, acceptor); - const ingressHost = getIssuerIngressHostForAcceptor(cr, acceptor); + const ingressHost = getIssuerIngressHostForAcceptor(cr, acceptor, podOrdinal); const [copied, setCopied] = useState(false); const clipboardCopyFunc = (text: string) => { diff --git a/src/jolokia/context.ts b/src/jolokia/context.ts index 124a877a..0b253f4a 100644 --- a/src/jolokia/context.ts +++ b/src/jolokia/context.ts @@ -8,6 +8,7 @@ export type JolokiaLogin = { isError: boolean; token: string; source: jolokiaLoginSource; + podOrdinal: number; }; export const AuthContext = createContext({ @@ -16,4 +17,5 @@ export const AuthContext = createContext({ isSuccess: false, isError: false, source: 'api', + podOrdinal: 0, }); diff --git a/src/jolokia/customHooks.ts b/src/jolokia/customHooks.ts index 6c47e83f..5a6b5bff 100644 --- a/src/jolokia/customHooks.ts +++ b/src/jolokia/customHooks.ts @@ -233,6 +233,7 @@ export const useJolokiaLogin = ( isError: isErrorRequestApi, token: token, source: 'session', + podOrdinal: ordinal, }; } return { @@ -241,6 +242,7 @@ export const useJolokiaLogin = ( isLoading: isLoginMutationLoading || isLoginMutationIdle, token: token, source: 'api', + podOrdinal: ordinal, }; }; diff --git a/src/reducers/7.12/reducer.test.ts b/src/reducers/7.12/reducer.test.ts index 937f3954..9e8c47c3 100644 --- a/src/reducers/7.12/reducer.test.ts +++ b/src/reducers/7.12/reducer.test.ts @@ -743,6 +743,134 @@ describe('test the creation broker reducer', () => { ).toBe('ing.' + 'bob' + '.' + 'bro' + '-0.' + 'space' + '.' + 'tttt.com'); }); + it('test changing number of replicas while in the PEM preset gives the correct number of hosts', () => { + const initialState = newArtemisCRState('namespace'); + const stateWith1Acceptor = artemisCrReducer(initialState, { + operation: ArtemisReducerOperations.addAcceptor, + }); + const stateWithIngressDomain = artemisCrReducer(stateWith1Acceptor, { + operation: ArtemisReducerOperations.setIngressDomain, + payload: 'apps-crc.testing', + }); + const stateWithPEM = artemisCrReducer(stateWithIngressDomain, { + operation: ArtemisReducerOperations.activatePEMGenerationForAcceptor, + payload: { + acceptor: 'acceptors0', + issuer: 'someIssuer', + }, + }); + expect(stateWithPEM.cr.spec.acceptors[0].sslEnabled).toBe(true); + expect(stateWithPEM.cr.spec.acceptors[0].exposeMode).toBe( + ExposeMode.ingress, + ); + expect(stateWithPEM.cr.spec.acceptors[0].ingressHost).toBe( + 'ing.$(ITEM_NAME).$(CR_NAME)-$(BROKER_ORDINAL).$(CR_NAMESPACE).$(INGRESS_DOMAIN)', + ); + expect(stateWithPEM.cr.spec.acceptors[0].sslSecret).toBe( + 'ex-aao-acceptors0-0-svc-ing-ptls', + ); + expect(stateWithPEM.cr.spec.resourceTemplates).toHaveLength(1); + expect(stateWithPEM.cr.spec.resourceTemplates[0].selector.name).toBe( + 'ex-aao' + '-' + 'acceptors0' + '-0-svc-ing', + ); + expect(stateWithPEM.cr.spec.resourceTemplates[0].selector.name).toBe( + 'ex-aao' + '-' + 'acceptors0' + '-0-svc-ing', + ); + expect( + stateWithPEM.cr.spec.resourceTemplates[0].patch.spec.tls[0].hosts[0], + ).toBe( + 'ing.' + + 'acceptors0' + + '.' + + 'ex-aao' + + '-0.' + + 'namespace' + + '.' + + 'apps-crc.testing', + ); + const stateWith2Replicas = artemisCrReducer(stateWithPEM, { + operation: ArtemisReducerOperations.incrementReplicas, + }); + expect( + stateWith2Replicas.cr.spec.resourceTemplates[0].patch.spec.tls[0] + .hosts[0], + ).toBe( + 'ing.' + + 'acceptors0' + + '.' + + 'ex-aao' + + '-0.' + + 'namespace' + + '.' + + 'apps-crc.testing', + ); + expect( + stateWith2Replicas.cr.spec.resourceTemplates[0].patch.spec.tls[0] + .hosts[1], + ).toBe( + 'ing.' + + 'acceptors0' + + '.' + + 'ex-aao' + + '-1.' + + 'namespace' + + '.' + + 'apps-crc.testing', + ); + expect( + stateWith2Replicas.cr.spec.resourceTemplates[0].patch.spec.tls[0].hosts, + ).toHaveLength(2); + + const newNumber = 10; + const stateWith10Replicas = artemisCrReducer(stateWith2Replicas, { + operation: ArtemisReducerOperations.setReplicasNumber, + payload: newNumber, + }); + expect( + stateWith10Replicas.cr.spec.resourceTemplates[0].patch.spec.tls[0].hosts, + ).toHaveLength(newNumber); + for (let i = 0; i < newNumber; i++) { + expect( + stateWith10Replicas.cr.spec.resourceTemplates[0].patch.spec.tls[0] + .hosts[i], + ).toBe( + 'ing.' + + 'acceptors0' + + '.' + + 'ex-aao' + + '-' + + i + + '.' + + 'namespace' + + '.' + + 'apps-crc.testing', + ); + } + const stateWith9Replicas = artemisCrReducer(stateWith10Replicas, { + operation: ArtemisReducerOperations.decrementReplicas, + }); + expect( + stateWith9Replicas.cr.spec.resourceTemplates[0].patch.spec.tls[0].hosts, + ).toHaveLength(9); + for (let i = 0; i < 9; i++) { + expect( + stateWith10Replicas.cr.spec.resourceTemplates[0].patch.spec.tls[0] + .hosts[i], + ).toBe( + 'ing.' + + 'acceptors0' + + '.' + + 'ex-aao' + + '-' + + i + + '.' + + 'namespace' + + '.' + + 'apps-crc.testing', + ); + } + }); + it('test deletePEMGenerationForAcceptor', () => { const initialState = newArtemisCRState('namespace'); const stateWith1Acceptor = artemisCrReducer(initialState, { diff --git a/src/reducers/7.12/reducer.ts b/src/reducers/7.12/reducer.ts index 89919145..8b33f83b 100644 --- a/src/reducers/7.12/reducer.ts +++ b/src/reducers/7.12/reducer.ts @@ -617,16 +617,19 @@ export const artemisCrReducer: React.Reducer< updateNamespace(formState.cr, action.payload); break; case ArtemisReducerOperations.setReplicasNumber: - formState.cr.spec.deploymentPlan.size = action.payload; + updateDeploymentSize(formState.cr, action.payload); break; case ArtemisReducerOperations.incrementReplicas: - formState.cr.spec.deploymentPlan.size += 1; + updateDeploymentSize( + formState.cr, + formState.cr.spec.deploymentPlan.size + 1, + ); break; case ArtemisReducerOperations.decrementReplicas: - formState.cr.spec.deploymentPlan.size -= 1; - if (formState.cr.spec.deploymentPlan.size < 1) { - formState.cr.spec.deploymentPlan.size = 1; - } + updateDeploymentSize( + formState.cr, + formState.cr.spec.deploymentPlan.size - 1, + ); break; case ArtemisReducerOperations.setBrokerName: updateBrokerName(formState.cr, action.payload); @@ -884,7 +887,31 @@ const updateIngressDomain = (cr: BrokerCR, newName: string) => { if (!rt) { return; } - rt.patch.spec.tls[0].hosts = [certManagerTlsHost(cr, acceptor.name)]; + rt.patch.spec.tls[0].hosts = certManagerTlsHosts(cr, acceptor.name); + }); +}; + +const updateDeploymentSize = (cr: BrokerCR, newSize: number) => { + cr.spec.deploymentPlan.size = newSize; + if (cr.spec.deploymentPlan.size < 1) { + cr.spec.deploymentPlan.size = 1; + } + // when the size changes, some annotations will need an update to + // stay in sync + if (!cr.spec.acceptors) { + return; + } + if (!cr.spec.resourceTemplates) { + return; + } + cr.spec.acceptors.forEach((acceptor) => { + const rt = cr.spec.resourceTemplates.find( + (rt) => rt.selector.name === certManagerSelector(cr, acceptor.name), + ); + if (!rt) { + return; + } + rt.patch.spec.tls[0].hosts = certManagerTlsHosts(cr, acceptor.name); }); }; @@ -905,7 +932,7 @@ const updateNamespace = (cr: BrokerCR, newName: string) => { if (!rt) { return; } - rt.patch.spec.tls[0].hosts = [certManagerTlsHost(cr, acceptor.name)]; + rt.patch.spec.tls[0].hosts = certManagerTlsHosts(cr, acceptor.name); }); }; @@ -934,7 +961,7 @@ const updateBrokerName = (cr: BrokerCR, newName: string) => { } rt.selector.name = certManagerSelector(cr, acceptor.name); rt.patch.spec.tls[0] = { - hosts: [certManagerTlsHost(cr, acceptor.name)], + hosts: certManagerTlsHosts(cr, acceptor.name), secretName: acceptor.sslSecret, }; }); @@ -991,16 +1018,25 @@ const setIssuerForAcceptor = ( } }; -// TODO handle multiple ordinals -const certManagerTlsHost = (cr: BrokerCR, acceptor: string) => - 'ing.' + - acceptor + - '.' + - cr.metadata.name + - '-0.' + - cr.metadata.namespace + - '.' + - cr.spec.ingressDomain; +const certManagerTlsHosts = (cr: BrokerCR, acceptor: string): string[] => { + const ret: string[] = []; + for (let i = 0; i < cr.spec.deploymentPlan.size; i++) { + ret.push( + 'ing.' + + acceptor + + '.' + + cr.metadata.name + + '-' + + i + + '.' + + cr.metadata.namespace + + '.' + + cr.spec.ingressDomain, + ); + } + + return ret; +}; const certManagerSelector = (cr: BrokerCR, acceptor: string) => cr.metadata.name + '-' + acceptor + '-0-svc-ing'; @@ -1028,8 +1064,7 @@ const updateAcceptorNameInResourceTemplate = ( // if there's a match update the required fields if (rt) { rt.selector.name = certManagerSelector(cr, newName); - // TODO support multiple ordinals - rt.patch.spec.tls[0].hosts = [certManagerTlsHost(cr, newName)]; + rt.patch.spec.tls[0].hosts = certManagerTlsHosts(cr, newName); rt.patch.spec.tls[0].secretName = certManagerSecret(cr, newName); } }; @@ -1044,7 +1079,6 @@ const createCertManagerResourceTemplate = ( acceptor: Acceptor, issuerName: string, ): ResourceTemplate => { - // TODO support multiple ordinals return { selector: { kind: 'Ingress', @@ -1058,7 +1092,7 @@ const createCertManagerResourceTemplate = ( spec: { tls: [ { - hosts: [certManagerTlsHost(cr, acceptor.name)], + hosts: certManagerTlsHosts(cr, acceptor.name), secretName: acceptor.sslSecret, }, ], @@ -1880,6 +1914,7 @@ export const getIssuerForAcceptor = (cr: BrokerCR, acceptor: Acceptor) => { export const getIssuerIngressHostForAcceptor = ( cr: BrokerCR, acceptor: Acceptor, + podOrdinal: number, ) => { if (!acceptor) { return ''; @@ -1894,7 +1929,7 @@ export const getIssuerIngressHostForAcceptor = ( (rt) => rt.selector?.name === selector, ); if (rt) { - return rt.patch.spec.tls[0].hosts[0]; + return rt.patch.spec.tls[0].hosts[podOrdinal]; } return ''; };