Skip to content

Commit

Permalink
Merge pull request #2879 from weili520/delete_project_quota_gpu
Browse files Browse the repository at this point in the history
fix: Cancel gpu field when edit project quota
  • Loading branch information
ks-ci-bot authored Dec 29, 2021
2 parents ade132d + ef407dd commit df7f53c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 89 deletions.
9 changes: 1 addition & 8 deletions src/actions/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ export default {
onOk: async data => {
const hard = get(data, 'spec.hard', {})

// set gpu parameters into requests and delete extra gpu parameters
const gpu = get(data, 'spec.gpu', {})
if (!isEmpty(gpu) && gpu.type !== '') {
set(data, `spec.hard["requests.${gpu.type}"]`, gpu.value)
}
data = omit(data, 'spec.gpu')

const params = {
name: data.name,
namespace: detail.name,
Expand Down Expand Up @@ -105,7 +98,7 @@ export default {
}

Modal.close(modal)

Notify.success({ content: t('UPDATE_SUCCESSFUL') })
success && success()
},
detail,
Expand Down
34 changes: 15 additions & 19 deletions src/components/Inputs/ResourceLimit/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
isFinite,
isEmpty,
isNaN,
omit,
isUndefined,
} from 'lodash'

Expand Down Expand Up @@ -193,35 +192,28 @@ export default class ResourceLimit extends React.Component {
}

static getGpuFromProps(value) {
const defaultGpuType = globals.config.supportGpuType[0]
const gpuValue = get(value, 'gpu.value', '')
const gpuType =
get(value, 'gpu.type', '') === ''
? defaultGpuType
: get(value, 'gpu.type')
// get gpu config from requests field
const supportGpuType = globals.config.supportGpuType
if (!value) {
return {
type: defaultGpuType,
type: supportGpuType[0],
value: '',
}
}
const types = Object.keys(value.requests).filter(key =>
supportGpuType.some(item => key.endsWith(item))
)
const type = !isEmpty(types) ? types[0] : supportGpuType[0]
return {
value: gpuValue,
type: gpuType,
type,
value: !isEmpty(types) ? value.requests[`${type}`] : '',
}
}

static gpuSetting(props) {
const value = get(props, 'value', {})
const defaultValue = get(props, 'defaultValue', {})
if (!isEmpty(value)) {
const gpuInfo = omit(value.limits, ['cpu', 'memory'])
if (!isEmpty(gpuInfo)) {
return {
type: Object.keys(gpuInfo)[0],
value: Object.values(gpuInfo)[0],
}
}
return ResourceLimit.getGpuFromProps(value)
}
if (!isEmpty(defaultValue)) {
Expand Down Expand Up @@ -457,7 +449,10 @@ export default class ResourceLimit extends React.Component {
if (limits.memory > 0 && limits.memory < Infinity) {
set(result, 'limits.memory', `${limits.memory}${memoryUnit}`)
}
set(result, 'gpu', gpu)

// pass gpu input config into limits and requests field
set(result, 'limits', { ...result.limits, [`${gpu.type}`]: gpu.value })
set(result, 'requests', { ...result.requests, [`${gpu.type}`]: gpu.value })

onChange(result)
}
Expand Down Expand Up @@ -542,9 +537,10 @@ export default class ResourceLimit extends React.Component {
renderGpuTip = () => {
const { workspaceLimitProps: pWL } = this.props
const { gpu } = this.state
const findResult = pWL.gpuLimit.filter(item => {
const findResult = pWL?.gpuLimit.filter(item => {
return isEmpty(item) ? item : Object.keys(item)[0].endsWith(gpu.type)
})[0]

return (
<div className={styles.message}>
<span>{t('GPU_TYPE')}:</span>
Expand Down
75 changes: 35 additions & 40 deletions src/components/Modals/QuotaEdit/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,7 @@
import React from 'react'
import PropTypes from 'prop-types'
import { toJS } from 'mobx'
import {
get,
set,
isEmpty,
mergeWith,
add,
omitBy,
endsWith,
pickBy,
} from 'lodash'
import { get, set, isEmpty, mergeWith, add, omitBy, endsWith } from 'lodash'
import { Form, Input } from '@kube-design/components'
import { Modal } from 'components/Base'
import { ResourceLimit } from 'components/Inputs'
Expand Down Expand Up @@ -106,7 +97,7 @@ export default class QuotaEditModal extends React.Component {
const storeDetail = toJS(this.store.detail)

this.setState({
formTemplate: this.cancelGpuSetting(storeDetail),
formTemplate: storeDetail,
})
}

Expand Down Expand Up @@ -170,6 +161,18 @@ export default class QuotaEditModal extends React.Component {
}
: {}

// get gpu config form spec.hard field and
// pass it to resourceLimit component
const supportGpu = globals.config.supportGpuType
const hard = get(formTemplate, 'spec.hard', {})
const whatTypeGpu = supportGpu.filter(type =>
Object.keys(hard).some(key => endsWith(key, type))
)
const gpuSetting = !isEmpty(whatTypeGpu)
? {
[`${whatTypeGpu[0]}`]: hard[`requests.${whatTypeGpu[0]}`],
}
: {}
return {
cpuProps: {
marks: [
Expand Down Expand Up @@ -209,8 +212,8 @@ export default class QuotaEditModal extends React.Component {
requests: {
cpu: get(formTemplate, 'spec.hard["requests.cpu"]'),
memory: get(formTemplate, 'spec.hard["requests.memory"]'),
...gpuSetting,
},
gpu: get(formTemplate, 'spec.gpu'),
},
workspaceLimitProps,
onChange: value => {
Expand All @@ -234,7 +237,26 @@ export default class QuotaEditModal extends React.Component {
'spec.hard["requests.memory"]',
get(value, 'requests.memory', null)
)
set(formTemplate, `spec.gpu`, get(value, 'gpu'))
const supportGpuArr = globals.config.supportGpuType
// exclude Gpu fields
const oldHard = get(formTemplate, 'spec.hard', {})
const noGpuHard = omitBy(oldHard, (_, key) =>
supportGpuArr.some(type => key.endsWith(type))
)
set(formTemplate, 'spec.hard', noGpuHard)

// set gpu config into hard field
const incomeGpu = Object.keys(get(value, 'requests', {})).filter(key =>
supportGpuArr.some(type => key.endsWith(type))
)
if (!isEmpty(incomeGpu)) {
const type = incomeGpu[0]
set(
formTemplate,
`spec.hard["requests.${type}"]`,
value.requests[`${type}`]
)
}
},
onError: error => {
this.setState({ error })
Expand All @@ -243,33 +265,6 @@ export default class QuotaEditModal extends React.Component {
}
}

cancelGpuSetting = formTemplate => {
const hard = get(formTemplate, 'spec.hard', {})
const supportGpu = globals.config.supportGpuType
const filterGpu = omitBy(hard, (_, key) =>
supportGpu.some(type => endsWith(key, type))
)
set(formTemplate, 'spec.hard', filterGpu)
if (!isEmpty(hard)) {
const gpu = pickBy(hard, (_, key) =>
supportGpu.some(type => endsWith(key, type))
)
if (!isEmpty(gpu)) {
const type = Object.keys(gpu)[0].split('.')
set(formTemplate, 'spec.gpu', {
type: type.slice(1).join('.'),
value: Object.values(gpu)[0],
})
}
} else {
set(formTemplate, 'spec.gpu', {
type: '',
value: '',
})
}
return formTemplate
}

render() {
const {
detail,
Expand Down
4 changes: 2 additions & 2 deletions src/stores/federated.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import { set, get, keyBy, findKey, cloneDeep } from 'lodash'
import { action, observable } from 'mobx'
import { withDryRun, getGpuFromRes } from 'utils'
import { withDryRun, LimitsEqualRequests } from 'utils'
import ObjectMapper from 'utils/object.mapper'
import { MODULE_KIND_MAP } from 'utils/constants'

Expand Down Expand Up @@ -223,7 +223,7 @@ export default class FederatedStore extends Base {
...ObjectMapper.federated(this.mapper)(item),
}))

getGpuFromRes(data)
LimitsEqualRequests(data)

this.list.update({
data,
Expand Down
4 changes: 2 additions & 2 deletions src/stores/limitrange.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import Base from 'stores/base'
import { action } from 'mobx'
import { getGpuFromRes } from 'utils'
import { LimitsEqualRequests } from 'utils'

export default class LimitRangeStore extends Base {
module = 'limitranges'
Expand All @@ -43,7 +43,7 @@ export default class LimitRangeStore extends Base {
...this.mapper(item),
}))

getGpuFromRes(data)
LimitsEqualRequests(data)

this.list.update({
data,
Expand Down
19 changes: 1 addition & 18 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ export const omitJobGpuLimit = (data, path) => {
}
}

export const getGpuFromRes = data => {
export const LimitsEqualRequests = data => {
if (data.length > 0) {
const limits = get(data, '[0].limit.default', {})
const requests = get(data, '[0].limit.defaultRequest', {})
Expand All @@ -711,23 +711,6 @@ export const getGpuFromRes = data => {
if (limitItem('memory') === reqItem('memory')) {
set(data[0].limit, 'defaultRequest.memory', undefined)
}
const gpu = isEmpty(limits) ? {} : omit(limits, ['cpu', 'memory'])
const gpuKey = Object.keys(gpu)[0]
if (isEmpty(gpu)) {
set(data[0].limit, 'gpu', {
type: supportGpuType[0],
value: '',
})
} else {
data[0] = omit(data[0], [
`limit.default['${gpuKey}']`,
`limit.defaultRequest['${gpuKey}']`,
])
set(data[0].limit, 'gpu', {
type: gpuKey,
value: Object.values(gpu)[0],
})
}
}
}

Expand Down

0 comments on commit df7f53c

Please sign in to comment.