From 121e31ebb708bdc3db0385570d2649b07f1faace 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 | 3 +++ .../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, 58 insertions(+) diff --git a/test/infrastructure/docker/api/v1alpha3/dockermachine_types.go b/test/infrastructure/docker/api/v1alpha3/dockermachine_types.go index f5d60d91588e..33a738f777f2 100644 --- a/test/infrastructure/docker/api/v1alpha3/dockermachine_types.go +++ b/test/infrastructure/docker/api/v1alpha3/dockermachine_types.go @@ -76,6 +76,9 @@ type DockerMachineStatus struct { // +optional Ready bool `json:"ready"` + // Addresses contains the associated addresses for the docker machine. + Addresses []clusterv1.MachineAddress `json:"addresses,omitempty"` + // LoadBalancerConfigured denotes that the machine has been // added to the load balancer // +optional 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..5af459e847aa 100644 --- a/test/infrastructure/docker/controllers/dockermachine_controller.go +++ b/test/infrastructure/docker/controllers/dockermachine_controller.go @@ -264,6 +264,28 @@ func (r *DockerMachineReconciler) reconcileNormal(ctx context.Context, machine * // Update the BootstrapExecSucceededCondition condition conditions.MarkTrue(dockerMachine, infrav1.BootstrapExecSucceededCondition) + // 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, + }, + } + // Usually a cloud provider will do this, but there is no docker-cloud provider. // Requeue if there is an error, as this is likely momentary load balancer // state changes during control plane provisioning. 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.