Form.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <template>
  2. <div class="w-75">
  3. <a-form :form="form.fc" v-bind="formItemLayout">
  4. <a-form-item :label="$t('k8s.text_41')">
  5. <a-input :placeholder="$t('k8s.text_60')" v-decorator="decorators.name" />
  6. </a-form-item>
  7. <a-form-item :label="$t('k8s.text_19')">
  8. <cluster-select v-decorator="decorators.cluster" @input="setCluster" />
  9. </a-form-item>
  10. <a-form-item :label="$t('k8s.text_23')">
  11. <namespace-select v-decorator="decorators.namespace" :cluster="cluster" @input="setNamespace" :namespaceObj.sync="namespaceObj" />
  12. </a-form-item>
  13. <a-form-item :label="$t('k8s.ingress.className')">
  14. <a-input v-decorator="decorators.className" :placeholder="$t('k8s.ingress.className.input')" @change="inputClassNameChange" />
  15. </a-form-item>
  16. <a-form-item :label="$t('k8s.text_14')" required>
  17. <ingress-rule :decorators="decorators.rule" :namespace="namespaceObj.name" :cluster="cluster" />
  18. </a-form-item>
  19. <div v-if="isNginxClass()">
  20. <a-form-item :label="$t('network.text_379')">
  21. <a-switch :disabled="disabled" v-decorator="decorators.stickySession.enabled" @change="stickySessionChange" />
  22. </a-form-item>
  23. <div v-if="stickySessionEnabled">
  24. <a-form-item :label="$t('k8s.ingress.nginx.session-cookie-name')">
  25. <a-input v-decorator="decorators.stickySession.name" />
  26. </a-form-item>
  27. <a-form-item :label="$t('k8s.ingress.nginx.session-cookie-expires')">
  28. <a-input v-decorator="decorators.stickySession.cookieExpires" type="number" />
  29. </a-form-item>
  30. </div>
  31. </div>
  32. </a-form>
  33. </div>
  34. </template>
  35. <script>
  36. import * as R from 'ramda'
  37. import IngressRule from './IngressRule'
  38. import ClusterSelect from '@K8S/sections/ClusterSelect'
  39. import NamespaceSelect from '@K8S/sections/NamespaceSelect'
  40. import k8sCreateMixin from '@K8S/mixins/create'
  41. const validateValidPath = (rule, value, callback) => {
  42. if (value.startsWith('/')) {
  43. callback()
  44. } else {
  45. callback(new Error(this.$t('k8s.text_131')))
  46. }
  47. }
  48. export default {
  49. name: 'K8sIngressCreate',
  50. components: {
  51. ClusterSelect,
  52. NamespaceSelect,
  53. IngressRule,
  54. },
  55. mixins: [k8sCreateMixin],
  56. data () {
  57. return {
  58. inputClassName: 'nginx',
  59. stickySessionEnabled: false,
  60. form: {
  61. fc: this.$form.createForm(this),
  62. },
  63. formItemLayout: {
  64. labelCol: { span: 4 },
  65. wrapperCol: { span: 20 },
  66. },
  67. errPanes: [], // 表单校验错误的tabs
  68. containerPanes: [], // 子组件同步的tabs
  69. decorators: {
  70. name: [
  71. 'name',
  72. {
  73. validateFirst: true,
  74. rules: [
  75. { required: true, message: this.$t('k8s.text_60') },
  76. { min: 2, max: 24, message: this.$t('k8s.text_132'), trigger: 'blur' },
  77. { validator: this.$validate('k8sName') },
  78. ],
  79. },
  80. ],
  81. cluster: [
  82. 'cluster',
  83. {
  84. initialValue: this.$store.state.common.k8s.cluster,
  85. rules: [
  86. { required: true, message: this.$t('k8s.text_30'), trigger: 'blur' },
  87. ],
  88. },
  89. ],
  90. namespace: [
  91. 'namespace',
  92. {
  93. initialValue: this.$store.state.common.k8s.namespace,
  94. rules: [
  95. { required: true, message: this.$t('k8s.text_61'), trigger: 'blur' },
  96. ],
  97. },
  98. ],
  99. className: [
  100. 'className',
  101. {
  102. initialValue: 'nginx',
  103. rules: [
  104. { required: true, message: this.$t('k8s.ingress.className.input'), trigger: 'blur' },
  105. ],
  106. },
  107. ],
  108. rule: {
  109. host: i => [
  110. `hosts[${i}]`,
  111. {
  112. rules: [
  113. { required: false, message: this.$t('k8s.text_205'), trigger: 'blur' },
  114. ],
  115. },
  116. ],
  117. services: i => ({
  118. serviceName: j => [
  119. `serviceNames[${i}][${j}]`,
  120. {
  121. rules: [
  122. { required: true, message: this.$t('k8s.text_206') },
  123. ],
  124. },
  125. ],
  126. servicePath: j => [
  127. `servicePaths[${i}][${j}]`,
  128. {
  129. validateFirst: true,
  130. rules: [
  131. { required: true, message: this.$t('k8s.text_237'), trigger: 'blur' },
  132. { validator: validateValidPath },
  133. ],
  134. },
  135. ],
  136. servicePort: j => [
  137. `servicePorts[${i}][${j}]`,
  138. {
  139. rules: [
  140. { type: 'number', required: true, message: this.$t('k8s.text_207'), trigger: 'blur' },
  141. ],
  142. },
  143. ],
  144. }),
  145. },
  146. stickySession: {
  147. enabled: [
  148. 'stickySession.enabled',
  149. {
  150. valuePropName: 'checked',
  151. },
  152. ],
  153. name: [
  154. 'stickySession.name',
  155. {
  156. initialValue: 'stickounet',
  157. },
  158. ],
  159. cookieExpires: [
  160. 'stickySession.cookieExpires',
  161. {
  162. initialValue: 1000,
  163. normalize: v => Number(v),
  164. rules: [
  165. { type: 'integer', min: 60, message: this.$t('k8s.ingress.nginx.session-cookie-expires'), trigger: 'blur' },
  166. ],
  167. },
  168. ],
  169. },
  170. },
  171. namespaceObj: {},
  172. }
  173. },
  174. methods: {
  175. inputClassNameChange (e) {
  176. this.inputClassName = e.target.value
  177. },
  178. isNginxClass () {
  179. return this.inputClassName === 'nginx'
  180. },
  181. stickySessionChange (val) {
  182. this.stickySessionEnabled = val
  183. },
  184. validateForm () {
  185. return new Promise((resolve, reject) => {
  186. this.form.fc.validateFieldsAndScroll({ scroll: { alignWithTop: true, offsetTop: 100 } }, (err, values) => {
  187. if (!err) {
  188. resolve(values)
  189. } else {
  190. reject(err)
  191. }
  192. })
  193. })
  194. },
  195. async _doCreate (data) {
  196. await new this.$Manager('ingresses', 'v1').create({ data })
  197. },
  198. async doCreate () {
  199. try {
  200. const values = await this.validateForm()
  201. const rules = []
  202. R.forEachObjIndexed((value, key) => {
  203. const item = {
  204. host: value,
  205. paths: [],
  206. }
  207. R.forEachObjIndexed((val, k) => {
  208. item.paths.push({
  209. path: val,
  210. backend: {
  211. serviceName: values.serviceNames[key][k],
  212. servicePort: values.servicePorts[key][k],
  213. },
  214. })
  215. }, values.servicePaths[key])
  216. rules.push(item)
  217. }, values.hosts)
  218. const params = {
  219. name: values.name,
  220. cluster: values.cluster,
  221. namespace: values.namespace,
  222. ingressClassName: values.className,
  223. stickySession: values.stickySession,
  224. rules,
  225. }
  226. await this._doCreate(params)
  227. this.$message.success(this.$t('k8s.text_46'))
  228. } catch (error) {
  229. throw error
  230. }
  231. },
  232. },
  233. }
  234. </script>