Skip to content

Commit

Permalink
Rename generateSnapshotConfigMapKey to generateSnapshotName
Browse files Browse the repository at this point in the history
... and use SafeConcatName to build the string.

Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
  • Loading branch information
brandond committed Oct 5, 2023
1 parent 51ee06d commit e50fd97
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
4 changes: 2 additions & 2 deletions pkg/etcd/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,13 @@ func (s *S3) listSnapshots(ctx context.Context) (map[string]snapshotFile, error)
Status: successfulSnapshotStatus,
Compressed: compressed,
}
sfKey := generateSnapshotConfigMapKey(sf)
sfKey := generateSnapshotName(sf)
snapshots[sfKey] = sf
}

for _, metadataKey := range metadatas {
filename := path.Base(metadataKey)
sfKey := generateSnapshotConfigMapKey(snapshotFile{Name: filename, NodeName: "s3"})
sfKey := generateSnapshotName(snapshotFile{Name: filename, NodeName: "s3"})
if sf, ok := snapshots[sfKey]; ok {
logrus.Debugf("Loading snapshot metadata from s3://%s/%s", s.config.EtcdS3BucketName, metadataKey)
if obj, err := s.client.GetObject(ctx, s.config.EtcdS3BucketName, metadataKey, minio.GetObjectOptions{}); err != nil {
Expand Down
29 changes: 17 additions & 12 deletions pkg/etcd/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/k3s-io/k3s/pkg/version"
"github.com/minio/minio-go/v7"
"github.com/pkg/errors"
"github.com/rancher/wrangler/v2/pkg/name"
"github.com/robfig/cron/v3"
"github.com/sirupsen/logrus"
"go.etcd.io/etcd/client/pkg/v3/logutil"
Expand Down Expand Up @@ -443,7 +444,7 @@ func (e *ETCD) listLocalSnapshots() (map[string]snapshotFile, error) {
Status: successfulSnapshotStatus,
Compressed: compressed,
}
sfKey := generateSnapshotConfigMapKey(sf)
sfKey := generateSnapshotName(sf)
snapshots[sfKey] = sf
return nil
}); err != nil {
Expand Down Expand Up @@ -583,15 +584,16 @@ func marshalSnapshotFile(sf snapshotFile) ([]byte, error) {
}

// AddSnapshotData adds the given snapshot file information to the snapshot configmap, using the existing extra metadata
// available at the time.
// available at the time. This is primarily necessary to record failures, as successful snapshots will have a file on disk
// or S3 that will be found when reconciling.
func (e *ETCD) addSnapshotData(sf snapshotFile) error {
// make sure the core.Factory is initialized. There can
// be a race between this core code startup.
for e.config.Runtime.Core == nil {
runtime.Gosched()
}

sfKey := generateSnapshotConfigMapKey(sf)
sfKey := generateSnapshotName(sf)
marshalledSnapshotFile, err := marshalSnapshotFile(sf)
if err != nil {
return err
Expand Down Expand Up @@ -638,12 +640,15 @@ func (e *ETCD) addSnapshotData(sf snapshotFile) error {
})
}

func generateSnapshotConfigMapKey(sf snapshotFile) string {
name := invalidKeyChars.ReplaceAllString(sf.Name, "_")
if sf.NodeName == "s3" {
return "s3-" + name
// generateSnapshotName generates a derived name for the snapshot that is safe for use
// as a resource name or configmap key.
func generateSnapshotName(sf snapshotFile) string {
snapshotName := invalidKeyChars.ReplaceAllString(sf.Name, "_")
nodeName := sf.NodeName
if nodeName != "s3" {
nodeName = "local"
}
return "local-" + name
return name.SafeConcatName(nodeName, snapshotName)
}

// pruneConfigMap drops the oldest entries from the configMap.
Expand Down Expand Up @@ -755,7 +760,7 @@ func (e *ETCD) ReconcileSnapshotData(ctx context.Context) error {
if (sf.NodeName == nodeName || (sf.NodeName == "s3" && s3ListSuccessful)) && sf.Status != failedSnapshotStatus {
// Only delete the snapshot if the snapshot was not failed
// sf.Status != FailedSnapshotStatus is intentional, as it is possible we are reconciling snapshots stored from older versions that did not set status
deletedSnapshots[generateSnapshotConfigMapKey(sf)] = v // store a copy of the snapshot
deletedSnapshots[generateSnapshotName(sf)] = v // store a copy of the snapshot
delete(snapshotConfigMap.Data, k)
} else if sf.Status == failedSnapshotStatus && sf.NodeName == nodeName && e.config.EtcdSnapshotRetention >= 1 {
// Handle locally failed snapshots.
Expand All @@ -776,7 +781,7 @@ func (e *ETCD) ReconcileSnapshotData(ctx context.Context) error {
})

for _, dfs := range failedSnapshots[:e.config.EtcdSnapshotRetention] {
sfKey := generateSnapshotConfigMapKey(dfs)
sfKey := generateSnapshotName(dfs)
marshalledSnapshot, err := marshalSnapshotFile(dfs)
if err != nil {
logrus.Errorf("Failed to marshal snapshot to store in configmap %v", err)
Expand All @@ -794,7 +799,7 @@ func (e *ETCD) ReconcileSnapshotData(ctx context.Context) error {
})

for _, dfs := range failedS3Snapshots[:e.config.EtcdSnapshotRetention] {
sfKey := generateSnapshotConfigMapKey(dfs)
sfKey := generateSnapshotName(dfs)
marshalledSnapshot, err := marshalSnapshotFile(dfs)
if err != nil {
logrus.Errorf("Failed to marshal snapshot to store in configmap %v", err)
Expand All @@ -807,7 +812,7 @@ func (e *ETCD) ReconcileSnapshotData(ctx context.Context) error {
// save the local entries to the ConfigMap if they are still on disk or in S3.
for _, snapshot := range snapshotFiles {
var sf snapshotFile
sfKey := generateSnapshotConfigMapKey(snapshot)
sfKey := generateSnapshotName(snapshot)
if v, ok := deletedSnapshots[sfKey]; ok {
// use the snapshot file we have from the existing configmap, and unmarshal it so we can manipulate it
if err := json.Unmarshal([]byte(v), &sf); err != nil {
Expand Down

0 comments on commit e50fd97

Please sign in to comment.