Skip to content

Commit cc812ca

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

File tree

8 files changed

+81
-5
lines changed

8 files changed

+81
-5
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.

config/crds/cluster_v1alpha1_machine.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ spec:
4242
to indicate what labels, annotations, name prefix, etc., should be
4343
used when creating the Node.
4444
type: object
45+
providerID:
46+
description: ProviderID is the identification ID of the machine provided
47+
by the provider. This field must match the provider ID as seen on
48+
the node object corresponding to this machine. This field is required
49+
by higher level consumers of cluster-api. Example use case is cluster
50+
autoscaler with cluster-api as provider. Clean-up login in the autoscaler
51+
compares machines v/s nodes to find out machines at provider which
52+
could not get registered as Kubernetes nodes. With cluster-api as
53+
a generic out-of-tree provider for autoscaler, this field is required
54+
by autoscaler to be able to have a provider view of the list of machines.
55+
Another list of nodes is queries from the k8s apiserver and then comparison
56+
is done to find out unregistered machines and are marked for delete.
57+
This field will be set by the actuators and consumed by higher level
58+
entities like autoscaler who will be interfacing with cluster-api
59+
as generic provider.
60+
type: string
4561
providerSpec:
4662
description: ProviderSpec details Provider-specific configuration to
4763
use during node creation.

config/crds/cluster_v1alpha1_machinedeployment.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@ spec:
136136
Use this to indicate what labels, annotations, name prefix,
137137
etc., should be used when creating the Node.
138138
type: object
139+
providerID:
140+
description: ProviderID is the identification ID of the machine
141+
provided by the provider. This field must match the provider
142+
ID as seen on the node object corresponding to this machine.
143+
This field is required by higher level consumers of cluster-api.
144+
Example use case is cluster autoscaler with cluster-api as
145+
provider. Clean-up login in the autoscaler compares machines
146+
v/s nodes to find out machines at provider which could not
147+
get registered as Kubernetes nodes. With cluster-api as a
148+
generic out-of-tree provider for autoscaler, this field is
149+
required by autoscaler to be able to have a provider view
150+
of the list of machines. Another list of nodes is queries
151+
from the k8s apiserver and then comparison is done to find
152+
out unregistered machines and are marked for delete. This
153+
field will be set by the actuators and consumed by higher
154+
level entities like autoscaler who will be interfacing with
155+
cluster-api as generic provider.
156+
type: string
139157
providerSpec:
140158
description: ProviderSpec details Provider-specific configuration
141159
to use during node creation.

config/crds/cluster_v1alpha1_machineset.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,24 @@ spec:
7676
Use this to indicate what labels, annotations, name prefix,
7777
etc., should be used when creating the Node.
7878
type: object
79+
providerID:
80+
description: ProviderID is the identification ID of the machine
81+
provided by the provider. This field must match the provider
82+
ID as seen on the node object corresponding to this machine.
83+
This field is required by higher level consumers of cluster-api.
84+
Example use case is cluster autoscaler with cluster-api as
85+
provider. Clean-up login in the autoscaler compares machines
86+
v/s nodes to find out machines at provider which could not
87+
get registered as Kubernetes nodes. With cluster-api as a
88+
generic out-of-tree provider for autoscaler, this field is
89+
required by autoscaler to be able to have a provider view
90+
of the list of machines. Another list of nodes is queries
91+
from the k8s apiserver and then comparison is done to find
92+
out unregistered machines and are marked for delete. This
93+
field will be set by the actuators and consumed by higher
94+
level entities like autoscaler who will be interfacing with
95+
cluster-api as generic provider.
96+
type: string
7997
providerSpec:
8098
description: ProviderSpec details Provider-specific configuration
8199
to use during node creation.

pkg/controller/machine/BUILD.bazel

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

pkg/controller/machine/controller.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
corev1 "k8s.io/api/core/v1"
2525
apierrors "k8s.io/apimachinery/pkg/api/errors"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/fields"
2728
"k8s.io/apimachinery/pkg/runtime"
2829
"k8s.io/klog"
2930
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
@@ -117,8 +118,7 @@ func (r *ReconcileMachine) Reconcile(request reconcile.Request) (reconcile.Resul
117118
// for machine management.
118119
cluster, err := r.getCluster(ctx, m)
119120
if err != nil {
120-
// Just log the error here.
121-
klog.V(4).Infof("Cluster not found, machine actuation might fail: %v", err)
121+
return reconcile.Result{}, err
122122
}
123123
// If object hasn't been deleted and doesn't have a finalizer, add one
124124
// Add a finalizer to newly created objects.
@@ -194,9 +194,14 @@ func (r *ReconcileMachine) Reconcile(request reconcile.Request) (reconcile.Resul
194194
}
195195

196196
func (r *ReconcileMachine) getCluster(ctx context.Context, machine *clusterv1.Machine) (*clusterv1.Cluster, error) {
197+
if machine.Labels["cluster"] == "" {
198+
return nil, errors.New("missing `cluster` label in Machine spec")
199+
}
200+
197201
clusterList := clusterv1.ClusterList{}
198202
listOptions := &client.ListOptions{
199-
Namespace: machine.Namespace,
203+
FieldSelector: fields.OneTermEqualSelector("metadata.name", machine.Labels["cluster"]),
204+
Namespace: machine.Namespace,
200205
// This is set so the fake client can be used for unit test. See:
201206
// https://github.com/kubernetes-sigs/controller-runtime/issues/168
202207
Raw: &metav1.ListOptions{

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+
"cluster": "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+
"cluster": "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+
"cluster": "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)