Skip to content

Commit

Permalink
lxd/storage/drivers/zfs: filter redundant options on `ensureInitialDa…
Browse files Browse the repository at this point in the history
…tasets`

Signed-off-by: hamistao <pedro.ribeiro@canonical.com>
  • Loading branch information
hamistao committed Apr 18, 2024
1 parent 42f3ea9 commit 7dbc233
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
34 changes: 24 additions & 10 deletions lxd/storage/drivers/driver_zfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,19 @@ func (d zfs) ensureInitialDatasets(warnOnExistingPolicyApplyError bool) error {
args = append(args, fmt.Sprintf("%s=%s", k, v))
}

err := d.setDatasetProperties(d.config["zfs.pool_name"], args...)
args, err := d.filterRedundantOptions(d.config["zfs.pool_name"], args...)
if err != nil {
if warnOnExistingPolicyApplyError {
d.logger.Warn("Failed applying policy to existing dataset", logger.Ctx{"dataset": d.config["zfs.pool_name"], "err": err})
} else {
return fmt.Errorf("Failed applying policy to existing dataset %q: %w", d.config["zfs.pool_name"], err)
return err
}

if len(args) > 0 {
err := d.setDatasetProperties(d.config["zfs.pool_name"], args...)
if err != nil {
if warnOnExistingPolicyApplyError {

Check failure on line 155 in lxd/storage/drivers/driver_zfs.go

View workflow job for this annotation

GitHub Actions / Code

early-return: if c { ... } else { ... return } can be simplified to if !c { ... return } ... (revive)
d.logger.Warn("Failed applying policy to existing dataset", logger.Ctx{"dataset": d.config["zfs.pool_name"], "err": err})
} else {
return fmt.Errorf("Failed applying policy to existing dataset %q: %w", d.config["zfs.pool_name"], err)
}
}
}

Expand All @@ -166,12 +173,19 @@ func (d zfs) ensureInitialDatasets(warnOnExistingPolicyApplyError bool) error {
}

if exists {
err = d.setDatasetProperties(datasetPath, properties...)
properties, err = d.filterRedundantOptions(dataset, properties...)
if err != nil {
if warnOnExistingPolicyApplyError {
d.logger.Warn("Failed applying policy to existing dataset", logger.Ctx{"dataset": datasetPath, "err": err})
} else {
return fmt.Errorf("Failed applying policy to existing dataset %q: %w", datasetPath, err)
return err
}

if len(properties) > 0 {
err = d.setDatasetProperties(datasetPath, properties...)
if err != nil {
if warnOnExistingPolicyApplyError {

Check failure on line 184 in lxd/storage/drivers/driver_zfs.go

View workflow job for this annotation

GitHub Actions / Code

early-return: if c { ... } else { ... return } can be simplified to if !c { ... return } ... (revive)
d.logger.Warn("Failed applying policy to existing dataset", logger.Ctx{"dataset": datasetPath, "err": err})
} else {
return fmt.Errorf("Failed applying policy to existing dataset %q: %w", datasetPath, err)
}
}
}
} else {
Expand Down
34 changes: 34 additions & 0 deletions lxd/storage/drivers/driver_zfs_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,40 @@ func (d *zfs) getDatasets(dataset string, types string) ([]string, error) {
return children, nil
}

func (d *zfs) filterRedundantOptions(dataset string, options ...string) ([]string, error) {
keys := []string{}
values := []string{}

// Extract keys and values from options.
for _, option := range options {
property := strings.Split(option, "=")

if len(property) != 2 {
continue
}

keys = append(keys, property[0])
values = append(values, property[1])
}

// Get current values for the keys.
currentProperties, err := d.getDatasetProperties(dataset, keys...)
if err != nil {
return nil, err
}

resultantOptions := []string{}

// Change property values that are different from the current value.
for propertyIndex := range keys {
if currentProperties[keys[propertyIndex]] != values[propertyIndex] {
resultantOptions = append(resultantOptions, options[propertyIndex])
}
}

return resultantOptions, nil
}

func (d *zfs) setDatasetProperties(dataset string, options ...string) error {
args := []string{"set"}
args = append(args, options...)
Expand Down

0 comments on commit 7dbc233

Please sign in to comment.