Skip to content

Commit 83e4671

Browse files
committed
Stronger link between Machine* <-> Cluster
Signed-off-by: Vince Prignano <[email protected]>
1 parent 7704642 commit 83e4671

File tree

6 files changed

+45
-10
lines changed

6 files changed

+45
-10
lines changed

Gopkg.lock

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

docs/book/common_code/machine_controller.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,22 @@ the host with a new one matching the updated spec. If a `Machine` object is
1010
deleted, the corresponding `Node` should have its external resources released by
1111
the provider-specific controller, and should be deleted as well.
1212

13+
Machines can be associated with a Cluster using a custom label
14+
`cluster.k8s.io/cluster-name`. When the label is set and non-empty,
15+
then it must reference the name of a cluster residing in the same namespace.
16+
The label must be set only once and updates are not permitted,
17+
an admission controller is going to enforce the change in a future version.
18+
1319
{% method %}
1420
## Machine
1521

1622
`Machine` has 4 fields:
1723

18-
`Spec` contains the desired cluster state specified by the object. While much
24+
`Spec` contains the desired machine state specified by the object. While much
1925
of the `Spec` is defined by users, unspecified parts may be filled in with
2026
defaults or by Controllers such as autoscalers.
2127

22-
`Status` contains only observed cluster state and is only written by
28+
`Status` contains only observed machine state and is only written by
2329
controllers. `Status` is not the source of truth for any information, but
2430
instead aggregates and publishes observed state.
2531

@@ -93,7 +99,7 @@ methods.
9399
`Delete()` will only be called when the `Machine` is in the process of being
94100
deleted.
95101

96-
The definition of `Exist()` is determined by the provider.
102+
The definition of `Exists()` is determined by the provider.
97103

98104
**TODO**: Provide more guidance on `Exists()`.
99105

@@ -108,7 +114,7 @@ We need a diagram tracing the logic from resource creation through updates
108114
and finally deletion.
109115
{% endpanel %}
110116

111-
0. Determine the `Cluster` associated with the `Machine`.
117+
0. Determine the `Cluster` associated with the `Machine` from its `cluster.k8s.io/cluster-name` label.
112118
- If the `Machine` hasn't been deleted and doesn't have a finalizer, add one.
113119
- If the `Machine` is being deleted, and there is no finalizer, we're done
114120
- Check if the `Machine` is allowed to be deleted. [^1]

pkg/controller/machine/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ go_library(
1515
"//pkg/util:go_default_library",
1616
"//vendor/k8s.io/api/core/v1:go_default_library",
1717
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
18+
"//vendor/k8s.io/apimachinery/pkg/fields:go_default_library",
1819
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
1920
"//vendor/k8s.io/klog:go_default_library",
2021
"//vendor/sigs.k8s.io/controller-runtime/pkg/client:go_default_library",

pkg/controller/machine/controller.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"errors"
2222
"os"
2323

24+
"k8s.io/apimachinery/pkg/fields"
25+
2426
corev1 "k8s.io/api/core/v1"
2527
apierrors "k8s.io/apimachinery/pkg/api/errors"
2628
"k8s.io/apimachinery/pkg/runtime"
@@ -36,7 +38,10 @@ import (
3638
"sigs.k8s.io/controller-runtime/pkg/source"
3739
)
3840

39-
const NodeNameEnvVar = "NODE_NAME"
41+
const (
42+
NodeNameEnvVar = "NODE_NAME"
43+
MachineClusterLabelName = "cluster.k8s.io/cluster-name"
44+
)
4045

4146
var DefaultActuator Actuator
4247

@@ -116,8 +121,7 @@ func (r *ReconcileMachine) Reconcile(request reconcile.Request) (reconcile.Resul
116121
// for machine management.
117122
cluster, err := r.getCluster(ctx, m)
118123
if err != nil {
119-
// Just log the error here.
120-
klog.V(4).Infof("Cluster not found, machine actuation might fail: %v", err)
124+
return reconcile.Result{}, err
121125
}
122126
// If object hasn't been deleted and doesn't have a finalizer, add one
123127
// Add a finalizer to newly created objects.
@@ -193,9 +197,15 @@ func (r *ReconcileMachine) Reconcile(request reconcile.Request) (reconcile.Resul
193197
}
194198

195199
func (r *ReconcileMachine) getCluster(ctx context.Context, machine *clusterv1.Machine) (*clusterv1.Cluster, error) {
200+
if machine.Labels[MachineClusterLabelName] == "" {
201+
klog.Infof("Machine %q in namespace %q doesn't specify %q label, assuming nil cluster", machine.Name, MachineClusterLabelName, machine.Namespace)
202+
return nil, nil
203+
}
204+
196205
clusterList := clusterv1.ClusterList{}
197206
listOptions := &client.ListOptions{
198-
Namespace: machine.Namespace,
207+
FieldSelector: fields.OneTermEqualSelector("metadata.name", machine.Labels[MachineClusterLabelName]),
208+
Namespace: machine.Namespace,
199209
}
200210

201211
if err := r.Client.List(ctx, listOptions, &clusterList); err != nil {

pkg/controller/machine/controller_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ func TestReconcileRequest(t *testing.T) {
3737
Name: "create",
3838
Namespace: "default",
3939
Finalizers: []string{v1alpha1.MachineFinalizer},
40+
Labels: map[string]string{
41+
MachineClusterLabelName: "testcluster",
42+
},
4043
},
4144
}
4245
machine2 := v1alpha1.Machine{
@@ -47,6 +50,9 @@ func TestReconcileRequest(t *testing.T) {
4750
Name: "update",
4851
Namespace: "default",
4952
Finalizers: []string{v1alpha1.MachineFinalizer},
53+
Labels: map[string]string{
54+
MachineClusterLabelName: "testcluster",
55+
},
5056
},
5157
}
5258
time := metav1.Now()
@@ -59,6 +65,9 @@ func TestReconcileRequest(t *testing.T) {
5965
Namespace: "default",
6066
Finalizers: []string{v1alpha1.MachineFinalizer},
6167
DeletionTimestamp: &time,
68+
Labels: map[string]string{
69+
MachineClusterLabelName: "testcluster",
70+
},
6271
},
6372
}
6473
clusterList := v1alpha1.ClusterList{
@@ -71,10 +80,19 @@ func TestReconcileRequest(t *testing.T) {
7180
Kind: "Cluster",
7281
},
7382
ObjectMeta: metav1.ObjectMeta{
74-
Name: "cluster",
83+
Name: "testcluster",
7584
Namespace: "default",
7685
},
7786
},
87+
{
88+
TypeMeta: metav1.TypeMeta{
89+
Kind: "Cluster",
90+
},
91+
ObjectMeta: metav1.ObjectMeta{
92+
Name: "rainbow",
93+
Namespace: "foo",
94+
},
95+
},
7896
},
7997
}
8098

pkg/controller/machinedeployment/util/util_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,6 @@ func TestMaxUnavailable(t *testing.T) {
698698
}
699699

700700
for _, test := range tests {
701-
t.Log(test.name)
702701
t.Run(test.name, func(t *testing.T) {
703702
maxUnavailable := MaxUnavailable(test.deployment)
704703
if test.expected != maxUnavailable {

0 commit comments

Comments
 (0)