Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
3576ed43
Commit
3576ed43
authored
Feb 15, 2024
by
Brad Davidson
Committed by
Brad Davidson
Mar 04, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clean up snapshotDir create/exists logic
Signed-off-by:
Brad Davidson
<
brad.davidson@rancher.com
>
parent
b164d7a2
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
25 deletions
+32
-25
snapshot.go
pkg/etcd/snapshot.go
+32
-25
No files found.
pkg/etcd/snapshot.go
View file @
3576ed43
...
...
@@ -74,26 +74,38 @@ var (
)
// snapshotDir ensures that the snapshot directory exists, and then returns its path.
// Only the default snapshot directory will be created; user-specified non-default
// snapshot directories must already exist.
func
snapshotDir
(
config
*
config
.
Control
,
create
bool
)
(
string
,
error
)
{
if
config
.
EtcdSnapshotDir
==
""
{
// we have to create the snapshot dir if we are using
// the default snapshot dir if it doesn't exist
defaultSnapshotDir
:=
filepath
.
Join
(
config
.
DataDir
,
"db"
,
"snapshots"
)
s
,
err
:=
os
.
Stat
(
defaultSnapshotDir
)
if
err
!=
nil
{
if
create
&&
os
.
IsNotExist
(
err
)
{
if
err
:=
os
.
MkdirAll
(
defaultSnapshotDir
,
0700
);
err
!=
nil
{
return
""
,
err
}
return
defaultSnapshotDir
,
nil
defaultSnapshotDir
:=
filepath
.
Join
(
config
.
DataDir
,
"db"
,
"snapshots"
)
snapshotDir
:=
config
.
EtcdSnapshotDir
if
snapshotDir
==
""
{
snapshotDir
=
defaultSnapshotDir
}
// Disable creation if not using the default snapshot dir.
// Non-default snapshot dirs must be created by the user.
if
snapshotDir
!=
defaultSnapshotDir
{
create
=
false
}
s
,
err
:=
os
.
Stat
(
snapshotDir
)
if
err
!=
nil
{
if
os
.
IsNotExist
(
err
)
&&
create
{
if
err
:=
os
.
MkdirAll
(
snapshotDir
,
0700
);
err
!=
nil
{
return
""
,
err
}
return
""
,
err
}
if
s
.
IsDir
()
{
return
defaultSnapshotDir
,
nil
return
snapshotDir
,
nil
}
return
""
,
err
}
if
!
s
.
IsDir
()
{
return
""
,
fmt
.
Errorf
(
"%s is not a directory"
,
snapshotDir
)
}
return
config
.
EtcdSnapshotDir
,
nil
return
snapshotDir
,
nil
}
// preSnapshotSetup checks to see if the necessary components are in place
...
...
@@ -248,12 +260,6 @@ func (e *ETCD) Snapshot(ctx context.Context) error {
return
errors
.
Wrap
(
err
,
"failed to get etcd-snapshot-dir"
)
}
if
info
,
err
:=
os
.
Stat
(
snapshotDir
);
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"failed to stat etcd-snapshot-dir %s"
,
snapshotDir
)
}
else
if
!
info
.
IsDir
()
{
return
fmt
.
Errorf
(
"etcd-snapshot-dir %s is not a directory"
,
snapshotDir
)
}
cfg
,
err
:=
getClientConfig
(
ctx
,
e
.
config
)
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to get config for etcd snapshot"
)
...
...
@@ -438,7 +444,7 @@ func (e *ETCD) listLocalSnapshots() (map[string]snapshotFile, error) {
snapshots
:=
make
(
map
[
string
]
snapshotFile
)
snapshotDir
,
err
:=
snapshotDir
(
e
.
config
,
true
)
if
err
!=
nil
{
return
snapshots
,
errors
.
Wrap
(
err
,
"failed to get
the snapshot
dir"
)
return
snapshots
,
errors
.
Wrap
(
err
,
"failed to get
etcd-snapshot-
dir"
)
}
if
err
:=
filepath
.
Walk
(
snapshotDir
,
func
(
path
string
,
file
os
.
FileInfo
,
err
error
)
error
{
...
...
@@ -502,8 +508,9 @@ func (e *ETCD) initS3IfNil(ctx context.Context) error {
func
(
e
*
ETCD
)
PruneSnapshots
(
ctx
context
.
Context
)
error
{
snapshotDir
,
err
:=
snapshotDir
(
e
.
config
,
false
)
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to get
the snapshot
dir"
)
return
errors
.
Wrap
(
err
,
"failed to get
etcd-snapshot-
dir"
)
}
if
err
:=
snapshotRetention
(
e
.
config
.
EtcdSnapshotRetention
,
e
.
config
.
EtcdSnapshotName
,
snapshotDir
);
err
!=
nil
{
logrus
.
Errorf
(
"Error applying snapshot retention policy: %v"
,
err
)
}
...
...
@@ -551,7 +558,7 @@ func (e *ETCD) ListSnapshots(ctx context.Context) (map[string]snapshotFile, erro
func
(
e
*
ETCD
)
DeleteSnapshots
(
ctx
context
.
Context
,
snapshots
[]
string
)
error
{
snapshotDir
,
err
:=
snapshotDir
(
e
.
config
,
false
)
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to get
the snapshot
dir"
)
return
errors
.
Wrap
(
err
,
"failed to get
etcd-snapshot-
dir"
)
}
if
e
.
config
.
EtcdS3
{
if
err
:=
e
.
initS3IfNil
(
ctx
);
err
!=
nil
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment