-
Notifications
You must be signed in to change notification settings - Fork 601
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: implement volume mount controller
Fixes #9602 Aggregate incoming volume mount requests, reconcile them with volume status, perform actual mounting, and produce mount status. Signed-off-by: Andrey Smirnov <[email protected]>
- Loading branch information
Showing
15 changed files
with
2,448 additions
and
464 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
internal/app/machined/pkg/controllers/block/mount_request.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package block | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/cosi-project/runtime/pkg/controller" | ||
"github.com/cosi-project/runtime/pkg/safe" | ||
"go.uber.org/zap" | ||
|
||
"github.com/siderolabs/gen/xslices" | ||
"github.com/siderolabs/talos/pkg/machinery/resources/block" | ||
) | ||
|
||
// MountRequestController provides mount requests based on VolumeMountRequests and VolumeStatuses. | ||
type MountRequestController struct{} | ||
|
||
// Name implements controller.Controller interface. | ||
func (ctrl *MountRequestController) Name() string { | ||
return "block.MountRequestController" | ||
} | ||
|
||
// Inputs implements controller.Controller interface. | ||
func (ctrl *MountRequestController) Inputs() []controller.Input { | ||
return []controller.Input{ | ||
{ | ||
Namespace: block.NamespaceName, | ||
Type: block.VolumeMountRequestType, | ||
Kind: controller.InputStrong, | ||
}, | ||
{ | ||
Namespace: block.NamespaceName, | ||
Type: block.VolumeStatusType, | ||
Kind: controller.InputWeak, | ||
}, | ||
{ | ||
Namespace: block.NamespaceName, | ||
Type: block.MountRequestType, | ||
Kind: controller.InputDestroyReady, | ||
}, | ||
} | ||
} | ||
|
||
// Outputs implements controller.Controller interface. | ||
func (ctrl *MountRequestController) Outputs() []controller.Output { | ||
return []controller.Output{ | ||
{ | ||
Type: block.MountRequestType, | ||
Kind: controller.OutputExclusive, | ||
}, | ||
} | ||
} | ||
|
||
func identity[T any](v T) T { | ||
return v | ||
} | ||
|
||
// Run implements controller.Controller interface. | ||
// | ||
//nolint:gocyclo | ||
func (ctrl *MountRequestController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error { | ||
for { | ||
select { | ||
case <-r.EventCh(): | ||
case <-ctx.Done(): | ||
return nil | ||
} | ||
|
||
volumeStatuses, err := safe.ReaderListAll[*block.VolumeStatus](ctx, r) | ||
if err != nil { | ||
return fmt.Errorf("failed to read volume statuses: %w", err) | ||
} | ||
|
||
volumeStatusMap := xslices.ToMap( | ||
safe.ToSlice( | ||
volumeStatuses, | ||
identity, | ||
), | ||
func(v *block.VolumeStatus) (string, *block.VolumeStatusSpec) { | ||
return v.Metadata().ID(), v.TypedSpec() | ||
}, | ||
) | ||
|
||
volumeMountRequests, err := safe.ReaderListAll[*block.VolumeMountRequest](ctx, r) | ||
if err != nil { | ||
return fmt.Errorf("failed to read volume mount requests: %w", err) | ||
} | ||
|
||
desiredMountRequests := map[string]*block.MountRequestSpec{} | ||
|
||
for volumeMountRequest := range volumeMountRequests.All() { | ||
volumeID := volumeMountRequest.TypedSpec().VolumeID | ||
|
||
volumeStatus, ok := volumeStatusMap[volumeID] | ||
if !ok || volumeStatus.Phase != block.VolumePhaseReady { | ||
continue | ||
} | ||
|
||
if _, exists := desiredMountRequests[volumeID]; !exists { | ||
desiredMountRequests[volumeID] = &block.MountRequestSpec{ | ||
Source: volumeStatus.MountLocation, | ||
Target: volumeStatus.MountSpec.TargetPath, | ||
FSType: volumeStatus.Filesystem.String(), | ||
Options: volumeStatus.MountSpec.Options, | ||
SelinuxLabel: volumeStatus.MountSpec.SelinuxLabel, | ||
} | ||
} | ||
|
||
desiredMountRequest := desiredMountRequests[volumeID] | ||
desiredMountRequest.Requesters = append(desiredMountRequest.Requesters, volumeMountRequest.TypedSpec().Requester) | ||
desiredMountRequest.RequesterIDs = append(desiredMountRequest.RequesterIDs, volumeMountRequest.Metadata().ID()) | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.