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

fix: Delete gpu field when creating workload #2895

Merged
merged 1 commit into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 2 additions & 14 deletions src/actions/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@
import { get, set, isEmpty, omit } from 'lodash'
import { Notify } from '@kube-design/components'
import { Modal } from 'components/Base'
import { mergeLabels, updateFederatedAnnotations, omitJobGpuLimit } from 'utils'
import { mergeLabels, updateFederatedAnnotations } from 'utils'
import FORM_TEMPLATES from 'utils/form.templates'
import {
MODULE_KIND_MAP,
MAPPER_GPU_SPEC_PATH,
OMIT_TOTAL_REPLICAS,
} from 'utils/constants'
import { MODULE_KIND_MAP, OMIT_TOTAL_REPLICAS } from 'utils/constants'

import ROUTER_FORM_STEPS from 'configs/steps/ingresses'

Expand Down Expand Up @@ -57,14 +53,6 @@ export default {
onOk: data => {
const deployments = omit(data, ['application', 'ingress'])
Object.keys(deployments).forEach(name => {
omitJobGpuLimit(
data,
`${name}.${MAPPER_GPU_SPEC_PATH.app_deployment}`
)
omitJobGpuLimit(
data,
`${name}.${MAPPER_GPU_SPEC_PATH.app_workload}`
)
data = omit(data, OMIT_TOTAL_REPLICAS(name))
})
store.create(data, { cluster, namespace }).then(() => {
Expand Down
20 changes: 3 additions & 17 deletions src/actions/federated.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with KubeSphere Console. If not, see <https://www.gnu.org/licenses/>.
*/

import { get, set, unset, cloneDeep, uniqBy, isEmpty, omit } from 'lodash'
import { get, set, unset, cloneDeep, uniqBy, isEmpty } from 'lodash'
import { Notify } from '@kube-design/components'
import { Modal } from 'components/Base'
import FedProjectCreateModal from 'components/Modals/FedProjectCreate'
Expand All @@ -26,7 +26,7 @@ import FedProjectAddClusterModal from 'workspaces/components/Modals/FedProjectAd
import DeleteModal from 'components/Modals/Delete'
import FORM_TEMPLATES from 'utils/form.templates'
import FED_TEMPLATES from 'utils/fed.templates'
import { multiCluster_overrides_gpu } from 'utils'
import { multiCluster_overrides_Dot } from 'utils'

import FederatedStore from 'stores/federated'
import ProjectStore from 'stores/project'
Expand Down Expand Up @@ -135,22 +135,8 @@ export default {
on({ store, detail, success, module, supportGpuSelect = false, ...props }) {
const modal = Modal.open({
onOk: data => {
const containers = get(
data,
'spec.template.spec.template.spec.containers',
[]
)
const newContainers = containers.map(item =>
omit(item, 'resources.gpu')
)
set(
data,
'spec.template.spec.template.spec.containers',
newContainers
)

const overrides = get(data, 'spec.overrides', [])
multiCluster_overrides_gpu(overrides)
multiCluster_overrides_Dot(overrides)

const customMode = get(data, 'spec.template.spec.customMode', {})
if (!isEmpty(customMode)) {
Expand Down
21 changes: 4 additions & 17 deletions src/actions/workload.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import { get, isEmpty, omit } from 'lodash'
import { toJS } from 'mobx'
import { withProps, omitJobGpuLimit, multiCluster_overrides_gpu } from 'utils'
import { withProps, multiCluster_overrides_Dot } from 'utils'
import { Notify } from '@kube-design/components'
import { Modal } from 'components/Base'

Expand All @@ -30,11 +30,7 @@ import EditConfigTemplateModal from 'projects/components/Modals/ConfigTemplate'
import EditServiceModal from 'projects/components/Modals/ServiceSetting/StatefulSet'
import ClusterDiffSettings from 'components/Forms/Workload/ClusterDiffSettings'
import DeleteModal from 'projects/components/Modals/WorkloadDelete'
import {
MODULE_KIND_MAP,
MAPPER_GPU_SPEC_PATH,
OMIT_TOTAL_REPLICAS,
} from 'utils/constants'
import { MODULE_KIND_MAP, OMIT_TOTAL_REPLICAS } from 'utils/constants'
import FORM_TEMPLATES from 'utils/form.templates'
import formPersist from 'utils/form.persist'
import DEPLOYMENTS_FORM_STEPS from 'configs/steps/deployments'
Expand Down Expand Up @@ -120,23 +116,15 @@ export default {
const modal = Modal.open({
onOk: newObject => {
if (isFederated) {
MAPPER_GPU_SPEC_PATH[kind] &&
omitJobGpuLimit(
newObject[kind],
MAPPER_GPU_SPEC_PATH[`Federate_${kind}`]
)
if (module === 'deployments') {
multiCluster_overrides_gpu(
multiCluster_overrides_Dot(
get(newObject, 'Deployment.spec.overrides', [])
)
} else if (module === 'statefulsets') {
multiCluster_overrides_gpu(
multiCluster_overrides_Dot(
get(newObject, 'StatefulSet.spec.overrides', [])
)
}
} else {
MAPPER_GPU_SPEC_PATH[kind] &&
omitJobGpuLimit(newObject[kind], MAPPER_GPU_SPEC_PATH[kind])
}
newObject = omit(newObject, OMIT_TOTAL_REPLICAS(kind))
let data = newObject
Expand Down Expand Up @@ -273,7 +261,6 @@ export default {
on({ store, detail, success, supportGpuSelect = false, ...props }) {
const modal = Modal.open({
onOk: data => {
omitJobGpuLimit(data, 'spec.template.spec.containers')
const customMode = get(data, 'spec.template.spec.customMode', {})

if (!isEmpty(customMode)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export default class ContainerSetting extends React.Component {
return {
requests: limitRange.defaultRequest || {},
limits: limitRange.default || {},
gpu: limitRange.gpu || {},
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/components/Forms/Workload/ContainerSettings/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
endsWith,
} from 'lodash'
import React from 'react'
import { generateId, getContainerGpu, resourceLimitKey } from 'utils'
import { generateId, cancelContainerDot, resourceLimitKey } from 'utils'
import { MODULE_KIND_MAP } from 'utils/constants'
import { getLeftQuota } from 'utils/workload'

Expand Down Expand Up @@ -518,11 +518,11 @@ export default class ContainerSetting extends React.Component {
})

_initContainers.forEach(item => {
getContainerGpu(item)
cancelContainerDot(item)
})

_containers.forEach(item => {
getContainerGpu(item)
cancelContainerDot(item)
})

set(this.fedFormTemplate, `${this.prefix}spec.containers`, _containers)
Expand Down
14 changes: 1 addition & 13 deletions src/pages/fedprojects/components/ContainerSetting/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
Columns,
Column,
} from '@kube-design/components'
import { omit, isEmpty, get } from 'lodash'
import { isEmpty, get } from 'lodash'
import { ResourceLimit } from 'components/Inputs'
import ToggleView from 'components/ToggleView'

Expand All @@ -39,26 +39,14 @@ import styles from './index.scss'
export default class ContainerSetting extends Base {
get defaultResourceLimit() {
const { limitRanges = {} } = this.props
const gpu = {
type: '',
value: '',
}

if (!limitRanges.limits && !limitRanges.requests) {
return undefined
}

const gpuInfo = omit(limitRanges.requests, ['cpu', 'memory'])

if (!isEmpty(gpuInfo)) {
gpu.type = Object.keys(gpuInfo)[0]
gpu.value = Object.values(gpuInfo)[0]
}

return {
requests: limitRanges.requests || {},
limits: limitRanges.limits || {},
gpu,
}
}

Expand Down
11 changes: 0 additions & 11 deletions src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -1111,17 +1111,6 @@ export const NODE_ROLE_TAG_TYPE = {
worker: 'default',
}

export const MAPPER_GPU_SPEC_PATH = {
Deployment: 'spec.template.spec.containers',
StatefulSet: 'spec.template.spec.containers',
Job: 'spec.template.spec.containers',
CronJob: 'spec.jobTemplate.spec.template.spec.containers',
Federate_Deployment: 'spec.template.spec.template.spec.containers',
Federate_StatefulSet: 'spec.template.spec.template.spec.containers',
app_deployment: 'Deployment.spec.template.spec.containers',
app_workload: 'workload.spec.template.spec.containers',
}

export const OMIT_TOTAL_REPLICAS = kind => [
`${kind}.spec.template.totalReplicas`,
'totalReplicas',
Expand Down
45 changes: 2 additions & 43 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
isEmpty,
trimStart,
isNumber,
omit,
pick,
pickBy,
endsWith,
Expand Down Expand Up @@ -658,47 +657,15 @@ const deal_With_Dot = hard => {
cancel_Num_Dot(cpuAndMemory, hard)
}

export const getContainerGpu = item => {
export const cancelContainerDot = item => {
if (!isEmpty(get(item, 'resources', {}))) {
const gpu = get(item, 'resources.gpu', { type: '', value: '' })
item.resources.limits = pick(item.resources.limits, ['cpu', 'memory'])
if (gpu.type !== '' && gpu.value !== '') {
const value = isUndefined(gpu.value) ? '' : gpu.value
set(item, `resources.limits["${gpu.type}"]`, value)
set(item, `resources.requests["${gpu.type}"]`, value)
}
const cpuAndMemory = pick(item.resources, ['requests', 'limits'])
Object.keys(cpuAndMemory).forEach(key => {
cancel_Num_Dot(cpuAndMemory[key], item.resources[key])
})
}
}

export const omitJobGpuLimit = (data, path) => {
const containers = get(data, path, [])
if (containers.length > 0) {
const newContainer = containers.map(item => {
const gpu = get(item, 'resources.gpu', {})
if (isEmpty(gpu.type)) {
return item
}
if (
isEmpty(gpu.type) ||
isEmpty(gpu.value) ||
isUndefined(gpu.type) ||
isUndefined(gpu.value)
) {
const limits = get(item, 'resources.limits', {})
const requests = get(item, 'resources.requests', {})
set(item, 'resources.limits', omit(limits, `${gpu.type}`))
set(item, 'resources.requests', omit(requests, `${gpu.type}`))
}
return omit(item, 'resources.gpu')
})
set(data, path, newContainer)
}
}

export const LimitsEqualRequests = data => {
if (data.length > 0) {
const limits = get(data, '[0].limit.default', {})
Expand Down Expand Up @@ -733,16 +700,10 @@ export const limits_Request_EndsWith_Dot = ({ limits, requests }) => {
return { limits: result[0], requests: result[1] }
}

export const multiCluster_overrides_gpu = overrides => {
export const multiCluster_overrides_Dot = overrides => {
overrides.forEach(clusterOverride => {
clusterOverride.clusterOverrides.forEach(item => {
if (item.path.endsWith('resources')) {
const gpu = get(item.value, 'gpu', {})
if (!isEmpty(gpu) && gpu.type !== '' && gpu.value !== '') {
set(item.value, `limits["${gpu.type}"]`, gpu.value)
set(item.value, `requests["${gpu.type}"]`, gpu.value)
}
item.value = omit(item.value, 'gpu')
Object.keys(item.value).forEach(key => {
cancel_Num_Dot(item.value[key], item.value[key])
})
Expand All @@ -758,8 +719,6 @@ export const resourceLimitKey = [
'requests.memory',
]

export const supportGpuType = ['nvidia.com/gpu']

const accessModeMapper = {
ReadWriteOnce: 'RWO',
ReadOnlyMany: 'ROX',
Expand Down