Skip to content

Commit

Permalink
fix: Fix workload name in service create
Browse files Browse the repository at this point in the history
Signed-off-by: leoliu <leoliu@yunify.com>
  • Loading branch information
leoliu committed Jul 23, 2020
1 parent fd00007 commit ad8e780
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 51 deletions.
2 changes: 1 addition & 1 deletion server/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ client:
title: Log Collections,
icon: file,
authKey: logging|events|auditing,
clusterModule: logging,
clusterModule: logging|events|auditing,
}

# access control page navigations
Expand Down
1 change: 1 addition & 0 deletions src/components/Forms/GrayRelease/Version/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export default class Version extends ContainerSettings {
name="metadata.labels.version"
onChange={this.handleVersionChange}
disabled={this.isEdit}
maxLength={16}
/>
</Form.Item>
<div className={styles.specify}>
Expand Down
117 changes: 78 additions & 39 deletions src/components/Forms/Service/BaseInfo/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@
* along with KubeSphere Console. If not, see <https://www.gnu.org/licenses/>.
*/

import { get, set, debounce } from 'lodash'
import { get, set } from 'lodash'
import React from 'react'
import { observer } from 'mobx-react'
import { Columns, Column, Input } from '@pitrix/lego-ui'
import { Form, TextArea } from 'components/Base'
import { updateLabels, updateFederatedAnnotations, generateId } from 'utils'
import { updateLabels } from 'utils'
import { ProjectSelect } from 'components/Inputs'

import { PATTERN_SERVICE_NAME, MODULE_KIND_MAP } from 'utils/constants'
import {
PATTERN_SERVICE_NAME,
PATTERN_SERVICE_VERSION,
MODULE_KIND_MAP,
} from 'utils/constants'

import ServiceStore from 'stores/service'
import WorkloadStore from 'stores/workload'
import FederatedStore from 'stores/federated'

@observer
Expand All @@ -40,8 +45,10 @@ export default class ServiceBaseInfo extends React.Component {
}

this.store = new ServiceStore()
this.workloadStore = new WorkloadStore(props.module)
if (props.isFederated) {
this.store = new FederatedStore(this.store)
this.workloadStore = new FederatedStore(this.workloadStore)
}
}

