index.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <template>
  2. <a-row :gutter="8">
  3. <a-col v-if="label" :span="24">
  4. <a-tag v-if="label.key">{{ label.key }}: {{ label.value }}</a-tag>
  5. <a-tag v-else>{{ label }}</a-tag>
  6. </a-col>
  7. <a-col :span="colSpan">
  8. <a-select
  9. :loading="registryLoading"
  10. :value="registry"
  11. optionLabelProp="label"
  12. @change="handleRegistryChange">
  13. <a-select-option value="" label="">{{ $t('common.tips.select', [$t('k8s.text_158')]) }}</a-select-option>
  14. <a-select-option
  15. v-for="cr in registrys"
  16. :key="cr.value"
  17. :value="cr.value"
  18. :label="cr.label">
  19. <div>{{ cr.label }}</div>
  20. <div style="font-size: 12px; color: #999;">{{ cr.url }}</div>
  21. </a-select-option>
  22. </a-select>
  23. </a-col>
  24. <template v-if="isCustomRegistry">
  25. <a-col :span="16">
  26. <a-input
  27. :value="customImageUrl"
  28. :placeholder="$t('k8s.repo.image.custom.placeholder')"
  29. @change="handleCustomImageUrlChange" />
  30. <div v-if="previewImage" style="font-size: 12px; color: #999; margin-top: 4px;">{{ previewImage }}</div>
  31. </a-col>
  32. </template>
  33. <template v-else>
  34. <a-col :span="colSpan">
  35. <a-select
  36. :loading="imageLoading"
  37. :value="image"
  38. @change="handleImageChange">
  39. <a-select-option value="">{{ $t('common.tips.select', [$t('k8s.text_42')]) }}</a-select-option>
  40. <a-select-option
  41. v-for="cr in images"
  42. :key="cr.value"
  43. :value="cr.value">{{ cr.label }}</a-select-option>
  44. </a-select>
  45. </a-col>
  46. <a-col :span="colSpan">
  47. <a-select
  48. :loading="tagLoading"
  49. :value="tag"
  50. @change="handleTagChange">
  51. <a-select-option value="">{{ $t('common.tips.select', [$t('k8s.repo.image.tag')]) }}</a-select-option>
  52. <a-select-option
  53. v-for="cr in tags"
  54. :key="cr.value"
  55. :value="cr.value">{{ cr.label }}</a-select-option>
  56. </a-select>
  57. </a-col>
  58. </template>
  59. </a-row>
  60. </template>
  61. <script>
  62. import { uuid } from '@/utils/utils'
  63. export default {
  64. name: 'MirrorRegistry',
  65. props: {
  66. isDefaultSelect: {
  67. type: Boolean,
  68. default: true,
  69. },
  70. label: {
  71. type: [String, Object],
  72. },
  73. },
  74. data () {
  75. return {
  76. registryLoading: false,
  77. registry: '',
  78. registrys: [],
  79. imageLoading: false,
  80. image: '',
  81. images: [],
  82. tagLoading: false,
  83. tag: '',
  84. tags: [],
  85. colSpan: 8,
  86. customImageUrl: '',
  87. }
  88. },
  89. computed: {
  90. isCustomRegistry () {
  91. const cur = this.registrys.find(o => o.value === this.registry)
  92. return cur?.type === 'custom'
  93. },
  94. previewImage () {
  95. const cur = this.registrys.find(o => o.value === this.registry)
  96. const url = cur?.url
  97. if (url && this.customImageUrl) {
  98. return `${this.stripProtocol(url)}/${this.customImageUrl}`
  99. }
  100. return ''
  101. },
  102. },
  103. watch: {
  104. registry (val) {
  105. if (!val) {
  106. this.$emit('change', '')
  107. }
  108. },
  109. },
  110. created () {
  111. this.getRegistrys()
  112. },
  113. methods: {
  114. handleRegistryChange (val) {
  115. if (val) {
  116. this.registry = val
  117. this.customImageUrl = ''
  118. const cur = this.registrys.find(o => o.value === val)
  119. this.$emit('credential-change', cur?.credential_id || '')
  120. if (cur?.type === 'custom') {
  121. this.image = ''
  122. this.images = []
  123. this.tag = ''
  124. this.tags = []
  125. this.$emit('change', '')
  126. } else {
  127. this.getImagesByRegistryId(val)
  128. }
  129. } else {
  130. this.registry = ''
  131. this.image = ''
  132. this.images = []
  133. this.tag = ''
  134. this.tags = []
  135. this.customImageUrl = ''
  136. this.$emit('change', '')
  137. this.$emit('credential-change', '')
  138. }
  139. },
  140. async getRegistrys () {
  141. try {
  142. const manager = new this.$Manager('container_registries')
  143. this.registryLoading = true
  144. this.registry = ''
  145. this.registrys = []
  146. const params = { details: true, limit: 20, offset: 0, scope: this.$store.getters.scope, $t: uuid() }
  147. const result = await manager.list({ params })
  148. const dataArr = result.data.data || []
  149. this.registrys = dataArr.map(item => {
  150. return {
  151. label: item.name,
  152. value: item.id,
  153. url: item.url,
  154. type: item.type,
  155. credential_id: item.credential_id,
  156. }
  157. })
  158. if (this.isDefaultSelect && this.registrys?.length > 0) {
  159. this.registry = this.registrys?.[0].value
  160. this.getImagesByRegistryId(this.registry)
  161. } else {
  162. this.image = ''
  163. this.tag = ''
  164. }
  165. } catch (error) {
  166. throw error
  167. } finally {
  168. this.registryLoading = false
  169. }
  170. },
  171. handleImageChange (val) {
  172. if (val) {
  173. this.image = val
  174. this.getTagsByImage(this.registry, val)
  175. } else {
  176. this.image = ''
  177. this.tag = ''
  178. this.$emit('change', '')
  179. }
  180. },
  181. async getImagesByRegistryId (rId) {
  182. try {
  183. const manager = new this.$Manager('container_registries')
  184. this.imageLoading = true
  185. this.image = ''
  186. this.images = []
  187. const result = await manager.getSpecific({ id: rId, spec: 'images', params: { $t: uuid() } })
  188. const dataArr = result.data.repositories || []
  189. this.images = dataArr.map(item => {
  190. const v = item.split('/')[1]
  191. return {
  192. label: v,
  193. value: v,
  194. }
  195. })
  196. if (this.images?.length > 0) {
  197. this.image = this.images?.[0].value
  198. this.getTagsByImage(rId, this.image)
  199. }
  200. } catch (error) {
  201. throw error
  202. } finally {
  203. this.imageLoading = false
  204. }
  205. },
  206. handleTagChange (val) {
  207. if (val) {
  208. this.tag = val
  209. this.triggerChange(this.registry, this.image, this.tag)
  210. } else {
  211. this.tag = ''
  212. this.$emit('change', '')
  213. }
  214. },
  215. async getTagsByImage (rId, image) {
  216. try {
  217. const manager = new this.$Manager('container_registries')
  218. this.tagLoading = true
  219. this.tag = ''
  220. this.tags = []
  221. const result = await manager.getSpecific({ id: rId, spec: 'image-tags', params: { repository: image, $t: uuid() } })
  222. const dataArr = result.data.tags || []
  223. this.tags = dataArr.map(item => {
  224. return {
  225. label: item,
  226. value: item,
  227. }
  228. })
  229. if (this.tags?.length > 0) {
  230. this.tag = this.tags?.[0].value
  231. this.triggerChange(this.registry, this.image, this.tag)
  232. }
  233. } catch (error) {
  234. throw error
  235. } finally {
  236. this.tagLoading = false
  237. }
  238. },
  239. stripProtocol (url) {
  240. return url.replace(/^https?:\/\//, '')
  241. },
  242. handleCustomImageUrlChange (e) {
  243. const val = e.target.value
  244. this.customImageUrl = val
  245. const curRegistry = this.registrys.find(o => o.value === this.registry)
  246. const url = curRegistry?.url
  247. if (url && val) {
  248. this.$emit('change', `${this.stripProtocol(url)}/${val}`)
  249. } else {
  250. this.$emit('change', '')
  251. }
  252. },
  253. triggerChange (registry, image, tag) {
  254. const curRegistry = this.registrys.find(o => o.value === registry)
  255. const url = curRegistry?.url
  256. if (url && image && tag) {
  257. this.$emit('change', `${this.stripProtocol(url)}/${image}:${tag}`)
  258. } else {
  259. this.$emit('change', '')
  260. }
  261. },
  262. },
  263. }
  264. </script>
  265. <style>
  266. </style>