From 3c293aa4b49884187b79ffe56f0d2c3e3c921083 Mon Sep 17 00:00:00 2001 From: Jiri Tomasek Date: Mon, 17 Jun 2019 13:52:32 +0200 Subject: [PATCH] Use machine selectors from @console/shared * update machine selector types to use specific kinds * remove selectors definition from machine module * use machine selectors from @console/shared in machine, machine-set and machine-deployment modules --- .../console-shared/src/selectors/machine.ts | 17 ++++++++++++----- .../metal3-plugin/src/components/host-role.tsx | 4 ++-- .../metal3-plugin/src/components/host.tsx | 9 +++++++-- .../metal3-plugin/src/selectors/index.ts | 4 ++-- .../public/components/machine-deployment.tsx | 2 +- frontend/public/components/machine-set.tsx | 3 ++- frontend/public/components/machine.tsx | 16 ++++++---------- 7 files changed, 32 insertions(+), 23 deletions(-) diff --git a/frontend/packages/console-shared/src/selectors/machine.ts b/frontend/packages/console-shared/src/selectors/machine.ts index 4f2742178db..bfeb8ba7651 100644 --- a/frontend/packages/console-shared/src/selectors/machine.ts +++ b/frontend/packages/console-shared/src/selectors/machine.ts @@ -1,11 +1,18 @@ import * as _ from 'lodash-es'; -import { K8sResourceKind } from '@console/internal/module/k8s'; +import { + MachineKind, + MachineSetKind, + MachineDeploymentKind, + NodeKind, +} from '@console/internal/module/k8s'; import { getName } from './common'; -export const getMachineRole = (machine) => - _.get(machine, ['metadata', 'labels', 'machine.openshift.io/cluster-api-machine-role']); -export const getMachineNodeName = (machine) => _.get(machine, 'status.nodeRef.name'); +export const getMachineRole = (obj: MachineKind | MachineSetKind | MachineDeploymentKind) => + _.get(obj, ['metadata', 'labels', 'machine.openshift.io/cluster-api-machine-role']); +export const getMachineNodeName = (obj: MachineKind) => _.get(obj, 'status.nodeRef.name'); +export const getMachineAWSPlacement = (machine: MachineKind) => + _.get(machine, 'spec.providerSpec.value.placement') || {}; -export const getMachineNode = (machine: K8sResourceKind, nodes: K8sResourceKind[]) => +export const getMachineNode = (machine: MachineKind, nodes: NodeKind[]) => nodes.find((node) => getMachineNodeName(machine) === getName(node)); diff --git a/frontend/packages/metal3-plugin/src/components/host-role.tsx b/frontend/packages/metal3-plugin/src/components/host-role.tsx index e4ed0ef480b..5df85d02a83 100644 --- a/frontend/packages/metal3-plugin/src/components/host-role.tsx +++ b/frontend/packages/metal3-plugin/src/components/host-role.tsx @@ -1,8 +1,8 @@ import { getMachineRole, DASH } from '@console/shared'; -import { K8sResourceKind } from '@console/internal/module/k8s'; +import { MachineKind } from '@console/internal/module/k8s'; type BaremetalHostRoleProps = { - machine: K8sResourceKind; + machine: MachineKind; }; export const BaremetalHostRole: React.FC = ({ machine }) => getMachineRole(machine) || DASH; diff --git a/frontend/packages/metal3-plugin/src/components/host.tsx b/frontend/packages/metal3-plugin/src/components/host.tsx index b77bf553144..78e25de6cc6 100644 --- a/frontend/packages/metal3-plugin/src/components/host.tsx +++ b/frontend/packages/metal3-plugin/src/components/host.tsx @@ -8,7 +8,12 @@ import { MachineModel, NodeModel } from '@console/internal/models'; import { MultiListPage, Table, TableRow, TableData } from '@console/internal/components/factory'; import { ResourceLink, Kebab } from '@console/internal/components/utils'; -import { referenceForModel, K8sResourceKind } from '@console/internal/module/k8s'; +import { + referenceForModel, + K8sResourceKind, + MachineKind, + NodeKind, +} from '@console/internal/module/k8s'; import { BaremetalHostModel } from '../models'; import { getHostBMCAddress, getHostMachine } from '../selectors'; @@ -56,7 +61,7 @@ const HostsTableHeader = () => [ ]; type HostsTableRowProps = { - obj: K8sResourceKind & { machine: K8sResourceKind; node: K8sResourceKind }; + obj: K8sResourceKind & { machine: MachineKind; node: NodeKind }; index: number; key?: string; style: React.StyleHTMLAttributes; diff --git a/frontend/packages/metal3-plugin/src/selectors/index.ts b/frontend/packages/metal3-plugin/src/selectors/index.ts index 1caaead9615..ac699b9f06c 100644 --- a/frontend/packages/metal3-plugin/src/selectors/index.ts +++ b/frontend/packages/metal3-plugin/src/selectors/index.ts @@ -1,6 +1,6 @@ import * as _ from 'lodash-es'; -import { K8sResourceKind } from '@console/internal/module/k8s'; +import { K8sResourceKind, MachineKind } from '@console/internal/module/k8s'; import { getName } from '@console/shared'; export const getOperationalStatus = (host) => _.get(host, 'status.operationalStatus'); @@ -17,5 +17,5 @@ export const getHostDescription = (host) => _.get(host, 'spec.description', ''); export const isHostPoweredOn = (host) => _.get(host, 'status.poweredOn', false); export const getHostTotalStorageCapacity = (host) => _.reduce(getHostStorage(host), (sum, disk) => sum + disk.sizeGiB, 0); -export const getHostMachine = (host: K8sResourceKind, machines: K8sResourceKind[]) => +export const getHostMachine = (host: K8sResourceKind, machines: MachineKind[]) => machines.find((machine) => getHostMachineName(host) === getName(machine)); diff --git a/frontend/public/components/machine-deployment.tsx b/frontend/public/components/machine-deployment.tsx index 73aab84463c..e8408a98b3f 100644 --- a/frontend/public/components/machine-deployment.tsx +++ b/frontend/public/components/machine-deployment.tsx @@ -3,9 +3,9 @@ import * as _ from 'lodash-es'; import { Link } from 'react-router-dom'; import { sortable } from '@patternfly/react-table'; import * as classNames from 'classnames'; +import { getMachineRole } from '@console/shared'; import { MachineModel, MachineDeploymentModel } from '../models'; import { MachineDeploymentKind, referenceForModel } from '../module/k8s'; -import { getMachineRole } from './machine'; import { editCountAction, getAWSPlacement, diff --git a/frontend/public/components/machine-set.tsx b/frontend/public/components/machine-set.tsx index 4dd43a207ff..6b52c8825a7 100644 --- a/frontend/public/components/machine-set.tsx +++ b/frontend/public/components/machine-set.tsx @@ -3,9 +3,10 @@ import * as _ from 'lodash-es'; import { Link } from 'react-router-dom'; import { sortable } from '@patternfly/react-table'; import * as classNames from 'classnames'; +import { getMachineRole } from '@console/shared'; import { MachineAutoscalerModel, MachineModel, MachineSetModel } from '../models'; import { K8sKind, MachineDeploymentKind, MachineSetKind, referenceForModel } from '../module/k8s'; -import { getMachineRole, MachinePage } from './machine'; +import { MachinePage } from './machine'; import { configureMachineAutoscalerModal, configureReplicaCountModal } from './modals'; import { DetailsPage, ListPage, Table, TableRow, TableData } from './factory'; import { diff --git a/frontend/public/components/machine.tsx b/frontend/public/components/machine.tsx index 91a0662039f..3e31c334a99 100644 --- a/frontend/public/components/machine.tsx +++ b/frontend/public/components/machine.tsx @@ -2,8 +2,9 @@ import * as React from 'react'; import * as _ from 'lodash-es'; import { sortable } from '@patternfly/react-table'; import * as classNames from 'classnames'; +import { getMachineRole, getMachineNodeName, getMachineAWSPlacement } from '@console/shared'; import { MachineModel } from '../models'; -import { MachineDeploymentKind, MachineKind, MachineSetKind, referenceForModel } from '../module/k8s'; +import { MachineKind, referenceForModel } from '../module/k8s'; import { Conditions } from './conditions'; import { NodeIPList } from './node'; import { DetailsPage, ListPage, Table, TableRow, TableData } from './factory'; @@ -21,11 +22,6 @@ import { breadcrumbsForOwnerRefs } from './utils/breadcrumbs'; const { common } = Kebab.factory; const menuActions = [...common]; export const machineReference = referenceForModel(MachineModel); -const getAWSPlacement = (machine: MachineKind) => _.get(machine, 'spec.providerSpec.value.placement') || {}; - -export const getMachineRole = (obj: MachineKind | MachineSetKind | MachineDeploymentKind) => _.get(obj, ['metadata', 'labels', 'sigs.k8s.io/cluster-api-machine-role']); - -const getNodeName = (obj) => _.get(obj, 'status.nodeRef.name'); const tableColumnClasses = [ classNames('pf-m-3-col-on-xl', 'pf-m-4-col-on-lg', 'pf-m-4-col-on-md', 'pf-m-6-col-on-sm'), @@ -66,8 +62,8 @@ const MachineTableHeader = () => { MachineTableHeader.displayName = 'MachineTableHeader'; const MachineTableRow: React.FC = ({obj, index, key, style}) => { - const { availabilityZone, region } = getAWSPlacement(obj); - const nodeName = getNodeName(obj); + const { availabilityZone, region } = getMachineAWSPlacement(obj); + const nodeName = getMachineNodeName(obj); return ( @@ -100,9 +96,9 @@ type MachineTableRowProps = { }; const MachineDetails: React.SFC = ({obj}: {obj: MachineKind}) => { - const nodeName = getNodeName(obj); + const nodeName = getMachineNodeName(obj); const machineRole = getMachineRole(obj); - const { availabilityZone, region } = getAWSPlacement(obj); + const { availabilityZone, region } = getMachineAWSPlacement(obj); return