Skip to content
This repository was archived by the owner on Nov 20, 2021. It is now read-only.

Commit 67cdc77

Browse files
committed
Add machine addresses
1 parent b7a76d6 commit 67cdc77

6 files changed

Lines changed: 77 additions & 64 deletions

File tree

api/v1alpha1/dockermachine_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20+
machinev1 "github.com/criticalstack/machine-api/api/v1alpha1"
2021
mapierrors "github.com/criticalstack/machine-api/errors"
2122
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2223
)
@@ -38,6 +39,10 @@ type DockerMachineSpec struct {
3839
// DockerMachineStatus defines the observed state of DockerMachine
3940
type DockerMachineStatus struct {
4041
Ready bool `json:"ready"`
42+
43+
// Addresses contains the docker container associated addresses.
44+
Addresses machinev1.MachineAddresses `json:"addresses,omitempty"`
45+
4146
// FailureReason will be set in the event that there is a terminal problem
4247
// reconciling the Machine and will contain a succinct value suitable
4348
// for machine interpretation.

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/infrastructure.crit.sh_dockerinfrastructureproviders.yaml

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,26 @@ spec:
2929
status: {}
3030
validation:
3131
openAPIV3Schema:
32-
description: DockerInfrastructureProvider is the Schema for the infrastructureproviders
33-
API
32+
description: DockerInfrastructureProvider is the Schema for the infrastructureproviders API
3433
properties:
3534
apiVersion:
36-
description: 'APIVersion defines the versioned schema of this representation
37-
of an object. Servers should convert recognized schemas to the latest
38-
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
35+
description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
3936
type: string
4037
kind:
41-
description: 'Kind is a string value representing the REST resource this
42-
object represents. Servers may infer this from the endpoint the client
43-
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
38+
description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
4439
type: string
4540
metadata:
4641
type: object
4742
spec:
48-
description: DockerInfrastructureProviderSpec defines the desired state
49-
of DockerInfrastructureProvider
43+
description: DockerInfrastructureProviderSpec defines the desired state of DockerInfrastructureProvider
5044
properties:
5145
defaultImage:
5246
type: string
5347
required:
5448
- defaultImage
5549
type: object
5650
status:
57-
description: InfrastructureProviderStatus defines the observed state of
58-
DockerInfrastructureProvider
51+
description: InfrastructureProviderStatus defines the observed state of DockerInfrastructureProvider
5952
properties:
6053
lastUpdated:
6154
format: date-time

config/crd/bases/infrastructure.crit.sh_dockermachines.yaml

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@ spec:
2424
description: DockerMachine is the Schema for the dockermachines API
2525
properties:
2626
apiVersion:
27-
description: 'APIVersion defines the versioned schema of this representation
28-
of an object. Servers should convert recognized schemas to the latest
29-
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
27+
description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
3028
type: string
3129
kind:
32-
description: 'Kind is a string value representing the REST resource this
33-
object represents. Servers may infer this from the endpoint the client
34-
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
30+
description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
3531
type: string
3632
metadata:
3733
type: object
@@ -54,35 +50,27 @@ spec:
5450
status:
5551
description: DockerMachineStatus defines the observed state of DockerMachine
5652
properties:
53+
addresses:
54+
description: Addresses contains the docker container associated addresses.
55+
items:
56+
description: MachineAddress contains information for the node's address.
57+
properties:
58+
address:
59+
description: The machine address.
60+
type: string
61+
type:
62+
description: Machine address type, one of Hostname, ExternalIP or InternalIP.
63+
type: string
64+
required:
65+
- address
66+
- type
67+
type: object
68+
type: array
5769
failureMessage:
58-
description: "FailureMessage will be set in the event that there is
59-
a terminal problem reconciling the Machine and will contain a more
60-
verbose string suitable for logging and human consumption. \n This
61-
field should not be set for transitive errors that a controller faces
62-
that are expected to be fixed automatically over time (like service
63-
outages), but instead indicate that something is fundamentally wrong
64-
with the Machine's spec or the configuration of the controller, and
65-
that manual intervention is required. Examples of terminal errors
66-
would be invalid combinations of settings in the spec, values that
67-
are unsupported by the controller, or the responsible controller itself
68-
being critically misconfigured. \n Any transient errors that occur
69-
during the reconciliation of Machines can be added as events to the
70-
Machine object and/or logged in the controller's output."
70+
description: "FailureMessage will be set in the event that there is a terminal problem reconciling the Machine and will contain a more verbose string suitable for logging and human consumption. \n This field should not be set for transitive errors that a controller faces that are expected to be fixed automatically over time (like service outages), but instead indicate that something is fundamentally wrong with the Machine's spec or the configuration of the controller, and that manual intervention is required. Examples of terminal errors would be invalid combinations of settings in the spec, values that are unsupported by the controller, or the responsible controller itself being critically misconfigured. \n Any transient errors that occur during the reconciliation of Machines can be added as events to the Machine object and/or logged in the controller's output."
7171
type: string
7272
failureReason:
73-
description: "FailureReason will be set in the event that there is a
74-
terminal problem reconciling the Machine and will contain a succinct
75-
value suitable for machine interpretation. \n This field should not
76-
be set for transitive errors that a controller faces that are expected
77-
to be fixed automatically over time (like service outages), but instead
78-
indicate that something is fundamentally wrong with the Machine's
79-
spec or the configuration of the controller, and that manual intervention
80-
is required. Examples of terminal errors would be invalid combinations
81-
of settings in the spec, values that are unsupported by the controller,
82-
or the responsible controller itself being critically misconfigured.
83-
\n Any transient errors that occur during the reconciliation of Machines
84-
can be added as events to the Machine object and/or logged in the
85-
controller's output."
73+
description: "FailureReason will be set in the event that there is a terminal problem reconciling the Machine and will contain a succinct value suitable for machine interpretation. \n This field should not be set for transitive errors that a controller faces that are expected to be fixed automatically over time (like service outages), but instead indicate that something is fundamentally wrong with the Machine's spec or the configuration of the controller, and that manual intervention is required. Examples of terminal errors would be invalid combinations of settings in the spec, values that are unsupported by the controller, or the responsible controller itself being critically misconfigured. \n Any transient errors that occur during the reconciliation of Machines can be added as events to the Machine object and/or logged in the controller's output."
8674
type: string
8775
ready:
8876
type: boolean

controllers/node_controller.go

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"errors"
2020
"fmt"
2121

22+
cinderapi "github.com/criticalstack/crit/cmd/cinder/api"
2223
nodeutil "github.com/criticalstack/crit/pkg/kubernetes/util/node"
2324
machinev1 "github.com/criticalstack/machine-api/api/v1alpha1"
2425
"github.com/go-logr/logr"
@@ -94,35 +95,35 @@ func (r *NodeReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
9495
if !ok {
9596
return ctrl.Result{}, errors.New("cannot find DockerMachine, missing infra annotation")
9697
}
97-
var amRef corev1.ObjectReference
98-
if err := json.Unmarshal([]byte(dockerRefData), &amRef); err != nil {
98+
var dmRef corev1.ObjectReference
99+
if err := json.Unmarshal([]byte(dockerRefData), &dmRef); err != nil {
99100
return ctrl.Result{}, err
100101
}
101-
//log.Info("machine label not found")
102-
am := &infrav1.DockerMachine{}
103-
if err := r.Get(ctx, client.ObjectKey{Namespace: metav1.NamespaceSystem, Name: amRef.Name}, am); err != nil {
102+
dm := &infrav1.DockerMachine{}
103+
if err := r.Get(ctx, client.ObjectKey{Namespace: metav1.NamespaceSystem, Name: dmRef.Name}, dm); err != nil {
104104
return ctrl.Result{}, err
105105
}
106-
if err := r.ensureMachineHasInfraRef(ctx, am, ref); err != nil {
106+
if err := r.ensureMachineHasInfraRef(ctx, dm, ref); err != nil {
107107
return ctrl.Result{}, err
108108
}
109109
}
110110
return ctrl.Result{}, nil
111111
}
112112

113-
func (r *NodeReconciler) ensureMachineHasInfraRef(ctx context.Context, am *infrav1.DockerMachine, ref corev1.ObjectReference) error {
113+
// TODO(chrism): move this to the machine controller
114+
func (r *NodeReconciler) ensureMachineHasInfraRef(ctx context.Context, dm *infrav1.DockerMachine, ref corev1.ObjectReference) error {
114115
m := &machinev1.Machine{}
115116
if err := r.Get(ctx, client.ObjectKey{Namespace: metav1.NamespaceSystem, Name: ref.Name}, m); err != nil {
116117
return err
117118
}
118-
if m.Spec.InfrastructureRef.Kind == "DockerMachine" && m.Spec.InfrastructureRef.Name == am.Name {
119+
if m.Spec.InfrastructureRef != nil && m.Spec.InfrastructureRef.Kind == "DockerMachine" && m.Spec.InfrastructureRef.Name == dm.Name {
119120
return nil
120121
}
121-
m.Spec.InfrastructureRef = corev1.ObjectReference{
122-
APIVersion: am.APIVersion,
122+
m.Spec.InfrastructureRef = &corev1.ObjectReference{
123+
APIVersion: dm.APIVersion,
123124
Kind: "DockerMachine",
124-
Name: am.ObjectMeta.Name,
125-
Namespace: am.Namespace,
125+
Name: dm.ObjectMeta.Name,
126+
Namespace: dm.Namespace,
126127
}
127128
if err := r.Update(ctx, m); err != nil {
128129
return err
@@ -142,7 +143,7 @@ func (r *NodeReconciler) ensureDockerMachineForNode(ctx context.Context, n *core
142143
return err
143144
}
144145
for _, m := range machines.Items {
145-
if m.Spec.ProviderID == n.Spec.ProviderID {
146+
if m.Spec.ProviderID != "" && m.Spec.ProviderID == n.Spec.ProviderID {
146147
log.V(1).Info("node already has a machine associated with it, only needs an annotation")
147148
return r.setDockerMachineAnnotation(ctx, &m, n.Name)
148149
}
@@ -151,7 +152,7 @@ func (r *NodeReconciler) ensureDockerMachineForNode(ctx context.Context, n *core
151152
if err != nil {
152153
return err
153154
}
154-
am := &infrav1.DockerMachine{
155+
dm := &infrav1.DockerMachine{
155156
ObjectMeta: metav1.ObjectMeta{
156157
Name: n.Name,
157158
Namespace: metav1.NamespaceSystem,
@@ -162,12 +163,32 @@ func (r *NodeReconciler) ensureDockerMachineForNode(ctx context.Context, n *core
162163
ContainerName: n.Name,
163164
Image: "",
164165
},
165-
Status: infrav1.DockerMachineStatus{},
166166
}
167-
if err := r.Create(ctx, am); err != nil {
167+
if err := r.Create(ctx, dm); err != nil {
168+
if !apierrors.IsAlreadyExists(err) {
169+
return err
170+
}
171+
}
172+
nodes, err := cinderapi.ListNodes(clusterName)
173+
if err != nil {
174+
return err
175+
}
176+
addresses := make([]machinev1.MachineAddress, 0)
177+
for _, node := range nodes {
178+
if node.String() == n.Name {
179+
addresses = append(addresses, machinev1.MachineAddress{
180+
Type: machinev1.MachineInternalIP,
181+
Address: node.IP(),
182+
})
183+
break
184+
}
185+
}
186+
dm.Status.Addresses = addresses
187+
dm.Status.Ready = true
188+
if err := r.Status().Update(ctx, dm); err != nil {
168189
return err
169190
}
170-
return r.setDockerMachineAnnotation(ctx, am, n.Name)
191+
return r.setDockerMachineAnnotation(ctx, dm, n.Name)
171192
}
172193

173194
func (r *NodeReconciler) findNodeCluster(nodeName string) (string, error) {
@@ -177,7 +198,7 @@ func (r *NodeReconciler) findNodeCluster(nodeName string) (string, error) {
177198
return "", err
178199
}
179200
for _, c := range clusters {
180-
nodes, err := provider.ListNodes(c)
201+
nodes, err := cinderapi.ListNodes(c)
181202
if err != nil {
182203
return "", err
183204
}
@@ -192,7 +213,7 @@ func (r *NodeReconciler) findNodeCluster(nodeName string) (string, error) {
192213

193214
func (r *NodeReconciler) setDockerMachineAnnotation(ctx context.Context, m *infrav1.DockerMachine, name string) error {
194215
ref := corev1.ObjectReference{
195-
APIVersion: m.APIVersion,
216+
APIVersion: infrav1.GroupVersion.String(),
196217
Kind: "DockerMachine",
197218
Name: m.ObjectMeta.Name,
198219
Namespace: m.Namespace,

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.14
44

55
require (
66
github.com/criticalstack/crit v1.0.3
7-
github.com/criticalstack/machine-api v1.0.3
7+
github.com/criticalstack/machine-api v1.0.5
88
github.com/go-logr/logr v0.1.0
99
github.com/go-openapi/spec v0.19.3
1010
github.com/onsi/ginkgo v1.12.1

0 commit comments

Comments
 (0)