SetImage.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <template>
  2. <base-dialog width="1200px" @cancel="cancelDialog">
  3. <div slot="header">{{$t('k8s.text_63')}}</div>
  4. <div slot="body">
  5. <dialog-selected-tips :count="params.data.length" :name="$t('k8s.text_64')" :action="$t('k8s.text_63')" />
  6. <dialog-table :data="params.data" :columns="params.columns.slice(0, 3)" />
  7. <a-form
  8. v-bind="formItemLayout"
  9. :form="form.fc">
  10. <a-alert :message="$t('k8s.text_65')" banner v-if="!containerImages.length && !initContainerImages.length" />
  11. <a-form-item :label="$t('k8s.repo.image.source')">
  12. <a-radio-group v-decorator="decorators.source">
  13. <a-radio-button value="custom">{{ $t('k8s.repo.image.custom') }}</a-radio-button>
  14. <a-radio-button value="registry">{{ $t('k8s.repo.image.registry') }}</a-radio-button>
  15. </a-radio-group>
  16. </a-form-item>
  17. <a-form-item :label="$t('k8s.text_66')" v-if="initContainerImages.length">
  18. <a-form-item v-for="(item, i) in initContainerImages" :key="i">
  19. <mirror-registry v-show="form.fd.source === 'registry'" v-decorator="decorators.registryImage(i)" :label="item.name" />
  20. <a-input v-show="form.fd.source !== 'registry'" v-decorator="decorators.initImage(i)" :placeholder="$t('k8s.text_67')" :addonBefore="item.name" />
  21. </a-form-item>
  22. </a-form-item>
  23. <a-form-item :label="$t('k8s.text_42')" v-if="containerImages.length">
  24. <a-form-item v-for="(item, i) in containerImages" :key="i">
  25. <mirror-registry v-show="form.fd.source === 'registry'" v-decorator="decorators.registryImage(i)" :label="item.name" />
  26. <a-input v-show="form.fd.source !== 'registry'" v-decorator="decorators.image(i)" :placeholder="$t('k8s.text_67')" :addonBefore="item.name" />
  27. </a-form-item>
  28. </a-form-item>
  29. </a-form>
  30. </div>
  31. <div slot="footer">
  32. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  33. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  34. </div>
  35. </base-dialog>
  36. </template>
  37. <script>
  38. import * as R from 'ramda'
  39. import MirrorRegistry from '@K8S/sections/MirrorRegistry'
  40. import DialogMixin from '@/mixins/dialog'
  41. import WindowsMixin from '@/mixins/windows'
  42. import expectStatus from '@/constants/expectStatus'
  43. import { removeHttp } from '@/utils/url'
  44. export default {
  45. name: 'K8SSetImageDialog',
  46. components: {
  47. MirrorRegistry,
  48. },
  49. mixins: [DialogMixin, WindowsMixin],
  50. data () {
  51. return {
  52. loading: false,
  53. form: {
  54. fc: this.$form.createForm(this, {
  55. onValuesChange: (props, values) => {
  56. Object.keys(values).forEach(key => {
  57. this.$set(this.form.fd, key, values[key])
  58. })
  59. },
  60. }),
  61. fd: {},
  62. },
  63. data: this.params.data[0],
  64. containerImages: [],
  65. initContainerImages: [],
  66. decorators: {
  67. source: [
  68. 'source',
  69. {
  70. initialValue: 'custom',
  71. },
  72. ],
  73. registryImage: i => [
  74. `registryImages${i}`,
  75. {
  76. rules: [
  77. { required: true, message: this.$t('common.tips.select', [this.$t('k8s.repo.image.registry')]) },
  78. ],
  79. },
  80. ],
  81. image: i => [
  82. `images${i}`,
  83. {
  84. rules: [
  85. { required: true, message: this.$t('k8s.text_67') },
  86. ],
  87. },
  88. ],
  89. initImage: i => [
  90. `initImages${i}`,
  91. {
  92. rules: [
  93. { required: true, message: this.$t('k8s.text_67') },
  94. ],
  95. },
  96. ],
  97. },
  98. formItemLayout: {
  99. wrapperCol: {
  100. span: 21,
  101. },
  102. labelCol: {
  103. span: 3,
  104. },
  105. },
  106. }
  107. },
  108. created () {
  109. this.fetchData()
  110. },
  111. methods: {
  112. async fetchData () {
  113. const { data } = await this.params.onManager('get', {
  114. managerArgs: {
  115. id: this.data.id,
  116. params: {
  117. cluster: this.data.cluster_id,
  118. namespace: this.data.namespace,
  119. },
  120. },
  121. })
  122. this.containerImages = data.containerImages
  123. this.initContainerImages = data.initContainerImages || []
  124. const imagesFieldValue = {}
  125. this.containerImages.forEach((item, i) => {
  126. imagesFieldValue[`images${i}`] = item.image
  127. })
  128. this.initContainerImages.forEach((item, i) => {
  129. imagesFieldValue[`initImages${i}`] = item.image
  130. })
  131. this.$nextTick(() => {
  132. this.form.fc.setFieldsValue(imagesFieldValue)
  133. })
  134. },
  135. async doUpdate (params) {
  136. const containers = []
  137. const initContainers = []
  138. const getImages = (field, decorator, detailField) => {
  139. Object.keys(params).forEach(key => {
  140. if (params.source === 'registry') {
  141. if (key.startsWith('registryImages')) {
  142. const i = key.replace('registryImages', '')
  143. if (this[detailField][i] && this[detailField][i].name) {
  144. field.push({
  145. name: this[detailField][i].name,
  146. image: removeHttp(params[key]),
  147. })
  148. }
  149. }
  150. } else {
  151. console.log(params)
  152. if (key.startsWith('images')) {
  153. const i = key.replace('images', '')
  154. if (this[detailField][i] && this[detailField][i].name) {
  155. field.push({
  156. name: this[detailField][i].name,
  157. image: removeHttp(params[key]),
  158. })
  159. }
  160. }
  161. }
  162. })
  163. return field
  164. }
  165. const data = {
  166. cluster: this.data.cluster,
  167. namespace: this.data.namespace,
  168. containers: getImages(containers, 'images', 'containerImages'),
  169. initContainers: getImages(initContainers, 'initImages', 'initContainerImages'),
  170. }
  171. try {
  172. await this.params.onManager('update', {
  173. id: this.data.id,
  174. managerArgs: {
  175. data,
  176. },
  177. steadyStatus: Object.values(expectStatus.k8s_resource).flat(),
  178. })
  179. } catch (error) {
  180. throw error
  181. }
  182. },
  183. async handleConfirm () {
  184. this.loading = true
  185. try {
  186. const values = await this.form.fc.validateFields()
  187. await this.doUpdate(values)
  188. this.loading = false
  189. this.cancelDialog()
  190. this.params.refresh && this.params.refresh()
  191. if (R.is(Function, this.params.success)) this.params.success()
  192. } catch (error) {
  193. this.loading = false
  194. throw error
  195. }
  196. },
  197. },
  198. }
  199. </script>