Skip to content

Commit 526abe7

Browse files
committed
Propagate node labels to the K8s API
1 parent d4415d5 commit 526abe7

File tree

6 files changed

+645
-11
lines changed

6 files changed

+645
-11
lines changed

README.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
# Device Tree Kubernetes Node Labeller
22

3-
This tool automatically labels nodes with [devicetree] properties, useful for targeted deployments into hybrid
4-
Kubernetes clusters, as well as for targeting heterogeneous accelerator resources in Edge deployments.
3+
This tool provides a custom Kubernetes controller for automatically labelling nodes with [devicetree] properties.
54

6-
[devicetree]: https://www.devicetree.org
5+
It is inspired by and re-uses much of the Kubernetes controller plumbing from the [amdgpu-node-labeller].
6+
7+
## Use Cases
8+
9+
`k8s-dt-node-labeller` was developed in order to facilitate targeted deployments into hybrid (e.g. armhf, arm64)
10+
Kubernetes clusters, as well as for targeting heterogeneous accelerators in Edge deployments.
11+
12+
## Usage
13+
14+
The node labeller is further expected to be run node-local, and will need to be invoked on each individual node
15+
requiring its own specific devicetree parsing and labelling.
716

817
By default, `compatible` strings from the top-level `/` node are discovered and converted to node labels.
918

@@ -15,7 +24,7 @@ beta.devicetree.org/nvidia-jetson-nano: 1
1524
beta.devicetree.org/nvidia-tegra210: 1
1625
```
1726

18-
node specifications are possible via the `-n` flag, as below:
27+
additional node specifications are possible via the `-n` flag, as below:
1928

2029
```
2130
$ k8s-dt-node-labeller -n gpu pwm-fan
@@ -33,9 +42,11 @@ beta.devicetree.org/pwm-fan: 1
3342
This project has received funding from the European Union’s Horizon 2020 research and innovation programme under grant
3443
agreement No 825480 ([SODALITE]).
3544

36-
[SODALITE]: https://www.sodalite.eu
37-
3845
## License
3946

4047
`k8s-dt-node-labeller` is licensed under the terms of the Apache 2.0 license, the full
4148
version of which can be found in the LICENSE file included in the distribution.
49+
50+
[devicetree]: https://www.devicetree.org
51+
[SODALITE]: https://www.sodalite.eu
52+
[amdgpu-node-labeller]: https://github.com/RadeonOpenCompute/k8s-device-plugin/tree/master/cmd/k8s-node-labeller

controller.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"context"
5+
6+
"github.com/go-logr/logr"
7+
8+
corev1 "k8s.io/api/core/v1"
9+
"k8s.io/apimachinery/pkg/api/errors"
10+
"sigs.k8s.io/controller-runtime/pkg/client"
11+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
12+
)
13+
14+
type reconcileNodeLabels struct {
15+
client client.Client
16+
log logr.Logger
17+
labels map[string]string
18+
}
19+
20+
// make sure reconcileNodeLabels implement the Reconciler interface
21+
var _ reconcile.Reconciler = &reconcileNodeLabels{}
22+
23+
func (r *reconcileNodeLabels) Reconcile(request reconcile.Request) (reconcile.Result, error) {
24+
// set up a convinient log object so we don't have to type request over and over again
25+
log := r.log.WithValues("request", request)
26+
27+
node := &corev1.Node{}
28+
err := r.client.Get(context.TODO(), request.NamespacedName, node)
29+
if errors.IsNotFound(err) {
30+
log.Error(nil, "Could not find Node")
31+
return reconcile.Result{}, nil
32+
}
33+
34+
if err != nil {
35+
log.Error(err, "Could not fetch Node")
36+
return reconcile.Result{}, err
37+
}
38+
39+
// Set the label
40+
if node.Labels == nil {
41+
node.Labels = map[string]string{}
42+
}
43+
44+
// Remove old labels
45+
for k := range node.Labels {
46+
if matchesLabelPrefix(k) {
47+
delete(node.Labels, k)
48+
}
49+
}
50+
51+
for k, v := range r.labels {
52+
node.Labels[k] = v
53+
}
54+
55+
err = r.client.Update(context.TODO(), node)
56+
if err != nil {
57+
log.Error(err, "Could not write Node")
58+
return reconcile.Result{}, err
59+
}
60+
61+
return reconcile.Result{}, nil
62+
}

go.mod

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@ module k8s-dt-node-labeller
22

33
go 1.14
44

5-
require github.com/platinasystems/fdt v1.0.1
5+
require (
6+
github.com/go-logr/logr v0.1.0
7+
github.com/platinasystems/fdt v1.0.1
8+
k8s.io/api v0.18.0
9+
k8s.io/apimachinery v0.18.0
10+
sigs.k8s.io/controller-runtime v0.5.2
11+
)

0 commit comments

Comments
 (0)