|
1 | 1 | import * as React from 'react'; |
2 | 2 | import { useTranslation } from 'react-i18next'; |
| 3 | +import { Action } from '@console/dynamic-plugin-sdk'; |
| 4 | +import { useK8sModel } from '@console/dynamic-plugin-sdk/src/lib-core'; |
3 | 5 | import { k8sUpdateResource } from '@console/dynamic-plugin-sdk/src/utils/k8s'; |
4 | | -import { deleteModal } from '@console/internal/components/modals/delete-modal'; |
5 | | -import { Kebab, KebabAction, asAccessReview } from '@console/internal/components/utils'; |
6 | | -import { CertificateSigningRequestModel, NodeModel } from '@console/internal/models'; |
7 | | -import { CertificateSigningRequestKind, K8sKind, NodeKind } from '@console/internal/module/k8s'; |
| 6 | +import { asAccessReview } from '@console/internal/components/utils'; |
| 7 | +import { CertificateSigningRequestModel } from '@console/internal/models'; |
| 8 | +import { |
| 9 | + CertificateSigningRequestKind, |
| 10 | + ExtensionHook, |
| 11 | + NodeKind, |
| 12 | + referenceFor, |
| 13 | +} from '@console/internal/module/k8s'; |
8 | 14 | import { isNodeUnschedulable } from '@console/shared'; |
| 15 | +import { getCommonResourceActions } from '../../actions/creators/common-factory'; |
9 | 16 | import { makeNodeSchedulable } from '../../k8s/requests/nodes'; |
10 | 17 | import { createConfigureUnschedulableModal } from './modals'; |
11 | 18 |
|
@@ -37,66 +44,38 @@ export const approveCSR = (csr: CertificateSigningRequestKind) => updateCSR(csr, |
37 | 44 |
|
38 | 45 | export const denyCSR = (csr: CertificateSigningRequestKind) => updateCSR(csr, 'Denied'); |
39 | 46 |
|
40 | | -export const MarkAsUnschedulable: KebabAction = (kind: K8sKind, obj: NodeKind) => ({ |
41 | | - labelKey: 'console-app~Mark as unschedulable', |
42 | | - hidden: isNodeUnschedulable(obj), |
43 | | - callback: () => createConfigureUnschedulableModal({ resource: obj }), |
44 | | - accessReview: { |
45 | | - group: kind.apiGroup, |
46 | | - resource: kind.plural, |
47 | | - name: obj.metadata.name, |
48 | | - namespace: obj.metadata.namespace, |
49 | | - verb: 'patch', |
50 | | - }, |
51 | | -}); |
| 47 | +export const useNodeActions: ExtensionHook<Action[], NodeKind> = (obj) => { |
| 48 | + const [kindObj, inFlight] = useK8sModel(referenceFor(obj)); |
| 49 | + const { t } = useTranslation(); |
| 50 | + const nodeActions = React.useMemo<Action[]>(() => { |
| 51 | + const actions: Action[] = []; |
| 52 | + if (isNodeUnschedulable(obj)) { |
| 53 | + actions.push({ |
| 54 | + id: 'mark-as-schedulable', |
| 55 | + label: t('console-app~Mark as schedulable'), |
| 56 | + cta: () => makeNodeSchedulable(obj), |
| 57 | + accessReview: asAccessReview(kindObj, obj, 'patch'), |
| 58 | + }); |
| 59 | + } else { |
| 60 | + actions.push({ |
| 61 | + id: 'mark-as-unschedulable', |
| 62 | + label: t('console-app~Mark as unschedulable'), |
| 63 | + cta: () => createConfigureUnschedulableModal({ resource: obj }), |
| 64 | + accessReview: asAccessReview(kindObj, obj, 'patch'), |
| 65 | + }); |
| 66 | + } |
52 | 67 |
|
53 | | -export const MarkAsSchedulable: KebabAction = ( |
54 | | - kind: K8sKind, |
55 | | - obj: NodeKind, |
56 | | - resources: {}, |
57 | | - { nodeMaintenance } = { nodeMaintenance: false }, // NOTE: used by node actions in metal3-plugin |
58 | | -) => ({ |
59 | | - labelKey: 'console-app~Mark as schedulable', |
60 | | - hidden: !isNodeUnschedulable(obj) || nodeMaintenance, |
61 | | - callback: () => makeNodeSchedulable(obj), |
62 | | - accessReview: { |
63 | | - group: kind.apiGroup, |
64 | | - resource: kind.plural, |
65 | | - name: obj.metadata.name, |
66 | | - namespace: obj.metadata.namespace, |
67 | | - verb: 'patch', |
68 | | - }, |
69 | | -}); |
| 68 | + const message = ( |
| 69 | + <p> |
| 70 | + {t( |
| 71 | + 'console-app~This action cannot be undone. Deleting a node will instruct Kubernetes that the node is down or unrecoverable and delete all pods scheduled to that node. If the node is still running but unresponsive and the node is deleted, stateful workloads and persistent volumes may suffer corruption or data loss. Only delete a node that you have confirmed is completely stopped and cannot be restored.', |
| 72 | + )} |
| 73 | + </p> |
| 74 | + ); |
70 | 75 |
|
71 | | -export const Delete: KebabAction = (kindObj: K8sKind, node: NodeKind) => { |
72 | | - const { t } = useTranslation(); |
73 | | - const message = ( |
74 | | - <p> |
75 | | - {t( |
76 | | - 'console-app~This action cannot be undone. Deleting a node will instruct Kubernetes that the node is down or unrecoverable and delete all pods scheduled to that node. If the node is still running but unresponsive and the node is deleted, stateful workloads and persistent volumes may suffer corruption or data loss. Only delete a node that you have confirmed is completely stopped and cannot be restored.', |
77 | | - )} |
78 | | - </p> |
79 | | - ); |
| 76 | + actions.push(...getCommonResourceActions(kindObj, obj, message)); |
| 77 | + return actions; |
| 78 | + }, [kindObj, obj, t]); |
80 | 79 |
|
81 | | - return { |
82 | | - // t('console-app~Delete node') |
83 | | - labelKey: 'console-app~Delete node', |
84 | | - callback: () => |
85 | | - deleteModal({ |
86 | | - kind: kindObj, |
87 | | - resource: node, |
88 | | - message, |
89 | | - }), |
90 | | - accessReview: asAccessReview(NodeModel, node, 'delete'), |
91 | | - }; |
| 80 | + return [nodeActions, !inFlight, undefined]; |
92 | 81 | }; |
93 | | - |
94 | | -const { ModifyLabels, ModifyAnnotations, Edit } = Kebab.factory; |
95 | | -export const menuActions = [ |
96 | | - MarkAsSchedulable, |
97 | | - MarkAsUnschedulable, |
98 | | - ModifyLabels, |
99 | | - ModifyAnnotations, |
100 | | - Edit, |
101 | | - Delete, |
102 | | -]; |
0 commit comments