Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add useBuildXDriver in build infra #501

Merged
merged 11 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtron-labs/devtron-fe-common-lib",
"version": "1.4.0-patch-1",
"version": "1.4.0-patch-1-beta-1",
"description": "Supporting common component library",
"type": "module",
"main": "dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/Common/AppStatus/AppStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default function AppStatus({
className="default-tt w-200"
arrow={false}
placement="top"
content="To fetch app status for GitOps based deployments open the app detail page"
content="To fetch app status for Helm based deployments open the app detail page"
>
<div className="flex">
<InfoIcon className="icon-dim-16 fcn-6" />
Expand Down
40 changes: 30 additions & 10 deletions src/Pages/GlobalConfigurations/BuildInfra/UseBuildInfraForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
BuildInfraToleranceOperatorType,
BuildInfraToleranceValueType,
CREATE_MODE_REQUIRED_INPUT_FIELDS,
createBuildInfraProfile,
DEFAULT_PROFILE_NAME,
DEFAULT_TOLERANCE_EFFECT,
DEFAULT_TOLERANCE_OPERATOR,
Expand All @@ -29,7 +28,7 @@ import {
ProfileInputErrorType,
TARGET_PLATFORM_ERROR_FIELDS_MAP,
ToleranceHeaderType,
updateBuildInfraProfile,
upsertBuildInfraProfile,
UseBuildInfraFormProps,
UseBuildInfraFormResponseType,
ValidateNodeSelectorParamsType,
Expand Down Expand Up @@ -285,6 +284,7 @@ const useBuildInfraForm = ({
name,
editProfile,
handleSuccessRedirection,
canConfigureUseK8sDriver = false,
}: UseBuildInfraFormProps): UseBuildInfraFormResponseType => {
const fromCreateView = !name

Expand All @@ -293,6 +293,7 @@ const useBuildInfraForm = ({
getBuildInfraProfileByName({
name: name ?? DEFAULT_PROFILE_NAME,
fromCreateView,
canConfigureUseK8sDriver,
}),
[name],
)
Expand Down Expand Up @@ -330,11 +331,16 @@ const useBuildInfraForm = ({
const currentInput = structuredClone(profileInput)
const currentInputErrors = structuredClone(profileInputErrors)
const targetPlatform =
'targetPlatform' in data && Object.hasOwn(data, 'targetPlatform') ? data.targetPlatform : ''
data && 'targetPlatform' in data && Object.hasOwn(data, 'targetPlatform') ? data.targetPlatform : ''
const currentConfiguration = currentInput.configurations[targetPlatform]
const lastSavedConfiguration = profileResponse.profile.configurations[targetPlatform] || currentConfiguration

switch (action) {
case BuildInfraProfileInputActionType.TOGGLE_USE_K8S_DRIVER: {
currentInput.useK8sDriver = !currentInput.useK8sDriver
break
}

case BuildInfraMetaConfigTypes.DESCRIPTION: {
const { value } = data
currentInput.description = value
Expand Down Expand Up @@ -599,6 +605,20 @@ const useBuildInfraForm = ({
if (Object.keys(currentInputErrors[BuildInfraConfigTypes.NODE_SELECTOR] || {}).length === 0) {
currentInputErrors[BuildInfraConfigTypes.NODE_SELECTOR] = null
}

// Will validate all the keys since checking duplicates
const existingKeys = currentConfiguration[BuildInfraConfigTypes.NODE_SELECTOR].value.map(
(selector) => selector.key,
)

currentConfiguration[BuildInfraConfigTypes.NODE_SELECTOR].value.forEach((selector) => {
validateNodeSelector({
selector,
existingKeys,
profileInputErrors: currentInputErrors,
})
})

break
}

Expand Down Expand Up @@ -748,8 +768,8 @@ const useBuildInfraForm = ({
setProfileInputErrors(currentInputErrors)
}

const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault()
const handleSubmit = async (event?: FormEvent<HTMLFormElement>) => {
event?.preventDefault()
// Since considering '' as a valid error message
const hasErrors =
Object.keys(profileInputErrors).filter(
Expand All @@ -766,11 +786,11 @@ const useBuildInfraForm = ({

setLoadingActionRequest(true)
try {
if (editProfile) {
await updateBuildInfraProfile({ name, profileInput })
} else {
await createBuildInfraProfile({ profileInput })
}
await upsertBuildInfraProfile({
name,
profileInput,
canConfigureUseK8sDriver,
})
setLoadingActionRequest(false)
ToastManager.showToast({
variant: ToastVariantType.success,
Expand Down
11 changes: 2 additions & 9 deletions src/Pages/GlobalConfigurations/BuildInfra/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import {
BuildInfraLocators,
BuildInfraMetaConfigTypes,
ProfileInputErrorType,
BuildInfraProfileBase,
BuildInfraProfileVariants,
HandleProfileInputChangeType,
BuildInfraProfileAdditionalErrorKeysType,
BuildInfraAPIVersionType,
Expand Down Expand Up @@ -176,13 +174,6 @@ export const PROFILE_INPUT_ERROR_FIELDS = Object.fromEntries(
// fields required to be filled before submitting the form in create view, since we pre-populate the form with default values so no need in configs
export const CREATE_MODE_REQUIRED_INPUT_FIELDS = [BuildInfraMetaConfigTypes.NAME]

export const CREATE_PROFILE_BASE_VALUE: BuildInfraProfileBase = {
name: '',
description: '',
type: BuildInfraProfileVariants.NORMAL,
appCount: 0,
}

export const BUILD_INFRA_TEST_IDS = {
SUBMIT_BUTTON: 'build-infra-submit-button',
CANCEL_BUTTON: 'build-infra-cancel-button',
Expand Down Expand Up @@ -256,3 +247,5 @@ export const DEFAULT_TOLERANCE_OPERATOR = BuildInfraToleranceOperatorType.EQUALS
export const INFRA_CONFIG_NOT_SUPPORTED_BY_BUILD_X: Partial<Record<BuildInfraConfigTypes, true>> = {
[BuildInfraConfigTypes.BUILD_TIMEOUT]: true,
}

export const USE_BUILD_X_DRIVER_FALLBACK = true
36 changes: 22 additions & 14 deletions src/Pages/GlobalConfigurations/BuildInfra/services.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import {
BuildInfraProfileDTO,
BuildInfraProfileInfoDTO,
BuildInfraProfileResponseType,
CreateBuildInfraProfileType,
GetBuildInfraProfileType,
UpdateBuildInfraProfileType,
UpsertBuildInfraProfileServiceParamsType,
} from './types'

export const getBuildInfraProfileByName = async ({
name,
fromCreateView,
canConfigureUseK8sDriver,
}: GetBuildInfraProfileType): Promise<BuildInfraProfileResponseType> => {
try {
const profilePayload: Pick<BuildInfraProfileDTO['profile'], 'name'> = { name }
Expand All @@ -45,25 +45,33 @@ export const getBuildInfraProfileByName = async ({
defaultConfigurations,
profile,
fromCreateView,
canConfigureUseK8sDriver,
})
} catch (error) {
showError(error)
throw error
}
}

export const updateBuildInfraProfile = async ({ name, profileInput }: UpdateBuildInfraProfileType) => {
const updateProfilePayload: Pick<BuildInfraProfileDTO['profile'], 'name'> = { name }
const response = await put<ReturnType<typeof put>, BuildInfraProfileInfoDTO>(
getUrlWithSearchParams(getBuildInfraProfileEndpoint(), updateProfilePayload),
getBuildInfraProfilePayload(profileInput),
)
export const upsertBuildInfraProfile = async ({
name,
profileInput,
canConfigureUseK8sDriver,
}: UpsertBuildInfraProfileServiceParamsType) => {
const isEditView = !!name
const baseEndpoint = getBuildInfraProfileEndpoint()
const payload = getBuildInfraProfilePayload(profileInput, canConfigureUseK8sDriver)

if (isEditView) {
const updateProfileQueryPayload: Pick<BuildInfraProfileDTO['profile'], 'name'> = { name }
const response = await put<ReturnType<typeof put>, BuildInfraProfileInfoDTO>(
getUrlWithSearchParams(baseEndpoint, updateProfileQueryPayload),
payload,
)

return response
}

const response = await post<ReturnType<typeof post>, BuildInfraProfileInfoDTO>(baseEndpoint, payload)
return response
}

export const createBuildInfraProfile = async ({ profileInput }: CreateBuildInfraProfileType) =>
post<ReturnType<typeof post>, BuildInfraProfileInfoDTO>(
getBuildInfraProfileEndpoint(),
getBuildInfraProfilePayload(profileInput),
)
67 changes: 48 additions & 19 deletions src/Pages/GlobalConfigurations/BuildInfra/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,27 @@ export type BuildInfraConfigurationMapTypeWithoutDefaultFallback = {

export type BuildInfraConfigurationMapType = Record<BuildInfraConfigTypes, BuildInfraConfigurationType>

export enum BuildXDriverType {
KUBERNETES = 'kubernetes',
DOCKER_CONTAINER = 'docker-container',
}

interface BuildInfraProfileBaseDTO {
id?: number
name?: string
description: string
type: BuildInfraProfileVariants
appCount?: number
active?: boolean
/**
* @default `BuildXDriverType.KUBERNETES`
*/
buildxDriverType?: BuildXDriverType
}

export interface BuildInfraProfileBase extends BuildInfraProfileBaseDTO {}
export interface BuildInfraProfileBase extends Omit<BuildInfraProfileBaseDTO, 'buildxDriverType'> {
useK8sDriver?: boolean
}

export interface BuildInfraProfileInfoDTO extends BuildInfraProfileBaseDTO {
configurations: BuildInfraPlatformConfigurationMapDTO
Expand All @@ -247,20 +258,6 @@ export interface BuildInfraProfileData extends BuildInfraProfileBase {
configurations: Record<string, BuildInfraConfigurationMapType>
}

export interface GetBuildInfraProfileType {
name: string
fromCreateView?: boolean
}

export interface BuildInfraProfileResponseType {
configurationUnits: BuildInfraUnitsMapType | null
profile: BuildInfraProfileData | null
/**
* To be used in case user is creating configuration for new platform
*/
fallbackPlatformConfigurationMap: BuildInfraProfileData['configurations']
}

export interface UseBuildInfraFormProps {
/**
* Name of the profile, if not provided assumption would be for create view
Expand All @@ -274,6 +271,24 @@ export interface UseBuildInfraFormProps {
* If true, call this on form submission success
*/
handleSuccessRedirection?: () => void
/**
* @default false
*/
canConfigureUseK8sDriver?: boolean
}

export interface GetBuildInfraProfileType extends Pick<UseBuildInfraFormProps, 'canConfigureUseK8sDriver'> {
name: string
fromCreateView?: boolean
}

export interface BuildInfraProfileResponseType {
configurationUnits: BuildInfraUnitsMapType | null
profile: BuildInfraProfileData | null
/**
* To be used in case user is creating configuration for new platform
*/
fallbackPlatformConfigurationMap: BuildInfraProfileData['configurations']
}

export enum BuildInfraProfileAdditionalErrorKeysType {
Expand Down Expand Up @@ -322,6 +337,7 @@ interface NumericBuildInfraConfigPayloadType {

export enum BuildInfraProfileInputActionType {
ADD_TARGET_PLATFORM = 'add_target_platform',
TOGGLE_USE_K8S_DRIVER = 'toggle_use_k8s_driver',
REMOVE_TARGET_PLATFORM = 'remove_target_platform',
RENAME_TARGET_PLATFORM = 'rename_target_platform',
RESTORE_PROFILE_CONFIG_SNAPSHOT = 'restore_profile_config_snapshot',
Expand All @@ -336,6 +352,10 @@ export enum BuildInfraProfileInputActionType {
}

export type HandleProfileInputChangeType =
| {
action: BuildInfraProfileInputActionType.TOGGLE_USE_K8S_DRIVER
data?: never
}
| {
action: NumericBuildInfraConfigTypes
data: ProfileInputDispatchDataType & NumericBuildInfraConfigPayloadType
Expand Down Expand Up @@ -402,7 +422,7 @@ export interface UseBuildInfraFormResponseType {
profileInputErrors: ProfileInputErrorType
handleProfileInputChange: ({ action, data }: HandleProfileInputChangeType) => void
loadingActionRequest: boolean
handleSubmit: (e: FormEvent<HTMLFormElement>) => Promise<void>
handleSubmit: (e?: FormEvent<HTMLFormElement>) => Promise<void>
}

export interface BuildInfraConfigFormProps
Expand Down Expand Up @@ -476,8 +496,13 @@ export interface FooterProps {
loading?: boolean
}

export interface UpdateBuildInfraProfileType extends Pick<UseBuildInfraFormResponseType, 'profileInput'> {
name: string
export interface UpsertBuildInfraProfileServiceParamsType
extends Pick<UseBuildInfraFormResponseType, 'profileInput'>,
Pick<UseBuildInfraFormProps, 'canConfigureUseK8sDriver'> {
/**
* If not given would consider as create view
*/
name?: string
}

export interface CreateBuildInfraProfileType extends Pick<UseBuildInfraFormResponseType, 'profileInput'> {}
Expand Down Expand Up @@ -519,7 +544,8 @@ export interface BuildInfraProfileDTO extends BaseBuildInfraProfileDTO {

export interface BuildInfraProfileTransformerParamsType
extends BuildInfraProfileDTO,
Pick<GetBuildInfraProfileType, 'fromCreateView'> {}
Pick<GetBuildInfraProfileType, 'fromCreateView'>,
Pick<GetBuildInfraProfileType, 'canConfigureUseK8sDriver'> {}

export interface GetPlatformConfigurationsWithDefaultValuesParamsType {
profileConfigurationsMap: BuildInfraConfigurationMapTypeWithoutDefaultFallback
Expand All @@ -544,3 +570,6 @@ export interface ValidateNodeSelectorParamsType
selector: BuildInfraNodeSelectorValueType
existingKeys: string[]
}

export interface GetBaseProfileObjectParamsType
extends Pick<BuildInfraProfileTransformerParamsType, 'canConfigureUseK8sDriver' | 'fromCreateView' | 'profile'> {}
Loading
Loading