Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions api/v1alpha1/lvmcluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,24 +426,6 @@ var _ = Describe("webhook acceptance tests", func() {
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
})

It("updating ThinPoolConfig.OverprovisionRatio is not allowed", func(ctx SpecContext) {
resource := defaultLVMClusterInUniqueNamespace(ctx)
Expect(k8sClient.Create(ctx, resource)).To(Succeed())

updated := resource.DeepCopy()

updated.Spec.Storage.DeviceClasses[0].ThinPoolConfig.OverprovisionRatio--

err := k8sClient.Update(ctx, updated)
Expect(err).To(HaveOccurred())
Expect(err).To(Satisfy(k8serrors.IsForbidden))
statusError := &k8serrors.StatusError{}
Expect(errors.As(err, &statusError)).To(BeTrue())
Expect(statusError.Status().Message).To(ContainSubstring(ErrThinPoolConfigCannotBeChanged.Error()))

Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
})

It("updating ThinPoolConfig.ChunkSizeCalculationPolicy is not allowed", func(ctx SpecContext) {
resource := defaultLVMClusterInUniqueNamespace(ctx)
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
Expand Down
2 changes: 0 additions & 2 deletions api/v1alpha1/lvmcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@ func (v *lvmClusterValidator) ValidateUpdate(_ context.Context, old, new runtime
return warnings, fmt.Errorf("ThinPoolConfig.Name is invalid: %w", ErrThinPoolConfigCannotBeChanged)
} else if newThinPoolConfig.SizePercent != oldThinPoolConfig.SizePercent {
return warnings, fmt.Errorf("ThinPoolConfig.SizePercent is invalid: %w", ErrThinPoolConfigCannotBeChanged)
} else if newThinPoolConfig.OverprovisionRatio != oldThinPoolConfig.OverprovisionRatio {
return warnings, fmt.Errorf("ThinPoolConfig.OverprovisionRatio is invalid: %w", ErrThinPoolConfigCannotBeChanged)
} else if newThinPoolConfig.ChunkSizeCalculationPolicy != oldThinPoolConfig.ChunkSizeCalculationPolicy {
return warnings, fmt.Errorf("ThinPoolConfig.ChunkSizeCalculationPolicy is invalid: %w", ErrThinPoolConfigCannotBeChanged)
} else if !reflect.DeepEqual(newThinPoolConfig.ChunkSize, oldThinPoolConfig.ChunkSize) {
Expand Down
17 changes: 11 additions & 6 deletions internal/controllers/vgmanager/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,17 +331,20 @@ func (r *Reconciler) applyLVMDConfig(ctx context.Context, volumeGroup *lvmv1alph
lvmdConfigWasMissing = true
lvmdConfig = &lvmd.Config{}
}
existingLvmdConfig := *lvmdConfig

oldConfig := lvmd.DeepCopyConfig(lvmdConfig)

// Add the volume group to device classes inside lvmd config if not exists
found := false
var dc *lvmd.DeviceClass
for _, deviceClass := range lvmdConfig.DeviceClasses {
if deviceClass.Name == volumeGroup.Name {
found = true
dc = deviceClass
break
}
}
if !found {
dc := &lvmd.DeviceClass{

if dc == nil {
dc = &lvmd.DeviceClass{
Name: volumeGroup.Name,
VolumeGroup: volumeGroup.Name,
Default: volumeGroup.Spec.Default,
Expand All @@ -360,9 +363,11 @@ func (r *Reconciler) applyLVMDConfig(ctx context.Context, volumeGroup *lvmv1alph
}

lvmdConfig.DeviceClasses = append(lvmdConfig.DeviceClasses, dc)
} else if dc.Type == lvmd.TypeThin {
dc.ThinPoolConfig.OverprovisionRatio = float64(volumeGroup.Spec.ThinPoolConfig.OverprovisionRatio)
}

if err := r.updateLVMDConfigAfterReconcile(ctx, volumeGroup, &existingLvmdConfig, lvmdConfig, lvmdConfigWasMissing); err != nil {
if err := r.updateLVMDConfigAfterReconcile(ctx, volumeGroup, oldConfig, lvmdConfig, lvmdConfigWasMissing); err != nil {
if _, err := r.setVolumeGroupFailedStatus(ctx, volumeGroup, vgs, devices, err); err != nil {
logger.Error(err, "failed to set status to failed")
}
Expand Down
45 changes: 45 additions & 0 deletions internal/controllers/vgmanager/lvmd/lvmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
lvmdCMD "github.com/topolvm/topolvm/cmd/lvmd/app"
lvmd "github.com/topolvm/topolvm/pkg/lvmd/types"

"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/yaml"
)
Expand All @@ -31,6 +32,50 @@ const (
maxReadLength = 2 * 1 << 20 // 2MB
)

func DeepCopyConfig(c *Config) *Config {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this? Couldnt you just deref and reref the Config? Or use deepcopy gen?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deref is not working because we have pointers inside. We can generate a deep copy if you think this will be better.

if c == nil {
return nil
}

conf := &Config{
SocketName: c.SocketName,
}

for _, dc := range c.DeviceClasses {
newDc := &DeviceClass{
Name: dc.Name,
VolumeGroup: dc.VolumeGroup,
Default: dc.Default,
StripeSize: dc.StripeSize,
LVCreateOptions: dc.LVCreateOptions,
Type: dc.Type,
}
if dc.SpareGB != nil {
newDc.SpareGB = ptr.To(*dc.SpareGB)
}
if dc.Stripe != nil {
newDc.Stripe = ptr.To(*dc.Stripe)
}
if dc.ThinPoolConfig != nil {
newDc.ThinPoolConfig = &ThinPoolConfig{
Name: dc.ThinPoolConfig.Name,
OverprovisionRatio: dc.ThinPoolConfig.OverprovisionRatio,
}
}
conf.DeviceClasses = append(conf.DeviceClasses, newDc)
}

for _, co := range c.LvcreateOptionClasses {
opt := &lvmd.LvcreateOptionClass{
Name: co.Name,
Options: co.Options,
}
conf.LvcreateOptionClasses = append(conf.LvcreateOptionClasses, opt)
}

return conf
}

func DefaultConfigurator() *CachedFileConfig {
return NewFileConfigurator(DefaultFileConfigPath)
}
Expand Down