Skip to content

Commit d7e3c28

Browse files
Separate user errors from internal errors
1 parent be08243 commit d7e3c28

21 files changed

+304
-229
lines changed

cmd/gce-pd-csi-driver/main.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func handle() {
107107
}
108108
extraVolumeLabels, err := common.ConvertLabelsStringToMap(*extraVolumeLabelsStr)
109109
if err != nil {
110-
klog.Fatalf("Bad extra volume labels: %w", err)
110+
klog.Fatalf("Bad extra volume labels: %v", err.Error())
111111
}
112112

113113
gceDriver := driver.GetGCEDriver()
@@ -124,7 +124,7 @@ func handle() {
124124
if *runControllerService {
125125
cloudProvider, err := gce.CreateCloudProvider(ctx, version, *cloudConfigFilePath, *computeEndpoint)
126126
if err != nil {
127-
klog.Fatalf("Failed to get cloud provider: %w", err)
127+
klog.Fatalf("Failed to get cloud provider: %v", err.Error())
128128
}
129129
initialBackoffDuration := time.Duration(*errorBackoffInitialDurationMs) * time.Millisecond
130130
maxBackoffDuration := time.Duration(*errorBackoffMaxDurationMs) * time.Millisecond
@@ -138,20 +138,20 @@ func handle() {
138138
if *runNodeService {
139139
mounter, err := mountmanager.NewSafeMounter()
140140
if err != nil {
141-
klog.Fatalf("Failed to get safe mounter: %w", err)
141+
klog.Fatalf("Failed to get safe mounter: %v", err.Error())
142142
}
143143
deviceUtils := deviceutils.NewDeviceUtils()
144144
statter := mountmanager.NewStatter(mounter)
145145
meta, err := metadataservice.NewMetadataService()
146146
if err != nil {
147-
klog.Fatalf("Failed to set up metadata service: %w", err)
147+
klog.Fatalf("Failed to set up metadata service: %v", err.Error())
148148
}
149149
nodeServer = driver.NewNodeServer(gceDriver, mounter, deviceUtils, meta, statter)
150150
}
151151

152152
err = gceDriver.SetupGCEDriver(driverName, version, extraVolumeLabels, identityServer, controllerServer, nodeServer)
153153
if err != nil {
154-
klog.Fatalf("Failed to initialize GCE CSI Driver: %w", err)
154+
klog.Fatalf("Failed to initialize GCE CSI Driver: %v", err.Error())
155155
}
156156

157157
gce.AttachDiskBackoff.Duration = *attachDiskBackoffDuration

pkg/deviceutils/device-utils.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (m *deviceUtils) GetDiskByIdPaths(deviceName string, partition string) []st
118118
func existingDevicePath(devicePaths []string) (string, error) {
119119
for _, devicePath := range devicePaths {
120120
if pathExists, err := pathExists(devicePath); err != nil {
121-
return "", fmt.Errorf("error checking if path exists: %v", err)
121+
return "", fmt.Errorf("error checking if path exists: %w", err)
122122
} else if pathExists {
123123
return devicePath, nil
124124
}
@@ -137,7 +137,7 @@ func getScsiSerial(devicePath string) (string, error) {
137137
"--whitelisted",
138138
fmt.Sprintf("--device=%v", devicePath)).CombinedOutput()
139139
if err != nil {
140-
return "", fmt.Errorf("scsi_id failed for device %q with output %s: %v", devicePath, string(out), err)
140+
return "", fmt.Errorf("scsi_id failed for device %q with output %s: %w", devicePath, string(out), err)
141141
}
142142

143143
return parseScsiSerial(string(out))
@@ -163,7 +163,7 @@ func getNvmeSerial(devicePath string) (string, error) {
163163
nvmeIdPath,
164164
fmt.Sprintf("-d%s", devicePath)).CombinedOutput()
165165
if err != nil {
166-
return "", fmt.Errorf("google_nvme_id failed for device %q with output %v: %v", devicePath, out, err)
166+
return "", fmt.Errorf("google_nvme_id failed for device %q with output %v: %w", devicePath, out, err)
167167
}
168168

169169
return parseNvmeSerial(string(out))
@@ -182,7 +182,7 @@ func parseNvmeSerial(output string) (string, error) {
182182
func ensureUdevToolExists(toolPath string) error {
183183
exists, err := pathutils.Exists(pathutils.CheckFollowSymlink, toolPath)
184184
if err != nil {
185-
return fmt.Errorf("failed to check existence of %q: %v", toolPath, err)
185+
return fmt.Errorf("failed to check existence of %q: %w", toolPath, err)
186186
}
187187
if !exists {
188188
// The driver should be containerized with the tool so maybe something is
@@ -225,7 +225,7 @@ func (m *deviceUtils) VerifyDevicePath(devicePaths []string, deviceName string)
225225

226226
devicePath, innerErr = existingDevicePath(devicePaths)
227227
if innerErr != nil {
228-
return false, fmt.Errorf("failed to check for existing device path: %v", innerErr)
228+
return false, fmt.Errorf("failed to check for existing device path: %w", innerErr)
229229
}
230230

231231
if len(devicePath) == 0 {
@@ -234,7 +234,7 @@ func (m *deviceUtils) VerifyDevicePath(devicePaths []string, deviceName string)
234234
// to repair the symlink.
235235
innerErr := udevadmTriggerForDiskIfExists(deviceName)
236236
if innerErr != nil {
237-
return false, fmt.Errorf("failed to trigger udevadm fix of non existent disk for %q: %v", deviceName, innerErr)
237+
return false, fmt.Errorf("failed to trigger udevadm fix of non existent disk for %q: %w", deviceName, innerErr)
238238
}
239239
// Go to next retry loop to get the deviceName again after
240240
// potentially fixing it with the udev command
@@ -246,12 +246,12 @@ func (m *deviceUtils) VerifyDevicePath(devicePaths []string, deviceName string)
246246
devFsPath, innerErr := filepath.EvalSymlinks(devicePath)
247247
klog.V(4).Infof("For disk %s the /dev/* path is %s", deviceName, devFsPath)
248248
if innerErr != nil {
249-
return false, fmt.Errorf("filepath.EvalSymlinks(%q) failed with %v", devicePath, innerErr)
249+
return false, fmt.Errorf("filepath.EvalSymlinks(%q) failed with %w", devicePath, innerErr)
250250
}
251251

252252
devFsSerial, innerErr := getDevFsSerial(devFsPath)
253253
if innerErr != nil {
254-
return false, fmt.Errorf("couldn't get serial number for disk %s at path %s: %v", deviceName, devFsPath, innerErr)
254+
return false, fmt.Errorf("couldn't get serial number for disk %s at path %s: %w", deviceName, devFsPath, innerErr)
255255
}
256256
// SUCCESS! devicePath points to a /dev/* path that has a serial
257257
// equivalent to our disk name
@@ -264,15 +264,15 @@ func (m *deviceUtils) VerifyDevicePath(devicePaths []string, deviceName string)
264264
// Attempt a repair
265265
innerErr = udevadmTriggerForDiskIfExists(deviceName)
266266
if innerErr != nil {
267-
return false, fmt.Errorf("failed to trigger udevadm fix of misconfigured disk for %q: %v", deviceName, innerErr)
267+
return false, fmt.Errorf("failed to trigger udevadm fix of misconfigured disk for %q: %w", deviceName, innerErr)
268268
}
269269
// Go to next retry loop to get the deviceName again after
270270
// potentially fixing it with the udev command
271271
return false, nil
272272
})
273273

274274
if err != nil {
275-
return "", fmt.Errorf("failed to find and re-link disk %s with udevadm after retrying for %v: %v", deviceName, pollTimeout, err)
275+
return "", fmt.Errorf("failed to find and re-link disk %s with udevadm after retrying for %v: %w", deviceName, pollTimeout, err)
276276
}
277277

278278
return devicePath, nil
@@ -298,11 +298,11 @@ func getDevFsSerial(devFsPath string) (string, error) {
298298
func findAvailableDevFsPaths() ([]string, error) {
299299
diskSDPaths, err := filepath.Glob(diskSDPattern)
300300
if err != nil {
301-
return nil, fmt.Errorf("failed to filepath.Glob(\"%s\"): %v", diskSDPattern, err)
301+
return nil, fmt.Errorf("failed to filepath.Glob(\"%s\"): %w", diskSDPattern, err)
302302
}
303303
diskNvmePaths, err := filepath.Glob(diskNvmePattern)
304304
if err != nil {
305-
return nil, fmt.Errorf("failed to filepath.Glob(\"%s\"): %v", diskNvmePattern, err)
305+
return nil, fmt.Errorf("failed to filepath.Glob(\"%s\"): %w", diskNvmePattern, err)
306306
}
307307
return append(diskSDPaths, diskNvmePaths...), nil
308308
}
@@ -318,7 +318,7 @@ func udevadmTriggerForDiskIfExists(deviceName string) error {
318318
if err != nil || len(devFsSerial) == 0 {
319319
// If we get an error, ignore. Either this isn't a block device, or it
320320
// isn't something we can get a serial number from
321-
klog.V(7).Infof("failed to get Serial num for disk %s at path %s: %v", deviceName, devFsPath, err)
321+
klog.V(7).Infof("failed to get Serial num for disk %s at path %s: %v", deviceName, devFsPath, err.Error())
322322
continue
323323
}
324324
devFsPathToSerial[devFsPath] = devFsSerial
@@ -328,7 +328,7 @@ func udevadmTriggerForDiskIfExists(deviceName string) error {
328328
klog.Warningf("udevadm --trigger running to fix disk at path %s which has serial numberID %s", devFsPath, devFsSerial)
329329
err := udevadmChangeToDrive(devFsPath)
330330
if err != nil {
331-
return fmt.Errorf("failed to fix disk which has serial numberID %s: %v", devFsSerial, err)
331+
return fmt.Errorf("failed to fix disk which has serial numberID %s: %w", devFsSerial, err)
332332
}
333333
return nil
334334
}
@@ -353,7 +353,7 @@ func udevadmChangeToDrive(devFsPath string) error {
353353
"--action=change",
354354
fmt.Sprintf("--property-match=DEVNAME=%s", devFsPath)).CombinedOutput()
355355
if err != nil {
356-
return fmt.Errorf("udevadmChangeToDrive: udevadm trigger failed for drive %q with output %s: %v.", devFsPath, string(out), err)
356+
return fmt.Errorf("udevadmChangeToDrive: udevadm trigger failed for drive %q with output %s: %w.", devFsPath, string(out), err)
357357
}
358358
return nil
359359
}

pkg/gce-cloud-provider/compute/fake-gce.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (cloud *FakeCloudProvider) RepairUnderspecifiedVolumeKey(ctx context.Contex
103103
}
104104
r, err := common.GetRegionFromZones([]string{cloud.zone})
105105
if err != nil {
106-
return "", nil, fmt.Errorf("failed to get region from zones: %v", err)
106+
return "", nil, fmt.Errorf("failed to get region from zones: %w", err)
107107
}
108108
volumeKey.Region = r
109109
return project, volumeKey, nil

pkg/gce-cloud-provider/compute/gce-compute.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (cloud *CloudProvider) GetDefaultZone() string {
115115
func (cloud *CloudProvider) ListDisks(ctx context.Context) ([]*computev1.Disk, string, error) {
116116
region, err := common.GetRegionFromZones([]string{cloud.zone})
117117
if err != nil {
118-
return nil, "", fmt.Errorf("failed to get region from zones: %v", err)
118+
return nil, "", fmt.Errorf("failed to get region from zones: %w", err)
119119
}
120120
zones, err := cloud.ListZones(ctx, region)
121121
if err != nil {
@@ -162,7 +162,7 @@ func (cloud *CloudProvider) RepairUnderspecifiedVolumeKey(ctx context.Context, p
162162
}
163163
region, err := common.GetRegionFromZones([]string{cloud.zone})
164164
if err != nil {
165-
return "", nil, fmt.Errorf("failed to get region from zones: %v", err)
165+
return "", nil, fmt.Errorf("failed to get region from zones: %w", err)
166166
}
167167
switch volumeKey.Type() {
168168
case meta.Zonal:
@@ -216,7 +216,7 @@ func (cloud *CloudProvider) ListZones(ctx context.Context, region string) ([]str
216216
zones := []string{}
217217
zoneList, err := cloud.service.Zones.List(cloud.project).Filter(fmt.Sprintf("region eq .*%s$", region)).Do()
218218
if err != nil {
219-
return nil, fmt.Errorf("failed to list zones in region %s: %v", region, err)
219+
return nil, fmt.Errorf("failed to list zones in region %s: %w", region, err)
220220
}
221221
for _, zone := range zoneList.Items {
222222
zones = append(zones, zone.Name)
@@ -515,7 +515,7 @@ func (cloud *CloudProvider) insertRegionalDisk(
515515
klog.Warningf("GCE PD %s already exists after wait, reusing", volKey.Name)
516516
return nil
517517
}
518-
return fmt.Errorf("unknown Insert disk operation error: %v", err)
518+
return fmt.Errorf("unknown Insert disk operation error: %w", err)
519519
}
520520
return nil
521521
}
@@ -605,7 +605,7 @@ func (cloud *CloudProvider) insertZonalDisk(
605605
klog.Warningf("GCE PD %s already exists, reusing", volKey.Name)
606606
return nil
607607
}
608-
return fmt.Errorf("unknown Insert disk error: %v", err)
608+
return fmt.Errorf("unknown Insert disk error: %w", err)
609609
}
610610
klog.V(5).Infof("InsertDisk operation %s for disk %s", opName, diskToCreate.Name)
611611

@@ -627,7 +627,7 @@ func (cloud *CloudProvider) insertZonalDisk(
627627
klog.Warningf("GCE PD %s already exists after wait, reusing", volKey.Name)
628628
return nil
629629
}
630-
return fmt.Errorf("unknown Insert disk operation error: %v", err)
630+
return fmt.Errorf("unknown Insert disk operation error: %w", err)
631631
}
632632
return nil
633633
}
@@ -686,7 +686,7 @@ func (cloud *CloudProvider) AttachDisk(ctx context.Context, project string, volK
686686

687687
deviceName, err := common.GetDeviceName(volKey)
688688
if err != nil {
689-
return fmt.Errorf("failed to get device name: %v", err)
689+
return fmt.Errorf("failed to get device name: %w", err)
690690
}
691691
attachedDiskV1 := &computev1.AttachedDisk{
692692
DeviceName: deviceName,
@@ -698,13 +698,13 @@ func (cloud *CloudProvider) AttachDisk(ctx context.Context, project string, volK
698698

699699
op, err := cloud.service.Instances.AttachDisk(project, instanceZone, instanceName, attachedDiskV1).Context(ctx).Do()
700700
if err != nil {
701-
return fmt.Errorf("failed cloud service attach disk call: %v", err)
701+
return fmt.Errorf("failed cloud service attach disk call: %w", err)
702702
}
703703
klog.V(5).Infof("AttachDisk operation %s for disk %s", op.Name, attachedDiskV1.DeviceName)
704704

705705
err = cloud.waitForZonalOp(ctx, project, op.Name, instanceZone)
706706
if err != nil {
707-
return fmt.Errorf("failed when waiting for zonal op: %v", err)
707+
return fmt.Errorf("failed when waiting for zonal op: %w", err)
708708
}
709709
return nil
710710
}
@@ -815,7 +815,7 @@ func (cloud *CloudProvider) WaitForAttach(ctx context.Context, project string, v
815815
klog.V(6).Infof("Polling for attach of disk %v to instance %v to complete for %v", volKey.Name, instanceName, time.Since(start))
816816
disk, err := cloud.GetDisk(ctx, project, volKey, GCEAPIVersionV1)
817817
if err != nil {
818-
return false, fmt.Errorf("GetDisk failed to get disk: %v", err)
818+
return false, fmt.Errorf("GetDisk failed to get disk: %w", err)
819819
}
820820

821821
if disk == nil {
@@ -948,7 +948,7 @@ func (cloud *CloudProvider) waitForImageCreation(ctx context.Context, project, i
948948
klog.V(6).Infof("Checking GCE Image %s.", imageName)
949949
image, err := cloud.GetImage(ctx, project, imageName)
950950
if err != nil {
951-
klog.Warningf("Error in getting image %s, %w", imageName, err)
951+
klog.Warningf("Error in getting image %s, %v", imageName, err.Error())
952952
} else if image != nil {
953953
if image.Status != "PENDING" {
954954
klog.V(6).Infof("Image %s status is %s", imageName, image.Status)
@@ -1011,7 +1011,7 @@ func (cloud *CloudProvider) ResizeDisk(ctx context.Context, project string, volK
10111011
klog.V(5).Infof("Resizing disk %v to size %v", volKey, requestBytes)
10121012
cloudDisk, err := cloud.GetDisk(ctx, project, volKey, GCEAPIVersionV1)
10131013
if err != nil {
1014-
return -1, fmt.Errorf("failed to get disk: %v", err)
1014+
return -1, fmt.Errorf("failed to get disk: %w", err)
10151015
}
10161016

10171017
sizeGb := cloudDisk.GetSizeGb()
@@ -1039,13 +1039,13 @@ func (cloud *CloudProvider) resizeZonalDisk(ctx context.Context, project string,
10391039
}
10401040
op, err := cloud.service.Disks.Resize(project, volKey.Zone, volKey.Name, resizeReq).Context(ctx).Do()
10411041
if err != nil {
1042-
return -1, fmt.Errorf("failed to resize zonal volume %v: %v", volKey.String(), err)
1042+
return -1, fmt.Errorf("failed to resize zonal volume %v: %w", volKey.String(), err)
10431043
}
10441044
klog.V(5).Infof("ResizeDisk operation %s for disk %s", op.Name, volKey.Name)
10451045

10461046
err = cloud.waitForZonalOp(ctx, project, op.Name, volKey.Zone)
10471047
if err != nil {
1048-
return -1, fmt.Errorf("failed waiting for op for zonal resize for %s: %v", volKey.String(), err)
1048+
return -1, fmt.Errorf("failed waiting for op for zonal resize for %s: %w", volKey.String(), err)
10491049
}
10501050

10511051
return requestGb, nil
@@ -1058,13 +1058,13 @@ func (cloud *CloudProvider) resizeRegionalDisk(ctx context.Context, project stri
10581058

10591059
op, err := cloud.service.RegionDisks.Resize(project, volKey.Region, volKey.Name, resizeReq).Context(ctx).Do()
10601060
if err != nil {
1061-
return -1, fmt.Errorf("failed to resize regional volume %v: %v", volKey.String(), err)
1061+
return -1, fmt.Errorf("failed to resize regional volume %v: %w", volKey.String(), err)
10621062
}
10631063
klog.V(5).Infof("ResizeDisk operation %s for disk %s", op.Name, volKey.Name)
10641064

10651065
err = cloud.waitForRegionalOp(ctx, project, op.Name, volKey.Region)
10661066
if err != nil {
1067-
return -1, fmt.Errorf("failed waiting for op for regional resize for %s: %v", volKey.String(), err)
1067+
return -1, fmt.Errorf("failed waiting for op for regional resize for %s: %w", volKey.String(), err)
10681068
}
10691069

10701070
return requestGb, nil
@@ -1116,7 +1116,7 @@ func (cloud *CloudProvider) waitForSnapshotCreation(ctx context.Context, project
11161116
klog.V(6).Infof("Checking GCE Snapshot %s.", snapshotName)
11171117
snapshot, err := cloud.GetSnapshot(ctx, project, snapshotName)
11181118
if err != nil {
1119-
klog.Warningf("Error in getting snapshot %s, %w", snapshotName, err)
1119+
klog.Warningf("Error in getting snapshot %s, %v", snapshotName, err.Error())
11201120
} else if snapshot != nil {
11211121
if snapshot.Status != "CREATING" {
11221122
klog.V(6).Infof("Snapshot %s status is %s", snapshotName, snapshot.Status)
@@ -1163,7 +1163,7 @@ func encodeTags(tags map[string]string) (string, error) {
11631163

11641164
enc, err := json.Marshal(tags)
11651165
if err != nil {
1166-
return "", fmt.Errorf("failed to encodeTags %v: %v", tags, err)
1166+
return "", fmt.Errorf("failed to encodeTags %v: %w", tags, err)
11671167
}
11681168
return string(enc), nil
11691169
}

pkg/gce-cloud-provider/compute/gce.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func CreateCloudProvider(ctx context.Context, vendorVersion string, configPath s
9696

9797
project, zone, err := getProjectAndZone(configFile)
9898
if err != nil {
99-
return nil, fmt.Errorf("Failed getting Project and Zone: %v", err)
99+
return nil, fmt.Errorf("Failed getting Project and Zone: %w", err)
100100
}
101101

102102
return &CloudProvider{
@@ -144,13 +144,13 @@ func readConfig(configPath string) (*ConfigFile, error) {
144144

145145
reader, err := os.Open(configPath)
146146
if err != nil {
147-
return nil, fmt.Errorf("couldn't open cloud provider configuration at %s: %v", configPath, err)
147+
return nil, fmt.Errorf("couldn't open cloud provider configuration at %s: %w", configPath, err)
148148
}
149149
defer reader.Close()
150150

151151
cfg := &ConfigFile{}
152152
if err := gcfg.FatalOnly(gcfg.ReadInto(cfg, reader)); err != nil {
153-
return nil, fmt.Errorf("couldn't read cloud provider configuration at %s: %v", configPath, err)
153+
return nil, fmt.Errorf("couldn't read cloud provider configuration at %s: %w", configPath, err)
154154
}
155155
return cfg, nil
156156
}
@@ -201,7 +201,7 @@ func createCloudServiceWithDefaultServiceAccount(ctx context.Context, vendorVers
201201
func newOauthClient(ctx context.Context, tokenSource oauth2.TokenSource) (*http.Client, error) {
202202
if err := wait.PollImmediate(5*time.Second, 30*time.Second, func() (bool, error) {
203203
if _, err := tokenSource.Token(); err != nil {
204-
klog.Errorf("error fetching initial token: %w", err)
204+
klog.Errorf("error fetching initial token: %v", err.Error())
205205
return false, nil
206206
}
207207
return true, nil

pkg/gce-cloud-provider/metadata/metadata.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ var _ MetadataService = &metadataServiceManager{}
4545
func NewMetadataService() (MetadataService, error) {
4646
zone, err := metadata.Zone()
4747
if err != nil {
48-
return nil, fmt.Errorf("failed to get current zone: %v", err)
48+
return nil, fmt.Errorf("failed to get current zone: %w", err)
4949
}
5050
projectID, err := metadata.ProjectID()
5151
if err != nil {
52-
return nil, fmt.Errorf("failed to get project: %v", err)
52+
return nil, fmt.Errorf("failed to get project: %w", err)
5353
}
5454
name, err := metadata.InstanceName()
5555
if err != nil {
56-
return nil, fmt.Errorf("failed to get instance name: %v", err)
56+
return nil, fmt.Errorf("failed to get instance name: %w", err)
5757
}
5858
fullMachineType, err := metadata.Get("instance/machine-type")
5959
if err != nil {
60-
return nil, fmt.Errorf("failed to get machine-type: %v", err)
60+
return nil, fmt.Errorf("failed to get machine-type: %w", err)
6161
}
6262
// Response format: "projects/[NUMERIC_PROJECT_ID]/machineTypes/[MACHINE_TYPE]"
6363
splits := strings.Split(fullMachineType, "/")

0 commit comments

Comments
 (0)