Skip to content
63 changes: 60 additions & 3 deletions images/agent/src/internal/controller/lvg/discoverer.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,16 @@ func (d *Discoverer) LVMVolumeGroupDiscoverReconcile(ctx context.Context) bool {
d.log.Info("[RunLVMVolumeGroupDiscoverController] no BlockDevices were found")
return false
}
d.log.Trace(fmt.Sprintf("[RunLVMVolumeGroupDiscoverController] BlockDevices: %+v", blockDevices))

filteredLVGs := filterLVGsByNode(currentLVMVGs, d.cfg.NodeName)
filteredBlockDevices := filterBlockDevicesByNodeName(blockDevices, d.cfg.NodeName)
d.log.Trace(fmt.Sprintf("[RunLVMVolumeGroupDiscoverController] Filtered LVMVolumeGroups: %+v", filteredLVGs))
d.log.Trace(fmt.Sprintf("[RunLVMVolumeGroupDiscoverController] Filtered BlockDevices: %+v", filteredBlockDevices))

d.log.Debug("[RunLVMVolumeGroupDiscoverController] tries to get LVMVolumeGroup candidates")
candidates, err := d.GetLVMVolumeGroupCandidates(blockDevices)

candidates, err := d.GetLVMVolumeGroupCandidates(filteredBlockDevices)
if err != nil {
d.log.Error(err, "[RunLVMVolumeGroupDiscoverController] unable to run GetLVMVolumeGroupCandidates")
for _, lvg := range filteredLVGs {
Expand All @@ -133,9 +138,9 @@ func (d *Discoverer) LVMVolumeGroupDiscoverReconcile(ctx context.Context) bool {

shouldRequeue := false
for _, candidate := range candidates {
d.log.Trace(fmt.Sprintf("[RunLVMVolumeGroupDiscoverController] candidate: %+v", candidate))
if lvg, exist := filteredLVGs[candidate.ActualVGNameOnTheNode]; exist {
d.log.Debug(fmt.Sprintf("[RunLVMVolumeGroupDiscoverController] the LVMVolumeGroup %s is already exist. Tries to update it", lvg.Name))
d.log.Trace(fmt.Sprintf("[RunLVMVolumeGroupDiscoverController] candidate: %+v", candidate))
d.log.Trace(fmt.Sprintf("[RunLVMVolumeGroupDiscoverController] lvg: %+v", lvg))

if !hasLVMVolumeGroupDiff(d.log, lvg, candidate) {
Expand Down Expand Up @@ -471,6 +476,10 @@ func (d *Discoverer) UpdateLVMVolumeGroupByCandidate(
lvg.Status.VGFree = candidate.VGFree
lvg.Status.VGUuid = candidate.VGUUID

lvg.Spec.BlockDeviceSelector, _ = updateBlockDeviceSelectorIfNeeded(lvg.Spec.BlockDeviceSelector, candidate.BlockDevicesNames)

d.log.Trace(fmt.Sprintf("[UpdateLVMVolumeGroupByCandidate] updated LVMVolumeGroup: %+v", lvg))

start := time.Now()
err = d.cl.Status().Update(ctx, lvg)
d.metrics.APIMethodsDuration(DiscovererName, "update").Observe(d.metrics.GetEstimatedTimeInSeconds(start))
Expand Down Expand Up @@ -800,12 +809,15 @@ func hasLVMVolumeGroupDiff(log logger.Logger, lvg v1alpha1.LVMVolumeGroup, candi
log.Trace(fmt.Sprintf(`VGUUID, candidate: %s, lvg: %s`, candidate.VGUUID, lvg.Status.VGUuid))
log.Trace(fmt.Sprintf(`Nodes, candidate: %+v, lvg: %+v`, convertLVMVGNodes(candidate.Nodes), lvg.Status.Nodes))

_, blockDeviceSelectorUpdated := updateBlockDeviceSelectorIfNeeded(lvg.Spec.BlockDeviceSelector, candidate.BlockDevicesNames)

return candidate.AllocatedSize.Value() != lvg.Status.AllocatedSize.Value() ||
hasStatusPoolDiff(convertedStatusPools, lvg.Status.ThinPools) ||
candidate.VGSize.Value() != lvg.Status.VGSize.Value() ||
candidate.VGFree.Value() != lvg.Status.VGFree.Value() ||
candidate.VGUUID != lvg.Status.VGUuid ||
hasStatusNodesDiff(log, convertLVMVGNodes(candidate.Nodes), lvg.Status.Nodes)
hasStatusNodesDiff(log, convertLVMVGNodes(candidate.Nodes), lvg.Status.Nodes) ||
blockDeviceSelectorUpdated
}

func hasStatusNodesDiff(log logger.Logger, first, second []v1alpha1.LVMVolumeGroupNode) bool {
Expand Down Expand Up @@ -870,6 +882,51 @@ func configureBlockDeviceSelector(candidate internal.LVMVolumeGroupCandidate) *m
}
}

func updateBlockDeviceSelectorIfNeeded(existedLabelSelector *metav1.LabelSelector, blockDeviceNames []string) (*metav1.LabelSelector, bool) {
if existedLabelSelector == nil {
return &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: internal.MetadataNameLabelKey,
Operator: metav1.LabelSelectorOpIn,
Values: blockDeviceNames,
},
},
}, true
}

updated := false
found := false
for i := range existedLabelSelector.MatchExpressions {
if existedLabelSelector.MatchExpressions[i].Key == internal.MetadataNameLabelKey {
found = true

existingValuesMap := make(map[string]struct{})
for _, v := range existedLabelSelector.MatchExpressions[i].Values {
existingValuesMap[v] = struct{}{}
}

for _, bd := range blockDeviceNames {
if _, exist := existingValuesMap[bd]; !exist {
existedLabelSelector.MatchExpressions[i].Values = append(existedLabelSelector.MatchExpressions[i].Values, bd)
existingValuesMap[bd] = struct{}{}
updated = true
}
}
}
}

if !found {
existedLabelSelector.MatchExpressions = append(existedLabelSelector.MatchExpressions, metav1.LabelSelectorRequirement{
Key: internal.MetadataNameLabelKey,
Operator: metav1.LabelSelectorOpIn,
Values: blockDeviceNames,
})
updated = true
}
return existedLabelSelector, updated
}

func convertLVMVGNodes(nodes map[string][]internal.LVMVGDevice) []v1alpha1.LVMVolumeGroupNode {
lvmvgNodes := make([]v1alpha1.LVMVolumeGroupNode, 0, len(nodes))

Expand Down
9 changes: 9 additions & 0 deletions images/agent/src/internal/controller/lvg/discoverer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,15 @@ func TestLVMVolumeGroupDiscover(t *testing.T) {
Spec: v1alpha1.LVMVolumeGroupSpec{
ThinPools: convertSpecThinPools(specThinPools),
Type: specType,
BlockDeviceSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: internal.MetadataNameLabelKey,
Operator: metav1.LabelSelectorOpIn,
Values: blockDevicesNames,
},
},
},
},
Status: v1alpha1.LVMVolumeGroupStatus{
AllocatedSize: resource.MustParse("9765625Ki"),
Expand Down
7 changes: 6 additions & 1 deletion images/agent/werf.inc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ git:
install:
- '**/*'

---
image: {{ $.ImageName }}-src-artifact-3p
from: {{ $.Root.BASE_ALT_P11 }}
final: false

shell:
install:
- apt-get update
Expand All @@ -32,7 +37,7 @@ from: {{ $.Root.BASE_ALT_P11 }}
final: false

import:
- image: {{ $.ImageName }}-src-artifact
- image: {{ $.ImageName }}-src-artifact-3p
add: /src
to: /src
before: install
Expand Down
Loading