Skip to content

Commit

Permalink
fix: Fix registry form update issues (#118)
Browse files Browse the repository at this point in the history
Jira: EPMDPEDP-13008
Related: #118
Change-Id: Ia32938cdf1fda91b4c3123fe1916471e3122c30e
  • Loading branch information
callmevladik committed Dec 15, 2023
1 parent 8a52861 commit 2f2e415
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ export const useSetupRegistry = ({ onSuccess }) => {
formData: { tektonServiceAccount, EDPConfigMap, pushAccountSecret, pullAccountSecret },
} = useFormContext<ManageRegistryDataContext>();

const {
editConfigMap,
mutations: { configMapEditMutation },
} = useConfigMapCRUD({});

const {
createSecret,
editSecret,
Expand All @@ -35,6 +30,13 @@ export const useSetupRegistry = ({ onSuccess }) => {
mutations: { serviceAccountEditMutation },
} = useEditServiceAccount({});

const {
editConfigMap,
mutations: { configMapEditMutation },
} = useConfigMapCRUD({
onSuccess: onSuccess,
});

const isLoading =
serviceAccountEditMutation.isLoading ||
secretCreateMutation.isLoading ||
Expand Down Expand Up @@ -79,7 +81,6 @@ export const useSetupRegistry = ({ onSuccess }) => {
awsRegion,
});

await editConfigMap({ configMapData: newEDPConfigMap });
await editServiceAccount({ serviceAccount: editedServiceAccount });

if (!!pushAccountSecret) {
Expand All @@ -88,9 +89,7 @@ export const useSetupRegistry = ({ onSuccess }) => {
await createSecret({ secretData: newECRSecretInstance });
}

if (onSuccess) {
await onSuccess();
}
await editConfigMap({ configMapData: newEDPConfigMap });
};

const setupDockerHub = async () => {
Expand All @@ -114,8 +113,6 @@ export const useSetupRegistry = ({ onSuccess }) => {
registryType,
});

await editConfigMap({ configMapData: newEDPConfigMap });

if (!!pushAccountSecret) {
await editSecret({ secretData: newKanikoSecretInstance });
} else {
Expand All @@ -128,9 +125,7 @@ export const useSetupRegistry = ({ onSuccess }) => {
await createSecret({ secretData: newRegcredSecretInstance });
}

if (onSuccess) {
await onSuccess();
}
await editConfigMap({ configMapData: newEDPConfigMap });
};

const setupHarbor = async () => {
Expand All @@ -154,8 +149,6 @@ export const useSetupRegistry = ({ onSuccess }) => {
registryType,
});

await editConfigMap({ configMapData: newEDPConfigMap });

if (!!pushAccountSecret) {
await editSecret({ secretData: newKanikoSecretInstance });
} else {
Expand All @@ -168,9 +161,7 @@ export const useSetupRegistry = ({ onSuccess }) => {
await createSecret({ secretData: newRegcredSecretInstance });
}

if (onSuccess) {
await onSuccess();
}
await editConfigMap({ configMapData: newEDPConfigMap });
};

const setupOpenshift = async () => {
Expand All @@ -186,17 +177,13 @@ export const useSetupRegistry = ({ onSuccess }) => {
registryType,
});

await editConfigMap({ configMapData: newEDPConfigMap });

if (!!pushAccountSecret) {
await editSecret({ secretData: newKanikoSecretInstance });
} else {
await createSecret({ secretData: newKanikoSecretInstance });
}

if (onSuccess) {
await onSuccess();
}
await editConfigMap({ configMapData: newEDPConfigMap });
};

