Skip to content

Commit c04d34e

Browse files
authored
Merge pull request #3668 from alexander-demichev/labels
✨ Label interruptible nodes
2 parents d85b6bf + 515a30a commit c04d34e

File tree

4 files changed

+238
-0
lines changed

4 files changed

+238
-0
lines changed

api/v1alpha4/common_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ const (
4848

4949
// ClusterSecretType defines the type of secret created by core components
5050
ClusterSecretType corev1.SecretType = "cluster.x-k8s.io/secret" //nolint:gosec
51+
52+
// InterruptibleLabel is the label used to mark the nodes that run on interruptible instances
53+
InterruptibleLabel = "cluster.x-k8s.io/interruptible"
5154
)
5255

5356
// MachineAddressType describes a valid MachineAddress type.

controllers/machine_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ func (r *MachineReconciler) reconcile(ctx context.Context, cluster *clusterv1.Cl
267267
r.reconcileBootstrap,
268268
r.reconcileInfrastructure,
269269
r.reconcileNode,
270+
r.reconcileInterruptibleNodeLabel,
270271
}
271272

272273
res := ctrl.Result{}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controllers
18+
19+
import (
20+
"context"
21+
22+
apicorev1 "k8s.io/api/core/v1"
23+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
24+
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
25+
"sigs.k8s.io/cluster-api/controllers/external"
26+
"sigs.k8s.io/cluster-api/util"
27+
"sigs.k8s.io/cluster-api/util/patch"
28+
ctrl "sigs.k8s.io/controller-runtime"
29+
"sigs.k8s.io/controller-runtime/pkg/client"
30+
)
31+
32+
func (r *MachineReconciler) reconcileInterruptibleNodeLabel(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine) (ctrl.Result, error) {
33+
// Check that the Machine hasn't been deleted or in the process
34+
// and that the Machine has a NodeRef.
35+
if !machine.DeletionTimestamp.IsZero() || machine.Status.NodeRef == nil {
36+
return ctrl.Result{}, nil
37+
}
38+
39+
// Get the infrastructure object
40+
infra, err := external.Get(ctx, r.Client, &machine.Spec.InfrastructureRef, machine.Namespace)
41+
if err != nil {
42+
return ctrl.Result{}, err
43+
}
44+
45+
log := ctrl.LoggerFrom(ctx)
46+
47+
// Get interruptible instance status from the infrastructure provider.
48+
interruptible, _, err := unstructured.NestedBool(infra.Object, "status", "interruptible")
49+
if err != nil {
50+
log.V(1).Error(err, "Failed to get interruptible status from infrastructure provider", "machinename", machine.Name)
51+
return ctrl.Result{}, nil
52+
}
53+
if !interruptible {
54+
return ctrl.Result{}, nil
55+
}
56+
57+
remoteClient, err := r.Tracker.GetClient(ctx, util.ObjectKey(cluster))
58+
if err != nil {
59+
return ctrl.Result{}, err
60+
}
61+
62+
if err := r.setInterruptibleNodeLabel(ctx, remoteClient, machine.Status.NodeRef.Name); err != nil {
63+
return ctrl.Result{}, err
64+
}
65+
66+
log.V(3).Info("Set interruptible label to Machine's Node", "nodename", machine.Status.NodeRef.Name)
67+
r.recorder.Event(machine, apicorev1.EventTypeNormal, "SuccessfulSetInterruptibleNodeLabel", machine.Status.NodeRef.Name)
68+
69+
return ctrl.Result{}, nil
70+
}
71+
72+
func (r *MachineReconciler) setInterruptibleNodeLabel(ctx context.Context, remoteClient client.Client, nodeName string) error {
73+
node := &apicorev1.Node{}
74+
if err := remoteClient.Get(ctx, client.ObjectKey{Name: nodeName}, node); err != nil {
75+
return err
76+
}
77+
78+
if node.Labels == nil {
79+
node.Labels = map[string]string{}
80+
}
81+
82+
if _, ok := node.Labels[clusterv1.InterruptibleLabel]; ok {
83+
return nil
84+
}
85+
86+
patchHelper, err := patch.NewHelper(node, r.Client)
87+
if err != nil {
88+
return err
89+
}
90+
91+
node.Labels[clusterv1.InterruptibleLabel] = ""
92+
93+
return patchHelper.Patch(ctx, node)
94+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controllers
18+
19+
import (
20+
"context"
21+
"testing"
22+
"time"
23+
24+
. "github.com/onsi/gomega"
25+
26+
corev1 "k8s.io/api/core/v1"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
29+
"k8s.io/client-go/kubernetes/scheme"
30+
"k8s.io/client-go/tools/record"
31+
32+
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
33+
"sigs.k8s.io/cluster-api/controllers/remote"
34+
"sigs.k8s.io/cluster-api/util/patch"
35+
"sigs.k8s.io/controller-runtime/pkg/client"
36+
"sigs.k8s.io/controller-runtime/pkg/log"
37+
)
38+
39+
func TestReconcileInterruptibleNodeLabel(t *testing.T) {
40+
g := NewWithT(t)
41+
42+
ns, err := testEnv.CreateNamespace(ctx, "test-interruptible-node-label")
43+
g.Expect(err).ToNot(HaveOccurred())
44+
45+
infraMachine := &unstructured.Unstructured{
46+
Object: map[string]interface{}{
47+
"kind": "InfrastructureMachine",
48+
"apiVersion": "infrastructure.cluster.x-k8s.io/v1alpha4",
49+
"metadata": map[string]interface{}{
50+
"name": "infra-config1",
51+
"namespace": ns.Name,
52+
},
53+
"status": map[string]interface{}{
54+
"interruptible": true,
55+
},
56+
},
57+
}
58+
59+
cluster := &clusterv1.Cluster{
60+
ObjectMeta: metav1.ObjectMeta{
61+
Name: "cluster-1",
62+
Namespace: ns.Name,
63+
},
64+
}
65+
66+
node := &corev1.Node{
67+
ObjectMeta: metav1.ObjectMeta{
68+
Name: "node-1",
69+
},
70+
}
71+
72+
machine := &clusterv1.Machine{
73+
ObjectMeta: metav1.ObjectMeta{
74+
Name: "machine-test",
75+
Namespace: ns.Name,
76+
},
77+
Spec: clusterv1.MachineSpec{
78+
ClusterName: cluster.Name,
79+
InfrastructureRef: corev1.ObjectReference{
80+
APIVersion: "infrastructure.cluster.x-k8s.io/v1alpha4",
81+
Kind: "InfrastructureMachine",
82+
Name: "infra-config1",
83+
Namespace: ns.Name,
84+
},
85+
Bootstrap: clusterv1.Bootstrap{
86+
ConfigRef: &corev1.ObjectReference{
87+
APIVersion: "bootstrap.cluster.x-k8s.io/v1alpha4",
88+
Kind: "BootstrapMachine",
89+
Name: "bootstrap-config1",
90+
},
91+
},
92+
},
93+
Status: clusterv1.MachineStatus{
94+
NodeRef: &corev1.ObjectReference{
95+
Name: "node-1",
96+
},
97+
},
98+
}
99+
100+
g.Expect(testEnv.Create(ctx, cluster)).To(Succeed())
101+
g.Expect(testEnv.Create(ctx, node)).To(Succeed())
102+
g.Expect(testEnv.Create(ctx, infraMachine)).To(Succeed())
103+
g.Expect(testEnv.Create(ctx, machine)).To(Succeed())
104+
105+
// Patch infra machine status
106+
patchHelper, err := patch.NewHelper(infraMachine, testEnv)
107+
g.Expect(err).ShouldNot(HaveOccurred())
108+
g.Expect(unstructured.SetNestedField(infraMachine.Object, true, "status", "interruptible")).To(Succeed())
109+
g.Expect(patchHelper.Patch(ctx, infraMachine, patch.WithStatusObservedGeneration{})).To(Succeed())
110+
111+
defer func(do ...client.Object) {
112+
g.Expect(testEnv.Cleanup(ctx, do...)).To(Succeed())
113+
}(cluster, node, infraMachine, machine)
114+
115+
r := &MachineReconciler{
116+
Client: testEnv.Client,
117+
Tracker: remote.NewTestClusterCacheTracker(log.NullLogger{}, testEnv.Client, scheme.Scheme, client.ObjectKey{Name: cluster.Name, Namespace: cluster.Namespace}),
118+
recorder: record.NewFakeRecorder(32),
119+
}
120+
121+
_, err = r.reconcileInterruptibleNodeLabel(context.Background(), cluster, machine)
122+
g.Expect(err).ToNot(HaveOccurred())
123+
124+
// Check if node gets interruptible label
125+
g.Eventually(func() bool {
126+
updatedNode := &corev1.Node{}
127+
err := testEnv.Get(ctx, client.ObjectKey{Name: node.Name}, updatedNode)
128+
if err != nil {
129+
return false
130+
}
131+
132+
if updatedNode.Labels == nil {
133+
return false
134+
}
135+
136+
_, ok := updatedNode.Labels[clusterv1.InterruptibleLabel]
137+
138+
return ok
139+
}, 10*time.Second).Should(BeTrue())
140+
}

0 commit comments

Comments
 (0)