Skip to content

Commit 5739756

Browse files
authored
Merge pull request #591 from torredil/cherrypick5123
Update release-1.16 branch for release
2 parents e4cf791 + 20b2648 commit 5739756

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

pkg/hostpath/controllerserver.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,23 @@ func (hp *hostPath) CreateVolume(ctx context.Context, req *csi.CreateVolumeReque
9797
requestedAccessType = state.MountAccess
9898
}
9999

100+
capacity := int64(req.GetCapacityRange().GetRequiredBytes())
101+
limit := int64(req.GetCapacityRange().GetLimitBytes())
102+
if capacity < 0 || limit < 0 {
103+
return nil, status.Error(codes.InvalidArgument, "cannot have negative capacity")
104+
}
105+
if limit > 0 && capacity > limit {
106+
return nil, status.Error(codes.InvalidArgument, "capacity cannot exceed limit")
107+
}
108+
if capacity == 0 && limit > 0 {
109+
capacity = limit
110+
}
111+
100112
// Lock before acting on global state. A production-quality
101113
// driver might use more fine-grained locking.
102114
hp.mutex.Lock()
103115
defer hp.mutex.Unlock()
104116

105-
capacity := int64(req.GetCapacityRange().GetRequiredBytes())
106117
topologies := []*csi.Topology{}
107118
if hp.config.EnableTopology {
108119
topologies = append(topologies, &csi.Topology{Segments: map[string]string{TopologyKeyNode: hp.config.NodeID}})
@@ -114,7 +125,7 @@ func (hp *hostPath) CreateVolume(ctx context.Context, req *csi.CreateVolumeReque
114125
// Since err is nil, it means the volume with the same name already exists
115126
// need to check if the size of existing volume is the same as in new
116127
// request
117-
if exVol.VolSize < capacity {
128+
if exVol.VolSize < capacity || (limit > 0 && exVol.VolSize > limit) {
118129
return nil, status.Errorf(codes.AlreadyExists, "Volume with the same name: %s but with different size already exist", req.GetName())
119130
}
120131
if req.GetVolumeContentSource() != nil {
@@ -146,6 +157,8 @@ func (hp *hostPath) CreateVolume(ctx context.Context, req *csi.CreateVolumeReque
146157

147158
volumeID := uuid.NewUUID().String()
148159
kind := req.GetParameters()[storageKind]
160+
// This code does not check whether hp.createVolume rounds capacity up;
161+
// a more robust driver would ensure any rounding does not exceed limit.
149162
vol, err := hp.createVolume(volumeID, req.GetName(), capacity, requestedAccessType, false /* ephemeral */, kind)
150163
if err != nil {
151164
return nil, err
@@ -182,7 +195,7 @@ func (hp *hostPath) CreateVolume(ctx context.Context, req *csi.CreateVolumeReque
182195
return &csi.CreateVolumeResponse{
183196
Volume: &csi.Volume{
184197
VolumeId: volumeID,
185-
CapacityBytes: req.GetCapacityRange().GetRequiredBytes(),
198+
CapacityBytes: capacity,
186199
VolumeContext: req.GetParameters(),
187200
ContentSource: req.GetVolumeContentSource(),
188201
AccessibleTopology: topologies,

pkg/hostpath/nodeserver.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package hostpath
1818

1919
import (
2020
"fmt"
21+
"math/rand"
2122
"os"
2223
"strings"
2324

@@ -365,6 +366,11 @@ func (hp *hostPath) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRequest
365366
resp.MaxVolumesPerNode = hp.config.AttachLimit
366367
}
367368

369+
// if attach limit is -1, set a random MaxVolumesPerNode between 1 and 10
370+
if hp.config.AttachLimit == -1 {
371+
resp.MaxVolumesPerNode = int64(rand.Intn(10) + 1)
372+
}
373+
368374
return resp, nil
369375
}
370376

pkg/hostpath/snapshotmetadata.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ func (cb *fileBlockReader) getChangedBlockMetadata(ctx context.Context) ([]*csi.
118118
blockIndex := cb.offset / cb.blockSize
119119
sBuffer := make([]byte, cb.blockSize)
120120
tBuffer := make([]byte, cb.blockSize)
121+
zeroBlock := make([]byte, cb.blockSize)
121122
eofBaseFile, eofTargetFile := false, false
122123

123124
changedBlocks := []*csi.BlockMetadata{}
@@ -143,15 +144,19 @@ func (cb *fileBlockReader) getChangedBlockMetadata(ctx context.Context) ([]*csi.
143144
if eofTargetFile {
144145
return changedBlocks, io.EOF
145146
}
146-
// if VARIABLE_LENGTH type is enabled, return blocks extend instead of individual blocks.
147-
blockMetadata := createBlockMetadata(blockIndex, cb.blockSize)
148-
if extendBlock(changedBlocks, csi.BlockMetadataType(cb.blockMetadataType), blockIndex, cb.blockSize) {
149-
changedBlocks[len(changedBlocks)-1].SizeBytes += cb.blockSize
150-
cb.offset += cb.blockSize
151-
blockIndex++
152-
continue
147+
// return only allocated blocks.
148+
if blockChanged(zeroBlock, tBuffer[:targetReadBytes]) {
149+
// if VARIABLE_LENGTH type is enabled, return blocks extend instead of individual blocks.
150+
blockMetadata := createBlockMetadata(blockIndex, cb.blockSize)
151+
if extendBlock(changedBlocks, csi.BlockMetadataType(cb.blockMetadataType), blockIndex, cb.blockSize) {
152+
changedBlocks[len(changedBlocks)-1].SizeBytes += cb.blockSize
153+
cb.offset += cb.blockSize
154+
blockIndex++
155+
continue
156+
}
157+
changedBlocks = append(changedBlocks, blockMetadata)
153158
}
154-
changedBlocks = append(changedBlocks, blockMetadata)
159+
155160
cb.offset += cb.blockSize
156161
blockIndex++
157162
continue

0 commit comments

Comments
 (0)