Skip to content

Commit 96dbb7b

Browse files
authored
[cinder-csi]: allow node service to run without openstack client (#2656)
1 parent 976675a commit 96dbb7b

File tree

4 files changed

+197
-13
lines changed

4 files changed

+197
-13
lines changed

cmd/cinder-csi-plugin/main.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var (
3838
httpEndpoint string
3939
provideControllerService bool
4040
provideNodeService bool
41+
noClient bool
4142
)
4243

4344
func main() {
@@ -70,6 +71,7 @@ func main() {
7071

7172
cmd.PersistentFlags().BoolVar(&provideControllerService, "provide-controller-service", true, "If set to true then the CSI driver does provide the controller service (default: true)")
7273
cmd.PersistentFlags().BoolVar(&provideNodeService, "provide-node-service", true, "If set to true then the CSI driver does provide the node service (default: true)")
74+
cmd.PersistentFlags().BoolVar(&noClient, "node-service-no-os-client", false, "If set to true then the CSI driver node service will not use the OpenStack client (default: false)")
7375

7476
openstack.AddExtraFlags(pflag.CommandLine)
7577

@@ -82,17 +84,24 @@ func handle() {
8284
d := cinder.NewDriver(&cinder.DriverOpts{Endpoint: endpoint, ClusterID: cluster})
8385

8486
openstack.InitOpenStackProvider(cloudConfig, httpEndpoint)
85-
cloud, err := openstack.GetOpenStackProvider()
86-
if err != nil {
87-
klog.Warningf("Failed to GetOpenStackProvider: %v", err)
88-
return
89-
}
9087

9188
if provideControllerService {
89+
cloud, err := openstack.GetOpenStackProvider(false)
90+
if err != nil {
91+
klog.Warningf("Failed to GetOpenStackProvider: %v", err)
92+
return
93+
}
94+
9295
d.SetupControllerService(cloud)
9396
}
9497

9598
if provideNodeService {
99+
cloud, err := openstack.GetOpenStackProvider(noClient)
100+
if err != nil {
101+
klog.Warningf("Failed to GetOpenStackProvider: %v", err)
102+
return
103+
}
104+
96105
//Initialize mount
97106
mount := mount.GetMountProvider()
98107

docs/cinder-csi-plugin/using-cinder-csi-plugin.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ In addition to the standard set of klog flags, `cinder-csi-plugin` accepts the f
111111

112112
The default is to provide the node service.
113113
</dd>
114+
115+
<dt>--node-service-no-os-client &lt;disabled&gt;</dt>
116+
<dd>
117+
If set to true then the CSI driver does not provide the OpenStack client in the node service.
118+
119+
The default is to provide the OpenStack client in the node service.
120+
</dd>
114121
</dl>
115122

116123
## Driver Config
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package openstack
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/backups"
23+
"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/snapshots"
24+
"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"
25+
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
26+
"k8s.io/cloud-provider-openstack/pkg/util/metadata"
27+
)
28+
29+
type NoopOpenStack struct {
30+
bsOpts BlockStorageOpts
31+
metadataOpts metadata.Opts
32+
}
33+
34+
func (os *NoopOpenStack) CreateVolume(name string, size int, vtype, availability string, snapshotID string, sourceVolID string, sourceBackupID string, tags map[string]string) (*volumes.Volume, error) {
35+
return nil, fmt.Errorf("CreateVolume is not implemented for ephemeral storage in this configuration")
36+
}
37+
38+
func (os *NoopOpenStack) ListVolumes(limit int, startingToken string) ([]volumes.Volume, string, error) {
39+
return nil, "", nil
40+
}
41+
42+
func (os *NoopOpenStack) GetVolumesByName(n string) ([]volumes.Volume, error) {
43+
return nil, nil
44+
}
45+
46+
func (os *NoopOpenStack) DeleteVolume(volumeID string) error {
47+
return nil
48+
}
49+
50+
func (os *NoopOpenStack) GetVolume(volumeID string) (*volumes.Volume, error) {
51+
return &volumes.Volume{ID: volumeID}, nil
52+
}
53+
54+
func (os *NoopOpenStack) AttachVolume(instanceID, volumeID string) (string, error) {
55+
return volumeID, nil
56+
}
57+
58+
func (os *NoopOpenStack) WaitDiskAttached(instanceID string, volumeID string) error {
59+
return nil
60+
}
61+
62+
func (os *NoopOpenStack) WaitVolumeTargetStatus(volumeID string, tStatus []string) error {
63+
return nil
64+
}
65+
66+
func (os *NoopOpenStack) DetachVolume(instanceID, volumeID string) error {
67+
return nil
68+
}
69+
70+
func (os *NoopOpenStack) WaitDiskDetached(instanceID string, volumeID string) error {
71+
return nil
72+
}
73+
74+
func (os *NoopOpenStack) GetAttachmentDiskPath(instanceID, volumeID string) (string, error) {
75+
return "", nil
76+
}
77+
78+
func (os *NoopOpenStack) ExpandVolume(volumeID string, status string, newSize int) error {
79+
return nil
80+
}
81+
82+
func (os *NoopOpenStack) GetMaxVolLimit() int64 {
83+
if os.bsOpts.NodeVolumeAttachLimit > 0 && os.bsOpts.NodeVolumeAttachLimit <= 256 {
84+
return os.bsOpts.NodeVolumeAttachLimit
85+
}
86+
87+
return defaultMaxVolAttachLimit
88+
}
89+
90+
func (os *NoopOpenStack) GetBlockStorageOpts() BlockStorageOpts {
91+
return os.bsOpts
92+
}
93+
94+
func (os *NoopOpenStack) GetMetadataOpts() metadata.Opts {
95+
return os.metadataOpts
96+
}
97+
98+
func (os *NoopOpenStack) CreateBackup(name, volID, snapshotID, availabilityZone string, tags map[string]string) (*backups.Backup, error) {
99+
return &backups.Backup{}, nil
100+
}
101+
102+
func (os *NoopOpenStack) BackupsAreEnabled() (bool, error) {
103+
return false, nil
104+
}
105+
106+
func (os *NoopOpenStack) DeleteBackup(backupID string) error {
107+
return nil
108+
}
109+
110+
func (os *NoopOpenStack) CreateSnapshot(name, volID string, tags map[string]string) (*snapshots.Snapshot, error) {
111+
return &snapshots.Snapshot{}, nil
112+
}
113+
114+
func (os *NoopOpenStack) DeleteSnapshot(snapID string) error {
115+
return nil
116+
}
117+
118+
func (os *NoopOpenStack) GetSnapshotByID(snapshotID string) (*snapshots.Snapshot, error) {
119+
return &snapshots.Snapshot{ID: snapshotID}, nil
120+
}
121+
122+
func (os *NoopOpenStack) ListSnapshots(filters map[string]string) ([]snapshots.Snapshot, string, error) {
123+
return nil, "", nil
124+
}
125+
126+
func (os *NoopOpenStack) WaitSnapshotReady(snapshotID string) (string, error) {
127+
return "", nil
128+
}
129+
130+
func (os *NoopOpenStack) GetBackupByID(backupID string) (*backups.Backup, error) {
131+
return &backups.Backup{ID: backupID}, nil
132+
}
133+
134+
func (os *NoopOpenStack) ListBackups(filters map[string]string) ([]backups.Backup, error) {
135+
return nil, nil
136+
}
137+
138+
func (os *NoopOpenStack) WaitBackupReady(backupID string, snapshotSize int, backupMaxDurationSecondsPerGB int) (string, error) {
139+
return "", nil
140+
}
141+
142+
func (os *NoopOpenStack) GetInstanceByID(instanceID string) (*servers.Server, error) {
143+
return &servers.Server{ID: instanceID}, nil
144+
}

pkg/csi/cinder/openstack/openstack.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ func GetConfigFromFiles(configFilePaths []string) (Config, error) {
139139
const defaultMaxVolAttachLimit int64 = 256
140140

141141
var OsInstance IOpenStack
142+
var NoopInstance IOpenStack
142143
var configFiles = []string{"/etc/cloud.conf"}
143144

144145
func InitOpenStackProvider(cfgFiles []string, httpEndpoint string) {
@@ -160,7 +161,7 @@ func InitOpenStackProvider(cfgFiles []string, httpEndpoint string) {
160161
}
161162

162163
// CreateOpenStackProvider creates Openstack Instance
163-
func CreateOpenStackProvider() (IOpenStack, error) {
164+
func CreateOpenStackProvider(noClient bool) (IOpenStack, error) {
164165
// Get config from file
165166
cfg, err := GetConfigFromFiles(configFiles)
166167
if err != nil {
@@ -169,6 +170,21 @@ func CreateOpenStackProvider() (IOpenStack, error) {
169170
}
170171
logcfg(cfg)
171172

173+
// if no search order given, use default
174+
if len(cfg.Metadata.SearchOrder) == 0 {
175+
cfg.Metadata.SearchOrder = fmt.Sprintf("%s,%s", metadata.ConfigDriveID, metadata.MetadataID)
176+
}
177+
178+
if noClient {
179+
// Init OpenStack
180+
NoopInstance = &NoopOpenStack{
181+
bsOpts: cfg.BlockStorage,
182+
metadataOpts: cfg.Metadata,
183+
}
184+
185+
return NoopInstance, nil
186+
}
187+
172188
provider, err := client.NewOpenStackClient(&cfg.Global, "cinder-csi-plugin", userAgentData...)
173189
if err != nil {
174190
return nil, err
@@ -191,11 +207,6 @@ func CreateOpenStackProvider() (IOpenStack, error) {
191207
return nil, err
192208
}
193209

194-
// if no search order given, use default
195-
if len(cfg.Metadata.SearchOrder) == 0 {
196-
cfg.Metadata.SearchOrder = fmt.Sprintf("%s,%s", metadata.ConfigDriveID, metadata.MetadataID)
197-
}
198-
199210
// Init OpenStack
200211
OsInstance = &OpenStack{
201212
compute: computeclient,
@@ -209,12 +220,25 @@ func CreateOpenStackProvider() (IOpenStack, error) {
209220
}
210221

211222
// GetOpenStackProvider returns Openstack Instance
212-
func GetOpenStackProvider() (IOpenStack, error) {
223+
func GetOpenStackProvider(noClient bool) (IOpenStack, error) {
224+
if noClient {
225+
if NoopInstance != nil {
226+
return NoopInstance, nil
227+
}
228+
var err error
229+
NoopInstance, err = CreateOpenStackProvider(noClient)
230+
if err != nil {
231+
return nil, err
232+
}
233+
234+
return NoopInstance, nil
235+
}
236+
213237
if OsInstance != nil {
214238
return OsInstance, nil
215239
}
216240
var err error
217-
OsInstance, err = CreateOpenStackProvider()
241+
OsInstance, err = CreateOpenStackProvider(noClient)
218242
if err != nil {
219243
return nil, err
220244
}

0 commit comments

Comments
 (0)