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
6b270883
Commit
6b270883
authored
Dec 29, 2018
by
mlmhl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use json format to get rbd image size
parent
cff46ab4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
19 deletions
+40
-19
rbd_test.go
pkg/volume/rbd/rbd_test.go
+25
-0
rbd_util.go
pkg/volume/rbd/rbd_util.go
+15
-19
No files found.
pkg/volume/rbd/rbd_test.go
View file @
6b270883
...
@@ -682,3 +682,28 @@ func TestRequiresRemount(t *testing.T) {
...
@@ -682,3 +682,28 @@ func TestRequiresRemount(t *testing.T) {
t
.
Errorf
(
"Exepcted RequiresRemount to be false, got %t"
,
has
)
t
.
Errorf
(
"Exepcted RequiresRemount to be false, got %t"
,
has
)
}
}
}
}
func
TestGetRbdImageSize
(
t
*
testing
.
T
)
{
for
i
,
c
:=
range
[]
struct
{
Output
string
TargetSize
int
}{
{
Output
:
`{"name":"kubernetes-dynamic-pvc-18e7a4d9-050d-11e9-b905-548998f3478f","size":10737418240,"objects":2560,"order":22,"object_size":4194304,"block_name_prefix":"rbd_data.9f4ff7238e1f29","format":2}`
,
TargetSize
:
10240
,
},
{
Output
:
`{"name":"kubernetes-dynamic-pvc-070635bf-e33f-11e8-aab7-548998f3478f","size":1073741824,"objects":256,"order":22,"object_size":4194304,"block_name_prefix":"rbd_data.670ac4238e1f29","format":2}`
,
TargetSize
:
1024
,
},
}
{
size
,
err
:=
getRbdImageSize
([]
byte
(
c
.
Output
))
if
err
!=
nil
{
t
.
Errorf
(
"Case %d: getRbdImageSize failed: %v"
,
i
,
err
)
continue
}
if
size
!=
c
.
TargetSize
{
t
.
Errorf
(
"Case %d: unexpected size, wanted %d, got %d"
,
i
,
c
.
TargetSize
,
size
)
}
}
}
pkg/volume/rbd/rbd_util.go
View file @
6b270883
...
@@ -47,13 +47,13 @@ import (
...
@@ -47,13 +47,13 @@ import (
const
(
const
(
imageWatcherStr
=
"watcher="
imageWatcherStr
=
"watcher="
imageSizeStr
=
"size "
imageSizeStr
=
"size "
sizeDivStr
=
" MB in"
kubeLockMagic
=
"kubelet_lock_magic_"
kubeLockMagic
=
"kubelet_lock_magic_"
// The following three values are used for 30 seconds timeout
// The following three values are used for 30 seconds timeout
// while waiting for RBD Watcher to expire.
// while waiting for RBD Watcher to expire.
rbdImageWatcherInitDelay
=
1
*
time
.
Second
rbdImageWatcherInitDelay
=
1
*
time
.
Second
rbdImageWatcherFactor
=
1.4
rbdImageWatcherFactor
=
1.4
rbdImageWatcherSteps
=
10
rbdImageWatcherSteps
=
10
rbdImageSizeUnitMiB
=
1024
*
1024
)
)
func
getDevFromImageAndPool
(
pool
,
image
string
)
(
string
,
bool
)
{
func
getDevFromImageAndPool
(
pool
,
image
string
)
(
string
,
bool
)
{
...
@@ -672,8 +672,7 @@ func (util *RBDUtil) ExpandImage(rbdExpander *rbdVolumeExpander, oldSize resourc
...
@@ -672,8 +672,7 @@ func (util *RBDUtil) ExpandImage(rbdExpander *rbdVolumeExpander, oldSize resourc
// rbdInfo runs `rbd info` command to get the current image size in MB.
// rbdInfo runs `rbd info` command to get the current image size in MB.
func
(
util
*
RBDUtil
)
rbdInfo
(
b
*
rbdMounter
)
(
int
,
error
)
{
func
(
util
*
RBDUtil
)
rbdInfo
(
b
*
rbdMounter
)
(
int
,
error
)
{
var
err
error
var
err
error
var
output
string
var
output
[]
byte
var
cmd
[]
byte
// If we don't have admin id/secret (e.g. attaching), fallback to user id/secret.
// If we don't have admin id/secret (e.g. attaching), fallback to user id/secret.
id
:=
b
.
adminId
id
:=
b
.
adminId
...
@@ -702,9 +701,8 @@ func (util *RBDUtil) rbdInfo(b *rbdMounter) (int, error) {
...
@@ -702,9 +701,8 @@ func (util *RBDUtil) rbdInfo(b *rbdMounter) (int, error) {
// rbd: error opening image 1234: (2) No such file or directory
// rbd: error opening image 1234: (2) No such file or directory
//
//
klog
.
V
(
4
)
.
Infof
(
"rbd: info %s using mon %s, pool %s id %s key %s"
,
b
.
Image
,
mon
,
b
.
Pool
,
id
,
secret
)
klog
.
V
(
4
)
.
Infof
(
"rbd: info %s using mon %s, pool %s id %s key %s"
,
b
.
Image
,
mon
,
b
.
Pool
,
id
,
secret
)
cmd
,
err
=
b
.
exec
.
Run
(
"rbd"
,
output
,
err
=
b
.
exec
.
Run
(
"rbd"
,
"info"
,
b
.
Image
,
"--pool"
,
b
.
Pool
,
"-m"
,
mon
,
"--id"
,
id
,
"--key="
+
secret
)
"info"
,
b
.
Image
,
"--pool"
,
b
.
Pool
,
"-m"
,
mon
,
"--id"
,
id
,
"--key="
+
secret
,
"--format=json"
)
output
=
string
(
cmd
)
if
err
,
ok
:=
err
.
(
*
exec
.
Error
);
ok
{
if
err
,
ok
:=
err
.
(
*
exec
.
Error
);
ok
{
if
err
.
Err
==
exec
.
ErrNotFound
{
if
err
.
Err
==
exec
.
ErrNotFound
{
...
@@ -720,22 +718,20 @@ func (util *RBDUtil) rbdInfo(b *rbdMounter) (int, error) {
...
@@ -720,22 +718,20 @@ func (util *RBDUtil) rbdInfo(b *rbdMounter) (int, error) {
}
}
if
len
(
output
)
==
0
{
if
len
(
output
)
==
0
{
return
0
,
fmt
.
Errorf
(
"can not get image size info %s: %s"
,
b
.
Image
,
output
)
return
0
,
fmt
.
Errorf
(
"can not get image size info %s: %s"
,
b
.
Image
,
string
(
output
)
)
}
}
// Get the size value string, just between `size ` and ` MB in`, such as `size 1024 MB in 256 objects`.
return
getRbdImageSize
(
output
)
sizeIndex
:=
strings
.
Index
(
output
,
imageSizeStr
)
}
divIndex
:=
strings
.
Index
(
output
,
sizeDivStr
)
if
sizeIndex
==
-
1
||
divIndex
==
-
1
||
divIndex
<=
sizeIndex
+
5
{
return
0
,
fmt
.
Errorf
(
"can not get image size info %s: %s"
,
b
.
Image
,
output
)
}
rbdSizeStr
:=
output
[
sizeIndex
+
5
:
divIndex
]
rbdSize
,
err
:=
strconv
.
Atoi
(
rbdSizeStr
)
if
err
!=
nil
{
return
0
,
fmt
.
Errorf
(
"can not convert size str: %s to int"
,
rbdSizeStr
)
}
return
rbdSize
,
nil
func
getRbdImageSize
(
output
[]
byte
)
(
int
,
error
)
{
info
:=
struct
{
Size
int64
`json:"size"`
}{}
if
err
:=
json
.
Unmarshal
(
output
,
&
info
);
err
!=
nil
{
return
0
,
fmt
.
Errorf
(
"parse rbd info output failed: %s, %v"
,
string
(
output
),
err
)
}
return
int
(
info
.
Size
/
rbdImageSizeUnitMiB
),
nil
}
}
// rbdStatus runs `rbd status` command to check if there is watcher on the image.
// rbdStatus runs `rbd status` command to check if there is watcher on the image.
...
...
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