-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support creating config patches in the infrastructure providers
Patches can be created for a single machine in the machine provision flow: the provider can call `CreateConfigPatch` method at any point. This will create a `ConfigPatchRequest` resource which will be turned into a `ConfigPatch` after the `MachineRequestStatus` UUID gets populated. Fixes: #728 Signed-off-by: Artem Chernyshev <[email protected]>
- Loading branch information
Showing
11 changed files
with
336 additions
and
2 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
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
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
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,50 @@ | ||
// 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 infra | ||
|
||
import ( | ||
"github.com/cosi-project/runtime/pkg/resource" | ||
"github.com/cosi-project/runtime/pkg/resource/meta" | ||
"github.com/cosi-project/runtime/pkg/resource/protobuf" | ||
"github.com/cosi-project/runtime/pkg/resource/typed" | ||
|
||
"github.com/siderolabs/omni/client/api/omni/specs" | ||
"github.com/siderolabs/omni/client/pkg/omni/resources" | ||
) | ||
|
||
// NewConfigPatchRequest creates new ConfigPatchRequest resource. | ||
func NewConfigPatchRequest(ns string, id resource.ID) *ConfigPatchRequest { | ||
return typed.NewResource[ConfigPatchRequestSpec, ConfigPatchRequestExtension]( | ||
resource.NewMetadata(ns, ConfigPatchRequestType, id, resource.VersionUndefined), | ||
protobuf.NewResourceSpec(&specs.ConfigPatchSpec{}), | ||
) | ||
} | ||
|
||
const ( | ||
// ConfigPatchRequestType is the type of the ConfigPatch resource. | ||
// tsgen:ConfigPatchRequestType | ||
ConfigPatchRequestType = resource.Type("ConfigPatchRequests.omni.sidero.dev") | ||
) | ||
|
||
// ConfigPatchRequest requests a config patch to be created for the machine. | ||
// The controller should copy this resource contents to the target config patch, if the patch is valid. | ||
type ConfigPatchRequest = typed.Resource[ConfigPatchRequestSpec, ConfigPatchRequestExtension] | ||
|
||
// ConfigPatchRequestSpec wraps specs.ConfigPatchRequestSpec. | ||
type ConfigPatchRequestSpec = protobuf.ResourceSpec[specs.ConfigPatchSpec, *specs.ConfigPatchSpec] | ||
|
||
// ConfigPatchRequestExtension provides auxiliary methods for ConfigPatch resource. | ||
type ConfigPatchRequestExtension struct{} | ||
|
||
// ResourceDefinition implements [typed.Extension] interface. | ||
func (ConfigPatchRequestExtension) ResourceDefinition() meta.ResourceDefinitionSpec { | ||
return meta.ResourceDefinitionSpec{ | ||
Type: ConfigPatchRequestType, | ||
Aliases: []resource.Type{}, | ||
DefaultNamespace: resources.InfraProviderNamespace, | ||
PrintColumns: []meta.PrintColumn{}, | ||
Sensitivity: meta.Sensitive, | ||
} | ||
} |
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
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
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
83 changes: 83 additions & 0 deletions
83
internal/backend/runtime/omni/controllers/omni/infra_provider_config_patch.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,83 @@ | ||
// Copyright (c) 2024 Sidero Labs, Inc. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the LICENSE file. | ||
|
||
package omni | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/cosi-project/runtime/pkg/controller" | ||
"github.com/cosi-project/runtime/pkg/controller/generic/qtransform" | ||
"github.com/cosi-project/runtime/pkg/resource" | ||
"github.com/cosi-project/runtime/pkg/safe" | ||
"github.com/cosi-project/runtime/pkg/state" | ||
"github.com/siderolabs/gen/xerrors" | ||
"go.uber.org/zap" | ||
|
||
"github.com/siderolabs/omni/client/pkg/omni/resources" | ||
"github.com/siderolabs/omni/client/pkg/omni/resources/infra" | ||
"github.com/siderolabs/omni/client/pkg/omni/resources/omni" | ||
"github.com/siderolabs/omni/internal/backend/runtime/omni/controllers/helpers" | ||
) | ||
|
||
// InfraProviderConfigPatchController manages endpoints for each Cluster. | ||
type InfraProviderConfigPatchController = qtransform.QController[*infra.ConfigPatchRequest, *omni.ConfigPatch] | ||
|
||
// NewInfraProviderConfigPatchController initializes ConfigPatchRequestController. | ||
func NewInfraProviderConfigPatchController() *InfraProviderConfigPatchController { | ||
return qtransform.NewQController( | ||
qtransform.Settings[*infra.ConfigPatchRequest, *omni.ConfigPatch]{ | ||
Name: "ConfigPatchRequestController", | ||
MapMetadataFunc: func(request *infra.ConfigPatchRequest) *omni.ConfigPatch { | ||
return omni.NewConfigPatch(resources.DefaultNamespace, request.Metadata().ID()) | ||
}, | ||
UnmapMetadataFunc: func(configPatch *omni.ConfigPatch) *infra.ConfigPatchRequest { | ||
return infra.NewConfigPatchRequest(resources.DefaultNamespace, configPatch.Metadata().ID()) | ||
}, | ||
TransformFunc: func(ctx context.Context, r controller.Reader, _ *zap.Logger, request *infra.ConfigPatchRequest, patch *omni.ConfigPatch) error { | ||
machineRequestID, ok := request.Metadata().Labels().Get(omni.LabelMachineRequest) | ||
if !ok { | ||
return xerrors.NewTaggedf[qtransform.DestroyOutputTag]("missing machine request label on the patch request") | ||
} | ||
|
||
machineRequestStatus, err := safe.ReaderGetByID[*infra.MachineRequestStatus](ctx, r, machineRequestID) | ||
if err != nil { | ||
if state.IsNotFoundError(err) { | ||
return xerrors.NewTaggedf[qtransform.DestroyOutputTag]("machine request status with id %q doesn't exist", machineRequestID) | ||
} | ||
|
||
return err | ||
} | ||
|
||
if machineRequestStatus.TypedSpec().Value.Id == "" { | ||
return errors.New("failed to create config patch from the request: machine request status doesn't have machine UUID") | ||
} | ||
|
||
patch.TypedSpec().Value = request.TypedSpec().Value | ||
|
||
helpers.CopyAllLabels(request, patch) | ||
|
||
patch.Metadata().Labels().Set(omni.LabelSystemPatch, "") | ||
patch.Metadata().Labels().Set(omni.LabelMachine, machineRequestStatus.TypedSpec().Value.Id) | ||
|
||
return nil | ||
}, | ||
}, | ||
qtransform.WithExtraMappedInput( | ||
func(ctx context.Context, _ *zap.Logger, r controller.QRuntime, machineRequestStatus *infra.MachineRequestStatus) ([]resource.Pointer, error) { | ||
patchRequests, err := safe.ReaderListAll[*infra.ConfigPatchRequest](ctx, r, state.WithLabelQuery( | ||
resource.LabelEqual(omni.LabelMachineRequest, machineRequestStatus.Metadata().ID())), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return safe.ToSlice(patchRequests, func(r *infra.ConfigPatchRequest) resource.Pointer { return r.Metadata() }), nil | ||
}, | ||
), | ||
qtransform.WithOutputKind(controller.OutputShared), | ||
) | ||
} |
Oops, something went wrong.