From 5cbe5d3013e864181dca4a22e86ee600bd1a3cdc Mon Sep 17 00:00:00 2001 From: Andrew Sy Kim Date: Wed, 24 Jun 2020 14:30:06 -0400 Subject: [PATCH] capd: set machine addresses Signed-off-by: Andrew Sy Kim --- .../api/v1alpha3/dockermachine_types.go | 4 ++++ .../api/v1alpha3/zz_generated.deepcopy.go | 5 +++++ ...cture.cluster.x-k8s.io_dockermachines.yaml | 19 ++++++++++++++++ .../controllers/dockermachine_controller.go | 22 +++++++++++++++++++ test/infrastructure/docker/docker/machine.go | 9 ++++++++ 5 files changed, 59 insertions(+) diff --git a/test/infrastructure/docker/api/v1alpha3/dockermachine_types.go b/test/infrastructure/docker/api/v1alpha3/dockermachine_types.go index f5d60d91588e..8fb81a1187c1 100644 --- a/test/infrastructure/docker/api/v1alpha3/dockermachine_types.go +++ b/test/infrastructure/docker/api/v1alpha3/dockermachine_types.go @@ -81,6 +81,10 @@ type DockerMachineStatus struct { // +optional LoadBalancerConfigured bool `json:"loadBalancerConfigured,omitempty"` + // Addresses contains the associated addresses for the docker machine. + // +optional + Addresses []clusterv1.MachineAddress `json:"addresses,omitempty"` + // Conditions defines current service state of the DockerMachine. // +optional Conditions clusterv1.Conditions `json:"conditions,omitempty"` diff --git a/test/infrastructure/docker/api/v1alpha3/zz_generated.deepcopy.go b/test/infrastructure/docker/api/v1alpha3/zz_generated.deepcopy.go index 6213b8e12067..e3a0e5704c56 100644 --- a/test/infrastructure/docker/api/v1alpha3/zz_generated.deepcopy.go +++ b/test/infrastructure/docker/api/v1alpha3/zz_generated.deepcopy.go @@ -243,6 +243,11 @@ func (in *DockerMachineSpec) DeepCopy() *DockerMachineSpec { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *DockerMachineStatus) DeepCopyInto(out *DockerMachineStatus) { *out = *in + if in.Addresses != nil { + in, out := &in.Addresses, &out.Addresses + *out = make([]apiv1alpha3.MachineAddress, len(*in)) + copy(*out, *in) + } if in.Conditions != nil { in, out := &in.Conditions, &out.Conditions *out = make(apiv1alpha3.Conditions, len(*in)) diff --git a/test/infrastructure/docker/config/crd/bases/infrastructure.cluster.x-k8s.io_dockermachines.yaml b/test/infrastructure/docker/config/crd/bases/infrastructure.cluster.x-k8s.io_dockermachines.yaml index 5f16c8af64f6..fee2052298de 100644 --- a/test/infrastructure/docker/config/crd/bases/infrastructure.cluster.x-k8s.io_dockermachines.yaml +++ b/test/infrastructure/docker/config/crd/bases/infrastructure.cluster.x-k8s.io_dockermachines.yaml @@ -82,6 +82,25 @@ spec: status: description: DockerMachineStatus defines the observed state of DockerMachine properties: + addresses: + description: Addresses contains the associated addresses for the docker + machine. + items: + description: MachineAddress contains information for the node's + address. + properties: + address: + description: The machine address. + type: string + type: + description: Machine address type, one of Hostname, ExternalIP + or InternalIP. + type: string + required: + - address + - type + type: object + type: array conditions: description: Conditions defines current service state of the DockerMachine. items: diff --git a/test/infrastructure/docker/controllers/dockermachine_controller.go b/test/infrastructure/docker/controllers/dockermachine_controller.go index f7e45c42a622..05e570df4a7c 100644 --- a/test/infrastructure/docker/controllers/dockermachine_controller.go +++ b/test/infrastructure/docker/controllers/dockermachine_controller.go @@ -277,6 +277,28 @@ func (r *DockerMachineReconciler) reconcileNormal(ctx context.Context, machine * dockerMachine.Status.Ready = true conditions.MarkTrue(dockerMachine, infrav1.ContainerProvisionedCondition) + // set address in machine status + machineAddress, err := externalMachine.Address() + if err != nil { + r.Log.Error(err, "failed to patch the Kubernetes node with the machine address") + return ctrl.Result{RequeueAfter: 5 * time.Second}, nil + } + + dockerMachine.Status.Addresses = []clusterv1.MachineAddress{ + { + Type: clusterv1.MachineHostName, + Address: externalMachine.ContainerName(), + }, + { + Type: clusterv1.MachineInternalIP, + Address: machineAddress, + }, + { + Type: clusterv1.MachineExternalIP, + Address: machineAddress, + }, + } + return ctrl.Result{}, nil } diff --git a/test/infrastructure/docker/docker/machine.go b/test/infrastructure/docker/docker/machine.go index 7dbc9889296b..78b075e3546c 100644 --- a/test/infrastructure/docker/docker/machine.go +++ b/test/infrastructure/docker/docker/machine.go @@ -104,6 +104,15 @@ func (m *Machine) ProviderID() string { return fmt.Sprintf("docker:////%s", m.ContainerName()) } +func (m *Machine) Address() (string, error) { + ipv4, _, err := m.container.IP(context.TODO()) + if err != nil { + return "", err + } + + return ipv4, nil +} + // Create creates a docker container hosting a Kubernetes node. func (m *Machine) Create(ctx context.Context, role string, version *string, mounts []infrav1.Mount) error { // Create if not exists.