index.vue 7.1 KB

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