switch (registryType) {
Expand Down
148 changes: 66 additions & 82 deletions src/widgets/ManageRegistry/components/Create/hooks/useDefaultValues.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { CONTAINER_REGISTRY_TYPE } from '../../../../../k8s/ConfigMap/constants';
import { ConfigMapKubeObjectInterface } from '../../../../../k8s/ConfigMap/types';
import { SecretKubeObjectInterface } from '../../../../../k8s/Secret/types';
import { ServiceAccountKubeObjectInterface } from '../../../../../k8s/ServiceAccount/types';
import { safeDecode } from '../../../../../utils/decodeEncode';
import { REGISTRY_NAMES } from '../../../names';
import { ManageRegistryDataContext } from '../../../types';
Expand Down Expand Up @@ -36,90 +37,73 @@ const getAuth = (secret: SecretKubeObjectInterface) => {
return { auth };
};

export const useDefaultValues = ({ formData }: { formData: ManageRegistryDataContext }) => {
const { EDPConfigMap, tektonServiceAccount, pushAccountSecret, pullAccountSecret } = formData;

const registryHost = EDPConfigMap?.data?.container_registry_host;
const registrySpace = EDPConfigMap?.data?.container_registry_space;
const registryType = EDPConfigMap?.data?.container_registry_type;
const awsRegion = EDPConfigMap?.data?.aws_region;

const handleECR = React.useCallback(() => {
const irsaRoleArn =
tektonServiceAccount?.metadata?.annotations?.['eks.amazonaws.com/role-arn'];

return {
[REGISTRY_NAMES.REGISTRY_TYPE]: registryType,
[REGISTRY_NAMES.REGISTRY_HOST]: registryHost,
[REGISTRY_NAMES.REGISTRY_SPACE]: registrySpace,
[REGISTRY_NAMES.IRSA_ROLE_ARN]: irsaRoleArn,
[REGISTRY_NAMES.AWS_REGION]: awsRegion,
};
}, [
awsRegion,
registryHost,
registrySpace,
registryType,
tektonServiceAccount?.metadata?.annotations,
]);

const handleDockerHubOrHarbor = React.useCallback(() => {
const { userName: pullUserName, password: pullPassword } =
getUsernameAndPassword(pullAccountSecret);
const { userName: pushUserName, password: pushPassword } =
getUsernameAndPassword(pushAccountSecret);

return {
[REGISTRY_NAMES.REGISTRY_TYPE]: registryType,
[REGISTRY_NAMES.REGISTRY_HOST]: registryHost,
[REGISTRY_NAMES.REGISTRY_SPACE]: registrySpace,
[REGISTRY_NAMES.PUSH_ACCOUNT_USER]: pushUserName,
[REGISTRY_NAMES.PUSH_ACCOUNT_PASSWORD]: pushPassword,
[REGISTRY_NAMES.PULL_ACCOUNT_USER]: pullUserName,
[REGISTRY_NAMES.PULL_ACCOUNT_PASSWORD]: pullPassword,
[REGISTRY_NAMES.USE_SAME_ACCOUNT]: !pullUserName && !pullPassword,
};
}, [pullAccountSecret, pushAccountSecret, registryHost, registrySpace, registryType]);
const handleECR = (
EDPConfigMap: ConfigMapKubeObjectInterface,
tektonServiceAccount: ServiceAccountKubeObjectInterface
) => {
return {
[REGISTRY_NAMES.REGISTRY_TYPE]: EDPConfigMap?.data?.container_registry_type,
[REGISTRY_NAMES.REGISTRY_HOST]: EDPConfigMap?.data?.container_registry_host,
[REGISTRY_NAMES.REGISTRY_SPACE]: EDPConfigMap?.data?.container_registry_space,
[REGISTRY_NAMES.IRSA_ROLE_ARN]:
tektonServiceAccount?.metadata?.annotations?.['eks.amazonaws.com/role-arn'],
[REGISTRY_NAMES.AWS_REGION]: EDPConfigMap?.data?.aws_region,
};
};

const handleOpenshift = React.useCallback(() => {
const { auth } = getAuth(pushAccountSecret);
const handleDockerHubOrHarbor = (
EDPConfigMap: ConfigMapKubeObjectInterface,
pushAccountSecret: SecretKubeObjectInterface,
pullAccountSecret: SecretKubeObjectInterface
) => {
const { userName: pullUserName, password: pullPassword } =
getUsernameAndPassword(pullAccountSecret);
const { userName: pushUserName, password: pushPassword } =
getUsernameAndPassword(pushAccountSecret);

return {
[REGISTRY_NAMES.REGISTRY_TYPE]: EDPConfigMap?.data?.container_registry_type,
[REGISTRY_NAMES.REGISTRY_HOST]: EDPConfigMap?.data?.container_registry_host,
[REGISTRY_NAMES.REGISTRY_SPACE]: EDPConfigMap?.data?.container_registry_space,
[REGISTRY_NAMES.PUSH_ACCOUNT_USER]: pushUserName,
[REGISTRY_NAMES.PUSH_ACCOUNT_PASSWORD]: pushPassword,
[REGISTRY_NAMES.PULL_ACCOUNT_USER]: pullUserName,
[REGISTRY_NAMES.PULL_ACCOUNT_PASSWORD]: pullPassword,
[REGISTRY_NAMES.USE_SAME_ACCOUNT]: !pullUserName && !pullPassword,
};
};

return {
[REGISTRY_NAMES.REGISTRY_TYPE]: registryType,
[REGISTRY_NAMES.REGISTRY_HOST]: registryHost,
[REGISTRY_NAMES.REGISTRY_SPACE]: registrySpace,
[REGISTRY_NAMES.PUSH_ACCOUNT_PASSWORD]: auth,
};
}, [pushAccountSecret, registryHost, registrySpace, registryType]);
const handleOpenshift = (
EDPConfigMap: ConfigMapKubeObjectInterface,
pushAccountSecret: SecretKubeObjectInterface
) => {
const { auth } = getAuth(pushAccountSecret);

return {
[REGISTRY_NAMES.REGISTRY_TYPE]: EDPConfigMap?.data?.container_registry_type,
[REGISTRY_NAMES.REGISTRY_HOST]: EDPConfigMap?.data?.container_registry_host,
[REGISTRY_NAMES.REGISTRY_SPACE]: EDPConfigMap?.data?.container_registry_space,
[REGISTRY_NAMES.PUSH_ACCOUNT_PASSWORD]: auth,
};
};

const handleDefault = React.useCallback(() => {
const { userName: pullUserName, password: pullPassword } =
getUsernameAndPassword(pullAccountSecret);
const { userName: pushUserName, password: pushPassword } =
getUsernameAndPassword(pushAccountSecret);
const { auth } = getAuth(pushAccountSecret);
export const useDefaultValues = ({ formData }: { formData: ManageRegistryDataContext }) => {
const { EDPConfigMap, tektonServiceAccount, pushAccountSecret, pullAccountSecret } = formData;

return {
[REGISTRY_NAMES.PUSH_ACCOUNT_USER]: pushUserName,
[REGISTRY_NAMES.PUSH_ACCOUNT_PASSWORD]: pushPassword || auth,
[REGISTRY_NAMES.PULL_ACCOUNT_USER]: pullUserName,
[REGISTRY_NAMES.PULL_ACCOUNT_PASSWORD]: pullPassword,
[REGISTRY_NAMES.USE_SAME_ACCOUNT]: !pullUserName && !pullPassword,
};
}, [pullAccountSecret, pushAccountSecret]);
if (!EDPConfigMap?.data?.container_registry_type) {
return {};
}

return React.useMemo(() => {
switch (registryType) {
case CONTAINER_REGISTRY_TYPE.ECR:
return handleECR();
case CONTAINER_REGISTRY_TYPE.HARBOR:
return handleDockerHubOrHarbor();
case CONTAINER_REGISTRY_TYPE.DOCKER_HUB:
return handleDockerHubOrHarbor();
case CONTAINER_REGISTRY_TYPE.OPENSHIFT_REGISTRY:
return handleOpenshift();
default:
return handleDefault();
}
}, [handleDefault, handleDockerHubOrHarbor, handleECR, handleOpenshift, registryType]);
switch (EDPConfigMap.data.container_registry_type) {
case CONTAINER_REGISTRY_TYPE.ECR:
return handleECR(EDPConfigMap, tektonServiceAccount);
case CONTAINER_REGISTRY_TYPE.HARBOR:
return handleDockerHubOrHarbor(EDPConfigMap, pushAccountSecret, pullAccountSecret);
case CONTAINER_REGISTRY_TYPE.DOCKER_HUB:
return handleDockerHubOrHarbor(EDPConfigMap, pushAccountSecret, pullAccountSecret);
case CONTAINER_REGISTRY_TYPE.OPENSHIFT_REGISTRY:
return handleOpenshift(EDPConfigMap, pushAccountSecret);
default:
break;
}
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useFormContext as useReactHookFormContext } from 'react-hook-form';
import { editResource } from '../../../../../../../k8s/common/editResource';
import { CONTAINER_REGISTRY_TYPE } from '../../../../../../../k8s/ConfigMap/constants';
import { useConfigMapCRUD } from '../../../../../../../k8s/ConfigMap/hooks/useConfigMapCRUD';
Expand All @@ -6,9 +7,10 @@ import { useEditServiceAccount } from '../../../../../../../k8s/ServiceAccount/h
import { ServiceAccountKubeObjectInterface } from '../../../../../../../k8s/ServiceAccount/types';
import { EDP_CONFIG_MAP_NAMES } from '../../../../../../../pages/edp-configuration/pages/edp-registry-list/names';
import { useFormContext } from '../../../../../../../providers/Form/hooks';
import { ManageRegistryDataContext, ManageRegistryValues } from '../../../../../types';
import { ManageRegistryDataContext } from '../../../../../types';

export const useResetRegistry = ({ onSuccess }) => {
const { getValues } = useReactHookFormContext();
const {
formData: { tektonServiceAccount, EDPConfigMap, pushAccountSecret, pullAccountSecret },
} = useFormContext<ManageRegistryDataContext>();
Expand All @@ -18,7 +20,9 @@ export const useResetRegistry = ({ onSuccess }) => {
const {
editConfigMap,
mutations: { configMapEditMutation },
} = useConfigMapCRUD({});
} = useConfigMapCRUD({
onSuccess: onSuccess,
});

const {
deleteSecret,
Expand All @@ -35,7 +39,9 @@ export const useResetRegistry = ({ onSuccess }) => {
secretDeleteMutation.isLoading ||
configMapEditMutation.isLoading;

const resetRegistry = async (formValues: ManageRegistryValues) => {
const resetRegistry = async () => {
const formValues = getValues();

const { registryType, irsaRoleArn } = formValues;

const resetECR = async () => {
Expand Down Expand Up @@ -65,10 +71,6 @@ export const useResetRegistry = ({ onSuccess }) => {

await editServiceAccount({ serviceAccount: editedServiceAccount });
await editConfigMap({ configMapData: newEDPConfigMap });

if (onSuccess) {
await onSuccess();
}
};

const resetDockerHub = async () => {
Expand All @@ -82,10 +84,6 @@ export const useResetRegistry = ({ onSuccess }) => {
await deleteSecret({ secretData: secret });
}
await editConfigMap({ configMapData: newEDPConfigMap });

if (onSuccess) {
await onSuccess();
}
};

const resetHarbor = async () => {
Expand All @@ -99,10 +97,6 @@ export const useResetRegistry = ({ onSuccess }) => {
await deleteSecret({ secretData: secret });
}
await editConfigMap({ configMapData: newEDPConfigMap });

if (onSuccess) {
await onSuccess();
}
};

const resetOpenshift = async () => {
Expand All @@ -115,13 +109,9 @@ export const useResetRegistry = ({ onSuccess }) => {
await deleteSecret({ secretData: pushAccountSecret });

await editConfigMap({ configMapData: newEDPConfigMap });

if (onSuccess) {
await onSuccess();
}
};

switch (registryType || EDPConfigMap?.data?.container_registry_type) {
switch (registryType) {
case CONTAINER_REGISTRY_TYPE.ECR:
await resetECR();
break;
Expand Down
Loading

0 comments on commit 2f2e415

Please sign in to comment.