Skip to content

Commit 9fedc86

Browse files
committed
Emit events attached to node when snapshot retention fails
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
1 parent 5a22c7d commit 9fedc86

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

pkg/etcd/snapshot.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3737
"k8s.io/apimachinery/pkg/labels"
3838
k8sruntime "k8s.io/apimachinery/pkg/runtime"
39+
"k8s.io/apimachinery/pkg/types"
3940
"k8s.io/apimachinery/pkg/util/wait"
4041
"k8s.io/client-go/tools/pager"
4142
"k8s.io/client-go/util/retry"
@@ -347,10 +348,10 @@ func (e *ETCD) snapshot(ctx context.Context) (_ *managed.SnapshotResult, rerr er
347348

348349
// Snapshot retention may prune some files before returning an error. Failing to prune is not fatal.
349350
deleted, err := snapshotRetention(e.config.EtcdSnapshotRetention, e.config.EtcdSnapshotName, snapshotDir)
351+
res.Deleted = append(res.Deleted, deleted...)
350352
if err != nil {
351-
logrus.Warnf("Failed to apply local snapshot retention policy: %v", err)
353+
e.warningEventf("ETDCSnapshotRetentionFailedLocal", "Failed to apply local snapshot retention policy: %v", err)
352354
}
353-
res.Deleted = append(res.Deleted, deleted...)
354355

355356
if e.config.EtcdS3 != nil {
356357
s3Start := time.Now()
@@ -390,7 +391,7 @@ func (e *ETCD) snapshot(ctx context.Context) (_ *managed.SnapshotResult, rerr er
390391
deleted, err := s3client.SnapshotRetention(ctx, e.config.EtcdSnapshotName)
391392
res.Deleted = append(res.Deleted, deleted...)
392393
if err != nil {
393-
logrus.Warnf("Failed to apply s3 snapshot retention policy: %v", err)
394+
e.warningEventf("ETCDSnapshotRetentionFailedS3", "Failed to apply S3 snapshot retention policy: %v", err)
394395
}
395396
}
396397
// sf is either s3 snapshot metadata, or s3 init/upload failure record.
@@ -650,7 +651,7 @@ func (e *ETCD) addSnapshotData(sf snapshot.File) error {
650651
created, err = snapshots.Create(esf)
651652
if err == nil {
652653
// Only emit an event for the snapshot when creating the resource
653-
e.emitEvent(created)
654+
e.snapshotEvent(created)
654655
}
655656
} else if !equality.Semantic.DeepEqual(existing, esf) {
656657
_, err = snapshots.Update(esf)
@@ -669,7 +670,8 @@ func generateETCDSnapshotFileConfigMapKey(esf k3s.ETCDSnapshotFile) string {
669670
return "local-" + name
670671
}
671672

672-
func (e *ETCD) emitEvent(esf *k3s.ETCDSnapshotFile) {
673+
// snapshotEvent emits an Event attached to the EtcdSnapshotFile resource
674+
func (e *ETCD) snapshotEvent(esf *k3s.ETCDSnapshotFile) {
673675
switch {
674676
case e.config.Runtime.Event == nil:
675677
case !esf.DeletionTimestamp.IsZero():
@@ -685,6 +687,23 @@ func (e *ETCD) emitEvent(esf *k3s.ETCDSnapshotFile) {
685687
}
686688
}
687689

690+
// warningEventf emits a warning Event attached to the Node resource,
691+
// or directly logs a warning if the event recorder is not available.
692+
func (e *ETCD) warningEventf(reason, messageFmt string, args ...any) {
693+
nodeName := os.Getenv("NODE_NAME")
694+
if nodeName != "" && e.config.Runtime.Event != nil {
695+
nodeRef := &v1.ObjectReference{
696+
Kind: "Node",
697+
Name: nodeName,
698+
UID: types.UID(nodeName),
699+
Namespace: "",
700+
}
701+
e.config.Runtime.Event.Eventf(nodeRef, v1.EventTypeWarning, reason, messageFmt, args...)
702+
} else {
703+
logrus.Warnf(messageFmt, args...)
704+
}
705+
}
706+
688707
// ReconcileSnapshotData reconciles snapshot data in the ETCDSnapshotFile resources.
689708
// It will reconcile snapshot data from disk locally always, and if S3 is enabled, will attempt to
690709
// list S3 snapshots and reconcile snapshots from S3.

pkg/etcd/snapshot_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func (e *etcdSnapshotHandler) onRemove(key string, esf *k3s.ETCDSnapshotFile) (*
166166
return nil, errors.WithMessage(err, "failed to remove snapshot from ConfigMap")
167167
}
168168
}
169-
e.etcd.emitEvent(esf)
169+
e.etcd.snapshotEvent(esf)
170170
return nil, nil
171171
}
172172

0 commit comments

Comments
 (0)