Skip to content

Commit a57ce1d

Browse files
authored
Add the current running node for a Pod into the Pod annotations (#1657)
* Add the current running node for a Pod into the Pod annotations
1 parent 39389d7 commit a57ce1d

File tree

5 files changed

+107
-69
lines changed

5 files changed

+107
-69
lines changed

api/v1beta2/foundationdb_labels.go

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ const (
4545
// IP for a pod.
4646
PublicIPAnnotation = "foundationdb.org/public-ip"
4747

48+
// NodeAnnotation is an annotation key that specifies where a Pod is currently running on.
49+
// The information is fetched from Pod.Spec.NodeName of the Pod resource.
50+
NodeAnnotation = "foundationdb.org/current-node"
51+
4852
// FDBProcessGroupIDLabel represents the label that is used to represent a instance ID
4953
FDBProcessGroupIDLabel = "foundationdb.org/fdb-process-group-id"
5054

controllers/cluster_controller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (r *FoundationDBClusterReconciler) Reconcile(ctx context.Context, request c
141141
removeIncompatibleProcesses{},
142142
updateSidecarVersions{},
143143
updatePodConfig{},
144-
updateLabels{},
144+
updateMetadata{},
145145
updateDatabaseConfiguration{},
146146
chooseRemovals{},
147147
excludeProcesses{},

controllers/cluster_controller_test.go

+24-20
Original file line numberDiff line numberDiff line change
@@ -1300,18 +1300,20 @@ var _ = Describe("cluster_controller", func() {
13001300
Expect(err).NotTo(HaveOccurred())
13011301
if item.Labels[fdbv1beta2.FDBProcessGroupIDLabel] == "storage-1" {
13021302
Expect(item.ObjectMeta.Annotations).To(Equal(map[string]string{
1303-
"foundationdb.org/last-applied-config-map": configMapHash,
1304-
"foundationdb.org/last-applied-spec": hash,
1305-
"foundationdb.org/public-ip-source": "pod",
1306-
"foundationdb.org/existing-annotation": "test-value",
1307-
"fdb-annotation": "value1",
1303+
fdbv1beta2.LastConfigMapKey: configMapHash,
1304+
fdbv1beta2.LastSpecKey: hash,
1305+
fdbv1beta2.PublicIPSourceAnnotation: "pod",
1306+
"foundationdb.org/existing-annotation": "test-value",
1307+
"fdb-annotation": "value1",
1308+
fdbv1beta2.NodeAnnotation: item.Spec.NodeName,
13081309
}))
13091310
} else {
13101311
Expect(item.ObjectMeta.Annotations).To(Equal(map[string]string{
1311-
"foundationdb.org/last-applied-config-map": configMapHash,
1312-
"foundationdb.org/last-applied-spec": hash,
1313-
"foundationdb.org/public-ip-source": "pod",
1314-
"fdb-annotation": "value1",
1312+
fdbv1beta2.LastConfigMapKey: configMapHash,
1313+
fdbv1beta2.LastSpecKey: hash,
1314+
fdbv1beta2.PublicIPSourceAnnotation: "pod",
1315+
"fdb-annotation": "value1",
1316+
fdbv1beta2.NodeAnnotation: item.Spec.NodeName,
13151317
}))
13161318
}
13171319
}
@@ -1323,7 +1325,7 @@ var _ = Describe("cluster_controller", func() {
13231325
Expect(err).NotTo(HaveOccurred())
13241326
for _, item := range pvcs.Items {
13251327
Expect(item.ObjectMeta.Annotations).To(Equal(map[string]string{
1326-
"foundationdb.org/last-applied-spec": "f0c8a45ea6c3dd26c2dc2b5f3c699f38d613dab273d0f8a6eae6abd9a9569063",
1328+
fdbv1beta2.LastSpecKey: "f0c8a45ea6c3dd26c2dc2b5f3c699f38d613dab273d0f8a6eae6abd9a9569063",
13271329
}))
13281330
}
13291331

@@ -1408,12 +1410,12 @@ var _ = Describe("cluster_controller", func() {
14081410
Expect(item.ObjectMeta.Annotations).To(Equal(map[string]string{
14091411
"fdb-annotation": "value1",
14101412
"foundationdb.org/existing-annotation": "test-value",
1411-
"foundationdb.org/last-applied-spec": "f0c8a45ea6c3dd26c2dc2b5f3c699f38d613dab273d0f8a6eae6abd9a9569063",
1413+
fdbv1beta2.LastSpecKey: "f0c8a45ea6c3dd26c2dc2b5f3c699f38d613dab273d0f8a6eae6abd9a9569063",
14121414
}))
14131415
} else {
14141416
Expect(item.ObjectMeta.Annotations).To(Equal(map[string]string{
1415-
"fdb-annotation": "value1",
1416-
"foundationdb.org/last-applied-spec": "f0c8a45ea6c3dd26c2dc2b5f3c699f38d613dab273d0f8a6eae6abd9a9569063",
1417+
"fdb-annotation": "value1",
1418+
fdbv1beta2.LastSpecKey: "f0c8a45ea6c3dd26c2dc2b5f3c699f38d613dab273d0f8a6eae6abd9a9569063",
14171419
}))
14181420

14191421
}
@@ -1438,9 +1440,10 @@ var _ = Describe("cluster_controller", func() {
14381440
configMapHash, err := getConfigMapHash(cluster, internal.GetProcessClassFromMeta(cluster, item.ObjectMeta), &item)
14391441
Expect(err).NotTo(HaveOccurred())
14401442
Expect(item.ObjectMeta.Annotations).To(Equal(map[string]string{
1441-
"foundationdb.org/last-applied-config-map": configMapHash,
1442-
"foundationdb.org/last-applied-spec": hash,
1443-
"foundationdb.org/public-ip-source": "pod",
1443+
fdbv1beta2.LastConfigMapKey: configMapHash,
1444+
fdbv1beta2.LastSpecKey: hash,
1445+
fdbv1beta2.PublicIPSourceAnnotation: "pod",
1446+
fdbv1beta2.NodeAnnotation: item.Spec.NodeName,
14441447
}))
14451448
}
14461449

@@ -1546,9 +1549,10 @@ var _ = Describe("cluster_controller", func() {
15461549
configMapHash, err := getConfigMapHash(cluster, internal.GetProcessClassFromMeta(cluster, item.ObjectMeta), &item)
15471550
Expect(err).NotTo(HaveOccurred())
15481551
Expect(item.ObjectMeta.Annotations).To(Equal(map[string]string{
1549-
"foundationdb.org/last-applied-config-map": configMapHash,
1550-
"foundationdb.org/last-applied-spec": hash,
1551-
"foundationdb.org/public-ip-source": "pod",
1552+
fdbv1beta2.LastConfigMapKey: configMapHash,
1553+
fdbv1beta2.LastSpecKey: hash,
1554+
fdbv1beta2.PublicIPSourceAnnotation: "pod",
1555+
fdbv1beta2.NodeAnnotation: item.Spec.NodeName,
15521556
}))
15531557
}
15541558

@@ -1557,7 +1561,7 @@ var _ = Describe("cluster_controller", func() {
15571561
Expect(err).NotTo(HaveOccurred())
15581562
for _, item := range pvcs.Items {
15591563
Expect(item.ObjectMeta.Annotations).To(Equal(map[string]string{
1560-
"foundationdb.org/last-applied-spec": "f0c8a45ea6c3dd26c2dc2b5f3c699f38d613dab273d0f8a6eae6abd9a9569063",
1564+
fdbv1beta2.LastSpecKey: "f0c8a45ea6c3dd26c2dc2b5f3c699f38d613dab273d0f8a6eae6abd9a9569063",
15611565
}))
15621566
}
15631567
})

controllers/update_labels.go controllers/update_metadata.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ import (
2626
"github.com/FoundationDB/fdb-kubernetes-operator/internal"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828

29-
fdbtypes "github.com/FoundationDB/fdb-kubernetes-operator/api/v1beta2"
29+
fdbv1beta2 "github.com/FoundationDB/fdb-kubernetes-operator/api/v1beta2"
3030
corev1 "k8s.io/api/core/v1"
3131
)
3232

33-
// updateLabels provides a reconciliation step for updating the labels on pods.
34-
type updateLabels struct{}
33+
// updateMetadata provides a reconciliation step for updating the metadata on Pods.
34+
type updateMetadata struct{}
3535

3636
// reconcile runs the reconciler's work.
37-
func (updateLabels) reconcile(ctx context.Context, r *FoundationDBClusterReconciler, cluster *fdbtypes.FoundationDBCluster) *requeue {
38-
logger := log.WithValues("namespace", cluster.Namespace, "cluster", cluster.Name, "reconciler", "updateLabels")
37+
func (updateMetadata) reconcile(ctx context.Context, r *FoundationDBClusterReconciler, cluster *fdbv1beta2.FoundationDBCluster) *requeue {
38+
logger := log.WithValues("namespace", cluster.Namespace, "cluster", cluster.Name, "reconciler", "updateMetadata")
3939
// TODO(johscheuer): Remove the use of the pvc map and directly make a get request.
4040
pvcs := &corev1.PersistentVolumeClaimList{}
4141
err := r.List(ctx, pvcs, internal.GetPodListOptions(cluster, "", "")...)
@@ -58,6 +58,10 @@ func (updateLabels) reconcile(ctx context.Context, r *FoundationDBClusterReconci
5858
metadata.Annotations = make(map[string]string, 1)
5959
}
6060

61+
if pod.Spec.NodeName != "" {
62+
metadata.Annotations[fdbv1beta2.NodeAnnotation] = pod.Spec.NodeName
63+
}
64+
6165
if !metadataCorrect(metadata, &pod.ObjectMeta) {
6266
err := r.PodLifecycleManager.UpdateMetadata(ctx, r, cluster, pod)
6367
if err != nil {
@@ -98,7 +102,7 @@ func (updateLabels) reconcile(ctx context.Context, r *FoundationDBClusterReconci
98102
}
99103

100104
func metadataCorrect(desiredMetadata metav1.ObjectMeta, currentMetadata *metav1.ObjectMeta) bool {
101-
desiredMetadata.Annotations[fdbtypes.LastSpecKey] = currentMetadata.Annotations[fdbtypes.LastSpecKey]
105+
desiredMetadata.Annotations[fdbv1beta2.LastSpecKey] = currentMetadata.Annotations[fdbv1beta2.LastSpecKey]
102106
// If the annotations or labels have changed the metadata has to be updated.
103107
return !mergeLabelsInMetadata(currentMetadata, desiredMetadata) && !mergeAnnotations(currentMetadata, desiredMetadata)
104108
}

0 commit comments

Comments
 (0)