Skip to content

Commit

Permalink
WIP: add regex transformation paterns for disk resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonskie committed Dec 16, 2024
1 parent a60ec2b commit f24b03f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 40 deletions.
23 changes: 23 additions & 0 deletions TEST.Md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@ ideas so far

IDS are coming from `/var/vcap/bosh/persistent_disk_hints.json`

# agent.json
the agent json regex
#### GCP
```
"DiskIDTransformPatern" "^(disk-.+)$",
"DiskIDTransformReplacement": "google-${1}"
```
#### AWS:
```
"DiskIDTransformPatern": "^(vol-(.+))$",
"DiskIDTransformReplacement": "nvme-Amazon_Elastic_Block_Store_vol${2}"
```
#### OpenStack:
```
"DiskIDTransformPatern": "^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$",
"DiskIDTransformReplacement": "scsi-${1}${2}${3}${4}${5}"
```
#### AliCloud:
```
"DiskIDTransformPatern": "^d-(.+)$",
"DiskIDTransformReplacement": "virtio-${1}"
```

# IAAS

## GCP:
Expand Down
59 changes: 23 additions & 36 deletions infrastructure/devicepathresolver/id_device_path_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@ import (
)

type idDevicePathResolver struct {
diskWaitTimeout time.Duration
udev boshudev.UdevDevice
fs boshsys.FileSystem
stripVolumeRegex string
stripVolumeCompiled *regexp.Regexp
diskWaitTimeout time.Duration
udev boshudev.UdevDevice
fs boshsys.FileSystem
DiskIDTransformPatern string
DiskIDTransformReplacement string
}

func NewIDDevicePathResolver(
diskWaitTimeout time.Duration,
udev boshudev.UdevDevice,
fs boshsys.FileSystem,
stripVolumeRegex string,
DiskIDTransformPatern string,
DiskIDTransformReplacement string,
) DevicePathResolver {
return &idDevicePathResolver{
diskWaitTimeout: diskWaitTimeout,
udev: udev,
fs: fs,
stripVolumeRegex: stripVolumeRegex,
stripVolumeCompiled: nil,
diskWaitTimeout: diskWaitTimeout,
udev: udev,
fs: fs,
DiskIDTransformPatern: DiskIDTransformPatern,
DiskIDTransformReplacement: DiskIDTransformReplacement,
}
}

Expand Down Expand Up @@ -60,13 +61,12 @@ func (idpr *idDevicePathResolver) GetRealDevicePath(diskSettings boshsettings.Di
var realPath string

diskID := diskSettings.ID
strippedDiskID, err := idpr.stripVolumeIfRequired(diskID)
TransformedDiskID, err := idpr.TransformDiskID(diskID)
if err != nil {
return "", false, err
}

deviceGlobPattern := fmt.Sprintf("*%s", strippedDiskID)
deviceIDPathGlobPattern := path.Join("/", "dev", "disk", "by-id", deviceGlobPattern)
deviceIDPathGlobPattern := path.Join("/", "dev", "disk", "by-id", TransformedDiskID)

for !found {
if time.Now().After(stopAfter) {
Expand Down Expand Up @@ -99,30 +99,17 @@ func (idpr *idDevicePathResolver) GetRealDevicePath(diskSettings boshsettings.Di
return realPath, false, nil
}

func (idpr *idDevicePathResolver) stripVolumeIfRequired(diskID string) (string, error) {
fmt.Println("DEBUG: stripVolumeRegex called with diskID:", diskID)
if idpr.stripVolumeRegex == "" {
fmt.Println("DEBUG: stripVolumeRegex is empty, returning diskID as is")
func (idpr *idDevicePathResolver) TransformDiskID(diskID string) (string, error) {
if idpr.DiskIDTransformPatern == "" {
fmt.Println("DEBUG: DiskIDTransformRules is empty, returning diskID as is")
return diskID, nil
}

if idpr.stripVolumeCompiled == nil {
var err error
idpr.stripVolumeCompiled, err = regexp.Compile(idpr.stripVolumeRegex)
if err != nil {
return "", bosherr.WrapError(err, "Compiling stripVolumeRegex")
}
}

// Ugly ducktape code to strip diskID from all the prefixes so we can later re-add them from agent.json
re := regexp.MustCompile(`^[^-]+-([0-9a-fA-F-]+)$`)
matches := re.FindStringSubmatch(diskID)
fmt.Println("DEBUG: all matches found:", matches)
if len(matches) > 1 {
fmt.Println("DEBUG: matches found:", idpr.stripVolumeRegex+matches[1])
strippedDiskID := matches[1]
return idpr.stripVolumeRegex + strippedDiskID, nil
transformed := diskID
re := regexp.MustCompile(idpr.DiskIDTransformPatern)
if re.MatchString(transformed) {
transformed = re.ReplaceAllString(transformed, idpr.DiskIDTransformReplacement)
}
fmt.Println("DEBUG: no matches found, returning diskID as is")
return idpr.stripVolumeCompiled.ReplaceAllLiteralString(diskID, ""), nil
fmt.Printf("DEBUG: DiskIDTransformRules: Original: %s -> Transformed: %s\n", diskID, transformed)
return transformed, nil
}
7 changes: 4 additions & 3 deletions platform/linux_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ type LinuxOptions struct {
// possible values: systemd, ""
ServiceManager string

// Regular expression specifying what part of volume ID to strip.
// possible values: valid RE2 regex e.g. "^vol-", "" (default is not to strip)
StripVolumeRegex string
// Regular expression specifying what part of disk ID to strip and transform
// example: "pattern": "^(disk-.+)$", "replacement": "google-${1}",
DiskIDTransformPatern string
DiskIDTransformReplacement string
}

type linux struct {
Expand Down
2 changes: 1 addition & 1 deletion platform/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func NewProvider(logger boshlog.Logger, dirProvider boshdirs.Provider, statsColl
switch options.Linux.DevicePathResolutionType {
case "virtio":
udev := boshudev.NewConcreteUdevDevice(runner, logger)
idDevicePathResolver := devicepathresolver.NewIDDevicePathResolver(500*time.Millisecond, udev, fs, options.Linux.StripVolumeRegex)
idDevicePathResolver := devicepathresolver.NewIDDevicePathResolver(500*time.Millisecond, udev, fs, options.Linux.DiskIDTransformPatern, options.Linux.DiskIDTransformReplacement)
mappedDevicePathResolver := devicepathresolver.NewMappedDevicePathResolver(30000*time.Millisecond, fs)
devicePathResolver = devicepathresolver.NewVirtioDevicePathResolver(idDevicePathResolver, mappedDevicePathResolver, logger)
case "scsi":
Expand Down

0 comments on commit f24b03f

Please sign in to comment.