// Module included in the following assemblies:
//
// * nodes/nodes/nodes-nodes-replace-control-plane.adoc

:_mod-docs-content-type: PROCEDURE
[id="linking-node-machine-bmh_{context}"]
= Linking the node, bare metal host, and machine together

Continue creating the new control plane node by creating a machine and then linking it with the new `BareMetalHost` object and node.

.Procedure

. Get the `providerID` for control plane nodes by running the following command:
+
[source,terminal]
----
$ oc get -n openshift-machine-api baremetalhost -l installer.openshift.io/role=control-plane -ojson | jq -r '.items[] | "baremetalhost:///openshift-machine-api/" + .metadata.name + "/" + .metadata.uid'
----
+
.Example output
[source,terminal]
----
baremetalhost:///openshift-machine-api/master-00/6214c5cf-c798-4168-8c78-1ff1a3cd2cb4
baremetalhost:///openshift-machine-api/master-01/58fb60bd-b2a6-4ff3-a88d-208c33abf954
baremetalhost:///openshift-machine-api/master-02/dc5a94f3-625b-43f6-ab5a-7cc4fc79f105
----

. Get cluster information for labels by running the following command:
+
[source,terminal]
----
$ oc get machine -n openshift-machine-api \
    -l machine.openshift.io/cluster-api-machine-role=master \
    -L machine.openshift.io/cluster-api-cluster
----
+
.Example output
[source,terminal]
----
NAME                           PHASE   TYPE  REGION  ZONE  AGE  CLUSTER-API-CLUSTER
ci-op-jcp3s7wx-ng5sd-master-0  Running                     10h  ci-op-jcp3s7wx-ng5sd
ci-op-jcp3s7wx-ng5sd-master-1  Running                     10h  ci-op-jcp3s7wx-ng5sd
ci-op-jcp3s7wx-ng5sd-master-2  Running                     10h  ci-op-jcp3s7wx-ng5sd
----

. Create a `Machine` object for the new control plane node by creating a yaml file similar to the following:
+
[source,yaml]
----
apiVersion: machine.openshift.io/v1beta1
kind: Machine
metadata:
  annotations:
    metal3.io/BareMetalHost: openshift-machine-api/<new_control_plane_machine> <1>
  finalizers:
    - machine.machine.openshift.io
  labels:
    machine.openshift.io/cluster-api-cluster: <cluster_api_cluster> <2>
    machine.openshift.io/cluster-api-machine-role: master
    machine.openshift.io/cluster-api-machine-type: master
  name: <new_control_plane_machine> <1>
  namespace: openshift-machine-api
spec:
  metadata: {}
  providerID: <provider_id> <3>
  providerSpec:
    value:
      apiVersion: baremetal.cluster.k8s.io/v1alpha1
      hostSelector: {}
      image:
        checksum: ""
        url: ""
      kind: BareMetalMachineProviderSpec
      userData:
        name: master-user-data-managed
----
+
--
where:

`<new_control_plane_machine>`:: Specifies the name of the new machine, which can be the same as the previously deleted machine name.
`<cluster_api_cluster>`:: Specifies the `CLUSTER-API-CLUSTER` value  for the other control plane machines, shown in the output of the previous step.
`<provider_id>`:: Specifies the `providerID` value of the new bare metal host, shown in the output of an earlier step.
--
+
The following warning is expected:
+
[source,terminal]
----
Warning: metadata.finalizers: "machine.machine.openshift.io": prefer a domain-qualified finalizer name to avoid accidental conflicts with other finalizer writers
----

. Link the new control plane node and `Machine` object to the `BareMetalHost` object by performing the following steps in a single bash shell session:

.. Define the `NEW_NODE_NAME` variable by running the following command:
+
[source,terminal]
----
$ NEW_NODE_NAME=<new_node_name>
----
+
Replace `<new_node_name>` with the name of the new control plane node.

.. Define the `NEW_MACHINE_NAME` variable by running the following command:
+
[source,terminal]
----
$ NEW_MACHINE_NAME=<new_machine_name>
----
+
Replace `<new_machine_name>` with the name of the new machine.

.. Define the `BMH_UID` by running the following commands to extract it from the new node's `BareMetalHost` object:
+
[source,terminal]
----
$ BMH_UID=$(oc get -n openshift-machine-api bmh $NEW_NODE_NAME -ojson | jq -r .metadata.uid)
----
+
[source,terminal]
----
$ echo $BMH_UID
----

.. Patch the `consumerRef` object into the bare metal host by running the following command:
+
[source,terminal]
----
$ oc patch -n openshift-machine-api bmh $NEW_NODE_NAME --type merge --patch '{"spec":{"consumerRef":{"apiVersion":"machine.openshift.io/v1beta1","kind":"Machine","name":"'$NEW_MACHINE_NAME'","namespace":"openshift-machine-api"}}}'
----

.. Patch the `providerID` value into the new node by running the following command:
+
[source,terminal]
----
$ oc patch node $NEW_NODE_NAME --type merge --patch '{"spec":{"providerID":"baremetalhost:///openshift-machine-api/'$NEW_NODE_NAME'/'$BMH_UID'"}}'
----

.. Review the `providerID` values by running the following command:
+
[source,terminal]
----
$ oc get node -l node-role.kubernetes.io/control-plane -ojson | jq -r '.items[] | .metadata.name + "  " + .spec.providerID'
----

. Set the `BareMetalHost` object's `poweredOn` status to `true` by running the following command:
+
[source,terminal]
----
$ oc patch -n openshift-machine-api bmh $NEW_NODE_NAME --subresource status --type json -p '[{"op":"replace","path":"/status/poweredOn","value":true}]'
----

. Review the `BareMetalHost` object's `poweredOn` status by running the following command:
+
[source,terminal]
----
$ oc get bmh -n openshift-machine-api -ojson | jq -r '.items[] | .metadata.name + "   PoweredOn:" +  (.status.poweredOn | tostring)'
----

. Review the `BareMetalHost` object's provisioning state by running the following command:
+
[source,terminal]
----
$ oc get bmh -n openshift-machine-api -ojson | jq -r '.items[] | .metadata.name + "   ProvisioningState:" +  .status.provisioning.state'
----
+
[IMPORTANT]
====
If the provisioning state is not `unmanaged`, change the provisioning state by running the following command:

[source,terminal]
----
$ oc patch -n openshift-machine-api bmh $NEW_NODE_NAME --subresource status --type json -p '[{"op":"replace","path":"/status/provisioning/state","value":"unmanaged"}]'
----
====

. Set the machine's state to `Provisioned` by running the following command:
+
[source,terminal]
----
$ oc patch -n openshift-machine-api machines $NEW_MACHINE_NAME -n openshift-machine-api --subresource status --type json -p '[{"op":"replace","path":"/status/phase","value":"Provisioned"}]'
----