Expand Down Expand Up @@ -90,21 +97,11 @@ export default class ServiceBaseInfo extends React.Component {

updateWorkload(value) {
const { module, formTemplate, isFederated } = this.props
const aliasName = get(
this.formTemplate,
'metadata.annotations["kubesphere.io/alias-name"]'
)
const labels = get(this.formTemplate, 'metadata.labels', {})
const namespace = get(this.formTemplate, 'metadata.namespace')
const workloadName = `${value}-${generateId()}`
const workloadName = `${value}-${labels.version}`
set(formTemplate[this.workloadKind], 'metadata.name', workloadName)
set(formTemplate[this.workloadKind], 'metadata.namespace', namespace)
set(
formTemplate[this.workloadKind],
'metadata.annotations["kubesphere.io/alias-name"]',
aliasName || value
)

set(formTemplate[this.workloadKind], 'metadata.labels.app', value)
updateLabels(
isFederated
Expand Down Expand Up @@ -134,24 +131,6 @@ export default class ServiceBaseInfo extends React.Component {
)
}

handleAliasNameChange = debounce(value => {
const { formTemplate, noWorkload } = this.props

const name = get(this.formTemplate, 'metadata.name')

if (!noWorkload) {
set(
formTemplate[this.workloadKind],
'metadata.annotations["kubesphere.io/alias-name"]',
value || name
)
}

if (this.props.isFederated) {
updateFederatedAnnotations(this.formTemplate)
}
}, 200)

nameValidator = (rule, value, callback) => {
if (!value) {
return callback()
Expand All @@ -171,8 +150,48 @@ export default class ServiceBaseInfo extends React.Component {
})
}

versionValidator = (rule, value, callback) => {
if (!value) {
return callback()
}

const serviceName = get(this.formTemplate, 'metadata.name')
const name = `${serviceName}-${value}`
this.workloadStore
.checkName({
name,
namespace: this.namespace,
cluster: this.props.cluster,
})
.then(resp => {
if (resp.exist) {
return callback({
message: t(
`${t(this.workloadKind)} ${name} ${t('exists')}, ${t(
'version number is invalid'
)}`
),
field: rule.field,
})
}
callback()
})
}

handleVersionChange = value => {
const serviceName = get(this.formTemplate, 'metadata.name')
const workloadName = `${serviceName}-${value}`
const { formTemplate } = this.props
set(formTemplate[this.workloadKind], 'metadata.name', workloadName)
set(
formTemplate.Service,
'metadata.annotations["kubesphere.io/workloadName"]',
workloadName
)
}

render() {
const { formRef } = this.props
const { formRef, noWorkload, cluster, namespace } = this.props

return (
<Form data={this.formTemplate} ref={formRef}>
Expand Down Expand Up @@ -200,15 +219,12 @@ export default class ServiceBaseInfo extends React.Component {
</Column>
<Column>
<Form.Item label={t('Alias')} desc={t('ALIAS_DESC')}>
<Input
name="metadata.annotations['kubesphere.io/alias-name']"
onChange={this.handleAliasNameChange}
/>
<Input name="metadata.annotations['kubesphere.io/alias-name']" />
</Form.Item>
</Column>
</Columns>
<Columns>
{!this.props.namespace && (
{!namespace && (
<Column>
<Form.Item
label={t('Project')}
Expand All @@ -219,12 +235,35 @@ export default class ServiceBaseInfo extends React.Component {
>
<ProjectSelect
name="metadata.namespace"
cluster={this.props.cluster}
cluster={cluster}
defaultValue={this.namespace}
/>
</Form.Item>
</Column>
)}
{!noWorkload && (
<Column>
<Form.Item
label={t('Version')}
desc={t('COMPONENT_VERSION_DESC')}
rules={[
{ required: true, message: t('COMPONENT_VERSION_DESC') },
{
pattern: PATTERN_SERVICE_VERSION,
message: t('COMPONENT_VERSION_DESC'),
},
{ validator: this.versionValidator },
]}
>
<Input
name="metadata.labels.version"
defaultValue="v1"
maxLength={16}
onChange={this.handleVersionChange}
/>
</Form.Item>
</Column>
)}
<Column>
<Form.Item label={t('Description')} desc={t('DESCRIPTION_DESC')}>
<TextArea
Expand Down
5 changes: 4 additions & 1 deletion src/components/Forms/Workload/ContainerSettings/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ export default class ContainerSetting extends React.Component {
let serviceName = get(this.props.formTemplate, 'Service.metadata.name')

if (workloadName && !serviceName) {
serviceName = `svc-${workloadName.slice(0, 54)}-${generateId(4)}`
serviceName = `${workloadName.slice(0, 57)}-${generateId(4)}`
if (!/^[a-z]/.test(serviceName)) {
serviceName = `s${serviceName}`
}

set(this.props.formTemplate, 'Service.metadata.name', serviceName)
set(
Expand Down
4 changes: 3 additions & 1 deletion src/core/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ export default class GlobalValue {

if (
item.clusterModule &&
!this.hasClusterModule(item.cluster, item.clusterModule)
item.clusterModule
.split('|')
.every(cm => !this.hasClusterModule(item.cluster, cm))
) {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion src/locales/en/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export default {
'It can only contain lowercase letters, numbers and hyphens("-"), and must begin and end with a lowercase letter or a number. The maximum length of characters is set to 14.',

COMPONENT_VERSION_DESC:
'For the application of governance to distinguish between components. It can only contain lowercase letters and numbers. The maximum length of characters is set to 16.',
'It can only contain lowercase letters and numbers. The maximum length of characters is set to 16.',

APP_WORKLOAD_TYPE_DESC:
'Support stateless services (Deployment) and stateful services (StatefulSet)',
Expand Down
3 changes: 1 addition & 2 deletions src/locales/tc/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ export default {
APP_GOVERNANCE_DESC:
'開啟應用治理後會在每個組件中以 SideCar 的方式注入 Istio-proxy 容器 <a href="https://istio.io/docs/setup/kubernetes/additional-setup/sidecar-injection/" target="_blank">了解更多</a>',

COMPONENT_VERSION_DESC:
'用於應用治理時區分組件版本, 最長 16 個字元,只能包含小寫字母及數字',
COMPONENT_VERSION_DESC: '最長 16 個字元,只能包含小寫字母及數字',

APP_ICON_TIP: '點擊上傳應用圖示,尺寸最大為 120px * 120px',

Expand Down
3 changes: 1 addition & 2 deletions src/locales/zh/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ export default {
APP_GOVERNANCE_DESC:
'开启应用治理后会在每个组件中以 SideCar 的方式注入 Istio-proxy 容器 <a href="https://istio.io/docs/setup/kubernetes/additional-setup/sidecar-injection/" target="_blank">了解更多</a>',

COMPONENT_VERSION_DESC:
'用于应用治理时区分组件版本, 最长 16 个字符,只能包含小写字母及数字',
COMPONENT_VERSION_DESC: '最长 16 个字符,只能包含小写字母及数字',

APP_ICON_TIP: '点击上传应用图标,尺寸最大为 120px * 120px',

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default class GatewaySettingModal extends React.Component {
const { cluster, namespace, selector } = detail || {}

const url = `api/v1/watch/${
cluster ? `klusters/${cluster}` : ''
cluster ? `klusters/${cluster}/` : ''
}namespaces/${namespace}/pods?labelSelector=${joinSelector(selector)}`

if (url && namespace && selector) {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/projects/components/WorkloadStatus/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default function WorkloadStatus({ data, module }) {
type={statusResult}
name={t(statusResult)}
total={get(data, 'status.desiredNumberScheduled', 0)}
ready={get(data, 'status.numberReady', 0)}
ready={get(data, 'status.numberAvailable', 0)}
flicker
/>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ export const GRAY_RELEASE_CATEGORIES = [

export const PATTERN_NAME = /^[a-z0-9]([-a-z0-9]*[a-z0-9])?$/
export const PATTERN_SERVICE_NAME = /^[a-z]([-a-z0-9]*[a-z0-9])?$/
export const PATTERN_SERVICE_VERSION = /^[a-z0-9]*$/
export const PATTERN_LABEL = /(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?/
export const PATTERN_PASSWORD = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{6,}$/
export const PATTERN_IMAGE = /^\S+$/
Expand Down
4 changes: 2 additions & 2 deletions src/utils/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ export const getStatefulSetStatus = ({ spec = {}, status = {} }) => {
}

export const getDaemonSetStatus = ({ status }) => {
if (status.numberReady === 0) {
if (status.numberAvailable === 0) {
return 'Stopped'
}
if (status.numberReady === status.desiredNumberScheduled) {
if (status.numberAvailable === status.desiredNumberScheduled) {
return 'Running'
}
return 'Updating'
Expand Down

0 comments on commit ad8e780

Please sign in to comment.