index.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <template>
  2. <div class="k8s-deployment-create 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" :clusterObj.sync="clusterObj" />
  9. </a-form-item>
  10. <a-form-item :label="$t('k8s.text_373')">
  11. <base-select
  12. :resource="'rbacclusterroles'"
  13. version="v1"
  14. :params="clusterRoleparams"
  15. need-params
  16. idKey="name"
  17. :select-props="{ placeholder: $t('common.select') }"
  18. v-decorator="decorators.role" />
  19. </a-form-item>
  20. <a-form-item label="Subject">
  21. <a-radio-group v-decorator="decorators.subjectType" @change="subjectTypeChange">
  22. <a-radio-button v-for="item in subjectTypeOpts" :value="item.key" :key="item.key">{{ item.label }}</a-radio-button>
  23. </a-radio-group>
  24. </a-form-item>
  25. <a-form-item :label="$t('k8s.text_23')" v-if="subjectType === 'ServiceAccount'">
  26. <namespace-select v-decorator="decorators.namespace" :cluster="cluster" :namespaceObj.sync="namespaceObj" />
  27. </a-form-item>
  28. <a-form-item label="Subject Name">
  29. <base-select
  30. v-if="subjectType === 'ServiceAccount'"
  31. v-decorator="decorators.subject"
  32. resource="serviceaccounts"
  33. version="v1"
  34. idKey="name"
  35. :params="params"
  36. need-params
  37. :select-props="{ placeholder: $t('common.select') }" />
  38. <template v-else>
  39. <base-select
  40. v-if="subjectOpts.length"
  41. v-decorator="decorators.subject"
  42. :options="subjectOpts"
  43. version="v1"
  44. idKey="name"
  45. :select-props="{ placeholder: $t('common.select') }" />
  46. <a-input v-else v-decorator="decorators.subject" :placeholder="$t('common.placeholder')" />
  47. </template>
  48. </a-form-item>
  49. </a-form>
  50. </div>
  51. </template>
  52. <script>
  53. import ClusterSelect from '@K8S/sections/ClusterSelect'
  54. import NamespaceSelect from '@K8S/sections/NamespaceSelect'
  55. import k8sCreateMixin from '@K8S/mixins/create'
  56. const API_GROUP = 'rbac.authorization.k8s.io'
  57. export default {
  58. name: 'K8sRbacclusterrolebindingFormCreate',
  59. components: {
  60. ClusterSelect,
  61. NamespaceSelect,
  62. },
  63. mixins: [k8sCreateMixin],
  64. data () {
  65. return {
  66. form: {
  67. fc: this.$form.createForm(this),
  68. },
  69. formItemLayout: {
  70. labelCol: { span: 4 },
  71. wrapperCol: { span: 20 },
  72. },
  73. decorators: {
  74. name: [
  75. 'name',
  76. {
  77. validateFirst: true,
  78. rules: [
  79. { required: true, message: this.$t('k8s.text_60') },
  80. { min: 2, max: 24, message: this.$t('k8s.text_132'), trigger: 'blur' },
  81. { validator: this.$validate('k8sName') },
  82. ],
  83. },
  84. ],
  85. cluster: [
  86. 'cluster',
  87. {
  88. initialValue: this.$store.state.common.k8s.cluster,
  89. rules: [
  90. { required: true, message: this.$t('k8s.text_30'), trigger: 'blur' },
  91. ],
  92. },
  93. ],
  94. namespace: [
  95. 'namespace',
  96. {
  97. initialValue: this.$store.state.common.k8s.namespace,
  98. rules: [
  99. { required: true, message: this.$t('k8s.text_61'), trigger: 'blur' },
  100. ],
  101. },
  102. ],
  103. role: [
  104. 'role',
  105. {
  106. rules: [
  107. { required: true, message: this.$t('common.select'), trigger: 'blur' },
  108. ],
  109. },
  110. ],
  111. subjectType: [
  112. 'subjectType',
  113. {
  114. initialValue: 'User',
  115. },
  116. ],
  117. subject: [
  118. 'subject',
  119. {
  120. rules: [
  121. { required: true, message: this.$t('common.select'), trigger: 'blur' },
  122. ],
  123. },
  124. ],
  125. },
  126. clusterObj: {},
  127. namespaceObj: {},
  128. subjectTypeOpts: [
  129. { key: 'User', label: 'User' },
  130. { key: 'Group', label: 'Group' },
  131. { key: 'ServiceAccount', label: this.$t('k8s.text_26') },
  132. ],
  133. subjectOpts: [],
  134. subjectType: 'User',
  135. }
  136. },
  137. computed: {
  138. clusterRoleparams () {
  139. const cluster = this.clusterObj.id
  140. if (!cluster) return {}
  141. const params = {
  142. cluster,
  143. limit: 0,
  144. scope: this.$store.getters.scope,
  145. }
  146. return params
  147. },
  148. params () {
  149. const cluster = this.clusterObj.id
  150. const namespace = this.namespaceObj.id
  151. if (!cluster || !namespace) return {}
  152. const params = {
  153. cluster,
  154. namespace,
  155. limit: 0,
  156. scope: this.$store.getters.scope,
  157. }
  158. return params
  159. },
  160. },
  161. watch: {
  162. clusterObj (val, oldV) {
  163. this.namespaceObj = {}
  164. this.getSubjectOpts()
  165. },
  166. },
  167. methods: {
  168. async getSubjectOpts () {
  169. try {
  170. const cluster = this.clusterObj.id
  171. let spec = ''
  172. if (this.subjectType === 'User') spec = 'cluster-users'
  173. if (this.subjectType === 'Group') spec = 'cluster-user-groups'
  174. if (cluster) {
  175. const { data } = await new this.$Manager('kubeclusters', 'v1').getSpecific({ id: cluster, spec, params: { scope: this.$store.getters.scope } })
  176. this.subjectOpts = data
  177. }
  178. } catch (error) {
  179. throw error
  180. }
  181. },
  182. subjectTypeChange (e) {
  183. this.subjectType = e.target.value
  184. this.getSubjectOpts()
  185. this.form.fc.setFieldsValue({
  186. [this.decorators.subject[0]]: undefined,
  187. })
  188. },
  189. async validateForm () {
  190. try {
  191. const values = await this.form.fc.validateFields()
  192. const subjects = [{
  193. kind: values.subjectType,
  194. name: values.subject,
  195. }]
  196. if (subjects[0].kind === 'ServiceAccount') {
  197. subjects[0].namespace = this.namespaceObj.name
  198. } else {
  199. subjects[0].apiGroup = API_GROUP
  200. }
  201. const data = {
  202. cluster_id: values.cluster,
  203. name: values.name,
  204. roleRef: {
  205. kind: 'ClusterRole',
  206. name: values.role,
  207. apiGroup: API_GROUP,
  208. },
  209. subjects,
  210. }
  211. return data
  212. } catch (error) {
  213. throw error
  214. }
  215. },
  216. async doCreate () {
  217. try {
  218. const values = await this.validateForm()
  219. this.$emit('submit', values)
  220. } catch (error) {
  221. throw error
  222. }
  223. },
  224. },
  225. }
  226. </script>