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
3aae7b87
Commit
3aae7b87
authored
Oct 19, 2022
by
iyear
Committed by
Brad Davidson
Nov 01, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix incorrect defer usage
Problem: Using defer inside a loop can lead to resource leaks Solution: Judge newer file in the separate function Signed-off-by:
iyear
<
ljyngup@gmail.com
>
parent
86be784a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
25 deletions
+40
-25
bootstrap.go
pkg/cluster/bootstrap.go
+40
-25
No files found.
pkg/cluster/bootstrap.go
View file @
3aae7b87
...
...
@@ -328,35 +328,15 @@ func (c *Cluster) ReconcileBootstrapData(ctx context.Context, buf io.ReadSeeker,
}
logrus
.
Debugf
(
"Reconciling %s at '%s'"
,
pathKey
,
path
)
f
,
err
:=
os
.
Open
(
path
)
updated
,
newer
,
err
:=
isNewerFile
(
path
,
fileData
)
if
err
!=
nil
{
if
os
.
IsNotExist
(
err
)
{
logrus
.
Warn
(
path
+
" doesn't exist. continuing..."
)
updateDisk
=
true
continue
}
return
errors
.
Wrapf
(
err
,
"reconcile failed to open %s"
,
pathKey
)
return
errors
.
Wrapf
(
err
,
"failed to get update status of %s"
,
pathKey
)
}
defer
f
.
Close
()
fData
,
err
:=
io
.
ReadAll
(
f
)
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"reconcile failed to read %s"
,
pathKey
)
if
newer
{
newerOnDisk
=
append
(
newerOnDisk
,
path
)
}
if
!
bytes
.
Equal
(
fileData
.
Content
,
fData
)
{
updateDisk
=
true
info
,
err
:=
f
.
Stat
()
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"reconcile failed to stat %s"
,
pathKey
)
}
if
info
.
ModTime
()
.
Unix
()
-
fileData
.
Timestamp
.
Unix
()
>=
systemTimeSkew
{
newerOnDisk
=
append
(
newerOnDisk
,
path
)
}
else
{
logrus
.
Warn
(
path
+
" will be updated from the datastore."
)
}
}
updateDisk
=
updateDisk
||
updated
}
if
c
.
config
.
ClusterReset
{
...
...
@@ -384,6 +364,41 @@ func (c *Cluster) ReconcileBootstrapData(ctx context.Context, buf io.ReadSeeker,
return
nil
}
// isNewerFile compares the file from disk and datastore, and returns
// update status.
func
isNewerFile
(
path
string
,
file
bootstrap
.
File
)
(
updated
bool
,
newerOnDisk
bool
,
_
error
)
{
f
,
err
:=
os
.
Open
(
path
)
if
err
!=
nil
{
if
os
.
IsNotExist
(
err
)
{
logrus
.
Warn
(
path
+
" doesn't exist. continuing..."
)
return
true
,
false
,
nil
}
return
false
,
false
,
errors
.
Wrapf
(
err
,
"reconcile failed to open"
)
}
defer
f
.
Close
()
data
,
err
:=
io
.
ReadAll
(
f
)
if
err
!=
nil
{
return
false
,
false
,
errors
.
Wrapf
(
err
,
"reconcile failed to read"
)
}
if
bytes
.
Equal
(
file
.
Content
,
data
)
{
return
false
,
false
,
nil
}
info
,
err
:=
f
.
Stat
()
if
err
!=
nil
{
return
false
,
false
,
errors
.
Wrapf
(
err
,
"reconcile failed to stat"
)
}
if
info
.
ModTime
()
.
Unix
()
-
file
.
Timestamp
.
Unix
()
>=
systemTimeSkew
{
return
true
,
true
,
nil
}
logrus
.
Warn
(
path
+
" will be updated from the datastore."
)
return
true
,
false
,
nil
}
// httpBootstrap retrieves bootstrap data (certs and keys, etc) from the remote server via HTTP
// and loads it into the ControlRuntimeBootstrap struct. Unlike the storage bootstrap path,
// this data does not need to be decrypted since it is generated on-demand by an existing server.
...
...
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