Skip to content

Commit 5024dd3

Browse files
committed
mock: optionally support topology
This is off by default. If enabled, the mock driver returns the capability for topology and a fixed "io.kubernetes.storage.mock/node: some-mock-node" topology segment for the node it runs on and all volumes. This makes it possible to test some more code paths in external-provisioner.
1 parent dbd6a5f commit 5024dd3

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

cmd/mock-driver/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func main() {
3737
flag.StringVar(&config.DriverName, "name", service.Name, "CSI driver name.")
3838
flag.Int64Var(&config.AttachLimit, "attach-limit", 2, "number of attachable volumes on a node")
3939
flag.BoolVar(&config.NodeExpansionRequired, "node-expand-required", false, "Enables NodeServiceCapability_RPC_EXPAND_VOLUME capacity.")
40+
flag.BoolVar(&config.EnableTopology, "enable-topology", false, "Enables PluginCapability_Service_VOLUME_ACCESSIBILITY_CONSTRAINTS capability.")
4041
flag.BoolVar(&config.DisableControllerExpansion, "disable-controller-expansion", false, "Disables ControllerServiceCapability_RPC_EXPAND_VOLUME capability.")
4142
flag.BoolVar(&config.DisableOnlineExpansion, "disable-online-expansion", false, "Disables online volume expansion capability.")
4243
flag.BoolVar(&config.PermissiveTargetPath, "permissive-target-path", false, "Allows the CO to create PublishVolumeRequest.TargetPath, which violates the CSI spec.")

mock/service/identity.go

+25-12
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,35 @@ func (s *service) GetPluginCapabilities(
4040
volExpType = csi.PluginCapability_VolumeExpansion_OFFLINE
4141
}
4242

43-
return &csi.GetPluginCapabilitiesResponse{
44-
Capabilities: []*csi.PluginCapability{
45-
{
46-
Type: &csi.PluginCapability_Service_{
47-
Service: &csi.PluginCapability_Service{
48-
Type: csi.PluginCapability_Service_CONTROLLER_SERVICE,
49-
},
43+
capabilities := []*csi.PluginCapability{
44+
{
45+
Type: &csi.PluginCapability_Service_{
46+
Service: &csi.PluginCapability_Service{
47+
Type: csi.PluginCapability_Service_CONTROLLER_SERVICE,
5048
},
5149
},
52-
{
53-
Type: &csi.PluginCapability_VolumeExpansion_{
54-
VolumeExpansion: &csi.PluginCapability_VolumeExpansion{
55-
Type: volExpType,
56-
},
50+
},
51+
{
52+
Type: &csi.PluginCapability_VolumeExpansion_{
53+
VolumeExpansion: &csi.PluginCapability_VolumeExpansion{
54+
Type: volExpType,
5755
},
5856
},
5957
},
58+
}
59+
60+
if s.config.EnableTopology {
61+
capabilities = append(capabilities,
62+
&csi.PluginCapability{
63+
Type: &csi.PluginCapability_Service_{
64+
Service: &csi.PluginCapability_Service{
65+
Type: csi.PluginCapability_Service_VOLUME_ACCESSIBILITY_CONSTRAINTS,
66+
},
67+
},
68+
})
69+
}
70+
71+
return &csi.GetPluginCapabilitiesResponse{
72+
Capabilities: capabilities,
6073
}, nil
6174
}

mock/service/node.go

+7
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,13 @@ func (s *service) NodeGetInfo(ctx context.Context,
381381
if s.config.AttachLimit > 0 {
382382
csiNodeResponse.MaxVolumesPerNode = s.config.AttachLimit
383383
}
384+
if s.config.EnableTopology {
385+
csiNodeResponse.AccessibleTopology = &csi.Topology{
386+
Segments: map[string]string{
387+
TopologyKey: TopologyValue,
388+
},
389+
}
390+
}
384391
return csiNodeResponse, nil
385392
}
386393

mock/service/service.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ const (
2424

2525
// VendorVersion is the version returned by GetPluginInfo.
2626
VendorVersion = "0.3.0"
27+
28+
// TopologyKey simulates a per-node topology.
29+
TopologyKey = Name + "/node"
30+
31+
// TopologyValue is the one, fixed node on which the driver runs.
32+
TopologyValue = "some-mock-node"
2733
)
2834

2935
// Manifest is the SP's manifest.
@@ -79,6 +85,7 @@ type Config struct {
7985
DisableControllerExpansion bool
8086
DisableOnlineExpansion bool
8187
PermissiveTargetPath bool
88+
EnableTopology bool
8289
ExecHooks *Hooks
8390
}
8491

@@ -154,11 +161,13 @@ const (
154161
)
155162

156163
func (s *service) newVolume(name string, capcity int64) csi.Volume {
157-
return csi.Volume{
164+
vol := csi.Volume{
158165
VolumeId: fmt.Sprintf("%d", atomic.AddUint64(&s.volsNID, 1)),
159166
VolumeContext: map[string]string{"name": name},
160167
CapacityBytes: capcity,
161168
}
169+
s.setTopology(&vol)
170+
return vol
162171
}
163172

164173
func (s *service) newVolumeFromSnapshot(name string, capacity int64, snapshotID int) csi.Volume {
@@ -170,6 +179,7 @@ func (s *service) newVolumeFromSnapshot(name string, capacity int64, snapshotID
170179
},
171180
},
172181
}
182+
s.setTopology(&vol)
173183
return vol
174184
}
175185

@@ -182,9 +192,22 @@ func (s *service) newVolumeFromVolume(name string, capacity int64, volumeID int)
182192
},
183193
},
184194
}
195+
s.setTopology(&vol)
185196
return vol
186197
}
187198

199+
func (s *service) setTopology(vol *csi.Volume) {
200+
if s.config.EnableTopology {
201+
vol.AccessibleTopology = []*csi.Topology{
202+
&csi.Topology{
203+
Segments: map[string]string{
204+
TopologyKey: TopologyValue,
205+
},
206+
},
207+
}
208+
}
209+
}
210+
188211
func (s *service) findVol(k, v string) (volIdx int, volInfo csi.Volume) {
189212
s.volsRWL.RLock()
190213
defer s.volsRWL.RUnlock()

0 commit comments

Comments
 (0)